From 6e230863720c63b6a39a53f3c0a15de771a373d2 Mon Sep 17 00:00:00 2001 From: Pascal Rigaux Date: Fri, 10 Jun 2005 04:40:18 +0000 Subject: move loopback.pm to fs/loopback.pm (since it's tightly tight to many fs* things) --- perl-install/bootloader.pm | 6 +- perl-install/diskdrake/hd_gtk.pm | 1 - perl-install/diskdrake/interactive.pm | 6 +- perl-install/fs.pm | 16 ++-- perl-install/fs/format.pm | 3 +- perl-install/fs/get.pm | 3 +- perl-install/fs/loopback.pm | 125 +++++++++++++++++++++++++++++ perl-install/fsedit.pm | 4 +- perl-install/install_steps.pm | 1 - perl-install/install_steps_interactive.pm | 1 - perl-install/loopback.pm | 126 ------------------------------ perl-install/pkgs.pm | 8 +- 12 files changed, 149 insertions(+), 151 deletions(-) create mode 100644 perl-install/fs/loopback.pm delete mode 100644 perl-install/loopback.pm diff --git a/perl-install/bootloader.pm b/perl-install/bootloader.pm index 1e93cd6ac..0d886f4cb 100644 --- a/perl-install/bootloader.pm +++ b/perl-install/bootloader.pm @@ -13,7 +13,7 @@ use fs::get; use log; use any; use devices; -use loopback; +use fs::loopback; use detect_devices; use partition_table::raw; use run_program; @@ -108,7 +108,7 @@ sub mkinitrd { my $initrd = $entry->{initrd}; $::testing || -e "$::prefix/$initrd" and return 1; - my $loop_boot = loopback::prepare_boot(); + my $loop_boot = fs::loopback::prepare_boot(); modules::load('loop'); my @options = ( @@ -121,7 +121,7 @@ sub mkinitrd { } add_boot_splash($entry->{initrd}, $entry->{vga}); - loopback::save_boot($loop_boot); + fs::loopback::save_boot($loop_boot); -e "$::prefix/$initrd"; } diff --git a/perl-install/diskdrake/hd_gtk.pm b/perl-install/diskdrake/hd_gtk.pm index 036bfff85..95860d7e5 100644 --- a/perl-install/diskdrake/hd_gtk.pm +++ b/perl-install/diskdrake/hd_gtk.pm @@ -10,7 +10,6 @@ use fs::type; use detect_devices; use diskdrake::interactive; use run_program; -use loopback; use devices; use raid; use any; diff --git a/perl-install/diskdrake/interactive.pm b/perl-install/diskdrake/interactive.pm index bb414f451..f7bcc9348 100644 --- a/perl-install/diskdrake/interactive.pm +++ b/perl-install/diskdrake/interactive.pm @@ -9,7 +9,7 @@ use partition_table; use partition_table::raw; use detect_devices; use run_program; -use loopback; +use fs::loopback; use devices; use fsedit; use raid; @@ -902,7 +902,7 @@ sub Loopback { my $handle = any::inspect($real_part) or $in->ask_warn('', N("This partition can not be used for loopback")), return; - my ($min, $max) = (1, loopback::getFree($handle->{dir}, $real_part)); + my ($min, $max) = (1, fs::loopback::getFree($handle->{dir}, $real_part)); $max = min($max, 1 << (31 - 9)) if $real_part->{fs_type} eq 'vfat'; #- FAT does not handle file size bigger than 2GB my $part = { maxsize => $max, size => 0, loopback_device => $real_part, notFormatted => 1 }; if (!fsedit::suggest_part($part, $all_hds)) { @@ -921,7 +921,7 @@ sub Loopback { complete => sub { $part->{loopback_file} or $in->ask_warn('', N("Give a file name")), return 1, 0; $part->{loopback_file} =~ s|^([^/])|/$1|; - if (my $size = loopback::verifFile($handle->{dir}, $part->{loopback_file}, $real_part)) { + if (my $size = fs::loopback::verifFile($handle->{dir}, $part->{loopback_file}, $real_part)) { $size == -1 and $in->ask_warn('', N("File is already used by another loopback, choose another one")), return 1, 0; $in->ask_yesorno('', N("File already exists. Use it?")) or return 1, 0; delete $part->{notFormatted}; diff --git a/perl-install/fs.pm b/perl-install/fs.pm index ad62b3e5c..5ba4d68ad 100644 --- a/perl-install/fs.pm +++ b/perl-install/fs.pm @@ -12,11 +12,11 @@ use fs::type; use fs::get; use fs::format; use fs::mount_options; +use fs::loopback; use run_program; use detect_devices; use modules; use fsedit; -use loopback; sub read_fstab { @@ -305,13 +305,13 @@ sub prepare_write_fstab { my $real_mntpoint = $_->{mntpoint} || ${{ '/tmp/hdimage' => '/mnt/hd' }}{$_->{real_mntpoint}}; mkdir_p("$o_prefix$real_mntpoint") if $real_mntpoint =~ m|^/|; - my $mntpoint = loopback::carryRootLoopback($_) ? '/initrd/loopfs' : $real_mntpoint; + my $mntpoint = fs::loopback::carryRootLoopback($_) ? '/initrd/loopfs' : $real_mntpoint; my ($freq, $passno) = exists $_->{freq} ? ($_->{freq}, $_->{passno}) : isTrueLocalFS($_) && $_->{options} !~ /encryption=/ && (!$_->{is_removable} || member($_->{mntpoint}, fs::type::directories_needed_to_boot())) ? - (1, $_->{mntpoint} eq '/' ? 1 : loopback::carryRootLoopback($_) ? 0 : 2) : + (1, $_->{mntpoint} eq '/' ? 1 : fs::loopback::carryRootLoopback($_) ? 0 : 2) : (0, 0); if (($device eq 'none' || !$new{$device}) && ($mntpoint eq 'swap' || !$new{$mntpoint})) { @@ -451,7 +451,7 @@ sub formatMount_part { formatMount_part($part->{loopback_device}, $raids, $fstab, $prefix, $wait_message); } if (my $p = fs::get::up_mount_point($part->{mntpoint}, $fstab)) { - formatMount_part($p, $raids, $fstab, $prefix, $wait_message) unless loopback::carryRootLoopback($part); + formatMount_part($p, $raids, $fstab, $prefix, $wait_message) if !fs::loopback::carryRootLoopback($part); } if ($part->{toFormat}) { fs::format::part($raids, $part, $prefix, $wait_message); @@ -465,7 +465,7 @@ sub formatMount_all { foreach sort { isLoopback($a) ? 1 : isSwap($a) ? -1 : 0 } grep { $_->{mntpoint} } @$fstab; #- ensure the link is there - loopback::carryRootCreateSymlink($_, $prefix) foreach @$fstab; + fs::loopback::carryRootCreateSymlink($_, $prefix) foreach @$fstab; #- for fun :) #- that way, when install exits via ctrl-c, it gives hand to partition @@ -568,7 +568,7 @@ sub mount_part { my ($part, $o_prefix, $b_rdonly, $o_wait_message) = @_; #- root carrier's link can not be mounted - loopback::carryRootCreateSymlink($part, $o_prefix); + fs::loopback::carryRootCreateSymlink($part, $o_prefix); log::l("mount_part: " . join(' ', map { "$_=$part->{$_}" } 'device', 'mntpoint', 'isMounted', 'real_mntpoint')); if ($part->{isMounted} && $part->{real_mntpoint} && $part->{mntpoint}) { @@ -601,7 +601,7 @@ sub mount_part { } elsif ($part->{options} =~ /encrypted/) { log::l("skip mounting $part->{device} since we do not have the encrypt_key"); return; - } elsif (loopback::carryRootLoopback($part)) { + } elsif (fs::loopback::carryRootLoopback($part)) { $mntpoint = "/initrd/loopfs"; } my $dev = $part->{real_device} || part2wild_device_name('', $part); @@ -620,7 +620,7 @@ sub umount_part { unless ($::testing) { if (isSwap($part)) { swapoff($part->{device}); - } elsif (loopback::carryRootLoopback($part)) { + } elsif (fs::loopback::carryRootLoopback($part)) { umount("/initrd/loopfs"); } else { umount(($o_prefix || '') . $part->{mntpoint} || devices::make($part->{device})); diff --git a/perl-install/fs/format.pm b/perl-install/fs/format.pm index ad4e77508..4f0aff353 100644 --- a/perl-install/fs/format.pm +++ b/perl-install/fs/format.pm @@ -6,6 +6,7 @@ use strict; use run_program; use common; use fs::type; +use fs::loopback; use log; my %cmds = ( @@ -46,7 +47,7 @@ sub part { raid::format_part($raids, $part); } elsif (isLoopback($part)) { $wait_message->(N("Creating and formatting file %s", $part->{loopback_file})) if $wait_message; - loopback::format_part($part, $prefix); + fs::loopback::format_part($part, $prefix); } else { $wait_message->(N("Formatting partition %s", $part->{device})) if $wait_message; part_raw($part, $wait_message); diff --git a/perl-install/fs/get.pm b/perl-install/fs/get.pm index 6b982c1e9..dca0a5aa9 100644 --- a/perl-install/fs/get.pm +++ b/perl-install/fs/get.pm @@ -5,6 +5,7 @@ use strict; use partition_table; use fs::type; +use fs::loopback; use fs; use common; use log; @@ -87,7 +88,7 @@ sub file2part { $file = $b_keep_simple_symlinks ? common::expand_symlinks_but_simple("$::prefix$file") : expand_symlinks("$::prefix$file"); unless ($file =~ s/^$::prefix//) { - my $part = find { loopback::carryRootLoopback($_) } @$fstab or die; + my $part = find { fs::loopback::carryRootLoopback($_) } @$fstab or die; log::l("found $part->{mntpoint}"); $file =~ s|/initrd/loopfs|$part->{mntpoint}|; } diff --git a/perl-install/fs/loopback.pm b/perl-install/fs/loopback.pm new file mode 100644 index 000000000..a7c6c913d --- /dev/null +++ b/perl-install/fs/loopback.pm @@ -0,0 +1,125 @@ +package fs::loopback; # $Id$ + +use diagnostics; +use strict; + +#-###################################################################################### +#- misc imports +#-###################################################################################### +use common; +use fs::type; +use fs; +use log; + + +sub carryRootLoopback { + my ($part) = @_; + $_->{mntpoint} eq '/' and return 1 foreach @{$part->{loopback} || []}; + 0; +} + +sub check_circular_mounts { + my ($part, $all_hds) = @_; + + my $fstab = [ fs::get::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::get::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::format::part_raw($part, undef); +} + +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 not 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; + diff --git a/perl-install/fsedit.pm b/perl-install/fsedit.pm index 2fc6dd999..a92b670e8 100644 --- a/perl-install/fsedit.pm +++ b/perl-install/fsedit.pm @@ -11,9 +11,9 @@ use common; use partition_table; use partition_table::raw; use fs::type; +use fs::loopback; use detect_devices; use devices; -use loopback; use log; use fs; @@ -424,7 +424,7 @@ Please be sure to add a /boot partition") if $mntpoint eq "/" && isLVM($part) && if $part->{options} =~ /encrypted/ && member($mntpoint, qw(/ /usr /var /boot)); local $part->{mntpoint} = $mntpoint; - loopback::check_circular_mounts($part, $all_hds); + fs::loopback::check_circular_mounts($part, $all_hds); } sub add { diff --git a/perl-install/install_steps.pm b/perl-install/install_steps.pm index 1a519868d..29ad182da 100644 --- a/perl-install/install_steps.pm +++ b/perl-install/install_steps.pm @@ -17,7 +17,6 @@ use run_program; use lang; use keyboard; use fsedit; -use loopback; use do_pkgs; use pkgs; use any; diff --git a/perl-install/install_steps_interactive.pm b/perl-install/install_steps_interactive.pm index 5bbe28763..765b69f43 100644 --- a/perl-install/install_steps_interactive.pm +++ b/perl-install/install_steps_interactive.pm @@ -21,7 +21,6 @@ use detect_devices; use run_program; use devices; use fsedit; -use loopback; use mouse; use modules; use modules::interactive; diff --git a/perl-install/loopback.pm b/perl-install/loopback.pm deleted file mode 100644 index 96e542ab8..000000000 --- a/perl-install/loopback.pm +++ /dev/null @@ -1,126 +0,0 @@ -package loopback; # $Id$ - -use diagnostics; -use strict; - -#-###################################################################################### -#- misc imports -#-###################################################################################### -use MDK::Common::System; -use common; -use fs::type; -use fs; -use log; - - -sub carryRootLoopback { - my ($part) = @_; - $_->{mntpoint} eq '/' and return 1 foreach @{$part->{loopback} || []}; - 0; -} - -sub check_circular_mounts { - my ($part, $all_hds) = @_; - - my $fstab = [ fs::get::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::get::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::format::part_raw($part, undef); -} - -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 not 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; - diff --git a/perl-install/pkgs.pm b/perl-install/pkgs.pm index d9ad942e2..2f8dd48dd 100644 --- a/perl-install/pkgs.pm +++ b/perl-install/pkgs.pm @@ -12,7 +12,7 @@ use run_program; use detect_devices; use log; use fs; -use loopback; +use fs::loopback; use c; our %preferred = map { $_ => undef } qw(lilo perl-base gstreamer-oss openjade ctags glibc curl sane-backends postfix mdkkdm gcc gcc-cpp gcc-c++ proftpd ghostscript-X vim-minimal kernel db1 db2 libxpm4 zlib1 libncurses5 harddrake cups apache); @@ -1050,7 +1050,7 @@ sub install { return if !@$toInstall; #- for root loopback'ed /boot - my $loop_boot = loopback::prepare_boot(); + my $loop_boot = fs::loopback::prepare_boot(); #- first stage to extract some important information #- about the selected packages. This is used to select @@ -1081,7 +1081,7 @@ sub install { if ($nb == 0 && scalar(@transToInstall) == 0) { cleanHeaders(); - loopback::save_boot($loop_boot); + fs::loopback::save_boot($loop_boot); return; } @@ -1265,7 +1265,7 @@ sub install { cleanHeaders(); - loopback::save_boot($loop_boot); + fs::loopback::save_boot($loop_boot); } sub remove { -- cgit v1.2.1