summaryrefslogtreecommitdiffstats
path: root/perl-install/fs/format.pm
blob: 039352928e9403489a49ad48a7911c7ec83fba96 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package fs::format;

use diagnostics;
use strict;
use String::ShellQuote;

use run_program;
use common;
use fs::type;
use fs::loopback;
use log;

=head1 SYNOPSYS

B<fs::format> enables to format filesystems.

=head1 Variables

=over

=item %cmds

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' ],
    f2fs     => [ 'f2fs-tools', 'mkfs.f2fs', '-f' ],
    reiserfs => [ 'reiserfsprogs', 'mkfs.reiserfs', '-ff' ],
    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', 'mkswap' ],
    ntfs     => [ 'ntfsprogs', 'mkntfs', '--fast' ],
   'ntfs-3g' => [ 'ntfsprogs', 'mkntfs', '--fast' ],
    btrfs    => [ 'btrfs-progs', 'mkfs.btrfs', '-f' ],
    nilfs2   => [ 'nilfs-utils', 'mkfs.nilfs2', '-f' ],
);


=item %LABELs

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 ],
    f2fs     => [ '-l', 16, 1 ],
    reiserfs => [ '-l', 16, 1 ],
    xfs      => [ '-L', 12, 1 ],
    jfs      => [ '-L', 16, 1 ],
    hfs      => [ '-l', 27, 0 ],
    dos      => [ '-n', 11, 0 ],
    vfat     => [ '-n', 11, 0 ],
    swap     => [ '-L', 15, 1 ],
    ntfs     => [ '-L', 128, 0 ],
   'ntfs-3g' => [ '-L', 128, 0 ],
    btrfs    => [ '-L', 256, 1 ],
    nilfs2   => [ '-L', 16, 1 ],
);

=item %edit_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' ],
    xfs      => [ 'xfsprogs', 'xfs_admin', '-L' ],
    jfs      => [ 'jfsutils', 'jfs_tune', '-L' ],
#    hfs
    dos      => [ 'mtools', 'mlabel', '-i' ],
    vfat     => [ 'mtools', 'mlabel', '-i' ],
    swap     => [ 'util-linux', 'swaplabel', '-L' ],
    ntfs     => [ 'ntfsprogs', 'ntfslabel' ],
   'ntfs-3g' => [ 'ntfsprogs', '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;
    $l->[0];
}

sub known_type {
    my ($part) = @_;
    to_bool($cmds{$part->{fs_type}});
}

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 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 Mageia One)
}

sub canEditLabel {
    my ($part) = @_;
    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)) {
	$wait_message->(N("Formatting partition %s", $part->{device})) if $wait_message;
	require raid;
	raid::format_part($all_hds->{raids}, $part);
    } elsif (isLoopback($part)) {
	$wait_message->(N("Creating and formatting file %s", $part->{loopback_file})) if $wait_message;
	fs::loopback::format_part($part);
    } else {
	$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;
    maybeFormatted($part) or return;

    if ($part->{encrypt_key}) {
	fs::mount::set_loop($part);
    }

    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 ($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, 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) = @_;

    $part->{isFormatted} and return;

    if ($part->{encrypt_key}) {
	fs::mount::set_loop($part);
    }

    my $dev = $part->{real_device} || $part->{device};

    my @options = if_($part->{toFormatCheck}, "-c");
    log::l("formatting device $dev (type $part->{fs_type})");

    my $fs_type = $part->{fs_type};

    if (member($fs_type, qw(ext2 ext3 ext4))) {
	push @options, "-m", "0" if $part->{mntpoint} =~ m|^/home|;
    } elsif (isDos($part)) {
	$fs_type = 'dos';
    } elsif ($fs_type eq 'hfs') {
        push @options, '-l', "Untitled";
    } elsif (isAppleBootstrap($part)) {
	push @options, '-l', 'bootstrap';
    }

    push @options, option_to_preserve_UUID_while_formating($part, $fs_type);
    
    if ($part->{device_LABEL}) {
	push @options, @{$LABELs{$fs_type}}[0], $part->{device_LABEL};
    }

    my ($_pkg, $cmd, @first_options) = @{$cmds{$fs_type} || die N("I do not know how to format %s in type %s", $part->{device}, $part->{fs_type})};

    my @args = ($cmd, @first_options, @options, devices::make($dev));

    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);
    }

    delete $part->{device_LABEL_changed};

    preserve_UUID_after_format($dev, $part, $fs_type);
    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);
    }
    
    if (member($fs_type, qw(ext3 ext4))) {
	disable_forced_fsck($dev);
    }
}

sub after_formatting {
    my ($part) = @_;

    my $p = fs::type::type_subpart_from_magic($part);
    $part->{device_UUID} = $p && $p->{device_UUID};

    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) = @_;

    my $cmd = shell_quote_best_effort(@args);
    log::l("running: $cmd");
    open(my $F, "$cmd |");

    local $/ = "\b";
    local $_;
    while (<$F>) {
	#- even if it still takes some time when format is over, we don't want the progress bar to stay at 85%
	$wait_message->('', $1, $2) if m!^\s*(\d+)/(\d+)\b!;
    }
    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));
}

sub clean_label {
    my ($part) = @_;
    if ($part->{device_LABEL}) {
	my $fs_type = $part->{fs_type};
	if ($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");
		$part->{device_LABEL} = $short;
	    }
	    delete $part->{prefer_device_LABEL}
	      if !$handled_by_mount || $part->{mntpoint} eq '/' && !member($fs_type, qw(ext2 ext3 ext4));
	} else {
	    log::l("dropping LABEL=$part->{device_LABEL} since we don't know how to set labels for fs_type $fs_type");
	    delete $part->{device_LABEL};
	    delete $part->{prefer_device_LABEL};
	    delete $part->{device_LABEL_changed};
	}
    }
}

sub formatMount_part {
    my ($part, $all_hds, $fstab, $wait_message) = @_;

    if (isLoopback($part)) {
	formatMount_part($part->{loopback_device}, $all_hds, $fstab, $wait_message);
    }
    if (my $p = fs::get::up_mount_point($part->{mntpoint}, $fstab)) {
	formatMount_part($p, $all_hds, $fstab, $wait_message) if !fs::type::carry_root_loopback($part);
    }

    clean_label($part);

    if ($part->{toFormat}) {
	fs::format::part($all_hds, $part, $wait_message);
    } else {
	fs::format::write_label($part);
    }

    #- setting user_xattr on /home (or "/" if no /home)
    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}));
    }

    fs::mount::part($part, 0, $wait_message);
}

sub formatMount_all {
    my ($all_hds, $fstab, $wait_message) = @_;
    formatMount_part($_, $all_hds, $fstab, $wait_message)
      foreach sort { isLoopback($a) ? 1 : isSwap($a) ? -1 : 0 } grep { $_->{mntpoint} } @$fstab;

    #- ensure the link is there
    fs::loopback::carryRootCreateSymlink($_) foreach @$fstab;

    #- for fun :)
    #- that way, when install exits via ctrl-c, it gives hand to partition
    eval {
	my ($_type, $major, $minor) = devices::entry(fs::get::root($fstab)->{device});
	output "/proc/sys/kernel/real-root-dev", makedev($major, $minor);
    };
}

=back

=cut

1;