Starting qemu-image creation.

This commit is contained in:
Sergiotarxz 2023-10-03 19:53:08 +02:00
parent f2fada8eae
commit 0438ade83a
8 changed files with 132 additions and 15 deletions

1
build_packages Normal file
View File

@ -0,0 +1 @@
syslinux

75
generate_qemu_img.pl Normal file
View File

@ -0,0 +1,75 @@
#!/usr/bin/env perl
use v5.36.0;
use strict;
use warnings;
use utf8;
use Path::Tiny;
use lib path(__FILE__)->parent->child('lib') . '';
use BaseUtils;
my $utils = BaseUtils->new;
main();
sub main() {
die 'Must be superuser.' if $< != 0;
my $source_dir = shift @ARGV or die 'No source dir passed.';
$source_dir = path $source_dir;
my $dest_img = shift @ARGV // 'gentoo.qcow2';
if (-e $dest_img) {
die "Image $dest_img already exists.";
}
$dest_img = path $dest_img;
my $size_img = shift @ARGV // 1000;
umountQemuNbd();
modprobeNbd();
createQemuImage($dest_img, $size_img);
mountQemuNbd($dest_img);
createPartitionTable();
}
sub createPartitionTable {
open my $fh, "|-", 'fdisk', '/dev/nbd1';
print $fh <<'EOF';
g
n
+20M
t
1
n
w
EOF
close $fh;
}
sub createQemuImage($dest_img, $size_img) {
my $return_code = system 'qemu-img', 'create', '-f', 'qcow2', $dest_img, $size_img.'M';
if ($return_code != 0) {
die 'Unable to create qemu-img.';
}
}
sub modprobeNbd {
system 'rmmod', '-f', 'nbd';
system 'modprobe', 'nbd';
}
sub mountQemuNbd($dest_img) {
my $return_code = system 'qemu-nbd', '--connect=/dev/nbd1', $dest_img;
if ($return_code != 0) {
die 'Unable to mount qemu-nbd.';
}
}
sub umountQemuNbd {
system 'qemu-nbd', '--disconnect', '/dev/nbd1';
}

View File

@ -24,10 +24,34 @@ sub main() {
$utils->createBasicMountsChroot($target_dir); $utils->createBasicMountsChroot($target_dir);
populateDirectory($destination_dir); populateDirectory($destination_dir);
my $mnt_gentoo = $target_dir->child('mnt/gentoo'); my $mnt_gentoo = $target_dir->child('mnt/gentoo');
$utils->createBasicMountsChroot($mnt_gentoo);
populateDirectory($mnt_gentoo); populateDirectory($mnt_gentoo);
$utils->mountRbind($destination_dir, $mnt_gentoo); $utils->mountRbind($destination_dir, $mnt_gentoo);
my $packages = $utils->readPackagesToInstall(); my $packages = $utils->readPackagesToInstall();
$utils->execChroot($target_dir, 'emerge', '--root', '/mnt/gentoo', '--noreplace', '-K', @$packages); $utils->execChroot($target_dir, 'emerge', '--root', '/mnt/gentoo', '--noreplace', '-K', @$packages);
$utils->execChroot($mnt_gentoo, 'rc-update add nginx default');
_installEfiSyslinux($target_dir, $mnt_gentoo);
}
sub _installEfiSyslinux($target_dir, $mnt_gentoo) {
my @files = grep { -f $_ } $target_dir->child('usr/share/syslinux/efi64')->children;
my $syslinux_dir = $mnt_gentoo->child('boot/efi/EFI/BOOT');
$syslinux_dir->mkpath;
for my $file (@files) {
if ($file =~ /\.efi$/i) {
my $return_code = system 'cp', '-v', $file, $syslinux_dir->child('BOOTx64.EFI');
_errIfSyslinuxFail($return_code);
next;
}
my $return_code = system 'cp', '-v', @files, $syslinux_dir;
_errIfSyslinuxFail($return_code);
}
}
sub _errIfSyslinuxFail($return_code) {
if ($return_code != 0) {
die 'Unable to install syslinux EFI files.';
}
} }
sub populateDirectory($dir) { sub populateDirectory($dir) {

View File

@ -76,11 +76,26 @@ sub readPackagesToInstall {
return \@packages; return \@packages;
} }
sub readBuildPackagesToInstall {
my @packages;
open my $fh, '<', path(__FILE__)->parent->parent->child('build_packages');
while ( my $line = <$fh> ) {
chomp $line;
push @packages, $line;
}
close $fh;
return \@packages;
}
sub createBasicMountsChroot ($self, $target) { sub createBasicMountsChroot ($self, $target) {
my $dest_proc = $target->child('proc'); my $dest_proc = $target->child('proc');
my $dest_dev = $target->child('dev'); my $dest_dev = $target->child('dev');
my $dest_sys = $target->child('sys'); my $dest_sys = $target->child('sys');
$dest_proc->mkpath;
$dest_dev->mkpath;
$dest_sys->mkpath;
$self->mountType( 'proc', $dest_proc ); $self->mountType( 'proc', $dest_proc );
$self->mountType( 'sysfs', $dest_sys ); $self->mountType( 'sysfs', $dest_sys );
$self->mountRbind( '/dev', $dest_dev ); $self->mountRbind( '/dev', $dest_dev );

View File

@ -8,8 +8,10 @@ CXXFLAGS="${COMMON_FLAGS}"
FCFLAGS="${COMMON_FLAGS}" FCFLAGS="${COMMON_FLAGS}"
FFLAGS="${COMMON_FLAGS}" FFLAGS="${COMMON_FLAGS}"
MAKEOPTS="-j12" MAKEOPTS="-j12"
FEATURES="buildpkg" FEATURES="buildpkg nodoc noman -nostrip"
USE="-ncurses" LINGUAS="en"
L10N="en-GB"
USE="-ncurses -doc"
# NOTE: This stage was built with the bindist Use flag enabled # NOTE: This stage was built with the bindist Use flag enabled

View File

@ -2,3 +2,5 @@ nginx
bash bash
coreutils coreutils
perl perl
openrc
gentoo-kernel

View File

@ -28,6 +28,7 @@ sub main {
my $zz_use = path('zz-use'); my $zz_use = path('zz-use');
my $packages = $utils->readPackagesToInstall(); my $packages = $utils->readPackagesToInstall();
my $build_packages = $utils->readBuildPackagesToInstall();
_checkValidSystem($target_dir); _checkValidSystem($target_dir);
_installMakeConf( $target_dir, $make_conf ); _installMakeConf( $target_dir, $make_conf );
@ -38,22 +39,20 @@ sub main {
$utils->createBasicMountsChroot($target_dir); $utils->createBasicMountsChroot($target_dir);
_webrsync($target_dir); _webrsync($target_dir);
_removePerlCleanerDep($target_dir); _removePerlCleanerDep($target_dir);
if ( $rebuild_bin ) {
_rebuildBinaries($target_dir);
}
_updateSystem($target_dir); _updateSystem($target_dir);
_depclean($target_dir); _depclean($target_dir);
_installNeededPackages( $target_dir, $packages ); _installNeededPackages( $target_dir, $packages, $build_packages, $rebuild_bin );
} }
sub _removePerlCleanerDep($target) { sub _removePerlCleanerDep ($target) {
my $path = 'var/db/repos/gentoo/dev-lang/perl'; my $path = 'var/db/repos/gentoo/dev-lang/perl';
my $perl_ebuild_dir = $target->child($path); my $perl_ebuild_dir = $target->child($path);
for my $file ($perl_ebuild_dir->children) { for my $file ( $perl_ebuild_dir->children ) {
next if ! -f $file; next if !-f $file;
next if $file !~ /\.ebuild$/; next if $file !~ /\.ebuild$/;
system 'sed', '-i', '/perl-cleaner/d', $file; system 'sed', '-i', '/perl-cleaner/d', $file;
$utils->execChroot($target, 'ebuild', "/$path/@{[$file->basename]}", 'manifest'); $utils->execChroot( $target, 'ebuild', "/$path/@{[$file->basename]}",
'manifest' );
} }
} }
@ -83,15 +82,14 @@ sub _rebuildBinaries ($target) {
} }
} }
sub _installNeededPackages ( $target, $packages ) { sub _installNeededPackages ( $target, $packages, $build_packages, $rebuild_bin ) {
my $return_code = my $return_code =
$utils->execChroot( $target, 'emerge', '--noreplace', @$packages ); $utils->execChroot( $target, 'emerge', ( $rebuild_bin ? ('-e') : ('--noreplace', ) ), @$packages, @$build_packages );
if ( $return_code != 0 ) { if ( $return_code != 0 ) {
die 'Unable to install needed packages.'; die 'Unable to install needed packages.';
} }
} }
sub _webrsync ($target) { sub _webrsync ($target) {
my $return_code = $utils->execChroot( $target, 'emerge-webrsync' ); my $return_code = $utils->execChroot( $target, 'emerge-webrsync' );
if ( $return_code != 0 ) { if ( $return_code != 0 ) {

2
zz-use
View File

@ -1,2 +1,2 @@
app-misc/mime-types nginx app-misc/mime-types nginx
sys-kernel/gentoo-kernel -initramfs