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 | #!/bin/bash
#
###########################################################################
#
# Description:
# Sets LABEL on MSDOS partition
#
# Usage:
# sudo $0 PARTITION LABEL
#
# Example:
# sudo $0 /dev/hda9 MY_LABEL
#
# IN: PARTITION LABEL
# OUT: LABEL set as label on partition PARTITION
#
# RC:
# 1 - Wrong number of parameters
# 2 - You forgot sudo.
# 3 - Partition $1 is not mounted.
# 4 - Package mtools is not installed.
# 5 - Could not copy /etc/mtools.conf to ~/.mtoolsrc.
# 6 - Could not write to ~/.mtoolsrc.
# 7 - Could not do mcd x:
# 8 - Could not set the new label for ${1}.
# 9 - You don´t want to set label $2 for ${1}, so I´ll exit now.
#
###########################################################################
#set -x
#########################
# functions - start
#########################
function usage () {
echo -e "\nUsage: sudo $0 PARTITION LABEL"
echo -e "Example: sudo $0 /dev/hda9 MY_LABEL\n"
}
function verify_uid () {
if [ "${UID}X" != "0X" ]
then
echo -e "\nYou forgot sudo."
usage
exit 2
fi
}
function verify_partition () {
# It is not necessary to verify the partition type, because
# mlabel can set label only on MSDOS partitions.
# But the partition has to be mounted!
MOUNTED=$(grep $1 /etc/mtab | wc -l | tr -d ' ')
if [ "${MOUNTED}X" != "1X" ]
then
echo -e "\nPartition $1 is not mounted."
echo -e "You should mount this partition before you run this script.\n"
exit 3
fi
}
function verify_mtools () {
MTOOLS=$(which mtools)
if [ "${MTOOLS}X" = "X" ]
then
echo -e "\nPackage mtools is not installed."
echo -e "You should run \"sudo apt-get install mtools\" or \"sudo aptitude install mtools\"!\n"
exit 4
fi
}
function save_mtoolsrc (){
# save a copy of your own ~/.mtoolsrc
if [ -f ~/.mtoolsrc ]
then
cp ~/.mtoolsrc ~/.mtoolsrc.sav
echo -e "\n~/.mtoolsrc saved to ~/.mtoolsrc.sav"
fi
}
function recover_mtoolsrc (){
# recover your own ~/.mtoolsrc
if [ -f ~/.mtoolsrc.sav ]
then
mv ~/.mtoolsrc.sav ~/.mtoolsrc
# files owned by root in user´s home directory
# it´s not nice, so reset the ownership
chown ${SUDO_USER} ~/.mtoolsrc ~/.mcwd
echo -e "\n~/.mtoolsrc recovered"
fi
}
function copy_mtools.conf_to_mtoolsrc (){
# if copy does not work, mtools does not seem installed
cp /etc/mtools.conf ~/.mtoolsrc
if [ $? -ne 0 ]
then
recover_mtoolsrc
echo -e "\nCould not copy /etc/mtools.conf to ~/.mtoolsrc.\n"
exit 5
fi
}
function rename_temp_mtoolsrc_and_recover_mtoolsrc (){
# copy your temporary ~/.mtoolsrc to ~/.mtoolsrc.error
RECOVERED=0
if [ -f ~/.mtoolsrc ]
then
mv ~/.mtoolsrc ~/.mtoolsrc.error
RECOVERED=1
fi
recover_mtoolsrc
if [ "${RECOVERED}X" = "1X" ]
then
echo -e "\nThere is a file ~/.mtoolsrc.error"
echo -e "~/.mtoolsrc.error was written by me."
echo -e "Please check this file to figure out what happened.\n"
exit $1
fi
}
function write_new_mtoolsrc (){
# there is a copy of your original ~/.mtoolsrc
# we are going to recover this file
# we have to be sure, that there are no other settings in this file
# so we overwrite this file temporarily and recover it thereafter
echo "drive x: file=\"${1}\"" > ~/.mtoolsrc
if [ $? -ne 0 ]
then
echo -e "\nCould not write to ~/.mtoolsrc."
rename_temp_mtoolsrc_and_recover_mtoolsrc 6
fi
}
function set_label_on_partition () {
# does the new ~/.mtoolsrc work?
mcd x:
if [ $? -ne 0 ]
then
echo -e "\nCould not do mcd x:"
echo "Check STDOUT and ~/.mtoolsrc*"
rename_temp_mtoolsrc_and_recover_mtoolsrc 7
fi
# Are you sure you want to change the lable?
echo -e "\nCurrent label for ${1}: $(mlabel -s x:)"
read -p "Do you really want to set $2 as label for ${1}? (y/n):"
case "$REPLY" in
y|yes|Y|Yes|YES|j|ja|J|Ja|JA)
# try to set the new label
mlabel x:$2
RC=$?
if [ $? -eq 0 ]
then
echo -e "\nNew label on ${1}: $(mlabel -s x:)"
# everything is OK, so we should only remove the temporary ~/.mtoolsrc and recover the original ~/.mtoolsrc
recover_mtoolsrc
else
echo -e "\nCould not set the new label for ${1}."
# something went wrong, so we should save the temporary ~/.mtoolsrc and recover the original ~/.mtoolsrc
rename_temp_mtoolsrc_and_recover_mtoolsrc 8
fi
;;
*)
echo -e "\nYou don´t want to set label $2 for ${1}, so I´ll exit now."
recover_mtoolsrc
echo ""
exit 9
;;
esac
}
#########################
# functions - start
#########################
#########################
# main - start
#########################
if [ $# -ne 2 ]
then
echo -e "\nWrong number of parameters."
usage
exit 1
fi
verify_uid
verify_partition $1
verify_mtools
save_mtoolsrc
copy_mtools.conf_to_mtoolsrc
write_new_mtoolsrc $1
set_label_on_partition $1 $2
exit 0
#########################
# main - end
#########################
|