3 # Copyright (C) 2007-2010 Red Hat, Inc. All rights reserved.
5 # This file is part of LVM2.
7 # This copyrighted material is made available to anyone wishing to use,
8 # modify, copy, or redistribute it subject to the terms and conditions
9 # of the GNU General Public License v.2.
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software Foundation,
13 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 # Author: Zdenek Kabelac <zkabelac at redhat.com>
17 # Script for resizing devices (usable for LVM resize)
20 # mount, umount, grep, readlink, blockdev, blkid, fsck, xfs_check
22 # ext2/ext3/ext4: resize2fs, tune2fs
23 # reiserfs: resize_reiserfs, reiserfstune
24 # xfs: xfs_growfs, xfs_info
30 # 3 unsupported online filesystem check for given mounted fs
34 PATH=/sbin:/usr/sbin:/bin:/usr/sbin:$PATH
39 TUNE_REISER=reiserfstune
40 RESIZE_REISER=resize_reiserfs
57 # user may override lvm location by setting LVM_BINARY
58 LVM=${LVM_BINARY:-lvm}
68 TEMPDIR="${TMPDIR:-/tmp}/${TOOL}_${RANDOM}$$/m"
74 PROCMOUNTS="/proc/mounts"
82 echo "${TOOL}: Utility to resize or check the filesystem on a device"
84 echo " ${TOOL} [options] check device"
85 echo " - Check the filesystem on device using fsck"
87 echo " ${TOOL} [options] resize device [new_size[BKMGTPE]]"
88 echo " - Change the size of the filesystem on device to new_size"
91 echo " -h | --help Show this help message"
92 echo " -v | --verbose Be verbose"
93 echo " -e | --ext-offline unmount filesystem before ext2/ext3/ext4 resize"
94 echo " -f | --force Bypass sanity checks"
95 echo " -n | --dry-run Print commands without running them"
96 echo " -l | --lvresize Resize given device (if it is LVM device)"
97 echo " -y | --yes Answer \"yes\" at any prompts"
99 echo " new_size - Absolute number of filesystem blocks to be in the filesystem,"
100 echo " or an absolute size using a suffix (in powers of 1024)."
101 echo " If new_size is not supplied, the whole device is used."
107 test -n "$VERB" && echo "$TOOL: $@" || true
116 if [ "$DRY" -ne 0 ]; then
117 verbose "Dry execution $@"
120 verbose "Executing $@"
126 # reset MOUNTPOINT - avoid recursion
127 test "$MOUNTPOINT" = "$TEMPDIR" && MOUNTPOINT="" temp_umount
128 if [ -n "$REMOUNT" ]; then
129 verbose "Remounting unmounted filesystem back"
130 dry $MOUNT "$VOLUME" "$MOUNTED"
135 test "$1" -eq 2 && verbose "Break detected"
137 if [ "$DO_LVRESIZE" -eq 2 ]; then
138 # start LVRESIZE with the filesystem modification flag
139 # and allow recursive call of fsadm
143 dry exec $LVM lvresize $VERB $FORCE -r -L${NEWSIZE}b $VOLUME_ORIG
146 # error exit status for break
150 # convert parameter from Exa/Peta/Tera/Giga/Mega/Kilo/Bytes and blocks
151 # (2^(60/50/40/30/20/10/0))
154 *[eE]) NEWSIZE=$(( ${1%[eE]} * 1152921504606846976 )) ;;
155 *[pP]) NEWSIZE=$(( ${1%[pP]} * 1125899906842624 )) ;;
156 *[tT]) NEWSIZE=$(( ${1%[tT]} * 1099511627776 )) ;;
157 *[gG]) NEWSIZE=$(( ${1%[gG]} * 1073741824 )) ;;
158 *[mM]) NEWSIZE=$(( ${1%[mM]} * 1048576 )) ;;
159 *[kK]) NEWSIZE=$(( ${1%[kK]} * 1024 )) ;;
160 *[bB]) NEWSIZE=${1%[bB]} ;;
161 *) NEWSIZE=$(( $1 * $2 )) ;;
163 #NEWBLOCKCOUNT=$(round_block_size $NEWSIZE $2)
164 NEWBLOCKCOUNT=$(( $NEWSIZE / $2 ))
166 if [ $DO_LVRESIZE -eq 1 ]; then
167 # start lvresize, but first cleanup mounted dirs
173 # detect filesystem on the given device
174 # dereference device name if it is symbolic link
178 VOLUME=$($READLINK $READLINK_E "/dev/$VOLUME") || error "Cannot get readlink $1"
182 read </sys/block/${RVOLUME#/dev/}/dm/name SYSVOLUME 2>&1 && VOLUME="/dev/mapper/$SYSVOLUME"
185 # use /dev/null as cache file to be sure about the result
186 # not using option '-o value' to be compatible with older version of blkid
187 FSTYPE=$($BLKID -c /dev/null -s TYPE "$VOLUME") || error "Cannot get FSTYPE of \"$VOLUME\""
188 FSTYPE=${FSTYPE##*TYPE=\"} # cut quotation marks
189 FSTYPE=${FSTYPE%%\"*}
190 verbose "\"$FSTYPE\" filesystem found on \"$VOLUME\""
193 # check if the given device is already mounted and where
194 # FIXME: resolve swap usage and device stacking
196 test -e $PROCMOUNTS || error "Cannot detect mounted device $VOLUME"
198 MOUNTED=$($GREP ^"$VOLUME" $PROCMOUNTS)
200 # for empty string try again with real volume name
201 test -z "$MOUNTED" && MOUNTED=$($GREP ^"$RVOLUME" $PROCMOUNTS)
203 # cut device name prefix and trim everything past mountpoint
204 # echo translates \040 to spaces
205 MOUNTED=${MOUNTED#* }
206 MOUNTED=$(echo -n -e ${MOUNTED%% *})
208 # for systems with different device names - check also mount output
209 if test -z "$MOUNTED" ; then
210 MOUNTED=$(LANG=C $MOUNT | $GREP ^"$VOLUME")
211 test -z "$MOUNTED" && MOUNTED=$(LANG=C $MOUNT | $GREP ^"$RVOLUME")
212 MOUNTED=${MOUNTED##* on }
213 MOUNTED=${MOUNTED% type *} # allow type in the mount name
219 # get the full size of device in bytes
220 detect_device_size() {
221 # check if blockdev supports getsize64
222 $BLOCKDEV 2>&1 | $GREP getsize64 >/dev/null
223 if test $? -eq 0; then
224 DEVSIZE=$($BLOCKDEV --getsize64 "$VOLUME") || error "Cannot read size of device \"$VOLUME\""
226 DEVSIZE=$($BLOCKDEV --getsize "$VOLUME") || error "Cannot read size of device \"$VOLUME\""
227 SSSIZE=$($BLOCKDEV --getss "$VOLUME") || error "Cannot block size read device \"$VOLUME\""
228 DEVSIZE=$(($DEVSIZE * $SSSIZE))
233 # could be needed to gaurantee 'at least given size'
234 # but it makes many troubles
235 round_up_block_size() {
236 echo $(( ($1 + $2 - 1) / $2 ))
240 dry $MKDIR -p -m 0000 "$TEMPDIR" || error "Failed to create $TEMPDIR"
241 dry $MOUNT "$VOLUME" "$TEMPDIR" || error "Failed to mount $TEMPDIR"
245 dry $UMOUNT "$TEMPDIR" || error "Failed to umount $TEMPDIR"
246 dry $RMDIR "${TEMPDIR}" || error "Failed to remove $TEMPDIR"
247 dry $RMDIR "${TEMPDIR%%m}" || error "Failed to remove ${TEMPDIR%%m}"
253 if [ -n "$YES" ]; then
257 while read -r -s -n 1 ANS ; do
259 "y" | "Y" | "") echo y ; return 0 ;;
260 "n" | "N") echo n ; return 1 ;;
266 yes_no "Do you want to unmount \"$MOUNTED\"" && dry $UMOUNT "$MOUNTED" && return 0
267 error "Cannot proceed with mounted filesystem \"$MOUNTED\""
271 test -n "$BLOCKSIZE" -a -n "$BLOCKCOUNT" || error "Cannot parse $1 output"
273 ####################################
274 # Resize ext2/ext3/ext4 filesystem
275 # - unmounted or mounted for upsize
276 # - unmounted for downsize
277 ####################################
279 verbose "Parsing $TUNE_EXT -l \"$VOLUME\""
280 for i in $(LANG=C $TUNE_EXT -l "$VOLUME"); do
282 "Block size"*) BLOCKSIZE=${i##* } ;;
283 "Block count"*) BLOCKCOUNT=${i##* } ;;
286 validate_parsing $TUNE_EXT
287 decode_size $1 $BLOCKSIZE
290 if [ "$NEWBLOCKCOUNT" -lt "$BLOCKCOUNT" -o "$EXTOFF" -eq 1 ]; then
291 detect_mounted && verbose "$RESIZE_EXT needs unmounted filesystem" && try_umount
293 if test -n "$MOUNTED" ; then
294 # Forced fsck -f for umounted extX filesystem.
296 *i*) dry $FSCK $YES -f "$VOLUME" ;;
297 *) dry $FSCK -f -p "$VOLUME" ;;
302 verbose "Resizing filesystem on device \"$VOLUME\" to $NEWSIZE bytes ($BLOCKCOUNT -> $NEWBLOCKCOUNT blocks of $BLOCKSIZE bytes)"
303 dry $RESIZE_EXT $FSFORCE "$VOLUME" $NEWBLOCKCOUNT
306 #############################
307 # Resize reiserfs filesystem
308 # - unmounted for upsize
309 # - unmounted for downsize
310 #############################
312 detect_mounted && verbose "ReiserFS resizes only unmounted filesystem" && try_umount
314 verbose "Parsing $TUNE_REISER \"$VOLUME\""
315 for i in $(LANG=C $TUNE_REISER "$VOLUME"); do
317 "Blocksize"*) BLOCKSIZE=${i##*: } ;;
318 "Count of blocks"*) BLOCKCOUNT=${i##*: } ;;
321 validate_parsing $TUNE_REISER
322 decode_size $1 $BLOCKSIZE
323 verbose "Resizing \"$VOLUME\" $BLOCKCOUNT -> $NEWBLOCKCOUNT blocks ($NEWSIZE bytes, bs: $NEWBLOCKCOUNT)"
324 if [ -n "$YES" ]; then
325 echo y | dry $RESIZE_REISER -s $NEWSIZE "$VOLUME"
327 dry $RESIZE_REISER -s $NEWSIZE "$VOLUME"
331 ########################
332 # Resize XFS filesystem
333 # - mounted for upsize
335 ########################
339 if [ -z "$MOUNTED" ]; then
341 temp_mount || error "Cannot mount Xfs filesystem"
343 verbose "Parsing $TUNE_XFS \"$MOUNTPOINT\""
344 for i in $(LANG=C $TUNE_XFS "$MOUNTPOINT"); do
346 "data"*) BLOCKSIZE=${i##*bsize=} ; BLOCKCOUNT=${i##*blocks=} ;;
349 BLOCKSIZE=${BLOCKSIZE%%[^0-9]*}
350 BLOCKCOUNT=${BLOCKCOUNT%%[^0-9]*}
351 validate_parsing $TUNE_XFS
352 decode_size $1 $BLOCKSIZE
353 if [ $NEWBLOCKCOUNT -gt $BLOCKCOUNT ]; then
354 verbose "Resizing Xfs mounted on \"$MOUNTPOINT\" to fill device \"$VOLUME\""
355 dry $RESIZE_XFS $MOUNTPOINT
356 elif [ $NEWBLOCKCOUNT -eq $BLOCKCOUNT ]; then
357 verbose "Xfs filesystem already has the right size"
359 error "Xfs filesystem shrinking is unsupported"
370 verbose "Device \"$VOLUME\" size is $DEVSIZE bytes"
371 # if the size parameter is missing use device size
372 #if [ -n "$NEWSIZE" -a $NEWSIZE <
373 test -z "$NEWSIZE" && NEWSIZE=${DEVSIZE}b
376 "ext3"|"ext2"|"ext4") resize_ext $NEWSIZE ;;
377 "reiserfs") resize_reiser $NEWSIZE ;;
378 "xfs") resize_xfs $NEWSIZE ;;
379 *) error "Filesystem \"$FSTYPE\" on device \"$VOLUME\" is not supported by this tool" ;;
380 esac || error "Resize $FSTYPE failed"
384 ####################################
385 # Calclulate diff between two dates
386 # LANG=C input is expected the
388 ####################################
390 echo $(( $($DATE -u -d"$1" +%s 2>/dev/null) - $($DATE -u -d"$2" +%s 2>/dev/null) ))
398 if detect_mounted ; then
399 verbose "Skipping filesystem check for device \"$VOLUME\" as the filesystem is mounted on $MOUNTED";
404 "ext2"|"ext3"|"ext4")
407 for i in $(LANG=C $TUNE_EXT -l "$VOLUME"); do
409 "Last mount"*) LASTMOUNT=${i##*: } ;;
410 "Last checked"*) LASTCHECKED=${i##*: } ;;
414 *"n/a") ;; # nothing to do - system was not mounted yet
416 LASTDIFF=$(diff_dates $LASTMOUNT $LASTCHECKED)
417 if test "$LASTDIFF" -gt 0 ; then
418 verbose "Filesystem has not been checked after the last mount, using fsck -f"
427 "xfs") dry $XFS_CHECK "$VOLUME" ;;
428 *) # check if executed from interactive shell environment
430 *i*) dry $FSCK $YES $FORCE "$VOLUME" ;;
431 *) dry $FSCK $FORCE -p "$VOLUME" ;;
436 #############################
437 # start point of this script
438 # - parsing parameters
439 #############################
442 # test if we are not invoked recursively
443 test -n "$FSADM_RUNNING" && exit 0
445 # test some prerequisities
446 test -n "$TUNE_EXT" -a -n "$RESIZE_EXT" -a -n "$TUNE_REISER" -a -n "$RESIZE_REISER" \
447 -a -n "$TUNE_XFS" -a -n "$RESIZE_XFS" -a -n "$MOUNT" -a -n "$UMOUNT" -a -n "$MKDIR" \
448 -a -n "$RMDIR" -a -n "$BLOCKDEV" -a -n "$BLKID" -a -n "$GREP" -a -n "$READLINK" \
449 -a -n "$DATE" -a -n "$FSCK" -a -n "$XFS_CHECK" -a -n "LVM" \
450 || error "Required command definitions in the script are missing!"
452 $LVM version >/dev/null 2>&1 || error "Could not run lvm binary '$LVM'"
453 $($READLINK -e / >/dev/null 2>&1) || READLINK_E="-f"
454 TEST64BIT=$(( 1000 * 1000000000000 ))
455 test $TEST64BIT -eq 1000000000000000 || error "Shell does not handle 64bit arithmetic"
456 $(echo Y | $GREP Y >/dev/null) || error "Grep does not work properly"
457 test $($DATE -u -d"Jan 01 00:00:01 1970" +%s) -eq 1 || error "Date translation does not work"
460 if [ "$#" -eq 0 ] ; then
468 "-h"|"--help") tool_usage ;;
469 "-v"|"--verbose") VERB="-v" ;;
470 "-n"|"--dry-run") DRY=1 ;;
471 "-f"|"--force") FORCE="-f" ;;
472 "-e"|"--ext-offline") EXTOFF=1 ;;
473 "-y"|"--yes") YES="-y" ;;
474 "-l"|"--lvresize") DO_LVRESIZE=1 ;;
475 "check") CHECK="$2" ; shift ;;
476 "resize") RESIZE="$2"; NEWSIZE="$3" ; shift 2 ;;
477 *) error "Wrong argument \"$1\". (see: $TOOL --help)"
482 if [ -n "$CHECK" ]; then
484 elif [ -n "$RESIZE" ]; then
485 export FSADM_RUNNING="fsadm"
486 resize "$RESIZE" "$NEWSIZE"
488 error "Missing command. (see: $TOOL --help)"