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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
#!/bin/bash
#
# Copyright (C) 2022 Red Hat, Inc. All rights reserved.
#
# This file is part of LVM2.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions
# of the GNU General Public License v.2.
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
errorexit() {
echo "$1" >&2
exit 1
}
logmsg() {
echo "$1"
logger "${SCRIPTNAME}: $1"
}
# Set to 1 while the fs is temporarily mounted on $TMPDIR
TMP_MOUNT_DONE=0
# Set to 1 if the fs resize command fails
RESIZEFS_FAILED=0
fsextend() {
if [ "$DO_UNMOUNT" -eq 1 ]; then
logmsg "unmount ${MOUNTDIR}"
umount "$MOUNTDIR"
if [ $? -eq 0 ]; then
logmsg "unmount done"
else
logmsg "unmount failed"
exit 1
fi
fi
if [ "$DO_FSCK" -eq 1 ]; then
logmsg "e2fsck ${DEVPATH}"
e2fsck -f -p "$DEVPATH"
if [ $? -eq 0 ]; then
logmsg "e2fsck done"
else
logmsg "e2fsck failed"
exit 1
fi
fi
if [ "$DO_CRYPTRESIZE" -eq 1 ]; then
logmsg "cryptsetup resize ${DEVPATH}"
cryptsetup resize "$DEVPATH"
if [ $? -eq 0 ]; then
logmsg "cryptsetup done"
else
logmsg "cryptsetup failed"
exit 1
fi
fi
if [ "$DO_MOUNT" -eq 1 ]; then
logmsg "mount ${DEVPATH} ${TMPDIR}"
mount -t "$FSTYPE" "$DEVPATH" "$TMPDIR"
if [ $? -eq 0 ]; then
logmsg "mount done"
TMP_MOUNT_DONE=1
else
logmsg "mount failed"
exit 1
fi
fi
if [[ "$FSTYPE" == "ext"* ]]; then
logmsg "resize2fs ${DEVPATH}"
resize2fs "$DEVPATH"
if [ $? -eq 0 ]; then
logmsg "resize2fs done"
else
logmsg "resize2fs failed"
RESIZEFS_FAILED=1
fi
elif [[ "$FSTYPE" == "xfs" ]]; then
logmsg "xfs_growfs ${DEVPATH}"
xfs_growfs "$DEVPATH"
if [ $? -eq 0 ]; then
logmsg "xfs_growfs done"
else
logmsg "xfs_growfs failed"
RESIZEFS_FAILED=1
fi
fi
# If the fs was temporarily mounted, now unmount it.
if [ $TMP_MOUNT_DONE -eq 1 ]; then
logmsg "cleanup unmount ${TMPDIR}"
umount "$TMPDIR"
if [ $? -eq 0 ]; then
logmsg "cleanup unmount done"
TMP_MOUNT_DONE=0
rmdir "$TMPDIR"
else
logmsg "cleanup unmount failed"
exit 1
fi
fi
# If the fs was temporarily unmounted, now remount it.
# Not considered a command failure if this fails.
if [[ $DO_UNMOUNT -eq 1 && $REMOUNT -eq 1 ]]; then
logmsg "remount ${DEVPATH} ${MOUNTDIR}"
mount -t "$FSTYPE" "$DEVPATH" "$MOUNTDIR"
if [ $? -eq 0 ]; then
logmsg "remount done"
else
logmsg "remount failed"
fi
fi
if [ $RESIZEFS_FAILED -eq 1 ]; then
logmsg "File system extend failed."
exit 1
fi
exit 0
}
fsreduce() {
if [ "$DO_UNMOUNT" -eq 1 ]; then
logmsg "unmount ${MOUNTDIR}"
umount "$MOUNTDIR"
if [ $? -eq 0 ]; then
logmsg "unmount done"
else
logmsg "unmount failed"
exit 1
fi
fi
if [ "$DO_FSCK" -eq 1 ]; then
logmsg "e2fsck ${DEVPATH}"
e2fsck -f -p "$DEVPATH"
if [ $? -eq 0 ]; then
logmsg "e2fsck done"
else
logmsg "e2fsck failed"
exit 1
fi
fi
if [ "$DO_MOUNT" -eq 1 ]; then
logmsg "mount ${DEVPATH} ${TMPDIR}"
mount -t "$FSTYPE" "$DEVPATH" "$TMPDIR"
if [ $? -eq 0 ]; then
logmsg "mount done"
TMP_MOUNT_DONE=1
else
logmsg "mount failed"
exit 1
fi
fi
if [[ "$FSTYPE" == "ext"* ]]; then
NEWSIZEKB=$(($NEWSIZEBYTES/1024))
logmsg "resize2fs ${DEVPATH} ${NEWSIZEKB}k"
resize2fs "$DEVPATH" "$NEWSIZEKB"k
if [ $? -eq 0 ]; then
logmsg "resize2fs done"
else
logmsg "resize2fs failed"
# will exit after cleanup unmount
RESIZEFS_FAILED=1
fi
fi
# If the fs was temporarily mounted, now unmount it.
if [ $TMP_MOUNT_DONE -eq 1 ]; then
logmsg "cleanup unmount ${TMPDIR}"
umount "$TMPDIR"
if [ $? -eq 0 ]; then
logmsg "cleanup unmount done"
TMP_MOUNT_DONE=0
rmdir "$TMPDIR"
else
logmsg "cleanup unmount failed"
exit 1
fi
fi
if [ $RESIZEFS_FAILED -eq 1 ]; then
logmsg "File system reduce failed."
exit 1
fi
if [ "$DO_CRYPTRESIZE" -eq 1 ]; then
NEWSIZESECTORS=$(($NEWSIZEBYTES/512))
logmsg "cryptsetup resize ${NEWSIZESECTORS} sectors ${DEVPATH}"
cryptsetup resize --size "$NEWSIZESECTORS" "$DEVPATH"
if [ $? -eq 0 ]; then
logmsg "cryptsetup done"
else
logmsg "cryptsetup failed"
exit 1
fi
fi
# If the fs was temporarily unmounted, now remount it.
# Not considered a command failure if this fails.
if [[ $DO_UNMOUNT -eq 1 && $REMOUNT -eq 1 ]]; then
logmsg "remount ${DEVPATH} ${MOUNTDIR}"
mount -t "$FSTYPE" "$DEVPATH" "$MOUNTDIR"
if [ $? -eq 0 ]; then
logmsg "remount done"
else
logmsg "remount failed"
fi
fi
exit 0
}
cryptresize() {
NEWSIZESECTORS=$(($NEWSIZEBYTES/512))
logmsg "cryptsetup resize ${NEWSIZESECTORS} sectors ${DEVPATH}"
cryptsetup resize --size "$NEWSIZESECTORS" "$DEVPATH"
if [ $? -eq 0 ]; then
logmsg "cryptsetup done"
else
logmsg "cryptsetup failed"
exit 1
fi
exit 0
}
usage() {
echo "${SCRIPTNAME}: helper script called by lvresize to resize file systems."
echo ""
echo "${SCRIPTNAME} --fsextend --fstype name --lvpath path"
echo " [ --mountdir path ]"
echo " [ --mount ]"
echo " [ --unmount ]"
echo " [ --remount ]"
echo " [ --fsck ]"
echo " [ --cryptresize ]"
echo " [ --cryptpath path ]"
echo ""
echo "${SCRIPTNAME} --fsreduce --fstype name --lvpath path"
echo " [ --newsizebytes num ]"
echo " [ --mountdir path ]"
echo " [ --mount ]"
echo " [ --unmount ]"
echo " [ --remount ]"
echo " [ --fsck ]"
echo " [ --cryptresize ]"
echo " [ --cryptpath path ]"
echo ""
echo "${SCRIPTNAME} --cryptresize --cryptpath path --newsizebytes num"
echo ""
echo "Options:"
echo " --fsextend"
echo " Extend the file system."
echo " --fsreduce"
echo " Reduce the file system."
echo " --fstype name"
echo " The type of file system (ext*, xfs)."
echo " --lvpath path"
echo " The path to the LV being resized."
echo " --mountdir path"
echo " The file system is currently mounted here."
echo " --mount"
echo " Mount the file system on a temporary directory before resizing."
echo " --unmount"
echo " Unmount the file system before resizing."
echo " --remount"
echo " Remount the file system after resizing if unmounted."
echo " --fsck"
echo " Run fsck on the file system before resizing (only with ext*)."
echo " --newsizebytes num"
echo " The new size of the file system."
echo " --cryptresize"
echo " Resize the crypt device between the LV and file system."
echo " --cryptpath path"
echo " The path to the crypt device."
echo ""
}
#
# BEGIN SCRIPT
#
PATH="/sbin:/usr/sbin:/bin:/usr/sbin:$PATH"
SCRIPTNAME=$(basename "$0")
# These are the only commands that this script will run.
# Each is enabled (1) by the corresponding command options:
# --fsextend, --fsreduce, --cryptresize, --mount, --unmount, --fsck
DO_FSEXTEND=0
DO_FSREDUCE=0
DO_CRYPTRESIZE=0
DO_MOUNT=0
DO_UNMOUNT=0
DO_FSCK=0
# --remount: attempt to remount the fs if it was originally
# mounted and the script unmounted it.
REMOUNT=0
if [ "$UID" != 0 ] && [ "$EUID" != 0 ]; then
errorexit "${SCRIPTNAME} must be run as root."
fi
GETOPT="getopt"
OPTIONS=$("$GETOPT" -o h -l help,fsextend,fsreduce,cryptresize,mount,unmount,remount,fsck,fstype:,lvpath:,newsizebytes:,mountdir:,cryptpath: -n "${SCRIPTNAME}" -- "$@")
eval set -- "$OPTIONS"
while true
do
case $1 in
--fsextend)
DO_FSEXTEND=1
shift
;;
--fsreduce)
DO_FSREDUCE=1
shift
;;
--cryptresize)
DO_CRYPTRESIZE=1
shift
;;
--mount)
DO_MOUNT=1
shift
;;
--unmount)
DO_UNMOUNT=1
shift
;;
--fsck)
DO_FSCK=1
shift
;;
--remount)
REMOUNT=1
shift
;;
--fstype)
FSTYPE=$2;
shift; shift
;;
--lvpath)
LVPATH=$2;
shift; shift
;;
--newsizebytes)
NEWSIZEBYTES=$2;
shift; shift
;;
--mountdir)
MOUNTDIR=$2;
shift; shift
;;
--cryptpath)
CRYPTPATH=$2;
shift; shift
;;
-h|--help)
usage
shift
exit 0
;;
--)
shift
break
;;
*)
errorexit "Unknown option \"$1\."
exit 1
;;
esac
done
#
# Input arg checking
#
# There are three top level commands: --fsextend, --fsreduce, --cryptresize.
if [[ "$DO_FSEXTEND" -eq 0 && "$DO_FSREDUCE" -eq 0 && "$DO_CRYPTRESIZE" -eq 0 ]]; then
errorexit "Missing --fsextend|--fsreduce|--cryptresize."
fi
if [[ "$DO_FSEXTEND" -eq 1 || "$DO_FSREDUCE" -eq 1 ]]; then
case "$FSTYPE" in
ext[234]) ;;
"xfs") ;;
*) errorexit "Cannot resize --fstype \"$FSTYPE\"."
esac
if [ -z "$LVPATH" ]; then
errorexit "Missing required --lvpath."
fi
fi
if [[ "$DO_CRYPTRESIZE" -eq 1 && -z "$CRYPTPATH" ]]; then
errorexit "Missing required --cryptpath for --cryptresize."
fi
if [ "$DO_CRYPTRESIZE" -eq 1 ]; then
DEVPATH=$CRYPTPATH
else
DEVPATH=$LVPATH
fi
if [ -z "$DEVPATH" ]; then
errorexit "Missing path to device."
fi
if [ ! -e "$DEVPATH" ]; then
errorexit "Device does not exist \"$DEVPATH\"."
fi
if [[ "$DO_UNMOUNT" -eq 1 && -z "$MOUNTDIR" ]]; then
errorexit "Missing required --mountdir for --unmount."
fi
if [[ "$DO_FSREDUCE" -eq 1 && "$FSTYPE" == "xfs" ]]; then
errorexit "Cannot reduce xfs."
fi
if [[ "$DO_FSCK" -eq 1 && "$FSTYPE" == "xfs" ]]; then
errorexit "Cannot use --fsck with xfs."
fi
if [ "$DO_MOUNT" -eq 1 ]; then
TMPDIR=$(mktemp --suffix _lvresize_$$ -d -p /tmp)
if [ ! -e "$TMPDIR" ]; then
errorexit "Failed to create temp dir."
fi
# In case the script terminates without doing cleanup
function finish {
if [ "$TMP_MOUNT_DONE" -eq 1 ]; then
logmsg "exit unmount ${TMPDIR}"
umount "$TMPDIR"
rmdir "$TMPDIR"
fi
}
trap finish EXIT
fi
#
# Main program function:
# - the two main functions are fsextend and fsreduce.
# - one special case function is cryptresize.
#
if [ "$DO_FSEXTEND" -eq 1 ]; then
fsextend
elif [ "$DO_FSREDUCE" -eq 1 ]; then
fsreduce
elif [ "$DO_CRYPTRESIZE" -eq 1 ]; then
cryptresize
fi
|