diff options
Diffstat (limited to 'perl-install/fs/format.pm')
-rw-r--r-- | perl-install/fs/format.pm | 229 |
1 files changed, 188 insertions, 41 deletions
diff --git a/perl-install/fs/format.pm b/perl-install/fs/format.pm index 6a7a0856b..1fa90c29e 100644 --- a/perl-install/fs/format.pm +++ b/perl-install/fs/format.pm @@ -1,4 +1,4 @@ -package fs::format; # $Id$ +package fs::format; use diagnostics; use strict; @@ -10,28 +10,57 @@ use fs::type; use fs::loopback; use log; +=head1 SYNOPSYS + +B<fs::format> enables to format filesystems. + +=head1 Variables + +=over + +=item %cmds + +Commands to format filesystem: + +For each filesystem, list: [ package_name, command_to_use, options_to_use ] + +=cut + my %cmds = ( ext2 => [ 'e2fsprogs', 'mkfs.ext2', '-F' ], ext3 => [ 'e2fsprogs', 'mkfs.ext3', '-F' ], - ext4 => [ 'e2fsprogs', 'mkfs.ext4', '-F' ], # FIXME: enable more options once we've better mkfs support + ext4 => [ 'e2fsprogs', 'mkfs.ext4', '-F' ], + f2fs => [ 'f2fs-tools', 'mkfs.f2fs', '-f' ], reiserfs => [ 'reiserfsprogs', 'mkfs.reiserfs', '-ff' ], - reiser4 => [ 'reiser4progs', 'mkfs.reiser4', '-f', '-y' ], xfs => [ 'xfsprogs', 'mkfs.xfs', '-f', '-q' ], jfs => [ 'jfsutils', 'mkfs.jfs', '-f' ], hfs => [ 'hfsutils', 'hformat' ], - dos => [ 'dosfstools', 'mkdosfs' ], - vfat => [ 'dosfstools', 'mkdosfs', '-F', '32' ], - swap => [ 'util-linux-ng', 'mkswap' ], - ntfs => [ 'ntfsprogs', 'mkntfs', '--fast' ], - 'ntfs-3g' => [ 'ntfsprogs', 'mkntfs', '--fast' ], + dos => [ 'dosfstools', 'mkfs.fat' ], + vfat => [ 'dosfstools', 'mkfs.fat', '-F', '32' ], + exfat => [ 'exfatprogs', 'mkfs.exfat' ], + swap => [ 'util-linux', 'mkswap' ], + ntfs => [ 'ntfs-3g', 'mkfs.ntfs', '--fast' ], + 'ntfs-3g' => [ 'ntfs-3g', 'mkfs.ntfs', '--fast' ], + btrfs => [ 'btrfs-progs', 'mkfs.btrfs', '-f' ], + nilfs2 => [ 'nilfs-utils', 'mkfs.nilfs2', '-f' ], ); -my %LABELs = ( #- option, length, handled_by_mount + +=item %LABELs + +mkfs option to use in order to set the label + label specs. + +For each filesystem, list: [ option, max_length, handled_by_mount ] + +=cut + +my %LABELs = ( ext2 => [ '-L', 16, 1 ], ext3 => [ '-L', 16, 1 ], ext4 => [ '-L', 16, 1 ], + exfat => [ '-L', 16, 1 ], + f2fs => [ '-l', 16, 1 ], reiserfs => [ '-l', 16, 1 ], - reiser4 => [ '-L', 16, 1 ], xfs => [ '-L', 12, 1 ], jfs => [ '-L', 16, 1 ], hfs => [ '-l', 27, 0 ], @@ -40,26 +69,67 @@ my %LABELs = ( #- option, length, handled_by_mount swap => [ '-L', 15, 1 ], ntfs => [ '-L', 128, 0 ], 'ntfs-3g' => [ '-L', 128, 0 ], + btrfs => [ '-L', 256, 1 ], + nilfs2 => [ '-L', 16, 1 ], ); -my %edit_LABEL = ( # package, command, option -# If option is defined, run <command> <option> <label> <device> -# If no option, run <command> <device> <label> +=item %edit_LABEL + +Commands to set the file system label. + +For each filesystem, list: [ package, command, option ] + +If option is defined, run <command> <option> <label> <device> + +If no option, run <command> <device> <label> + +=cut + +my %edit_LABEL = ( # ext2 => [ 'e2fsprogs', 'tune2fs', '-L' ], ext3 => [ 'e2fsprogs', 'tune2fs', '-L' ], ext4 => [ 'e2fsprogs', 'tune2fs', '-L' ], reiserfs => [ 'reiserfsprogs', 'reiserfstune', '-l' ], -# reiser4 xfs => [ 'xfsprogs', 'xfs_admin', '-L' ], jfs => [ 'jfsutils', 'jfs_tune', '-L' ], # hfs - dos => [ 'dosfstools', 'dosfslabel' ], - vfat => [ 'dosfstools', 'dosfslabel' ], -# swap => [ 'util-linux-ng', 'mkswap' ], - ntfs => [ 'ntfsprogs', 'ntfslabel' ], - 'ntfs-3g' => [ 'ntfsprogs', 'ntfslabel' ], + dos => [ 'mtools', 'mlabel', '-i' ], + exfat => [ 'exfatprogs', 'tune.exfat', '-L' ], + vfat => [ 'mtools', 'mlabel', '-i' ], + swap => [ 'util-linux', 'swaplabel', '-L' ], + ntfs => [ 'ntfs-3g', 'ntfslabel' ], + 'ntfs-3g' => [ 'ntfs-3g', 'ntfslabel' ], + btrfs => [ 'btrfs-progs', 'btrfs', 'filesystem', 'label' ], + nilfs2 => [ 'nilfs-utils', 'nilfs-tune', '-L' ], ); +=item %preserve_UUID + +For each filesystem, list: [ option, max_length, handled_by_mount ] + +Those are used in order to preserve UUID on fs where we couldn't enforce it while formatting. + +=cut + +my %preserve_UUID = ( # package, command + jfs => [ 'jfsutils', 'jfs_tune', ], + xfs => [ 'xfsprogs', 'xfs_admin' ], + nilfs2 => [ 'nilfs-utils', 'nilfs-tune' ], +); + + +=back + +=head1 Functions + +=over + +=item package_needed_for_partition_type($part) + +Return the package needed for that partition's type. + +=cut + sub package_needed_for_partition_type { my ($part) = @_; my $l = $cmds{$part->{fs_type}} or return; @@ -75,14 +145,14 @@ sub check_package_is_installed_format { my ($do_pkgs, $fs_type) = @_; my ($pkg, $binary) = @{$cmds{$fs_type} || return}; - whereis_binary($binary) || $do_pkgs->ensure_binary_is_installed($pkg, $binary); #- ensure_binary_is_installed checks binary chrooted, whereas we run the binary non-chrooted (pb for Mandriva One) + whereis_binary($binary) || $do_pkgs->ensure_binary_is_installed($pkg, $binary); #- ensure_binary_is_installed checks binary chrooted, whereas we run the binary non-chrooted (pb for Mageia One) } sub check_package_is_installed_label { my ($do_pkgs, $fs_type) = @_; my ($pkg, $binary) = @{$edit_LABEL{$fs_type} || return}; - whereis_binary($binary) || $do_pkgs->ensure_binary_is_installed($pkg, $binary); #- ensure_binary_is_installed checks binary chrooted, whereas we run the binary non-chrooted (pb for Mandriva One) + whereis_binary($binary) || $do_pkgs->ensure_binary_is_installed($pkg, $binary); #- ensure_binary_is_installed checks binary chrooted, whereas we run the binary non-chrooted (pb for Mageia One) } sub canEditLabel { @@ -90,6 +160,12 @@ sub canEditLabel { to_bool($edit_LABEL{$part->{fs_type}}); } +=item part($all_hds, $part, $wait_message) + +Frontend to part_raw() + +=cut + sub part { my ($all_hds, $part, $wait_message) = @_; if (isRAID($part)) { @@ -103,13 +179,20 @@ sub part { $wait_message->(N("Formatting partition %s", $part->{device})) if $wait_message; part_raw($part, $wait_message); } + undef $part->{toFormat}; } +=item write_label($part) + +Set the label on the filesystem hold in $part. + +=cut + sub write_label { my ($part) = @_; $part->{device_LABEL_changed} or return; - $part->{isNotFormatted} and return; + maybeFormatted($part) or return; if ($part->{encrypt_key}) { fs::mount::set_loop($part); @@ -118,15 +201,50 @@ sub write_label { my $dev = $part->{real_device} || $part->{device}; my ($_pkg, $cmd, @first_options) = @{$edit_LABEL{$part->{fs_type}} || die N("I do not know how to set label on %s with type %s", $part->{device}, $part->{fs_type})}; my @args; - if (defined $first_options[0]) { + if ($cmd eq 'mlabel') { + @args = ($cmd, @first_options, devices::make($dev), '::' . $part->{device_LABEL}); + } elsif ($cmd eq 'btrfs') { + # btrfs needs reverse ordering + @args = ($cmd, @first_options, devices::make($dev), $part->{device_LABEL}); + } elsif (defined $first_options[0]) { @args = ($cmd, @first_options, $part->{device_LABEL}, devices::make($dev)); } else { @args = ($cmd, devices::make($dev), $part->{device_LABEL}); } - run_program::raw({ timeout => 'never' }, @args) or die N("setting label on %s failed", $dev); + run_program::raw({ timeout => 'never' }, @args) or die N("setting label on %s failed, is it formatted?", $dev); delete $part->{device_LABEL_changed}; } +sub write_btrfs_uuid { + my ($UUID, $dev) = @_; + $dev = devices::make($dev); + my $status = system("echo y|btrfstune -U $UUID $dev") == 0; + die "failed to set UUID to '$UUID' on $dev (status=$status)" if !$status; +} + +=item sub option_to_preserve_UUID_while_formating($part, $fs_type) + +Return the options needed to preserve UUID while formating + +=cut + +sub option_to_preserve_UUID_while_formating { + my ($part, $fs_type) = @_; + if (member($fs_type, qw(swap ext2 ext3 ext4))) { + return '-U', $part->{device_UUID} if $part->{device_UUID}; + } elsif ($fs_type eq 'reiserfs') { + return '-u', $part->{device_UUID} if $part->{device_UUID}; + } + return (); +} + +=item part_raw($part, $wait_message) + +Actually format the $part partition disk. $wait_message is only used when formating ext3/4. +If not set, ext[3-4] will be formated without any progression bar, like other fses... + +=cut + sub part_raw { my ($part, $wait_message) = @_; @@ -153,13 +271,8 @@ sub part_raw { push @options, '-l', 'bootstrap'; } - # Preserve UUID - if (member($fs_type, 'swap', 'ext2', 'ext3', 'ext4')) { - push @options, '-U', $part->{device_UUID} if $part->{device_UUID}; - } elsif ($fs_type eq 'reiserfs') { - push @options, '-u', $part->{device_UUID} if $part->{device_UUID}; - } - + push @options, option_to_preserve_UUID_while_formating($part, $fs_type); + if ($part->{device_LABEL}) { push @options, @{$LABELs{$fs_type}}[0], $part->{device_LABEL}; } @@ -168,19 +281,16 @@ sub part_raw { my @args = ($cmd, @first_options, @options, devices::make($dev)); - if ($cmd eq 'mkfs.ext3' && $wait_message) { + if ($cmd =~ /^mkfs.ext[34]$/ && $wait_message) { mkfs_ext3($wait_message, @args) or die N("%s formatting of %s failed", $fs_type, $dev); } else { run_program::raw({ timeout => 'never' }, @args) or die N("%s formatting of %s failed", $fs_type, $dev); } - # Preserve UUID on fs where we couldn't enforce it while formatting - if ($fs_type eq 'jfs') { - run_program::raw('jfs_tune', '-U', devices::make($dev)); - } elsif ($fs_type eq 'xfs') { - run_program::raw('xfs_admin', '-U', devices::make($dev)); - } - + delete $part->{device_LABEL_changed}; + + preserve_UUID_after_format($dev, $part, $fs_type); + if (member($fs_type, qw(ext3 ext4))) { disable_forced_fsck($dev); } @@ -188,6 +298,28 @@ sub part_raw { after_formatting($part); } +=item preserve_UUID_after_format($dev, $part, $fs_type) + +Preserve UUID on fs where we couldn't enforce it while formatting + +=cut + +sub preserve_UUID_after_format { + my ($dev, $part, $fs_type) = @_; + if (my $uuid_cmd = $preserve_UUID{$fs_type}) { + my (undef, $cmd) = @$uuid_cmd; + run_program::raw({}, $cmd, '-U', $part->{device_UUID}, devices::make($dev)) if $cmd; + } elsif ($fs_type eq 'btrfs' && $part->{device_UUID}) { + write_btrfs_uuid($part->{device_UUID}, $dev); + } +} + +=item after_formatting($part) + +Misc post formating tags (rereading UUID & setting state) + +=cut + sub after_formatting { my ($part) = @_; @@ -197,6 +329,12 @@ sub after_formatting { set_isFormatted($part, 1); } +=item mkfs_ext3($wait_message, @args) + +Display a progression bar whike formating ext3/4 + +=cut + sub mkfs_ext3 { my ($wait_message, @args) = @_; @@ -213,6 +351,12 @@ sub mkfs_ext3 { return close($F); } +=item disable_forced_fsck($dev) + +Disable automatic fsck on extX (infinite number of mounts & duration between 2 fsck runs) + +=cut + sub disable_forced_fsck { my ($dev) = @_; run_program::run("tune2fs", "-c0", "-i0", devices::make($dev)); @@ -223,7 +367,7 @@ sub clean_label { if ($part->{device_LABEL}) { my $fs_type = $part->{fs_type}; if ($LABELs{$fs_type}) { - my ($option, $length, $handled_by_mount) = @{$LABELs{$fs_type}}; + my ($_option, $length, $handled_by_mount) = @{$LABELs{$fs_type}}; if (length $part->{device_LABEL} > $length) { my $short = substr($part->{device_LABEL}, 0, $length); log::l("shortening LABEL $part->{device_LABEL} to $short"); @@ -259,7 +403,7 @@ sub formatMount_part { } #- setting user_xattr on /home (or "/" if no /home) - if (!$part->{isMounted} && member($part->{fs_type}, qw(ext3 ext4)) + if (!$part->{isMounted} && member($part->{fs_type}, qw(ext2 ext3 ext4)) && ($part->{mntpoint} eq '/home' || !fs::get::has_mntpoint('/home', $all_hds) && $part->{mntpoint} eq '/')) { run_program::run('tune2fs', '-o', 'user_xattr', devices::make($part->{real_device} || $part->{device})); @@ -284,5 +428,8 @@ sub formatMount_all { }; } +=back + +=cut 1; |