Refactoring how we exec commands in chroot to make it less error prone.

This commit is contained in:
Sergiotarxz 2023-10-01 11:45:16 +02:00
parent 0c2a06934b
commit 09039d1900
2 changed files with 99 additions and 88 deletions

37
lib/BaseUtils.pm Normal file
View File

@ -0,0 +1,37 @@
package BaseUtils;
use v5.36.0;
use strict;
use warnings;
use utf8;
use Moo;
sub forkWait($self) {
my $pid = fork;
if ($pid) {
return sub {
wait;
my $return_code = $?;
return $return_code;
};
}
return;
}
sub chroot($self, $target) {
chroot "$target";
chdir '/';
}
sub execChroot($self, $target, @command) {
my $parent_subroutine = $self->forkWait();
if (defined $parent_subroutine) {
return $parent_subroutine->();
}
$self->chroot($target);
my $return_code = system @command;
exit $return_code;
}
1;

View File

@ -7,6 +7,11 @@ use warnings;
use utf8; use utf8;
use Path::Tiny; use Path::Tiny;
use lib path(__FILE__)->parent->child('lib') . '';
use BaseUtils;
my $utils = BaseUtils->new;
main(); main();
@ -22,18 +27,17 @@ sub main {
my $resolv_conf = path('/etc/resolv.conf'); my $resolv_conf = path('/etc/resolv.conf');
my $zz_use = path('zz-use'); my $zz_use = path('zz-use');
my $packages = _readPackagesToInstall(); my $packages = _readPackagesToInstall();
_checkValidSystem($target_dir); _checkValidSystem($target_dir);
my $make_conf_changed = _installMakeConf( $target_dir, $make_conf ); _installMakeConf( $target_dir, $make_conf );
_installGentooConf( $target_dir, $gentoo_conf ); _installGentooConf( $target_dir, $gentoo_conf );
_installResolvConf( $target_dir, $resolv_conf ); _installResolvConf( $target_dir, $resolv_conf );
my $package_use_changed = _installPackageUse($target_dir, $zz_use); _installPackageUse( $target_dir, $zz_use );
_mountNecessaryFilesystems($target_dir); _mountNecessaryFilesystems($target_dir);
_webrsync($target_dir); _webrsync($target_dir);
if ($rebuild_bin && $package_use_changed || $make_conf_changed) { if ( $rebuild_bin ) {
_rebuildBinaries($target_dir); _rebuildBinaries($target_dir);
} }
_updateSystem($target_dir); _updateSystem($target_dir);
@ -42,47 +46,37 @@ sub main {
} }
sub _depclean ($target) { sub _depclean ($target) {
_forkWait() or return; my $return_code = $utils->execChroot( $target, 'emerge', '--depclean' );
_chroot($target);
my $return_code = system 'emerge', '--depclean';
if ( $return_code != 0 ) { if ( $return_code != 0 ) {
die 'Unable to depclean system.'; die 'Unable to depclean system.';
} }
exit 0;
} }
sub _updateSystem ($target) { sub _updateSystem ($target) {
_forkWait() or return; my $return_code =
_chroot($target); $utils->execChroot( $target, 'emerge', '-uUDN', '@world' );
my $return_code = system 'emerge', '-uUDN', '@world';
if ( $return_code != 0 ) { if ( $return_code != 0 ) {
die 'Unable to update the system.'; die 'Unable to update the system.';
} }
exit 0;
} }
sub _rebuildBinaries ($target) { sub _rebuildBinaries ($target) {
_forkWait() or return; my $return_code = $utils->execChroot( $target, 'emerge', '-e', '@system' );
_chroot($target);
my $return_code = system 'emerge', '-e', '@system';
if ( $return_code != 0 ) { if ( $return_code != 0 ) {
die 'Unable to rebuild binaries for system.'; die 'Unable to rebuild binaries for system.';
} }
my $return_code = system 'emerge', '-e', '@world'; $return_code = $utils->execChroot( $target, 'emerge', '-e', '@world' );
if ( $return_code != 0 ) { if ( $return_code != 0 ) {
die 'Unable to rebuild binaries for world.'; die 'Unable to rebuild binaries for world.';
} }
exit 0;
} }
sub _installNeededPackages ( $target, $packages ) { sub _installNeededPackages ( $target, $packages ) {
_forkWait() or return; my $return_code =
_chroot($target); $utils->execChroot( $target, 'emerge', '--noreplace', @$packages );
my $return_code = system 'emerge', '--noreplace', @$packages;
if ( $return_code != 0 ) { if ( $return_code != 0 ) {
exit $return_code; die 'Unable to install needed packages.';
} }
exit 0;
} }
sub _readPackagesToInstall { sub _readPackagesToInstall {
@ -96,32 +90,11 @@ sub _readPackagesToInstall {
return \@packages; return \@packages;
} }
sub _forkWait {
my $pid = fork;
if ($pid) {
wait;
my $return_code = $?;
if ($? != 0) {
exit $?;
}
return 0;
}
return 1;
}
sub _webrsync ($target) { sub _webrsync ($target) {
_forkWait() or return; my $return_code = $utils->execChroot( $target, 'emerge-webrsync' );
_chroot($target);
my $return_code = system 'emerge-webrsync';
if ( $return_code != 0 ) { if ( $return_code != 0 ) {
return $return_code; die 'Unable to webrsync.';
} }
exit 0;
}
sub _chroot($target) {
chroot "$target";
chdir '/';
} }
sub _mountNecessaryFilesystems ($target) { sub _mountNecessaryFilesystems ($target) {
@ -150,6 +123,7 @@ sub _mountRbind($source, $fs) {
{ {
my $mounts; my $mounts;
sub _getMounts { sub _getMounts {
if ( !defined $mounts ) { if ( !defined $mounts ) {
open my $fh, '-|', 'sudo', 'mount'; open my $fh, '-|', 'sudo', 'mount';