-
Notifications
You must be signed in to change notification settings - Fork 0
/
Devices__MountAllPartitions.sh
256 lines (216 loc) · 6.85 KB
/
Devices__MountAllPartitions.sh
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
#!/bin/sh
#23456789+123456789+123456789+123456789+123456789+123456789+123456789+123456789+123456789+123456789+
####################################################################################################
###
### $Id: Devices__MountAllPartitions.sh,v 1.4 2020/11/14 02:50:25 root Exp $
###
### Script to mount all partitions that detected by the system and are not mounted.
###
####################################################################################################
reportDataLine()
{
echo "\n ===> ${dDev} ${dFtype} ${dLabel} ${dUuid} ${dStatus} ${dPath} ..."
}
TMP=/tmp/`basename $0 ".sh" `.tmp
rm -f ${TMP}.*
rootPref=`df / | grep '/dev' | awk '{ print $1 }' | cut -c1-8 `
echo "\n\t ROOT device is '${rootPref}' ..."
DBG=0
VERB=0
DATA=0
SWAP=0
INPUT=0
while [ $# -gt 0 ]
do
case $1 in
--debug )
DBG=1 ; shift
;;
--verbose" )
VERB=1 ; shift
;;
--input" )
INPUT=1 ; shift
;;
--data" )
DATA=1 ; SWAP=0 ; shift ; sString="_F"
;;
--swap" )
SWAP=1 ; DATA=0 ; shift ; sString="_S"
;;
* ) echo "\n\t Invalid parameter used on command line. Only options allowed: [ --debug | --verbose | --input | --data | --swap ]\n" ; exit 1
;;
esac
done
#classParts=${TMP}.parts
allParts=${TMP}.parts
COM_repParts="Devices__ReportDiskParts.sh"
tester=`which ${COM_repParts} `
if [ -z "${tester}" ]
then
echo "\n\t Unable to proceed. Unable to locate '${COM_repParts}'\n\n Bye!\n" ; exit 1
fi
${COM_repParts} >${allParts} 2>>/dev/null
### Report Format:
#/dev/sda14 ext4 DB001_F7 58f622cd-2841-4967-8def-86dd38192769 Mounted /DB001_F7
#/dev/sdb1 ext4 DB002_F1 0aa50783-954b-4024-99c0-77a2a54a05c2 Not_Mounted /site/DB002_F1
#/dev/sdb2 swap DB002_S1 7dd23169-56c6-4c2c-afbb-9e75d4de7652 Enabled [SWAP]
if [ ${DBG} -eq 1 ] ; then echo "\n ======== Report from '${COM_repParts}' :" ; cat ${allParts} ; fi
showLiveSwapDetails()
{
COM=" swapon --show 2>&1 >${TMP}.swap ; if [ ${DBG} -eq 1 ] ; then head -1 ${TMP}.swap ; fi ; cat ${TMP}.swap | grep '${dDev} ' "
echo "${COM}" >${TMP}.job
if [ ${VERB} -eq 1 ] ; then echo "\n\t COMMAND: \"${COM}\" ..." ; fi
. ${TMP}.job | awk '{ printf("\t %-12s %-12s %-12s %-12s %-12s\n", $1, $2, $3, $4, $5 ) }'
if [ ${DBG} -eq 1 ] ; then echo "\n=== Contents of '${TMP}.swap' :" ; cat ${TMP}.swap ; fi
if [ ${DBG} -eq 1 ] ; then echo "\n=== Contents of '${TMP}.job' :" ; cat ${TMP}.job ; fi
}
logicSWAP()
{
if [ ${DATA} != 1 ]
then
if [ ${INPUT} -eq 1 ] ; then reportDataLine ; fi
if [ "${dStatus}" = "Enabled" ]
then
if [ ${DBG} -eq 1 ] ; then echo "\n=== swap1 ===" ; fi
if [ ${VERB} -eq 1 ]
then echo "\n\t SWAP partition '${dLabel} [${dDev}|${dFtype}]' already mounted. No action taken ..." ; fi
showLiveSwapDetails
else
tUuid=`grep -v '^#' /etc/fstab | grep '^UUID=' | grep ${dUuid} | head -1 `
if [ -n "${tUuid}" ]
then
# Format of swap entry in fstab
# UUID=7dd23169-56c6-4c2c-afbb-9e75d4de7652 none swap sw,pri=1 0 0
pri=`echo "${tUuid}" | awk '{ print $4 }' | cut -f2 -d\= `
if [ ${DBG} -eq 1 ] ; then echo "\n=== swap2a ===" ; fi
if [ -n "${pri}" ]
then
COM="swapon -v -p${pri} -U ${dUuid}"
else
COM="swapon -v -U ${dUuid}"
fi
if [ ${VERB} -eq 1 ] ; then echo "\n\t COMMAND: '${COM}' ..." ; fi
else
tDev=`grep -v '^#' /etc/fstab | grep "^${dDev} " | head -1 `
if [ -n "${tDev}" ]
then
# Format of swap entry in fstab
# UUID=7dd23169-56c6-4c2c-afbb-9e75d4de7652 none swap sw,pri=1 0 0
pri=`echo "${tDev}" | grep 'pri=' | awk '{ print $4 }' | cut -f2 -d= `
fi
if [ ${DBG} -eq 1 ] ; then echo "\n=== swap2b ===" ; fi
if [ -n "${pri}" ]
then
COM="swapon -v -p${pri} ${dDev}"
else
COM="swapon -v ${dDev}"
fi
if [ ${VERB} -eq 1 ] ; then echo "\n\t COMMAND: '${COM}' ..." ; fi
fi
eval ${COM} 2>&1 | awk '{ printf("\n\t ACTION: %s\n", $0 ) }'
showLiveSwapDetails
fi
#if [ ${DBG} -eq 1 ] ; then exit 0 ; fi
fi
}
logicDATA()
{
if [ ${SWAP} != 1 ]
then
if [ ${INPUT} -eq 1 ] ; then reportDataLine ; fi
if [ "${dStatus}" = "Mounted" ]
then
if [ ${DBG} -eq 1 ] ; then echo "\n=== data1 ===" ; fi
if [ ${VERB} -eq 1 ]
then echo "\n\t DATA partition '${dLabel} [${dDev}|${dFtype}]' already mounted. No action taken ..." ; fi
#if [ ${VERB} -eq 1 ] ; then echo "\n\t COMMAND: '${COM}' ..." ; fi
case ${dPath} in
/ )
df -h / 2>&1 | grep '/dev' | awk '{ printf("\t %s\n", $0 ) }'
;;
* )
df -h 2>&1 | grep ${dLabel} | awk '{ printf("\t %s\n", $0 ) }'
;;
esac
else
tUuid=`grep -v '^#' /etc/fstab | grep '^UUID=' | grep ${dUuid} | head -1 `
### Auto-mounted USB
#rw,codepage=437,iocharset=iso8859-1,shortname=mixed,utf8,uhelper=udisks2
#uid=1000,gid=1000
#fmask=0022,dmask=0022,nosuid,nodev,relatime,showexec,flush,errors=remount-ro
case ${dFtype} in
vfat ) OPTIONS_1="noauto,rw,user,group,exec,dev,suid,mand,async,dirsync"
OPTIONS_2="uid=1000,gid=1000,codepage=437,iocharset=iso8859-1,shortname=mixed,utf8,uhelper=udisks2"
#OPTIONS_2="codepage=437,iocharset=iso8859-1,shortname=mixed,utf8,uhelper=udisks2"
mOptions="--options ${OPTIONS_1},${OPTIONS_2}"
;;
* ) mOptions="" ;;
esac
if [ -n "${tUuid}" ]
then
if [ ${DBG} -eq 1 ] ; then echo "\n=== data2a ===" ; fi
COM="mount -v -t ${dFtype} ${mOptions} --uuid ${dUuid} ${dPath}"
if [ ${VERB} -eq 1 ] ; then echo "\n\t COMMAND: '${COM}' ..." ; fi
else
if [ ${DBG} -eq 1 ] ; then echo "\n=== data2b ===" ; fi
COM="mount -v -t ${dFtype} ${mOptions} ${dDev} ${dPath}"
if [ ${VERB} -eq 1 ] ; then echo "\n\t COMMAND: '${COM}' ..." ; fi
fi
{ set -x
eval ${COM}
if [ $? -ne 0 ] ; then reportDataLine ; fi
} 2>&1 | awk '{ printf("\n\t ACTION: %s\n", $0 ) }'
df -h 2>&1 | grep ${dLabel} | awk '{ printf("\t %s\n", $0 ) }'
fi
#if [ ${DBG} -eq 1 ] ; then exit 0 ; fi
fi
}
mountPartitions()
{
while read dDev dFtype dLabel dUuid dStatus dPath
do
case ${dFtype} in
swap )
logicSWAP
;;
ext? | vfat | ntfs )
logicDATA
;;
* ) echo "\n\t Logic has not been tested for partition filesystem type '${dFtype}'." >&2
echo " \t No action taken for partition labelled '${dLabel}' ..." >&2
;;
esac
done <${classParts}
}
main()
{
for class in DATA SWAP
do
classParts=${TMP}.${class}
rm -f ${classParts}
case ${class} in
DATA )
grep -v swap ${allParts} >${classParts}
if [ \( ${VERB} -eq 1 \) -a \( ${SWAP} != 1 \) ]
then
echo "\n ======== Sub-group for DATA class partitions :" ; cat ${classParts}
fi
;;
SWAP )
grep swap ${allParts} >${classParts}
if [ \( ${VERB} -eq 1 \) -a \( ${DATA} != 1 \) ]
then
echo "\n ======== Sub-group for SWAP class partitions :" ; cat ${classParts}
fi
;;
esac
mountPartitions
echo ""
done
}
main
rm -f ${TMP}.*
exit 0
exit 0
exit 0