-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsize-drive
320 lines (269 loc) · 8.5 KB
/
size-drive
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
#!/usr/bin/env bash
## Developed for Linux
## License: GNU GPL v.3 http://www.gnu.org/licenses/gpl-3.0.en.html
VERSION='2'
## Destiny: To compare partition size.
## Script usage: bash script_name
## Warning: This script from the output tries to separate only the "sd" words,
## so not will work with "hd" names.
## https://tldp.org/HOWTO/Partition-Mass-Storage-Definitions-Naming-HOWTO/x99.html
## https://linuxhandbook.com/inode-linux/
## https://tldp.org/HOWTO/Partition/recovering.html
## https://www.kernel.org/doc/html/latest/block/stat.html
NC='\e[0m' # Reset Color
GC='\e[0;32m' # Green ECHO
GREEN_ECHO()
{
echo -e "${GC}$@${NC}"
}
CHECK() {
SKIP=0
# If app not installed and file not found and dir not found
if [[ -z $(type -P "$1") ]] && [[ ! -f "$1" ]] && [[ ! -d "$1" ]]; then
echo " "
echo " Warning: not found and we skipped this: $1"
echo " "
SKIP=1
else
## Check if root is needed.
if [[ "$2" == "root" ]] ; then
if [[ "$EUID" -ne 0 ]] ; then
echo " "
echo " Warning: command need root and we skipped this: $1 "
echo " "
SKIP=1
fi
fi
fi
echo -----------------
}
#====================================================================
-lsblk() {
CHECK lsblk
if [[ "$SKIP" == 0 ]] ; then
GREEN_ECHO " lsblk (GiB)"
lsblk -o NAME,FSTYPE,SIZE,FSUSED,MOUNTPOINT,LABEL | grep -v ^"loop"
fi
}
#echo -----------------
-example() {
CHECK lsblk
if [[ "$SKIP" == 0 ]] ; then
EXAMPLE=$(lsblk -bo NAME,SIZE | grep -v ^"loop" | grep "├─" | head -n1)
echo "Example partitionin in B , GiB , GB:"
echo "$EXAMPLE Bytes"
BYTE=$( awk '{print $2}' <<< "$EXAMPLE" )
NAME=$( awk '{print $1}' <<< "$EXAMPLE" )
GiB="$(echo ""$BYTE"/1024/1024/1024" | bc -l | tr '.' ',')"
GiB=$(printf "%.2f GiB \n" "$GiB")
GB="$(echo ""$BYTE"/1000/1000/1000" | bc -l | tr '.' ',')"
GB=$(printf "%.2f GB \n" "$GB")
printf " %47s \n" "$GiB"
printf " %47s \n" "$GB"
fi
}
#echo -----------------
-procpart() {
CHECK /proc/partitions
if [[ "$SKIP" == 0 ]] ; then
GREEN_ECHO " From /proc/partitions"
echo "NAME SIZE"
while read -r L1 L2 L3 L4 ; do
if grep -q ^"sd" <<< "$L4" ; then
P_GiB=$(echo "$L3 / 1024 / 1024" | bc -l | tr '.' ',')
printf "$L4 %.2f GiB \n" $P_GiB
fi
done < /proc/partitions
fi
}
#echo -----------------
-df() {
CHECK df
if [[ "$SKIP" == 0 ]] ; then
GREEN_ECHO " From df (GiB)"
GREEN_ECHO " Info: the size inside df may not be correct."
df -Th | grep -v tmpfs
fi
}
#echo -----------------
-sysblock() {
CHECK /sys/block/
if [[ "$SKIP" == 0 ]] ; then
GREEN_ECHO " From /sys/block/"
echo "NAME SIZE"
while read -r L1 ; do
#echo "$L1"
while read -r L1L1 ; do
#echo "$L1L1"
S_GiB=$( echo "$(cat /sys/block/"$L1"/"$L1L1"/size) * 512 / 1024 / 1024 / 1024" | bc -l | tr '.' ',' )
PARTNAME=$( grep -ri PARTNAME /sys/block/"$L1"/"$L1L1"/uevent | awk -F'=' '{print $2}' )
printf "$L1L1 %.2f GiB $PARTNAME \n" "$S_GiB"
done <<< $(ls /sys/block/"$L1" | grep ^"sd")
done <<< $(ls /sys/block/ | grep ^"sd")
fi
}
#echo -----------------
-blockdev() {
CHECK blockdev root
if [[ "$SKIP" == 0 ]] ; then
GREEN_ECHO " blockdev --report"
while read -r L1 L2 L3 L4 L5 L6 L7 ; do
if [[ "$L1" == RO ]] ; then
printf "%-7s %-7s %-7s %-7s %-14s %-16s %-7s %-7s \n" $L1 $L2 $L3 $L4 $L5 $L6 $L7
elif grep -q "/dev/sd" <<< "$L7" ; then
## Change size from Bytes to GiB
L6=$( echo "$L6 / 1024 / 1024 / 1024" | bc -l | tr '.' ',')
L6=$( printf "%.2f GiB" "$L6" )
printf "%-7s %-7s %-7s %-7s %-14s %-7s %-7s %-7s \n" $L1 $L2 $L3 $L4 $L5 $L6 $L7
fi
done <<< $(blockdev --report)
fi
}
#echo -----------------
-parted() {
CHECK parted root
if [[ "$SKIP" == 0 ]] ; then
GREEN_ECHO " parted -l (GB)"
parted -l
fi
}
#echo -----------------
SHORT_OUTPUT() {
NOT_PRINT_L=0 ; EMPTY_LINE=0 ; while read -r L1 ; do
# If line empty
if [ -z "$L1" ] ; then
# Reset
NOT_PRINT=0
if [[ "$EMPTY_LINE" == 0 ]] ; then
echo " "
fi
EMPTY_LINE=1
else
if grep -q "/dev/" <<< "$L1" ; then
if grep -q "/dev/sd" <<< "$L1" ; then
NOT_PRINT=0
else
NOT_PRINT=1
fi
fi
if [[ $NOT_PRINT == 0 ]] ; then
echo "$L1"
fi
EMPTY_LINE=0
fi
done <<< $($@)
}
-fdisk() {
CHECK fdisk root
if [[ "$SKIP" == 0 ]] ; then
GREEN_ECHO " fdisk -l (GiB)"
SHORT_OUTPUT "fdisk -l"
fi
}
#echo -----------------
-sfdisk() {
CHECK sfdisk root
if [[ "$SKIP" == 0 ]] ; then
GREEN_ECHO " sfdisk -l (GiB)"
SHORT_OUTPUT "sfdisk -l"
fi
}
#echo -----------------
READ_OUTPUT() {
COMMAND="$@"
if [ -f /proc/partitions ] ; then
while read -r L1 L2 L3 L4 ; do
OUTPUT=$($COMMAND /dev/"$L4")
while read -r L1L1 ; do
if grep -q "Block count:" <<< "$L1L1" ; then
B_COUNT=$( awk '{print $3}' <<< "$L1L1" )
elif grep -q "Block size:" <<< "$L1L1" ; then
B_SIZE=$( awk '{print $3}' <<< "$L1L1" )
elif grep -q "Journal checksum:" <<< "$L1L1" ; then
break
fi
done <<< "$OUTPUT"
echo " "
echo "Name: /dev/$L4"
if [ ! -z "$B_COUNT" ] ; then
BYTE=$(($B_COUNT * $B_SIZE))
GiB="$(echo ""$BYTE"/1024/1024/1024" | bc -l | tr '.' ',')"
GiB=$(printf "%.2f GiB \n" "$GiB")
GB="$(echo ""$BYTE"/1000/1000/1000" | bc -l | tr '.' ',')"
GB=$(printf "%.2f GB \n" "$GB")
echo "------------------{"
echo "SIZE (Byte) = Block count * Block size "
echo "SIZE: $BYTE BYTES"
echo "SIZE: $GiB GiB"
echo "SIZE: $GB GB"
echo "------------------}"
fi
unset OUTPUT
unset B_COUNT
unset B_SIZE
echo " "
done <<< $(grep sd /proc/partitions)
else
echo " Warning: not found /proc/partitions "
echo " so We will skip test. "
fi
}
-dumpe2fs() {
CHECK dumpe2fs root
if [[ "$SKIP" == 0 ]] ; then
GREEN_ECHO " dumpe2fs (GiB)"
READ_OUTPUT "dumpe2fs" "-h"
fi
}
#echo -----------------
-tune2fs() {
CHECK tune2fs root
if [[ "$SKIP" == 0 ]] ; then
GREEN_ECHO " tune2fs (GiB)"
READ_OUTPUT "tune2fs" "-l"
fi
}
## Other tools:
## gparted , gnome-disks , cfdisk
##========================================={
## Options
case $1 in
"--help"|"-h")
echo " "
echo " No options = run all options."
echo " "
echo " -o Specify which commands to print."
echo " "
echo " Available commands with -o argument:"
echo " -lsblk Like lsblk command "
echo " -example Example with lsblk command "
echo " -procpart Like /proc/partitions file "
echo " -df Like df command, "
echo " the size inside df may not be correct."
echo " -sysblock Like /sys/block/ file "
echo " -blockdev Like blockdev command"
echo " -parted Like parted command"
echo " -fdisk Like fdisk command"
echo " -sfdisk Like sfdisk command"
echo " -dumpe2fs Like dumpe2fs command"
echo " -tune2fs Like tune2fs command"
;;
"")
-lsblk ; -example ; -procpart ; -df ; -sysblock ; -blockdev
-parted ; -fdisk ; -sfdisk ; -dumpe2fs ; -tune2fs
;;
"-o")
ARGS="${*:2}"
for OPT in $ARGS ; do
"$OPT"
done
;;
"--version"|"-v")
echo "Version = $VERSION"
exit 0
;;
*)
echo "Unknown option."
exit 1
;;
esac