summaryrefslogtreecommitdiffstats
path: root/perl-install/loopback.pm
blob: be432c3a64d49293a7f31bdf2ddd91390c98dd7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package loopback; # $Id$

use diagnostics;
use strict;

#-######################################################################################
#- misc imports
#-######################################################################################
use MDK::Common::System;
use common;
use partition_table qw(:types);
use fs;
use fsedit;
use log;


sub carryRootLoopback {
    my ($part) = @_;
    $_->{mntpoint} eq '/' and return 1 foreach @{$part->{loopback} || []};
    0;
}

sub check_circular_mounts {
    my ($_hd, $part, $all_hds) = @_;

    my $fstab = [ fsedit::get_all_fstab($all_hds), $part ]; # no pb if $part is already in $all_hds

    my $base_mntpoint = $part->{mntpoint};
    my $check; $check = sub {
	my ($part, @seen) = @_;
	push @seen, $part->{mntpoint} || return;
	@seen > 1 && $part->{mntpoint} eq $base_mntpoint and die N("Circular mounts %s\n", join(", ", @seen));
	if (my $part = fs::up_mount_point($part->{mntpoint}, $fstab)) {
	    #- '/' carrier is a special case, it will be mounted first
	    $check->($part, @seen) if !carryRootLoopback($part);
	}
	if (isLoopback($part)) {
	    $check->($part->{loopback_device}, @seen);
	}
    };
    $check->($part) if !($base_mntpoint eq '/' && isLoopback($part)); #- '/' is a special case, no loop check
}

sub carryRootCreateSymlink {
    my ($part, $prefix) = @_;

    carryRootLoopback($part) or return;

    my $mntpoint = "$prefix$part->{mntpoint}";
    unless (-e $mntpoint) {
	eval { mkdir_p(dirname($mntpoint)) };
	#- do non-relative link for install, should be changed to relative link before rebooting
	symlink "/initrd/loopfs", $mntpoint;

	mkdir_p("/initrd/loopfs/lnx4win/boot");
	symlink "/initrd/loopfs/lnx4win/boot", "$prefix/boot";
    }
    #- indicate kernel to keep initrd
    mkdir_p("$prefix/initrd");
}


sub format_part {
    my ($part, $prefix) = @_;
    fs::mount_part($part->{loopback_device}, $prefix);
    create($part, $prefix);
    fs::real_format_part($part);
}

sub create {
    my ($part, $prefix) = @_;
    my $f = $part->{device} = "$prefix$part->{loopback_device}{mntpoint}$part->{loopback_file}";
    return if -e $f;

    eval { mkdir_p(dirname($f)) };

    log::l("creating loopback file $f ($part->{size} sectors)");

    my $block_size = 128;
    my $s = "\0" x (512 * $block_size);
    sysopen(my $F, $f, 2 | c::O_CREAT()) or die "failed to create loopback file";
    for (my $i = 0; $i < $part->{size}; $i += $block_size) {
	syswrite $F, $s or die "failed to create loopback file";
    }
}

sub getFree {
    my ($dir, $part) = @_;
    my $freespace = $dir ? 
      2 * (MDK::Common::System::df($dir))[1] : #- df in KiB
      $part->{size};

    $freespace - sum map { $_->{size} } @{$part->{loopback} || []};
}

#- returns the size of the loopback file if it already exists
#- returns -1 is the loopback file can't be used
sub verifFile {
    my ($dir, $file, $part) = @_;
    -e "$dir$file" and return -s "$dir$file";

    $_->{loopback_file} eq $file and return -1 foreach @{$part->{loopback} || []};

    undef;
}

sub prepare_boot {
    my $r = readlink "$::prefix/boot"; 
    unlink "$::prefix/boot"; 
    mkdir_p("$::prefix/boot");
    [$r, $::prefix];
}

sub save_boot {
    my ($loop_boot, $prefix) = @{$_[0]};
    
    $loop_boot or return;

    my @files = glob_("$prefix/boot/*");
    cp_af(@files, $loop_boot) if @files;
    rm_rf("$prefix/boot");
    symlink $loop_boot, "$prefix/boot";
}


1;

hl opt">} #------------------------------------------------------------------------------ sub selectPath {} #------------------------------------------------------------------------------ sub selectInstallClass($@) {} #------------------------------------------------------------------------------ sub setupSCSI { modules::load_thiskind('scsi') } #------------------------------------------------------------------------------ sub doPartitionDisks($$) { my ($o, $hds) = @_; return if $::testing; partition_table::write($_) foreach $hds; } #------------------------------------------------------------------------------ sub rebootNeeded($) { my ($o) = @_; log::l("Rebooting..."); exit "true"; } sub choosePartitionsToFormat($$) { my ($o, $fstab) = @_; foreach (@$fstab) { $_->{toFormat} = ($_->{mntpoint} && isExt2($_) || isSwap($_)) && ($_->{notFormatted} || $o->{partitioning}{autoformat}); } } sub formatPartitions { my $o = shift; foreach (@_) { fs::format_part($_) if $_->{toFormat}; } } #------------------------------------------------------------------------------ sub setPackages { my ($o, $install_classes) = @_; install_any::setPackages($o, $install_classes); } sub choosePackages($$$) { my ($o, $packages, $compss) = @_; } sub beforeInstallPackages { my ($o) = @_; network::add2hosts("$o->{prefix}/etc/hosts", "localhost.localdomain", "127.0.0.1"); pkgs::init_db($o->{prefix}, $o->{isUpgrade}); } sub installPackages($$) { my ($o, $packages) = @_; my $toInstall = [ grep { $_->{selected} && !$_->{installed} } values %$packages ]; pkgs::install($o->{prefix}, $toInstall, $o->{isUpgrade}, 0); } sub afterInstallPackages($) { my ($o) = @_; #- why not? cuz weather is nice today :-) [pixel] sync(); sync(); # configPCMCIA($o->{rootPath}, $o->{pcmcia}); } #------------------------------------------------------------------------------ sub mouseConfig($) { my ($o) = @_; setVarsInSh("$o->{prefix}/etc/sysconfig/mouse", $o->{mouse}); symlink $o->{mouse}{DEVICE}, "$o->{prefix}/dev/mouse" or log::l("creating /dev/mouse symlink failed"); } #------------------------------------------------------------------------------ sub configureNetwork($) { my ($o) = @_; my $etc = "$o->{prefix}/etc"; network::write_conf("$etc/sysconfig/network", $o->{netc}); network::write_resolv_conf("$etc/resolv.conf", $o->{netc}); network::write_interface_conf("$etc/sysconfig/network-scripts/ifcfg-$_->{DEVICE}", $_) foreach @{$o->{intf}}; network::add2hosts("$etc/hosts", $o->{netc}{HOSTNAME}, map { $_->{IPADDR} } @{$o->{intf}}); network::sethostname($o->{netc}) unless $::testing; network::addDefaultRoute($o->{netc}) unless $::testing; #-res_init(); #- reinit the resolver so DNS changes take affect } #------------------------------------------------------------------------------ sub timeConfig { my ($o, $f) = @_; my $t = $o->{timezone}; eval { commands::cp("-f", "/usr/share/zoneinfo/$t->{timezone}", "/etc/localtime") }; $@ and log::l("installing /etc/localtime failed"); setVarsInSh($f, { ZONE => $t->{timezone}, GMT => bool2text($t->{GMT}), ARC => "false", }); } #------------------------------------------------------------------------------ sub servicesConfig {} #------------------------------------------------------------------------------ sub printerConfig { my($o) = @_; if ($o->{printer}{complete}) { pkgs::select($o->{packages}, $o->{packages}{'rhs-printfilters'}); $o->installPackages($o->{packages}); printer::configure_queue($o->{printer}); } } #------------------------------------------------------------------------------ my @etc_pass_fields = qw(name password uid gid realname home shell); sub setRootPassword($) { my ($o) = @_; my %u = %{$o->{superuser}}; my $p = $o->{prefix}; $u{password} = crypt_($u{password}) if $u{password}; my @lines = cat_(my $f = "$p/etc/passwd") or log::l("missing passwd file"), return; local *F; open F, "> $f" or die "failed to write file $f: $!\n"; foreach (@lines) { if (/^root:/) { chomp; my %l; @l{@etc_pass_fields} = split ':'; add2hash(\%u, \%l); $_ = join(':', @u{@etc_pass_fields}) . "\n"; } print F $_; } } #------------------------------------------------------------------------------ sub addUser($) { my ($o) = @_; my %u = %{$o->{user}}; my $p = $o->{prefix}; my @passwd = cat_("$p/etc/passwd");; !$u{name} || getpwnam($u{name}) and return; for ($u{uid} = 500; getpwuid($u{uid}); $u{uid}++) {} for ($u{gid} = 500; getgrgid($u{gid}); $u{gid}++) {} $u{home} ||= "/home/$u{name}"; $u{password} = crypt_($u{password}) if $u{password}; return if $::testing; local *F; open F, ">> $p/etc/passwd" or die "can't append to passwd file: $!"; print F join(':', @u{@etc_pass_fields}), "\n"; open F, ">> $p/etc/group" or die "can't append to group file: $!"; print F "$u{name}::$u{gid}:\n"; eval { commands::cp("-f", "$p/etc/skel", "$p$u{home}") }; $@ and log::l("copying of skel failed: $@"), mkdir("$p$u{home}", 0750); commands::chown_("-r", "$u{uid}.$u{gid}", "$p$u{home}"); } #------------------------------------------------------------------------------