/[soft]/drakx/trunk/perl-install/fs/format.pm
ViewVC logotype

Contents of /drakx/trunk/perl-install/fs/format.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2758 - (show annotations) (download)
Mon Jan 23 19:05:25 2012 UTC (12 years, 5 months ago) by tv
File size: 9719 byte(s)
perl_checker cleanup
1 package fs::format; # $Id: format.pm 268842 2010-05-17 06:54:44Z pterjan $
2
3 use diagnostics;
4 use strict;
5 use String::ShellQuote;
6
7 use run_program;
8 use common;
9 use fs::type;
10 use fs::loopback;
11 use log;
12
13 my %cmds = (
14 ext2 => [ 'e2fsprogs', 'mkfs.ext2', '-F' ],
15 ext3 => [ 'e2fsprogs', 'mkfs.ext3', '-F' ],
16 ext4 => [ 'e2fsprogs', 'mkfs.ext4', '-F' ],
17 reiserfs => [ 'reiserfsprogs', 'mkfs.reiserfs', '-ff' ],
18 reiser4 => [ 'reiser4progs', 'mkfs.reiser4', '-f', '-y' ],
19 xfs => [ 'xfsprogs', 'mkfs.xfs', '-f', '-q' ],
20 jfs => [ 'jfsutils', 'mkfs.jfs', '-f' ],
21 hfs => [ 'hfsutils', 'hformat' ],
22 dos => [ 'dosfstools', 'mkdosfs' ],
23 vfat => [ 'dosfstools', 'mkdosfs', '-F', '32' ],
24 swap => [ 'util-linux', 'mkswap' ],
25 ntfs => [ 'ntfsprogs', 'mkntfs', '--fast' ],
26 'ntfs-3g' => [ 'ntfsprogs', 'mkntfs', '--fast' ],
27 btrfs => [ 'btrfs-progs', 'mkfs.btrfs' ],
28 nilfs2 => [ 'nilfs-utils', 'mkfs.nilfs2' ],
29 );
30
31 my %LABELs = ( #- option, length, handled_by_mount
32 ext2 => [ '-L', 16, 1 ],
33 ext3 => [ '-L', 16, 1 ],
34 ext4 => [ '-L', 16, 1 ],
35 reiserfs => [ '-l', 16, 1 ],
36 reiser4 => [ '-L', 16, 1 ],
37 xfs => [ '-L', 12, 1 ],
38 jfs => [ '-L', 16, 1 ],
39 hfs => [ '-l', 27, 0 ],
40 dos => [ '-n', 11, 0 ],
41 vfat => [ '-n', 11, 0 ],
42 swap => [ '-L', 15, 1 ],
43 ntfs => [ '-L', 128, 0 ],
44 'ntfs-3g' => [ '-L', 128, 0 ],
45 btrfs => [ '-L', 256, 1 ],
46 nilfs2 => [ '-L', 16, 1 ],
47 );
48
49 my %edit_LABEL = ( # package, command, option
50 # If option is defined, run <command> <option> <label> <device>
51 # If no option, run <command> <device> <label>
52 ext2 => [ 'e2fsprogs', 'tune2fs', '-L' ],
53 ext3 => [ 'e2fsprogs', 'tune2fs', '-L' ],
54 ext4 => [ 'e2fsprogs', 'tune2fs', '-L' ],
55 reiserfs => [ 'reiserfsprogs', 'reiserfstune', '-l' ],
56 # reiser4
57 xfs => [ 'xfsprogs', 'xfs_admin', '-L' ],
58 jfs => [ 'jfsutils', 'jfs_tune', '-L' ],
59 # hfs
60 dos => [ 'mtools', 'mlabel', '-i' ],
61 vfat => [ 'mtools', 'mlabel', '-i' ],
62 # swap => [ 'util-linux-ng', 'mkswap' ],
63 ntfs => [ 'ntfsprogs', 'ntfslabel' ],
64 'ntfs-3g' => [ 'ntfsprogs', 'ntfslabel' ],
65 # btrfs
66 # nilfs2
67 );
68
69 sub package_needed_for_partition_type {
70 my ($part) = @_;
71 my $l = $cmds{$part->{fs_type}} or return;
72 $l->[0];
73 }
74
75 sub known_type {
76 my ($part) = @_;
77 to_bool($cmds{$part->{fs_type}});
78 }
79
80 sub check_package_is_installed_format {
81 my ($do_pkgs, $fs_type) = @_;
82
83 my ($pkg, $binary) = @{$cmds{$fs_type} || return};
84 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)
85 }
86
87 sub check_package_is_installed_label {
88 my ($do_pkgs, $fs_type) = @_;
89
90 my ($pkg, $binary) = @{$edit_LABEL{$fs_type} || return};
91 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)
92 }
93
94 sub canEditLabel {
95 my ($part) = @_;
96 to_bool($edit_LABEL{$part->{fs_type}});
97 }
98
99 sub part {
100 my ($all_hds, $part, $wait_message) = @_;
101 if (isRAID($part)) {
102 $wait_message->(N("Formatting partition %s", $part->{device})) if $wait_message;
103 require raid;
104 raid::format_part($all_hds->{raids}, $part);
105 } elsif (isLoopback($part)) {
106 $wait_message->(N("Creating and formatting file %s", $part->{loopback_file})) if $wait_message;
107 fs::loopback::format_part($part);
108 } else {
109 $wait_message->(N("Formatting partition %s", $part->{device})) if $wait_message;
110 part_raw($part, $wait_message);
111 }
112 }
113
114 sub write_label {
115 my ($part) = @_;
116
117 $part->{device_LABEL_changed} or return;
118 maybeFormatted($part) or return;
119
120 if ($part->{encrypt_key}) {
121 fs::mount::set_loop($part);
122 }
123
124 my $dev = $part->{real_device} || $part->{device};
125 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})};
126 my @args;
127 if ($cmd eq 'mlabel') {
128 @args = ($cmd, @first_options, devices::make($dev), '::' . $part->{device_LABEL});
129 } elsif (defined $first_options[0]) {
130 @args = ($cmd, @first_options, $part->{device_LABEL}, devices::make($dev));
131 } else {
132 @args = ($cmd, devices::make($dev), $part->{device_LABEL});
133 }
134 run_program::raw({ timeout => 'never' }, @args) or die N("setting label on %s failed, is it formatted?", $dev);
135 delete $part->{device_LABEL_changed};
136 }
137
138 sub part_raw {
139 my ($part, $wait_message) = @_;
140
141 $part->{isFormatted} and return;
142
143 if ($part->{encrypt_key}) {
144 fs::mount::set_loop($part);
145 }
146
147 my $dev = $part->{real_device} || $part->{device};
148
149 my @options = if_($part->{toFormatCheck}, "-c");
150 log::l("formatting device $dev (type $part->{fs_type})");
151
152 my $fs_type = $part->{fs_type};
153
154 if (member($fs_type, qw(ext2 ext3 ext4))) {
155 push @options, "-m", "0" if $part->{mntpoint} =~ m|^/home|;
156 } elsif (isDos($part)) {
157 $fs_type = 'dos';
158 } elsif ($fs_type eq 'hfs') {
159 push @options, '-l', "Untitled";
160 } elsif (isAppleBootstrap($part)) {
161 push @options, '-l', 'bootstrap';
162 }
163
164 # Preserve UUID
165 if (member($fs_type, 'swap', 'ext2', 'ext3', 'ext4')) {
166 push @options, '-U', $part->{device_UUID} if $part->{device_UUID};
167 } elsif ($fs_type eq 'reiserfs') {
168 push @options, '-u', $part->{device_UUID} if $part->{device_UUID};
169 }
170
171 if ($part->{device_LABEL}) {
172 push @options, @{$LABELs{$fs_type}}[0], $part->{device_LABEL};
173 }
174
175 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})};
176
177 my @args = ($cmd, @first_options, @options, devices::make($dev));
178
179 if ($cmd =~ /^mkfs.ext[34]$/ && $wait_message) {
180 mkfs_ext3($wait_message, @args) or die N("%s formatting of %s failed", $fs_type, $dev);
181 } else {
182 run_program::raw({ timeout => 'never' }, @args) or die N("%s formatting of %s failed", $fs_type, $dev);
183 }
184
185 delete $part->{device_LABEL_changed};
186
187 # Preserve UUID on fs where we couldn't enforce it while formatting
188 if ($fs_type eq 'jfs') {
189 run_program::raw('jfs_tune', '-U', devices::make($dev));
190 } elsif ($fs_type eq 'xfs') {
191 run_program::raw('xfs_admin', '-U', devices::make($dev));
192 } elsif ($fs_type eq 'btrfs') {
193 #FIXME
194 }
195
196 if (member($fs_type, qw(ext3 ext4))) {
197 disable_forced_fsck($dev);
198 }
199
200 after_formatting($part);
201 }
202
203 sub after_formatting {
204 my ($part) = @_;
205
206 my $p = fs::type::type_subpart_from_magic($part);
207 $part->{device_UUID} = $p && $p->{device_UUID};
208
209 set_isFormatted($part, 1);
210 }
211
212 sub mkfs_ext3 {
213 my ($wait_message, @args) = @_;
214
215 my $cmd = shell_quote_best_effort(@args);
216 log::l("running: $cmd");
217 open(my $F, "$cmd |");
218
219 local $/ = "\b";
220 local $_;
221 while (<$F>) {
222 #- even if it still takes some time when format is over, we don't want the progress bar to stay at 85%
223 $wait_message->('', $1, $2) if m!^\s*(\d+)/(\d+)\b!;
224 }
225 return close($F);
226 }
227
228 sub disable_forced_fsck {
229 my ($dev) = @_;
230 run_program::run("tune2fs", "-c0", "-i0", devices::make($dev));
231 }
232
233 sub clean_label {
234 my ($part) = @_;
235 if ($part->{device_LABEL}) {
236 my $fs_type = $part->{fs_type};
237 if ($LABELs{$fs_type}) {
238 my ($option, $length, $handled_by_mount) = @{$LABELs{$fs_type}};
239 if (length $part->{device_LABEL} > $length) {
240 my $short = substr($part->{device_LABEL}, 0, $length);
241 log::l("shortening LABEL $part->{device_LABEL} to $short");
242 $part->{device_LABEL} = $short;
243 }
244 delete $part->{prefer_device_LABEL}
245 if !$handled_by_mount || $part->{mntpoint} eq '/' && !member($fs_type, qw(ext2 ext3 ext4));
246 } else {
247 log::l("dropping LABEL=$part->{device_LABEL} since we don't know how to set labels for fs_type $fs_type");
248 delete $part->{device_LABEL};
249 delete $part->{prefer_device_LABEL};
250 delete $part->{device_LABEL_changed};
251 }
252 }
253 }
254
255 sub formatMount_part {
256 my ($part, $all_hds, $fstab, $wait_message) = @_;
257
258 if (isLoopback($part)) {
259 formatMount_part($part->{loopback_device}, $all_hds, $fstab, $wait_message);
260 }
261 if (my $p = fs::get::up_mount_point($part->{mntpoint}, $fstab)) {
262 formatMount_part($p, $all_hds, $fstab, $wait_message) if !fs::type::carry_root_loopback($part);
263 }
264
265 clean_label($part);
266
267 if ($part->{toFormat}) {
268 fs::format::part($all_hds, $part, $wait_message);
269 } else {
270 fs::format::write_label($part);
271 }
272
273 #- setting user_xattr on /home (or "/" if no /home)
274 if (!$part->{isMounted} && member($part->{fs_type}, qw(ext2 ext3 ext4))
275 && ($part->{mntpoint} eq '/home' ||
276 !fs::get::has_mntpoint('/home', $all_hds) && $part->{mntpoint} eq '/')) {
277 run_program::run('tune2fs', '-o', 'user_xattr', devices::make($part->{real_device} || $part->{device}));
278 }
279
280 fs::mount::part($part, 0, $wait_message);
281 }
282
283 sub formatMount_all {
284 my ($all_hds, $fstab, $wait_message) = @_;
285 formatMount_part($_, $all_hds, $fstab, $wait_message)
286 foreach sort { isLoopback($a) ? 1 : isSwap($a) ? -1 : 0 } grep { $_->{mntpoint} } @$fstab;
287
288 #- ensure the link is there
289 fs::loopback::carryRootCreateSymlink($_) foreach @$fstab;
290
291 #- for fun :)
292 #- that way, when install exits via ctrl-c, it gives hand to partition
293 eval {
294 my ($_type, $major, $minor) = devices::entry(fs::get::root($fstab)->{device});
295 output "/proc/sys/kernel/real-root-dev", makedev($major, $minor);
296 };
297 }
298
299
300 1;

  ViewVC Help
Powered by ViewVC 1.1.30