package fs; # $Id$ use diagnostics; use strict; use MDK::Common::System; use MDK::Common::Various; use common; use log; use devices; use partition_table qw(:types); use run_program; use swap; use detect_devices; use modules; use fsedit; use loopback; sub read_fstab { my ($prefix, $file, @reading_options) = @_; my %comments; my $comment; my @l = grep { if (/^\s*#/) { $comment .= chomp_($_) . "\n"; 0; } else { $comments{$_} = $comment if $comment; $comment = ''; 1; } } cat_("$prefix$file"); #- attach comments at the end of fstab to the previous line $comments{$l[-1]} = $comment if $comment; map { my ($dev, $mntpoint, $type, $options, $freq, $passno) = split; my $comment = $comments{$_}; $options = 'defaults' if $options eq 'rw'; # clean-up for mtab read $type = fs2type($type); if ($type eq 'supermount') { # normalize this bloody supermount $options = join(",", 'supermount', grep { if (/fs=(.*)/) { $type = $1; 0; } elsif (/dev=(.*)/) { $dev = $1; 0; } elsif ($_ eq '--') { 0; } else { 1; } } split(',', $options)); } elsif ($type eq 'smb') { # prefering type "smbfs" over "smb" $type = 'smbfs'; } $mntpoint =~ s/\\040/ /g; $dev =~ s/\\040/ /g; my $h = { device => $dev, mntpoint => $mntpoint, type => $type, options => $options, comment => $comment, if_(member('keep_freq_passno', @reading_options), freq => $freq, passno => $passno), }; if ($dev =~ /^LABEL=/) { if (my $e = find { $_->{mntpoint} eq $mntpoint } read_fstab('', '/proc/mounts')) { $h->{device_LABEL} = $dev; $dev = $h->{device} = $e->{device}; } } if ($dev =~ m,/(tmp|dev)/,) { ($h->{major}, $h->{minor}) = unmakedev((stat "$prefix$dev")[6]); my $symlink = readlink("$prefix$dev"); $dev =~ s,/(tmp|dev)/,,; if ($symlink =~ m|^[^/]+$|) { $h->{device_alias} = $dev; $h->{device} = $symlink; } else { $h->{device} = $dev; } } if ($h->{options} =~ /credentials=/ && !member('verbatim_credentials', @reading_options)) { require network::smb; #- remove credentials=file with username=foo,password=bar,domain=zoo #- the other way is done in fstab_to_string my ($options, $unknown) = mount_options_unpack($h); my $file = delete $options->{'credentials='}; my $credentials = network::smb::read_credentials_raw($file); if ($credentials->{username}) { $options->{"$_="} = $credentials->{$_} foreach qw(username password domain); mount_options_pack($h, $options, $unknown); } } $h; } @l; } sub merge_fstabs { my ($loose, $fstab, @l) = @_; foreach my $p (@$fstab) { my ($l1, $l2) = partition { fsedit::is_same_hd($_, $p) } @l; my ($p2) = @$l1 or next; @l = @$l2; $p->{mntpoint} = $p2->{mntpoint} if delete $p->{unsafeMntpoint}; $p->{type} = $p2->{type} if $p2->{type} && !$loose; $p->{options} = $p2->{options} if $p2->{options} && !$loose; #- important to get isMounted property else DrakX may try to mount already mounted partitions :-( add2hash($p, $p2); $p->{device_alias} ||= $p2->{device_alias} || $p2->{device} if $p->{device} ne $p2->{device} && $p2->{device} !~ m|/|; $p->{type} && $p2->{type} && $p->{type} ne $p2->{type} && type2fs($p) ne type2fs($p2) && $p->{type} ne 'auto' && $p2->{type} ne 'auto' and log::l("err, fstab and partition table do not agree for $p->{device} type: " . (type2fs($p) || type2name($p->{type})) . " vs ", (type2fs($p2) || type2name($p2->{type}))); } @l; } sub add2all_hds { my ($all_hds, @l) = @_; @l = merge_fstabs('', [ fsedit::get_really_all_fstab($all_hds) ], @l); foreach (@l) { my $s = isThisFs('nfs', $_) ? 'nfss' : isThisFs('smbfs', $_) ? 'smbs' : isThisFs('davfs', $_) ? 'davs' : 'special'; push @{$all_hds->{$s}}, $_; } } sub get_major_minor { eval { my (undef, $major, $minor) = devices::entry($_->{device}); ($_->{major}, $_->{minor}) = ($major, $minor); } foreach @_; } sub merge_info_from_mtab { my ($fstab) = @_; my @l1 = map { my $l = $_; my %l = (type => fs2type('swap')); $l{$_} = $l->{$_} foreach qw(device major minor); \%l; } read_fstab('', '/proc/swaps'); my @l2 = map { read_fstab('', $_) } '/etc/mtab', '/proc/mounts'; foreach (@l1, @l2) { log::l("found mounted partition on $_->{device} with $_->{mntpoint}"); if ($::isInstall && $_->{mntpoint} eq '/tmp/hdimage') { log::l("found hdimage on $_->{device}"); $_->{real_mntpoint} = delete $_->{mntpoint}; $_->{mntpoint} = common::usingRamdisk() && "/mnt/hd"; #- remap for hd install. } $_->{isMounted} = $_->{isFormatted} = 1; delete $_->{options}; } merge_fstabs('loose', $fstab, @l1, @l2); } # - when using "$loose", it does not merge in type&options from the fstab sub merge_info_from_fstab { my ($fstab, $prefix, $uniq, $loose) = @_; my @l = grep { if ($uniq) { my $part = fsedit::mntpoint2part($_->{mntpoint}, $fstab); !$part || fsedit::is_same_hd($part, $_); #- keep it only if it is the mountpoint AND the same device } else { 1; } } read_fstab($prefix, "/etc/fstab", 'keep_freq_passno'); merge_fstabs($loose, $fstab, @l); } sub prepare_write_fstab { my ($fstab, $prefix, $keep_smb_credentials) = @_; $prefix ||= ''; my %new; my @smb_credentials; my @l = map { my $device = isLoopback($_) ? ($_->{mntpoint} eq '/' ? "/initrd/loopfs" : $_->{loopback_device}{mntpoint}) . $_->{loopback_file} : part2device($prefix, $_->{device}, $_->{type}); my $real_mntpoint = $_->{mntpoint} || ${{ '/tmp/hdimage' => '/mnt/hd' }}{$_->{real_mntpoint}}; mkdir_p("$prefix$real_mntpoint") if $real_mntpoint =~ m|^/|; my $mntpoint = loopback::carryRootLoopback($_) ? '/initrd/loopfs' : $real_mntpoint; my ($freq, $passno) = exists $_->{freq} ? ($_->{freq}, $_->{passno}) : isTrueFS($_) && $_->{options} !~ /encryption=/ ? (1, $_->{mntpoint} eq '/' ? 1 : loopback::carryRootLoopback($_) ? 0 : 2) : (0, 0); if (($device eq 'none' || !$new{$device}) && ($mntpoint eq 'swap' || !$new{$mntpoint})) { #- keep in mind the new line for fstab. $new{$device} = 1; $new{$mntpoint} = 1; my $options = $_->{options}; if (isThisFs('smbfs', $_) && $options =~ /password=/ && !$keep_smb_credentials) { require network::smb; if (my ($opts, $smb_credentials) = network::smb::fstab_entry_to_credentials($_)) { $options = $opts; push @smb_credentials, $smb_credentials; } } my $type = type2fs($_); my $dev = $_->{device_LABEL} ? $_->{device_LABEL} : $_->{device_alias} ? "/dev/$_->{device_alias}" : $device; $mntpoint =~ s/ /\\040/g; $dev =~ s/ /\\040/g; # handle bloody supermount special case if ($options =~ /supermount/) { my @l = grep { $_ ne 'supermount' } split(',', $options); my @l1 = grep { member($_, 'ro', 'exec') } @l; my @l2 = difference2(\@l, \@l1); $options = join(",", "dev=$dev", "fs=$type", @l1, if_(@l2, '--', @l2)); ($dev, $type) = ('none', 'supermount'); } [ $mntpoint, $_->{comment} . join(' ', $dev, $mntpoint, $type, $options || 'defaults', $freq, $passno) . "\n" ]; } else { () } } grep { $_->{device} && ($_->{mntpoint} || $_->{real_mntpoint}) && $_->{type} } @$fstab; join('', map { $_->[1] } sort { $a->[0] cmp $b->[0] } @l), \@smb_credentials; } sub fstab_to_string { my ($all_hds, $prefix) = @_; my $fstab = [ fsedit::get_really_all_fstab($all_hds), @{$all_hds->{special}} ]; my ($s, undef) = prepare_write_fstab($fstab, $prefix, 'keep_smb_credentials'); $s; } sub write_fstab { my ($all_hds, $prefix) = @_; log::l("writing $prefix/etc/fstab"); my $fstab = [ fsedit::get_really_all_fstab($all_hds), @{$all_hds->{special}} ]; my ($s, $smb_credentials) = prepare_write_fstab($fstab, $prefix, ''); output("$prefix/etc/fstab", $s); network::smb::save_credentials($_) foreach @$smb_credentials; } sub part2device { my ($prefix, $dev, $type) = @_; $dev eq 'none' || member($type, qw(nfs smbfs davfs)) ? $dev : do { my $dir = $dev =~ m!^(/|LABEL=)! ? '' : '/dev/'; eval { devices::make("$prefix$dir$dev") }; "$dir$dev"; }; } sub auto_fs() { grep { chop; $_ && !/nodev/ } cat_("/etc/filesystems"); } sub mount_options { my %non_defaults = ( sync => 'async', noatime => 'atime', noauto => 'auto', ro => 'rw', user => 'nouser', nodev => 'dev', noexec => 'exec', nosuid => 'suid', ); my @user_implies = qw(noexec nodev nosuid); \%non_defaults, \@user_implies; } sub mount_options_unpack { my ($part) = @_; my $packed_options = $part->{options}; my ($non_defaults, $user_implies) = mount_options(); my @auto_fs = auto_fs(); my %per_fs = ( iso9660 => [ qw(unhide) ], vfat => [ qw(umask=0) ], ntfs => [ qw(umask=0) ], nfs => [ qw(rsize=8192 wsize=8192) ], smbfs => [ qw(username= password=) ], davfs => [ qw(username= password= uid= gid=) ], reiserfs => [ 'notail' ], ); push @{$per_fs{$_}}, 'usrquota', 'grpquota' foreach 'ext2', 'ext3', 'xfs'; while (my ($fs, $l) = each %per_fs) { isThisFs($fs, $part) || $part->{type} eq 'auto' && member($fs, @auto_fs) or next; $non_defaults->{$_} = 1 foreach @$l; } $non_defaults->{encrypted} = 1 if !$part->{isFormatted} || isSwap($part); $non_defaults->{supermount} = 1 if member(type2fs($part), 'auto', @auto_fs); my $defaults = { reverse %$non_defaults }; my %options = map { $_ => '' } keys %$non_defaults; my @unknown; foreach (split(",", $packed_options)) { if ($_ eq 'user') { $options{$_} = 1 foreach 'user', @$user_implies; } elsif (exists $non_defaults->{$_}) { $options{$_} = 1; } elsif ($defaults->{$_}) { $options{$defaults->{$_}} = 0; } elsif (/(.*?=)(.*)/) { $options{$1} = $2; } else { push @unknown, $_; } } # merge those, for cleaner help $options{'rsize=8192,wsize=8192'} = delete $options{'rsize=8192'} && delete $options{'wsize=8192'} if exists $options{'rsize=8192'}; my $unknown = join(",", @unknown); \%options, $unknown; } sub mount_options_pack_ { my ($_part, $options, $unknown) = @_; my ($non_defaults, $user_implies) = mount_options(); my @l; if (delete $options->{user}) { push @l, 'user'; foreach (@$user_implies) { if (!delete $options->{$_}) { # overriding $options->{$non_defaults->{$_}} = 1; } } } push @l, map_each { if_($::b, $::a =~ /=$/ ? "$::a$::b" : $::a) } %$options; push @l, $unknown; join(",", uniq(grep { $_ } @l)); } sub mount_options_pack { my ($part, $options, $unknown) = @_; $part->{options} = mount_options_pack_($part, $options, $unknown); noreturn(); } sub mount_options_help { my %help = map { $_ => '' } @_; my %short = map { if_(/(.*?)=/, "$1=" => $_) } keys %help; foreach (split(':', $ENV{LANGUAGE}), '') { my $manpage = "/usr/share/man/$_/man8/mount.8.bz2"; -e $manpage or next; my ($tp, $option); foreach (`bzip2 -dc $manpage`) { my $prev_tp = $tp; $tp = /^\.(TP|RE)/; my ($s) = /^\.B (.*)/; if (($prev_tp && $s eq '\-o') .. /X^/) { if (my $v = ($prev_tp && $s =~ /^[a-z]/i) .. $tp) { if ($v == 1) { $s = $short{$s} || $s; $option = exists $help{$s} && !$help{$s} ? $s : ''; } elsif ($v !~ /E0/) { s/\\//g; s/\s*"(.*?)"\s*/$1/g if s/^\.BR\s+//; s/^\.B\s+//; $help{$option} .= $_ if $option; } } } } } %help; } sub set_default_options { my ($part, $is_removable, $useSupermount, $security, $iocharset, $codepage) = @_; my ($options, $unknown) = mount_options_unpack($part); if ($is_removable) { $options->{supermount} = $useSupermount; $part->{type} = 'auto'; } my $is_auto = isThisFs('auto', $part); if ($part->{media_type} eq 'cdrom') { $options->{ro} = 1; } if ($part->{media_type} eq 'fd') { # slow device so don't loose time, write now! $options->{sync} = 1; } if (isTrueFS($part)) { #- noatime on laptops (do not wake up the hd) #- Do not update inode access times on this #- file system (e.g, for faster access on the #- news spool to speed up news servers). $options->{noatime} = detect_devices::isLaptop(); } if (isThisFs('nfs', $part)) { put_in_hash($options, { nosuid => 1, 'rsize=8192,wsize=8192' => 1, soft => 1, }); } if (isThisFs('smbfs', $part)) { add2hash($options, { 'username=' => '%' }) if !$options->{'credentials='}; } if (isFat($part) || $is_auto) { put_in_hash($options, { user => 1, noexec => 0, }) if $is_removable; put_in_hash($options, { 'umask=0' => $security < 3, 'iocharset=' => $iocharset, 'codepage=' => $codepage, }); } if (isThisFs('ntfs', $part)) { put_in_hash($options, { ro => 1, 'umask=0' => $security < 3, 'iocharset=' => $iocharset }); } if (isThisFs('iso9660', $part) || $is_auto) { put_in_hash($options, { user => 1, noexec => 0, 'iocharset=' => $iocharset }); } if (isThisFs('reiserfs', $part)) { $options->{notail} = 1; } else { $options->{notail} = 0; } if (isLoopback($part) && !isSwap($part)) { #- no need for loop option for swap files $options->{loop} = 1; } # rationalize: no need for user if ($options->{autofs} || $options->{supermount}) { $options->{user} = 0; } # have noauto when we have user $options->{noauto} = 1 if $options->{user}; if ($options->{user}) { # ensure security (user_implies - noexec as noexec is not a security matter) $options->{$_} = 1 foreach 'nodev', 'nosuid'; } mount_options_pack($part, $options, $unknown); } sub set_all_default_options { my ($all_hds, $useSupermount, $security, $iocharset, $codepage) = @_; my @removables = @{$all_hds->{raw_hds}}; foreach my $part (fsedit::get_really_all_fstab($all_hds)) { set_default_options($part, member($part, @removables), $useSupermount, $security, $iocharset, $codepage); } } sub set_removable_mntpoints { my ($all_hds) = @_; my %names; foreach (@{$all_hds->{raw_hds}}) { my $name = detect_devices::suggest_mount_point($_) or next; my $s = ++$names{$name}; $_->{mntpoint} ||= "/mnt/$name" . ($s == 1 ? '' : $s); } } sub get_raw_hds { my ($prefix, $all_hds) = @_; $all_hds->{raw_hds} = [ detect_devices::removables() ]; get_major_minor(@{$all_hds->{raw_hds}}); my @fstab = read_fstab($prefix, "/etc/fstab", 'keep_freq_passno'); $all_hds->{nfss} = [ grep { isThisFs('nfs', $_) } @fstab ]; $all_hds->{smbs} = [ grep { isThisFs('smbfs', $_) } @fstab ]; $all_hds->{davs} = [ grep { isThisFs('davfs', $_) } @fstab ]; $all_hds->{special} = [ (grep { isThisFs('tmpfs', $_) } @fstab), { device => 'none', mntpoint => '/proc', type => 'proc' }, { device => 'none', mntpoint => '/dev/pts', type => 'devpts', options => 'mode=0620' }, ]; } ################################################################################ # formatting functions ################################################################################ sub disable_forced_fsck { my ($dev) = @_; run_program::run("tune2fs", "-c0", "-i0", devices::make($dev)); } sub format_ext2($@) { #- mke2fs -b (1024|2048|4096) -c -i(1024 > 262144) -N (1 > 100000000) -m (0-100%) -L volume-label #- tune2fs my ($dev, @options) = @_; $dev =~ m,(rd|ida|cciss)/, and push @options, qw(-b 4096 -R stride=16); #- For RAID only. push @options, qw(-b 1024 -O none) if arch() =~ /alpha/; run_program::raw({ timeout => 60 * 60 }, 'mke2fs', '-F', @options, devices::make($dev)) or die N("%s formatting of %s failed", any { $_ eq '-j' } @options ? "ext3" : "ext2", $dev); } sub format_ext3 { my ($dev, @options) = @_; format_ext2($dev, "-j", @options); disable_forced_fsck($dev); } sub format_reiserfs { my ($dev, @options) = @_; #TODO add -h tea run_program::raw({ timeout => 60 * 60 }, "mkreiserfs", "-ff", @options, devices::make($dev)) or die N("%s formatting of %s failed", "reiserfs", $dev); } sub format_xfs { my ($dev, @options) = @_; run_program::raw({ timeout => 60 * 60 }, "mkfs.xfs", "-f", "-q", @options, devices::make($dev)) or die N("%s formatting of %s failed", "xfs", $dev); } sub format_jfs { my ($dev, @options) = @_; run_program::raw({ timeout => 60 * 60 }, "mkfs.jfs", "-f", @options, devices::make($dev)) or die N("%s formatting of %s failed", "jfs", $dev); } sub format_dos { my ($dev, @options) = @_; run_program::raw({ timeout => 60 * 60 }, "mkdosfs", @options, devices::make($dev)) or die N("%s formatting of %s failed", "dos", $dev); } sub format_hfs { my ($dev, @options) = @_; run_program::raw({ timeout => 60 * 60 }, "hformat", @options, devices::make($dev)) or die N("%s formatting of %s failed", "HFS", $dev); } sub real_format_part { my ($part) = @_; $part->{isFormatted} and return; my $dev = $part->{real_device} || $part->{device}; my @options = if_($part->{toFormatCheck}, "-c"); log::l("formatting device $dev (type ", type2name($part->{type}), ")"); if (isExt2($part)) { push @options, "-F" if isLoopback($part); push @options, "-m", "0" if $part->{mntpoint} =~ m|^/home|; format_ext2($dev, @options); } elsif (isThisFs("ext3", $part)) { push @options, "-m", "0" if $part->{mntpoint} =~ m|^/home|; format_ext3($dev, @options); } elsif (isThisFs("reiserfs", $part)) { format_reiserfs($dev, @options, if_(c::kernel_version() =~ /^\Q2.2/, "-v", "1")); } elsif (isThisFs("xfs", $part)) { format_xfs($dev, @options); } elsif (isThisFs("jfs", $part)) { format_jfs($dev, @options); } elsif (isDos($part)) { format_dos($dev, @options); } elsif (isWin($part)) { format_dos($dev, @options, '-F', 32); } elsif (isThisFs('hfs', $part)) { format_hfs($dev, @options, '-l', "Untitled"); } elsif (isAppleBootstrap($part)) { format_hfs($dev, @options, '-l', "bootstrap"); } elsif (isSwap($part)) { my $check_blocks = any { /^-c$/ } @options; swap::make($dev, $check_blocks); } else { die N("I don't know how to format %s in type %s", $part->{device}, type2name($part->{type})); } $part->{isFormatted} = 1; } sub format_part { my ($raids, $part, $prefix) = @_; if (isRAID($part)) { require raid; raid::format_part($raids, $part); } elsif (isLoopback($part)) { loopback::format_part($part, $prefix); } else { real_format_part($part); } } ################################################################################ # mounting functions ################################################################################ sub set_loop { my ($part) = @_; if (!$part->{real_device}) { eval { modules::load('loop') }; $part->{real_device} = devices::set_loop(devices::make($part->{device}), $part->{encrypt_key}, $part->{options} =~ /encryption=(\w+)/); } } sub formatMount_part { my ($part, $raids, $fstab, $prefix, $callback) = @_; if (isLoopback($part)) { formatMount_part($part->{loopback_device}, $raids, $fstab, $prefix, $callback); } if (my $p = up_mount_point($part->{mntpoint}, $fstab)) { formatMount_part($p, $raids, $fstab, $prefix, $callback) unless loopback::carryRootLoopback($part); } if ($part->{encrypt_key}) { set_loop($part); } if ($part->{toFormat}) { $callback->($part) if $callback; format_part($raids, $part, $prefix); } mount_part($part, $prefix); } sub formatMount_all { my ($raids, $fstab, $prefix, $callback) = @_; formatMount_part($_, $raids, $fstab, $prefix, $callback) foreach sort { isLoopback($a) ? 1 : isSwap($a) ? -1 : 0 } grep { $_->{mntpoint} } @$fstab; #- ensure the link is there loopback::carryRootCreateSymlink($_, $prefix) foreach @$fstab; #- for fun :) #- that way, when install exits via ctrl-c, it gives hand to partition eval { local $SIG{__DIE__} = 'ignore'; my ($_type, $major, $minor) = devices::entry(fsedit::get_root($fstab)->{device}); output "/proc/sys/kernel/real-root-dev", makedev($major, $minor); }; } sub mount { my ($dev, $where, $fs, $rdonly, $options) = @_; log::l("mounting $dev on $where as type $fs, options $options"); -d $where or mkdir_p($where); $dev = part2device('', $dev, $fs); my @fs_modules = qw(vfat hfs romfs ufs reiserfs xfs jfs ext3); if (member($fs, 'smb', 'smbfs', 'nfs', 'davfs', 'ntfs') && $::isStandalone) { system('mount', '-t', $fs, $dev, $where, if_($options, '-o', $options)) == 0 or die N("mounting partition %s in directory %s failed", $dev, $where); return; #- do not update mtab, already done by mount(8) } elsif (member($fs, 'ext2', 'proc', 'usbdevfs', 'iso9660', @fs_modules)) { $where =~ s|/$||; my $flag = c::MS_MGC_VAL(); $flag |= c::MS_RDONLY() if $rdonly; my $mount_opt = ""; if ($fs eq 'vfat') { $mount_opt = 'check=relaxed'; } elsif ($fs eq 'reiserfs') { #- could be better if we knew if there is a /boot or not #- without knowing it, / is forced to be mounted with notail # if $where =~ m|/(boot)?$|; $mount_opt = 'notail'; #- notail in any case } elsif ($fs eq 'jfs' && !$rdonly) { #- needed if the system is dirty otherwise mounting read-write simply fails run_program::raw({ timeout => 60 * 60 }, "fsck.jfs", $dev) or do { my $err = $?; die "fsck.jfs failed" if $err & 0xfc00; }; } elsif ($fs eq 'ext2' || $fs eq 'ext3' && $::isInstall) { if (!$rdonly) { foreach ('-a', '-y') { run_program::raw({ timeout => 60 * 60 }, "fsck.ext2", $_, $dev); my $err = $?; if ($err & 0x0100) { log::l("fsck corrected partition $dev"); } if ($err & 0xfeff) { my $txt = sprintf("fsck failed on %s with exit code %d or signal %d", $dev, $err >> 8, $err & 255); $_ eq '-y' ? die($txt) : cdie($txt); } else { last; } } } # really mount as ext2 during install for speed up $fs = 'ext2'; } if (member($fs, @fs_modules)) { eval { modules::load($fs) }; } elsif ($fs eq 'iso9660') { eval { modules::load('isofs') }; } log::l("calling mount($dev, $where, $fs, $flag, $mount_opt)"); syscall_('mount', $dev, $where, $fs, $flag, $mount_opt) or die N("mounting partition %s in directory %s failed", $dev, $where) . " ($!)"; } else { log::l("skipping mounting $fs partition"); return; } eval { #- fail silently, /etc must be read-only append_to_file("/etc/mtab", "$dev $where $fs defaults 0 0\n"); }; } #- takes the mount point to umount (can also be the device) sub umount { my ($mntpoint) = @_; $mntpoint =~ s|/$||; log::l("calling umount($mntpoint)"); syscall_('umount', $mntpoint) or die N("error unmounting %s: %s", $mntpoint, $!); substInFile { $_ = '' if /(^|\s)$mntpoint\s/ } '/etc/mtab'; #- don't care about error, if we can't read, we won't manage to write... (and mess mtab) } sub mount_part { my ($part, $prefix, $rdonly) = @_; #- root carrier's link can't be mounted loopback::carryRootCreateSymlink($part, $prefix); log::l("isMounted=$part->{isMounted}, real_mntpoint=$part->{real_mntpoint}, mntpoint=$part->{mntpoint}"); if ($part->{isMounted} && $part->{real_mntpoint} && $part->{mntpoint}) { log::l("remounting partition on $prefix$part->{mntpoint} instead of $part->{real_mntpoint}"); if ($::isInstall) { #- ensure partition will not be busy. require install_any; install_any::getFile('XXX'); } eval { umount($part->{real_mntpoint}); rmdir $part->{real_mntpoint}; symlinkf "$prefix$part->{mntpoint}", $part->{real_mntpoint}; delete $part->{real_mntpoint}; $part->{isMounted} = 0; }; } return if $part->{isMounted}; unless ($::testing) { if (isSwap($part)) { swap::swapon($part->{device}); } else { $part->{mntpoint} or die "missing mount point for partition $part->{device}"; my $mntpoint = ($prefix || '') . $part->{mntpoint}; if (isLoopback($part) || $part->{encrypt_key}) { set_loop($part); } elsif (loopback::carryRootLoopback($part)) { $mntpoint = "/initrd/loopfs"; } my $dev = $part->{real_device} || $part->{device}; mount($dev, $mntpoint, type2fs($part), $rdonly, $part->{options}); rmdir "$mntpoint/lost+found"; } } $part->{isMounted} = $part->{isFormatted} = 1; #- assume that if mount works, partition is formatted } sub umount_part { my ($part, $prefix) = @_; $part->{isMounted} || $part->{real_mntpoint} or return; unless ($::testing) { if (isSwap($part)) { swap::swapoff($part->{device}); } elsif (loopback::carryRootLoopback($part)) { umount("/initrd/loopfs"); } else { umount(($prefix || '') . $part->{mntpoint} || devices::make($part->{device})); devices::del_loop(delete $part->{real_device}) if $part->{real_device}; } } $part->{isMounted} = 0; } sub umount_all($;$) { my ($fstab, $prefix) = @_; log::l("unmounting all filesystems"); foreach (sort { $b->{mntpoint} cmp $a->{mntpoint} } @$fstab) { $_->{mntpoint} and umount_part($_, $prefix); } } ################################################################################ # various functions ################################################################################ sub df { my ($part, $prefix) = @_; my $dir = "/tmp/tmp_fs_df"; return $part->{free} if exists $part->{free}; if ($part->{isMounted}) { $dir = ($prefix || '') . $part->{mntpoint}; } elsif ($part->{notFormatted} && !$part->{isFormatted}) { return; #- won't even try! } else { mkdir_p($dir); eval { mount($part->{device}, $dir, type2fs($part), 'readonly') }; if ($@) { $part->{notFormatted} = 1; $part->{isFormatted} = 0; unlink $dir; return; } } my (undef, $free) = MDK::Common::System::df($dir); if (!$part->{isMounted}) { umount($dir); unlink($dir) } $part->{free} = 2 * $free if defined $free; $part->{free}; } sub up_mount_point { my ($mntpoint, $fstab) = @_; while (1) { $mntpoint = dirname($mntpoint); $mntpoint ne "." or return; $_->{mntpoint} eq $mntpoint and return $_ foreach @$fstab; } } 1; >616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
package install_any;
use diagnostics;
use strict;
use vars qw(@ISA %EXPORT_TAGS @EXPORT_OK);
@ISA = qw(Exporter);
%EXPORT_TAGS = (
all => [ qw(getNextStep spawnShell addToBeDone) ],
);
@EXPORT_OK = map { @$_ } values %EXPORT_TAGS;
#-######################################################################################
#- misc imports
#-######################################################################################
use common qw(:common :system :functional :file);
use commands;
use run_program;
use partition_table qw(:types);
use partition_table_raw;
use devices;
use fsedit;
use network;
use modules;
use detect_devices;
use fs;
use log;
#-######################################################################################
#- Functions
#-######################################################################################
sub relGetFile($) {
local $_ = $_[0];
my $dir = m|/| ? "mdkinst" :
(member($_, qw(compss compssList compssUsers depslist hdlist)) ? "base" : "RPMS");
$_ = "Mandrake/$dir/$_";
s/i386/i586/;
$_;
}
sub getFile($) {
local $^W = 0;
if ($::o->{method} && $::o->{method} eq "ftp") {
require ftp;
*install_any::getFile = \&ftp::getFile;
} elsif ($::o->{method} && $::o->{method} eq "http") {
require http;
*install_any::getFile = \&http::getFile;
} else {
*install_any::getFile = sub($) {
open getFile, "/tmp/rhimage/" . relGetFile($_[0]) or return;
*getFile;
};
}
goto &getFile;
}
sub kernelVersion {
local $_ = readlink("$::o->{prefix}/boot/vmlinuz") || $::testing && "vmlinuz-2.2.testversion" or die "I couldn't find the kernel package!";
first(/vmlinuz-(.*)/);
}
sub getNextStep {
my ($s) = $::o->{steps}{first};
$s = $::o->{steps}{$s}{next} while $::o->{steps}{$s}{done} || !$::o->{steps}{$s}{reachable};
$s;
}
sub spawnShell {
return if $::o->{localInstall} || $::testing;
-x "/bin/sh" or die "cannot open shell - /usr/bin/sh doesn't exist";
fork and return;
local *F;
sysopen F, "/dev/tty2", 2 or die "cannot open /dev/tty2 -- no shell will be provided";
open STDIN, "<&F" or die '';
open STDOUT, ">&F" or die '';
open STDERR, ">&F" or die '';
close F;
c::setsid();
ioctl(STDIN, c::TIOCSCTTY(), 0) or warn "could not set new controlling tty: $!";
exec {"/bin/sh"} "-/bin/sh" or log::l("exec of /bin/sh failed: $!");
}
sub shells($) {
my ($o) = @_;
my @l = grep { -x "$o->{prefix}$_" } @{$o->{shells}};
@l ? @l : "/bin/bash";
}
sub getAvailableSpace {
my ($o) = @_;
do { $_->{mntpoint} eq '/usr' and return int($_->{size} * 512 / 1.07) } foreach @{$o->{fstab}};
do { $_->{mntpoint} eq '/' and return int($_->{size} * 512 / 1.07) } foreach @{$o->{fstab}};
if ($::testing) {
log::l("taking 200MB for testing");
return 200 << 20;
}
die "missing root partition";
}
sub setPackages($) {
my ($o) = @_;
require pkgs;
if (is_empty_hash_ref($o->{packages})) {
my $useHdlist = $o->{method} !~ /nfs|hd/ || $o->{isUpgrade};
eval { $o->{packages} = pkgs::psUsingHdlist() } if $useHdlist;
$o->{packages} = pkgs::psUsingDirectory() if !$useHdlist || $@;
push @{$o->{default_packages}}, "nfs-utils-clients" if $o->{method} eq "nfs";
push @{$o->{default_packages}}, "numlock" if $o->{miscellaneous}{numlock};
push @{$o->{default_packages}}, "kernel-secure" if $o->{security} > 3;
push @{$o->{default_packages}}, "kernel-smp" if $o->{security} <= 3 && detect_devices::hasSMP(); #- no need for kernel-smp if we have kernel-secure which is smp
push @{$o->{default_packages}}, "kernel-pcmcia-cs" if $o->{pcmcia};
push @{$o->{default_packages}}, "apmd" if $o->{pcmcia};
push @{$o->{default_packages}}, "raidtools" if $o->{raid} && !is_empty_array_ref($o->{raid}{raid});
pkgs::getDeps($o->{packages});
my $c; ($o->{compss}, $c) = pkgs::readCompss($o->{packages});
$o->{compssListLevels} = pkgs::readCompssList($o->{packages}, $c);
($o->{compssUsers}, $o->{compssUsersSorted}) = pkgs::readCompssUsers($o->{packages}, $o->{compss});
my @l = ();
push @l, "kapm" if $o->{pcmcia};
$_->{values} = [ map { $_ + 50 } @{$_->{values}} ] foreach grep {$_} map { $o->{packages}{$_} } @l;
grep { !$o->{packages}{$_} && log::l("missing base package $_") } @{$o->{base}} and die "missing some base packages";
} else {
$_->{selected} = 0 foreach values %{$o->{packages}};
}
#- this will be done if necessary in the selectPackagesToUpgrade,
#- move the selection here ? this will remove the little window.
unless ($o->{isUpgrade}) {
do {
my $p = $o->{packages}{$_} or log::l("missing base package $_"), next;
pkgs::select($o->{packages}, $p, 1);
} foreach @{$o->{base}};
do {
my $p = $o->{packages}{$_} or log::l("missing add-on package $_"), next;
pkgs::select($o->{packages}, $p);
} foreach @{$o->{default_packages}};
}
}
sub selectPackagesToUpgrade($) {
my ($o) = @_;
require pkgs;
pkgs::selectPackagesToUpgrade($o->{packages}, $o->{prefix}, $o->{base}, $o->{toRemove}, $o->{toSave});
}
sub addToBeDone(&$) {
my ($f, $step) = @_;
return &$f() if $::o->{steps}{$step}{done};
push @{$::o->{steps}{$step}{toBeDone}}, $f;
}
sub getHds {
my ($o) = @_;
my ($ok, $ok2) = 1;
my @drives = detect_devices::hds();
add2hash_($o->{partitioning}, { readonly => 1 }) if partition_table_raw::typeOfMBR($drives[0]{device}) eq 'system_commander';
getHds:
$o->{hds} = catch_cdie { fsedit::hds(\@drives, $o->{partitioning}) }
sub {
my ($err) = $@ =~ /(.*) at /;
$@ =~ /overlapping/ and $o->ask_warn('', $@), return 1;
$o->ask_okcancel(_("Error"),
[_("I can't read your partition table, it's too corrupted for me :(
I'll try to go on blanking bad partitions"), $err]) unless $o->{partitioning}{readonly};
$ok = 0; 1
};
if (is_empty_array_ref($o->{hds}) && $o->{autoSCSI}) {
$o->setupSCSI; #- ask for an unautodetected scsi card
goto getHds;
}
($o->{hds}, $o->{fstab}, $ok2) = fsedit::verifyHds($o->{hds}, $o->{partitioning}{readonly}, $ok);
fs::check_mounted($o->{fstab});
$o->{partitioning}{clearall} and return 1;
$o->ask_warn('',
_("DiskDrake failed to read correctly the partition table.
Continue at your own risk!")) if !$ok2 && $ok && !$o->{partitioning}{readonly};
$ok2;
}
sub searchAndMount4Upgrade {
my ($o) = @_;
my ($root, $found);
my $w = $::beginner && $o->wait_message('', _("Searching root partition."));
#- try to find the partition where the system is installed if beginner
#- else ask the user the right partition, and test it after.
getHds($o);
#- get all ext2 partition that may be root partition.
my %Parts = my %parts = map { $_->{device} => $_ } grep { isExt2($_) } @{$o->{fstab}};
while (keys(%parts) > 0) {
$root = $::beginner ? first(%parts) : $o->selectRootPartition(keys %parts);
$root = delete $parts{$root};
my $r; unless ($r = $root->{realMntpoint}) {
$r = $o->{prefix};
$root->{mntpoint} = "/";
log::l("trying to mount partition $root->{device}");
eval { fs::mount_part($root, $o->{prefix}, 'readonly') };
$r = "/*ERROR*" if $@;
}
$found = -d "$r/etc/sysconfig" && [ fs::read_fstab("$r/etc/fstab") ];
unless ($root->{realMntpoint}) {
log::l("umounting partition $root->{device}");
eval { fs::umount_part($root, $o->{prefix}) };
}
last if !is_empty_array_ref($found);
delete $root->{mntpoint};
$o->ask_warn(_("Information"),
_("%s: This is not a root partition, please select another one.", $root->{device})) unless $::beginner;
}
is_empty_array_ref($found) and die _("No root partition found");
log::l("found root partition : $root->{device}");
#- test if the partition has to be fschecked and remounted rw.
if ($root->{realMntpoint}) {
($o->{prefix}, $root->{mntpoint}) = ($root->{realMntpoint}, '/');
} else {
delete $root->{mntpoint};
($Parts{$_->{device}} || {})->{mntpoint} = $_->{mntpoint} foreach @$found;
map { $_->{mntpoint} = 'swap_upgrade' } grep { isSwap($_) } @{$o->{fstab}}; #- use all available swap.
#- TODO fsck, create check_mount_all ?
fs::mount_all([ grep { isExt2($_) || isSwap($_) } @{$o->{fstab}} ], $o->{prefix});
}
}
sub write_ldsoconf {
my ($prefix) = @_;
my $file = "$prefix/etc/ld.so.conf";
#- write a minimal ld.so.conf file unless it already exists.
unless (-s "$file") {
local *F;
open F, ">$file" or die "unable to open for writing $file";
print F "/usr/lib\n";
}
}
sub setAuthentication() {
my ($shadow, $md5, $nis, $nis_server) = @{$::o->{authentication} || {}}{qw(shadow md5 NIS NIS_server)};
my $p = $::o->{prefix};
enableMD5Shadow($p, $shadow, $md5);
enableShadow() if $shadow;
if ($nis) {
pkg_install($::o, "ypbind");
my $domain = $::o->{netc}{NISDOMAIN};
$domain || $nis_server ne "broadcast" or die _("Can't use broadcast with no NIS domain");
my $t = $domain ? "domain $domain" . ($nis_server ne "broadcast" && " server")
: "ypserver";
substInFile {
$_ = "#~$_" unless /^#/;
$_ .= "$t $nis_server\n" if eof;
} "$p/etc/yp.conf";
network::write_conf("$p/etc/sysconfig/network", $::o->{netc});
}
}
sub enableShadow() {
my $p = $::o->{prefix};
run_program::rooted($p, "pwconv") or log::l("pwconv failed");
run_program::rooted($p, "grpconv") or log::l("grpconv failed");
#- my $chpasswd = sub {
#- my ($name, $password) = @_;
#- $password =~ s/"/\\"/;
#-
#- local *log::l = sub {}; #- disable the logging (otherwise password visible in the log)
#- run_program::rooted($p, qq((echo "$password" ; sleep 1 ; echo "$password") | passwd $name));
#-#- run_program::rooted($p, "echo $name:$password | chpasswd");
#- };
#- &$chpasswd("root", $::o->{superuser}{password});
#- &$chpasswd($_->{name}, $_->{password}) foreach @{$::o->{users} || []};
}
sub enableMD5Shadow($$$) {
my ($prefix, $shadow, $md5) = @_;
substInFile {
if (/^password.*pam_pwdb.so/) {
s/\s*shadow//; s/\s*md5//;
s/$/ shadow/ if $shadow;
s/$/ md5/ if $md5;
}
} grep { -r $_ } map { "$prefix/etc/pam.d/$_" } qw(login rlogin passwd);
}
sub crypt($) {
my ($password) = @_;
$::o->{authentication}{md5} ?
c::crypt_md5($password, salt(8)) :
crypt ($password, salt(2));
}
sub lnx4win_postinstall {
my ($prefix) = @_;
my $dir = "/dos/lnx4win";
my $kernel = "$dir/vmlinuz";
rename $kernel, "$kernel.old";
commands::dd("if=$prefix/boot/vmlinuz", "of=$kernel");
run_program::run("rdev", $kernel, "/dev/loop7");
unlink "$dir/size.txt";
unlink "$dir/swapsize.txt";
mkdir "$prefix/initrd", 0755;
symlinkf "/initrd/dos", "$prefix/mnt/dos";
}
sub killCardServices {
my $pid = chop_(cat_("/tmp/cardmgr.pid"));
$pid and kill(15, $pid); #- send SIGTERM
}
sub hdInstallPath() {
cat_("/proc/mounts") =~ m|/tmp/(\S+)\s+/tmp/hdimage| or return;
my ($part) = grep { $_->{device} eq $1 } @{$::o->{fstab}};
$part->{mntpoint} or grep { $_->{mntpoint} eq "/mnt/hd" } @{$::o->{fstab}} and return;
$part->{mntpoint} ||= "/mnt/hd";
$part->{mntpoint} . first(readlink("/tmp/rhimage") =~ m|^/tmp/hdimage/(.*)|);
}
sub unlockCdrom() {
cat_("/proc/mounts") =~ m|/tmp/(\S+)\s+/tmp/rhimage| or return;
eval { ioctl detect_devices::tryOpen($1), c::CDROM_LOCKDOOR(), 0 };
}
sub ejectCdrom() {
cat_("/proc/mounts") =~ m|/tmp/(\S+)\s+/tmp/rhimage| or return;
my $f = eval { detect_devices::tryOpen($1) } or return;
getFile("XXX"); #- close still opened filehandle
eval { fs::umount("/tmp/rhimage") };
ioctl $f, c::CDROMEJECT(), 1;
}
sub setupFB {
my ($o, $vga) = @_;
#- install needed packages for frame buffer.
require pkgs;
pkgs::select($o->{packages}, $o->{packages}{'kernel-fb'});
pkgs::select($o->{packages}, $o->{packages}{'XFree86-FBDev'});
$o->installPackages($o->{packages});
$vga ||= 785; #- assume at least 640x480x16.
require lilo;
#- update lilo entries with a new fb label. a bit hack unless
#- a frame buffer kernel is used, in such case we use it instead
#- with the right mode, nothing more to do.
foreach (qw(secure smp)) {
if ($o->{bootloader}{entries}{"/boot/vmlinuz-$_"}) {
$o->{bootloader}{entries}{"/boot/vmlinuz-$_"}{vga} = $vga;
lilo::install($o->{prefix}, $o->{bootloader});
return 1;
}
}
my $root = $o->{bootloader}{entries}{'/boot/vmlinuz'}{root};
if (lilo::add_kernel($o->{prefix}, $o->{bootloader}, kernelVersion(), 'fb',
{
label => 'linux-fb',
root => $root,
vga => $vga,
})) {
$o->{bootloader}{default} = 'linux-fb';
lilo::install($o->{prefix}, $o->{bootloader});
} else {
log::l("unable to install kernel with frame buffer support, disabling");
return 0;
}
1;
}
sub auto_inst_file() { ($::g_auto_install ? "/tmp" : "$::o->{prefix}/root") . "/auto_inst.cfg.pl" }
sub g_auto_install(;$) {
my ($f) = @_; $f ||= auto_inst_file;
my $o = {};
$o->{default_packages} = [ map { $_->{name} } grep { $_->{selected} && !$_->{base} } values %{$::o->{packages}} ];
my @fields = qw(mntpoint type size);
$o->{partitions} = [ map { my %l; @l{@fields} = @$_{@fields}; \%l } grep { $_->{mntpoint} } @{$::o->{fstab}} ];
exists $::o->{$_} and $o->{$_} = $::o->{$_} foreach qw(lang autoSCSI authentication printer mouse netc timezone superuser intf keyboard mkbootdisk base users installClass partitioning isUpgrade manualFstab nomouseprobe crypto modem useSupermount); #- TODO modules bootloader
if (my $card = $::o->{X}{card}) {
$o->{X}{card}{$_} = $card->{$_} foreach qw(default_depth);
if ($card->{default_depth} and my $depth = $card->{depth}{$card->{default_depth}}) {
$depth ||= [];
$o->{X}{card}{resolution_wanted} ||= join "x", @{$depth->[0]} unless is_empty_array_ref($depth->[0]);
}
}
#- local $o->{partitioning}{clearall} = 1;
$_ = { %{$_ || {}} }, delete @$_{qw(oldu oldg password password2)} foreach $o->{superuser}, @{$o->{users} || []};
local *F;
open F, ">$f" or log::l("can't output the auto_install script in $f"), return;
print F Data::Dumper->Dump([$o], ['$o']), "\0";
}
sub loadO {
my ($O, $f) = @_; $f ||= auto_inst_file;
my $o;
if ($f =~ /^(floppy|patch)$/) {
my $f = $f eq "floppy" ? "auto_inst.cfg" : "patch";
unless ($::testing) {
fs::mount(devices::make("fd0"), "/mnt", "vfat", 0);
$f = "/mnt/$f";
}
-e $f or $f .= ".pl";
my $b = before_leaving {
fs::umount("/mnt") unless $::testing;
modules::unload($_) foreach qw(vfat fat);
};
$o = loadO($O, $f);
} else {
-e $f or $f .= ".pl";
{
local *F;
open F, $f or die _("Error reading file $f");
local $/ = "\0";
no strict;
eval <F>;
}
$@ and log::l _("Bad kickstart file %s (failed %s)", $f, $@);
add2hash_($o ||= {}, $O);
}
bless $o, ref $O;
}
sub pkg_install {
my ($o, $name) = @_;
require pkgs;
pkgs::select($o->{packages}, $o->{packages}{$name} || die "$name rpm not found");
install_steps::installPackages ($o, $o->{packages});
}
sub fsck_option() {
my $y = $::o->{security} < 3 && $::beginner ? "-y " : "";
substInFile { s/^(\s*fsckoptions="?)(-y )?/$1$y/ } "$::o->{prefix}/etc/rc.d/rc.sysinit";
}
sub install_urpmi {
my ($prefix, $method) = @_;
(my $name = _("installation")) =~ s/\s/_/g; #- in case translators are too good :-/
my $f = "$prefix/var/lib/urpmi/hdlist.$name";
{
my $fd = getFile("hdlist") or return;
local *OUT;
open OUT, ">$f" or log::l("failed to write $f"), return;
local $/ = \ (16 * 1024);
print OUT foreach <$fd>;
}
{
local *F = getFile("depslist");
output("$prefix/var/lib/urpmi/depslist", <F>);
}
{
local *LIST;
open LIST, ">$prefix/var/lib/urpmi/list.$name" or log::l("failed to write list.$name"), return;
my $dir = ${{ nfs => "file://mnt/nfs",
hd => "file:/" . hdInstallPath,
ftp => $ENV{URLPREFIX},
http => $ENV{URLPREFIX},
cdrom => "removable_cdrom_1://mnt/cdrom" }}{$method};
local *FILES; open FILES, "hdlist2files $f|";
chop, print LIST "$dir/Mandrake/RPMS/$_\n" foreach <FILES>;
close FILES or log::l("hdlist2files failed"), return;
run_program::run("gzip", "-9", $f);
$dir .= "/Mandrake/RPMS with ../base/hdlist" if $method =~ /ftp|http/;
eval { output "$prefix/etc/urpmi/urpmi.cfg", "$name $dir\n" };
}
}
sub list_passwd() {
my ($e, @l);
setpwent();
while (@{$e = [ getpwent() ]}) { push @l, $e }
endpwent();
@l;
}
sub list_home() {
map { $_->[7] } grep { $_->[2] >= 500 } list_passwd();
}
sub template2userfile($$$$%) {
my ($prefix, $inputfile, $outputrelfile, $force, %toreplace) = @_;
foreach ("/etc/skel", "/root", list_home()) {
my $outputfile = "$prefix/$_/$outputrelfile";
if (-d dirname($outputfile) && ($force || ! -e $outputfile)) {
log::l("generating $outputfile from template $inputfile");
template2file($inputfile, $outputfile, %toreplace);
}
}
}
sub update_userkderc($$$) {
my ($prefix, $cat, $subst) = @_;
foreach ("/etc/skel", "/root", list_home()) {
my ($inputfile, $outputfile) = ("$prefix$_/.kderc", "$prefix$_/.kderc.new");
my %tosubst = (%$subst);
local *INFILE; local *OUTFILE;
open INFILE, $inputfile or return;
open OUTFILE, ">$outputfile" or return;
print OUTFILE map {
if (my $i = /^\s*\[$cat\]/i ... /^\s*\[/) {
if (/^\s*(\w*)=/ && $tosubst{lc($1)}) {
delete $tosubst{lc($1)};
} else {
($i > 1 && /^\s*\[/ && join '', values %tosubst). $_;
}
} else {
$_;
}
} <INFILE>;
print OUTFILE "[$cat]\n", values %tosubst if values %tosubst; #- if categorie has not been found above.