Adding minimum perl, coreutils, bash and nginx installation.

This commit is contained in:
Sergiotarxz 2023-10-01 20:30:38 +02:00
parent 09039d1900
commit f2fada8eae
5 changed files with 120 additions and 61 deletions

38
install_system.pl Normal file
View File

@ -0,0 +1,38 @@
#!/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 $destination_dir = shift @ARGV or die 'No destination dir passed.';
$target_dir = path($target_dir);
$destination_dir = path($destination_dir);
$utils->createBasicMountsChroot($target_dir);
populateDirectory($destination_dir);
my $mnt_gentoo = $target_dir->child('mnt/gentoo');
populateDirectory($mnt_gentoo);
$utils->mountRbind($destination_dir, $mnt_gentoo);
my $packages = $utils->readPackagesToInstall();
$utils->execChroot($target_dir, 'emerge', '--root', '/mnt/gentoo', '--noreplace', '-K', @$packages);
}
sub populateDirectory($dir) {
my $return_code = system 'mkdir', '-pv', $dir;
if ($return_code != 0) {
die "Unable to create $dir.";
}
}

View File

@ -7,6 +7,7 @@ use warnings;
use utf8;
use Moo;
use Path::Tiny;
sub forkWait($self) {
my $pid = fork;
@ -34,4 +35,66 @@ sub execChroot($self, $target, @command) {
my $return_code = system @command;
exit $return_code;
}
sub mountType ( $self, $fs_type, $fs ) {
if($self->checkMounted($fs)) {
say "$fs already mounted";
return;
}
my $return_code = system 'sudo', 'mount', '-t', $fs_type, $fs_type, $fs;
if ( $return_code != 0 ) {
die "Unable to mount $fs.";
}
}
sub checkMounted ($self, $fs) {
$fs = $fs->realpath;
my $return = index( $self->getMounts(), $fs ) != -1;
say "$fs is mounted." if $return;
return $return;
}
sub mountRbind ( $self, $source, $fs ) {
if($self->checkMounted($fs)) {
say "$fs already mounted";
return;
}
my $return_code = system 'sudo', 'mount', '--rbind', $source, $fs;
if ( $return_code != 0 ) {
die "Unable to mount $fs.";
}
}
sub readPackagesToInstall {
my @packages;
open my $fh, '<', path(__FILE__)->parent->parent->child('packages');
while ( my $line = <$fh> ) {
chomp $line;
push @packages, $line;
}
close $fh;
return \@packages;
}
sub createBasicMountsChroot ($self, $target) {
my $dest_proc = $target->child('proc');
my $dest_dev = $target->child('dev');
my $dest_sys = $target->child('sys');
$self->mountType( 'proc', $dest_proc );
$self->mountType( 'sysfs', $dest_sys );
$self->mountRbind( '/dev', $dest_dev );
}
{
my $mounts;
sub getMounts {
if ( !defined $mounts ) {
open my $fh, '-|', 'sudo', 'mount';
$mounts = join '', <$fh>;
close $fh;
}
return $mounts;
}
}
1;

View File

@ -2,13 +2,14 @@
# built this stage.
# Please consult /usr/share/portage/config/make.conf.example for a more
# detailed example.
COMMON_FLAGS="-O2 -pipe"
COMMON_FLAGS="-Os -pipe"
CFLAGS="${COMMON_FLAGS}"
CXXFLAGS="${COMMON_FLAGS}"
FCFLAGS="${COMMON_FLAGS}"
FFLAGS="${COMMON_FLAGS}"
MAKEOPTS="-j12"
FEATURES="buildpkg"
USE="-ncurses"
# NOTE: This stage was built with the bindist Use flag enabled

View File

@ -1,5 +1,4 @@
vim
app-misc/screen
sudo
nginx
certbot
bash
coreutils
perl

View File

@ -27,7 +27,7 @@ sub main {
my $resolv_conf = path('/etc/resolv.conf');
my $zz_use = path('zz-use');
my $packages = _readPackagesToInstall();
my $packages = $utils->readPackagesToInstall();
_checkValidSystem($target_dir);
_installMakeConf( $target_dir, $make_conf );
@ -35,8 +35,9 @@ sub main {
_installResolvConf( $target_dir, $resolv_conf );
_installPackageUse( $target_dir, $zz_use );
_mountNecessaryFilesystems($target_dir);
$utils->createBasicMountsChroot($target_dir);
_webrsync($target_dir);
_removePerlCleanerDep($target_dir);
if ( $rebuild_bin ) {
_rebuildBinaries($target_dir);
}
@ -45,6 +46,17 @@ sub main {
_installNeededPackages( $target_dir, $packages );
}
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 ) {
@ -79,16 +91,6 @@ sub _installNeededPackages ( $target, $packages ) {
}
}
sub _readPackagesToInstall {
my @packages;
open my $fh, '<', 'packages';
while ( my $line = <$fh> ) {
chomp $line;
push @packages, $line;
}
close $fh;
return \@packages;
}
sub _webrsync ($target) {
my $return_code = $utils->execChroot( $target, 'emerge-webrsync' );
@ -97,50 +99,6 @@ sub _webrsync ($target) {
}
}
sub _mountNecessaryFilesystems ($target) {
my $dest_proc = $target->child('proc');
my $dest_dev = $target->child('dev');
my $dest_sys = $target->child('sys');
_mountType( 'proc', $dest_proc ) if !_checkMounted($dest_proc);
_mountType( 'sysfs', $dest_sys ) if !_checkMounted($dest_sys);
_mountRbind( '/dev', $dest_dev ) if !_checkMounted($dest_dev);
}
sub _checkMounted ($fs) {
$fs = $fs->realpath;
my $return = index( _getMounts(), $fs ) != -1;
say "$fs is mounted." if $return;
return $return;
}
sub _mountRbind ( $source, $fs ) {
my $return_code = system 'sudo', 'mount', '--rbind', $source, $fs;
if ( $return_code != 0 ) {
die "Unable to mount $fs.";
}
}
{
my $mounts;
sub _getMounts {
if ( !defined $mounts ) {
open my $fh, '-|', 'sudo', 'mount';
$mounts = join '', <$fh>;
close $fh;
}
return $mounts;
}
}
sub _mountType ( $fs_type, $fs ) {
my $return_code = system 'sudo', 'mount', '-t', $fs_type, $fs_type, $fs;
if ( $return_code != 0 ) {
die "Unable to mount $fs.";
}
}
sub _checkValidSystem ($target) {
if ( !-d $target ) {
die 'Target system not populated yet.';