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
|
#!/usr/bin/perl
# drakupdate_fstab
# Copyright (C) 2002-2004 Mandrakesoft (pixel@mandrakesoft.com)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
use lib qw(/usr/lib/libDrakX);
use detect_devices;
use security::level;
use common;
use fsedit;
use lang;
use any;
use fs;
$::isStandalone = 1; #- not using standalone.pm which generates too many logs for drakupdate_fstab purpose
log::l("drakupdate_fstab called with @ARGV\n");
$::testing = $ARGV[0] eq '--test' && shift @ARGV;
$::auto = $ARGV[0] eq '--auto' && shift @ARGV;
my $no_flag = $ARGV[0] eq '--no-flag' && shift @ARGV;
my ($raw_action, $device_name) = @ARGV;
my ($action) = $raw_action =~ /^--(add|del)/;
@ARGV == 2 && $action or die "usage: drakupdate_fstab [--test] [--auto] [--no-flag] [--add | --del] <device>\n";
main($action, $device_name);
sub check_hard_drives {
my ($name) = @_;
#- do not do anything if there are many partitions
#- otherwise we may add main extended partitions
if ($name =~ s|/part\d+$||) {
my @parts = grep { /part/ } all($name);
@parts <= 1;
} else {
1;
}
}
sub device_name_to_entry {
my ($name) = @_;
$name =~ s|/dev/||;
$name =~ /fd[01]/ && !$::auto and return { device => $name };
my @l = detect_devices::get();
my ($e, $nb);
if ((my $devfs_prefix, $nb) = $name =~ m,(.*)/(?:cd|disc|part(\d+))$,) {
$e = find { $_->{devfs_prefix} eq $devfs_prefix } @l;
$e->{devfs_prefix} ||= $devfs_prefix;
} else {
if ($e = find { $name eq $_->{device} } @l) {
$nb = '';
} else {
(my $prefix, $nb) = $name =~ m/^(.*?)(\d*)$/;
$e = find { $prefix eq ($_->{prefix} || $_->{device}) } @l;
}
}
if ($nb) {
$e->{devfs_device} = $e->{devfs_prefix} . '/part' . $nb;
$e->{device} = ($e->{prefix} || $e->{device}) . $nb;
}
$e ||= {};
if ($e->{devfs_device} eq $name) {
$e->{prefer_devfs_name} = 1;
} else {
$e->{device} = $name; #- keeping the exact name given (often is the devfs name)
}
$e->{media_type} = 'fd' if $name =~ /fd[01]/;
$e;
}
sub set_options {
my ($part, $useSupermount) = @_;
$part->{is_removable} = 1; #- force removable flag
fs::mount_options::set_default($part,
useSupermount => $useSupermount,
security => security::level::get(),
lang::fs_options(lang::read()));
my ($options, $unknown) = fs::mount_options::unpack($part);
$options->{kudzu} = 1 if !$no_flag;
fs::mount_options::pack($part, $options, $unknown);
}
sub set_mount_point {
my ($part, $fstab) = @_;
my $mntpoint = detect_devices::suggest_mount_point($part) or return;
$mntpoint = "/mnt/$mntpoint";
foreach ('', 2 .. 10) {
next if fs::get::mntpoint2part("$mntpoint$_", $fstab);
$part->{mntpoint} = "$mntpoint$_";
return 1;
}
0;
}
sub main {
my ($action, $device_name) = @_;
if ($::auto) {
check_hard_drives($device_name) or return;
}
my $part = device_name_to_entry($device_name);
my $fstab_file = '/etc/fstab';
if (!$part) {
print STDERR "Can't find device $device_name\n" if $::testing;
return;
} elsif ($::testing) {
cp_af('/etc/fstab', $fstab_file = '/tmp/fstab');
}
my $fstab = [ fs::read_fstab('', '/etc/fstab', 'keep_freq_passno', 'keep_devfs_name', 'verbatim_credentials') ];
my ($existing_fstab_entries, $fstab_) = partition { fsedit::is_same_hd($_, $part) } @$fstab;
if ($action eq 'add') {
if (@$existing_fstab_entries) {
print STDERR "Already in fstab\n" if $::testing;
return;
}
my $useSupermount = ${{ getVarsFromSh('/etc/sysconfig/dynamic') }}{SUPERMOUNT} eq 'no' ? '' : 'magicdev';
set_options($part, $useSupermount);
set_mount_point($part, $fstab) or return;
my ($line) = fs::prepare_write_fstab([$part]);
if ($line) {
append_to_file($fstab_file, $line);
system("mount $part->{mntpoint}") if !$::testing && $device_name =~ /^fd\d+/;
}
if ($::auto) {
print $part->{mntpoint}, " ", $useSupermount ? 'supermount' : 'user', "\n";
}
} else {
if (!@$existing_fstab_entries) {
print STDERR "Not found in fstab\n" if $::testing;
return;
}
foreach (@$existing_fstab_entries) {
if (!$no_flag && $_->{options} !~ /\bkudzu\b/) {
print STDERR "Not a 'kudzu'-flagged entry\n" if $::testing;
return;
}
}
my ($s) = fs::prepare_write_fstab($fstab_, '', 'keep_smb_credentials');
output($fstab_file, $s);
if ($::auto) {
print "$_->{mntpoint}\n" foreach @$existing_fstab_entries;
}
}
if ($::testing) {
print "fstab would have changed:\n";
system("diff -u /etc/fstab $fstab_file");
}
}
|