package log; # $Id$ use diagnostics; use strict; use c; my ($LOG, $LOG2); #-##################################################################################### #- Globals #-##################################################################################### #-###################################################################################### #- Functions #-###################################################################################### sub F() { $LOG } sub l { $LOG or openLog(); if ($::testing) { print STDERR @_, "\n"; } elsif ($LOG) { print $LOG "* ", @_, "\n"; print $LOG2 "* ", @_, "\n" if $LOG2; } elsif ($::isStandalone) { c::syslog(c::LOG_WARNING(), join("", @_)); } else { print STDERR @_, "\n"; } } sub openLog { my ($o_file) = @_; if ($o_file) { #- useLocal open $LOG, "> $o_file"; } elsif ($::isInstall) { open $LOG, "> /dev/tty3"; open $LOG2, ">> /tmp/ddebug.log"; } select((select($LOG), $| = 1)[0]) if $LOG; select((select($LOG2), $| = 1)[0]) if $LOG2; } sub closeLog() { if ($LOG) { close $LOG; close $LOG2; } elsif ($::isStandalone) { c::closelog(); } } sub explanations { if ($::isStandalone) { c::syslog(c::LOG_INFO()|c::LOG_LOCAL1(), "@_"); } else { l(@_); } } 1; istro/mes5 Mageia Installer and base platform for many utilitiesThierry Vignaud [tv]
summaryrefslogtreecommitdiffstats
path: root/perl-install/fs.pm
blob: ac118cdc008120664b7731e8c8d9f91fdaed0675 (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
package fs;

use diagnostics;
use strict;

use common;
use log;
use devices;
use fs::type;
use fs::get;
use fs::format;
use fs::mount_options;
use fs::loopback;
use fs::mount;
use run_program;
use detect_devices;
use modules;
use fsedit;


sub read_fstab {
    my ($prefix, $file, @reading_options) = @_;

    if (member('keep_default', @reading_options)) {
	push @reading_options, 'freq_passno', 'keep_device_LABEL', 'keep_device_UUID';
    }

    my %comments;
    my $comment;
    my @l = grep {
	if (/^Filename\s*Type\s*Size/) {
	    0; #- when reading /proc/swaps
	} elsif (/^\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, $fs_type, $options, $freq, $passno) = split;
	my $comment = $comments{$_};

	$options = 'defaults' if $options eq 'rw'; # clean-up for mtab read

	s/\\040/ /g foreach $mntpoint, $dev, $options;

	if ($fs_type eq 'ext4') {
	    $options = join(",", grep {	!/extents/ } split(',', $options)) || 'defaults';
	}

	my $h = { 
		 mntpoint => $mntpoint, fs_type => $fs_type,
		 options => $options, comment => $comment,
		 if_(member('keep_freq_passno', @reading_options), freq => $freq, passno => $passno),
		};

	put_in_hash($h, fs::wild_device::to_subpart($dev));

	if ($h->{device_LABEL} && !$h->{device_alias} && member('keep_device_LABEL', @reading_options)) {
	    $h->{prefer_device_LABEL} = 1;
	} elsif ($h->{device_UUID} && !$h->{device_alias} && member('keep_device_UUID', @reading_options)) {
	    $h->{prefer_device_UUID} = 1;
	} else {
	    $h->{prefer_device} = 1;
	}

	if ($h->{options} =~ /credentials=/ && !member('verbatim_credentials', @reading_options)) {
	    require fs::remote::smb;
	    #- remove credentials=file with username=foo,password=bar,domain=zoo
	    #- the other way is done in fstab_to_string
	    my ($options, $unknown) = fs::mount_options::unpack($h);
	    my $file = delete $options->{'credentials='};
	    my $credentials = fs::remote::smb::read_credentials_raw($file);
	    if ($credentials->{username}) {
		$options->{"$_="} = $credentials->{$_} foreach qw(username password domain);
		fs::mount_options::pack($h, $options, $unknown);
	    }
	} elsif ($h->{fs_type} eq 'davfs2' && !member('verbatim_credentials', @reading_options)) {
	    require fs::remote::davfs;
	    if (my $credentials = fs::remote::davfs::read_credentials($h->{mntpoint})) {
		my ($options, $unknown) = fs::mount_options::unpack($h);
		$options->{"$_="} = $credentials->{$_} foreach qw(username password);
		fs::mount_options::pack($h, $options, $unknown);
	    }
	}

	$h;
    } @l;
}

sub merge_fstabs {
    my ($loose, $fstab, @l) = @_;

    foreach my $p (@$fstab) {
	my ($l1, $l2) = partition { fs::get::is_same_hd($_, $p) } @l;
	my ($p2) = @$l1 or next;
	@l = @$l2;

	$p->{mntpoint} = $p2->{mntpoint} if delete $p->{unsafeMntpoint};

	if (!$loose) {
	    $p->{fs_type} = $p2->{fs_type} if $p2->{fs_type};
	    $p->{options} = $p2->{options} if $p2->{options};
	    add2hash_($p, $p2);
	} else {
	    $p->{isMounted} ||= $p2->{isMounted};
	    $p->{real_mntpoint} ||= $p2->{real_mntpoint};
	}
	$p->{device_alias} ||= $p2->{device_alias} if $p->{device} ne $p2->{device} && $p2->{device} !~ m|/|;

	$p->{fs_type} && $p2->{fs_type} && $p->{fs_type} ne $p2->{fs_type}
	  && $p->{fs_type} ne 'auto' && $p2->{fs_type} ne 'auto' and
	    log::l("err, fstab and partition table do not agree for $p->{device} type: $p->{fs_type} vs $p2->{fs_type}");
    }
    @l;
}

sub add2all_hds {
    my ($all_hds, @l) = @_;

    @l = merge_fstabs('', [ fs::get::really_all_fstab($all_hds) ], @l);

    foreach (@l) {
	my $s = 
	    $_->{fs_type} eq 'nfs' ? 'nfss' :
	    $_->{fs_type} eq 'cifs' ? 'smbs' :
	    $_->{fs_type} eq 'davfs2' ? 'davs' :
	    isTrueLocalFS($_) || isSwap($_) || isOtherAvailableFS($_) ? '' :
	    'special';
	push @{$all_hds->{$s}}, $_ if $s;
    }
}

sub get_major_minor {
    my ($fstab) = @_;
    foreach (@$fstab) {
	eval {
	    my (undef, $major, $minor) = devices::entry($_->{device});
	    ($_->{major}, $_->{minor}) = ($major, $minor);
	} if !$_->{major};
    }
}

sub merge_info_from_mtab {
    my ($fstab) = @_;

    my @l1 = map { my $l = $_; 
		   my $h = fs::type::fs_type2subpart('swap');
		   $h->{$_} = $l->{$_} foreach qw(device major minor); 
		   $h;
	       } 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} =~ m!^/tmp/\w*image$!) {
	    $_->{real_mntpoint} = delete $_->{mntpoint};
	}
	$_->{isMounted} = 1;
	set_isFormatted($_, 1);
    } 
    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 = fs::get::mntpoint2part($_->{mntpoint}, $fstab);
	    !$part || fs::get::is_same_hd($part, $_); #- keep it only if it is the mount point AND the same device
	} else {
	    1;
	}
    } read_fstab($prefix, '/etc/fstab', 'keep_default');

    merge_fstabs($loose, $fstab, @l);
}

sub get_info_from_fstab {
    my ($all_hds) = @_;
    my @l = read_fstab($::prefix, '/etc/fstab', 'keep_default');
    add2all_hds($all_hds, @l);
}

sub prepare_write_fstab {
    my ($fstab, $o_prefix, $b_keep_credentials) = @_;
    $o_prefix ||= '';

    my %new;
    my (@smb_credentials, @davfs_credentials);
    my @l = map { 
	my $device = 
	  isLoopback($_) ? 
	      ($_->{mntpoint} eq '/' ? "/initrd/loopfs" : $_->{loopback_device}{mntpoint}) . $_->{loopback_file} :
	  fs::wild_device::from_part($o_prefix, $_);

	my $comment = $_->{comment};
	$comment = '' if $comment =~ m!^Entry for /dev/.* :!;
	$comment ||= "# Entry for /dev/$_->{device} :\n" if $device =~ /^(UUID|LABEL)=/;

	my $real_mntpoint = $_->{mntpoint} || ${{ '/tmp/hdimage' => '/mnt/hd' }}{$_->{real_mntpoint}};
	if (!member('bind', split(',', $_->{options}))) {
	   mkdir_p("$o_prefix$real_mntpoint") if $real_mntpoint =~ m|^/|;
	}
	my $mntpoint = fs::type::carry_root_loopback($_) ? '/initrd/loopfs' : $real_mntpoint;
	my $needed_to_boot = member($_->{mntpoint}, fs::type::directories_needed_to_boot());

	my ($freq, $passno) =
	  exists $_->{freq} ?
	    ($_->{freq}, $_->{passno}) :
	    isTrueLocalFS($_) && !$_->{dmcrypt_name} && $_->{options} !~ /encryption=/ && (!$_->{is_removable} || $needed_to_boot) ? 
	    (1, $_->{mntpoint} eq '/' ? 1 : fs::type::carry_root_loopback($_) ? 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} || 'defaults';
	     if (($_->{is_removable} || member($_->{fs_type}, qw(ntfs ntfs-3g))) && !$needed_to_boot && $_->{options} !~ /nofail/) {
			 $options .= ',nofail';
		         }

	    if ($_->{fs_type} eq 'cifs' && $options =~ /password=/ && !$b_keep_credentials) {
		require fs::remote::smb;