BaseUtilsGentoo/prepare_system.pl

145 lines
4.7 KiB
Perl

#!/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 $target_dir = shift @ARGV or die 'No target dir passed.';
my $rebuild_bin = shift @ARGV // 0;
$target_dir = path($target_dir);
my $gentoo_conf = path('gentoo.conf');
my $make_conf = path('make.conf');
my $resolv_conf = path('/etc/resolv.conf');
my $zz_use = path('zz-use');
my $packages = $utils->readPackagesToInstall();
my $build_packages = $utils->readBuildPackagesToInstall();
_checkValidSystem($target_dir);
$utils->_installRootFiles( $target_dir, path('root_stage') );
_installMakeConf( $target_dir, $make_conf );
_installGentooConf( $target_dir, $gentoo_conf );
_installResolvConf( $target_dir, $resolv_conf );
_installPackageUse( $target_dir, $zz_use );
$utils->createBasicMountsChroot($target_dir);
_webrsync($target_dir);
_removePerlCleanerDep($target_dir);
_updateSystem($target_dir);
_depclean($target_dir);
_installNeededPackages( $target_dir, $packages, $build_packages,
$rebuild_bin );
}
sub _removePerlCleanerDep ($target) {
my $path = 'var/db/repos/gentoo/dev-lang/perl';
my $perl_ebuild_dir = $target->child($path);
for my $file ( $perl_ebuild_dir->children ) {
next if !-f $file;
next if $file !~ /\.ebuild$/;
system 'sed', '-i', '/perl-cleaner/d', $file;
$utils->execChroot( $target, 'ebuild', "/$path/@{[$file->basename]}",
'manifest' );
}
}
sub _depclean ($target) {
my $return_code = $utils->execChroot( $target, 'emerge', '--depclean' );
if ( $return_code != 0 ) {
die 'Unable to depclean system.';
}
}
sub _updateSystem ($target) {
$utils->execChroot($target, 'cp', '/var/db/repos/gentoo/app-containers/cri-tools/cri-tools-1.27.0.ebuild', '/var/db/repos/gentoo/app-containers/cri-tools/cri-tools-1.28.0.ebuild');
$utils->execChroot($target, 'ebuild', '/var/db/repos/gentoo/app-containers/cri-tools/cri-tools-1.28.0.ebuild', 'manifest');
$utils->execChroot($target, 'cp', '/var/db/repos/gentoo/sys-cluster/cilium-cli/cilium-cli-0.13.0.ebuild', '/var/db/repos/gentoo/sys-cluster/cilium-cli/cilium-cli-0.15.11.ebuild',);
$utils->execChroot($target, 'ebuild', '/var/db/repos/gentoo/sys-cluster/cilium-cli/cilium-cli-0.15.11.ebuild', 'manifest');
my $return_code =
$utils->execChroot( $target, 'emerge', '-uUDN', '@world' );
if ( $return_code != 0 ) {
die 'Unable to update the system.';
}
}
sub _rebuildBinaries ($target) {
my $return_code = $utils->execChroot( $target, 'emerge', '-e', '@system' );
if ( $return_code != 0 ) {
die 'Unable to rebuild binaries for system.';
}
$return_code = $utils->execChroot( $target, 'emerge', '-e', '@world' );
if ( $return_code != 0 ) {
die 'Unable to rebuild binaries for world.';
}
}
sub _installNeededPackages ( $target, $packages, $build_packages, $rebuild_bin )
{
my $return_code =
$utils->execChroot( $target, 'emerge',
( $rebuild_bin ? ('-e') : ( '--noreplace', ) ),
@$packages, @$build_packages );
if ( $return_code != 0 ) {
die 'Unable to install needed packages.';
}
}
sub _webrsync ($target) {
my $return_code = $utils->execChroot( $target, 'emerge-webrsync' );
if ( $return_code != 0 ) {
die 'Unable to webrsync.';
}
}
sub _checkValidSystem ($target) {
if ( !-d $target ) {
die 'Target system not populated yet.';
}
}
sub _installResolvConf ( $target, $resolv_conf ) {
my $dest_resolv_conf = $target->child('etc/resolv.conf');
$utils->install( $resolv_conf, $dest_resolv_conf );
}
sub _installGentooConf ( $target, $gentoo_conf ) {
my $dest_gentoo_conf = $target->child('etc/portage/repos.conf/gentoo.conf');
my $repos_conf = $dest_gentoo_conf->parent;
my $return_code = system 'sudo', 'mkdir', '-p', $repos_conf;
if ( $return_code != 0 ) {
say "Unable to create $repos_conf.";
}
$utils->install( $gentoo_conf, $dest_gentoo_conf );
}
sub _installPackageUse ( $target, $zz_use ) {
my $dest_zz_use = $target->child('etc/portage/package.use/zz-use');
my $package_use = $dest_zz_use->parent;
my $return_code = system 'sudo', 'mkdir', '-p', $package_use;
if ( $return_code != 0 ) {
say "Unable to create $package_use.";
}
$utils->install( $zz_use, $dest_zz_use );
}
sub _installMakeConf ( $target, $make_conf ) {
my $dest_make_conf = $target->child('etc/portage/make.conf');
return $utils->install( $make_conf, $dest_make_conf );
}