Skip to contentSkip to conversation

Add support for btrfs into fsadm script

PATCH New Ondrej Kozina 1 participant 4 messages Nov 23, 2012

lore
Share & tools
lore
#0 lore raw

cover Add support for btrfs into fsadm script

This patch propose basic support for btrfs in fsadm script. But in my opinion, btrfs enabled fsadm script shouldn't be used before taking into consideration following notes:

btrfsck doesn't work as one would expect. It can't detect block device was resized w/o proper filesystem resize in advance. It's reproducible by trivial test (not subject to LVM/dm only. You'll get same results if you use standard partition instead):

- lvcreate -n test_lv -L1G test_vg
- mkfs.btrfs /dev/mapper/test_vg-test_lv
- (mount and fill w/ data)
- lvresize -nf -L512M test_vg/test_lv
- btrfsck /dev/mapper/test_vg-test_lv

btrfsck tool doesn't complain about missing data chunks. Neither does mount command. So you never know something ugly happened to your fs until you start getting wall of i/o errors in syslog. Hopefully, this will fixed as btrfsck tool mature over time.

Also there are some difficulties identifying mountpoint for btrfs (sub)volume. This is caused mainly by the fact that btrfs (pool) can span across multiple devices and there's only one entry in /dev/disk/by-uuid directory per btrfs main instance. Moreover, the entry may change over time as devices are added to or removed from the pool. This is the reason I can't use simple command as:

findmnt -o TARGET -S UUID=

but instead I have to check for any mounted btrfs (sub)volume and grep for first mountpoint in the output.

btrfs-progs version used 0.20-rc1
kernel 3.6

Ondrej Kozina (3):
- add btrfs support into fsadm
- minor change in fsadm man page
- add simple test for fsadm btrfs suport

 man/fsadm.8.in            |   6 +-
 scripts/fsadm.sh          | 141 ++++++++++++++++++++++++++++++++++++++---
 test/shell/fsadm-btrfs.sh | 156 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 293 insertions(+), 10 deletions(-)
 create mode 100644 test/shell/fsadm-btrfs.sh
Signature · 1 line
1.8.0
#1 lore raw

PATCH 1/3 - add btrfs support into fsadm

---
 scripts/fsadm.sh | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 133 insertions(+), 8 deletions(-)
scripts/fsadm.sh +133 -8
index 4624a1c..6adbe09 100755
@@ -17,11 +17,12 @@
 # Script for resizing devices (usable for LVM resize)
 #
 # Needed utilities:
-#   mount, umount, grep, readlink, blockdev, blkid, fsck, xfs_check
+#   mount, umount, grep, readlink, blockdev, blkid, fsck, xfs_check, btrfs-progs, lsblk, findmnt
 #
 # ext2/ext3/ext4: resize2fs, tune2fs
 # reiserfs: resize_reiserfs, reiserfstune
 # xfs: xfs_growfs, xfs_info
+# btrfs: btrfs, btrfsck
 #
 # Return values:
 #   0 success
@@ -41,6 +42,7 @@ TUNE_REISER=reiserfstune
 RESIZE_REISER=resize_reiserfs
 TUNE_XFS=xfs_info
 RESIZE_XFS=xfs_growfs
+BTRFS=btrfs
 
 MOUNT=mount
 UMOUNT=umount
@@ -48,12 +50,15 @@ MKDIR=mkdir
 RMDIR=rmdir
 BLOCKDEV=blockdev
 BLKID=blkid
+# set by check_findmnt()
+FINDMNT=
 DATE=date
 GREP=grep
 READLINK=readlink
 READLINK_E="-e"
 FSCK=fsck
 XFS_CHECK=xfs_check
+BTRFS_CHECK=btrfsck
 
 # user may override lvm location by setting LVM_BINARY
 LVM=${LVM_BINARY:-lvm}
@@ -74,12 +79,16 @@ MOUNTPOINT=
 MOUNTED=
 REMOUNT=
 PROCMOUNTS="/proc/mounts"
+PROCPARTITIONS="/proc/partitions"
 NULL="$DM_DEV_DIR/null"
+BTRFS_DEVID=
+UUID=
 
 IFS_OLD=$IFS
 # without bash $'\n'
 NL='
 '
+READ_IFS=" $(echo -n -e '\t')"
 
 tool_usage() {
 	echo "${TOOL}: Utility to resize or check the filesystem on a device"
@@ -152,6 +161,10 @@ cleanup() {
 	exit ${1:-1}
 }
 
+check_findmnt() {
+	FINDMNT=$(which findmnt 2> $NULL);
+}
+
 # convert parameter from Exa/Peta/Tera/Giga/Mega/Kilo/Bytes and blocks
 # (2^(60/50/40/30/20/10/0))
 decode_size() {
@@ -196,15 +209,87 @@ detect_fs() {
 	verbose "\"$FSTYPE\" filesystem found on \"$VOLUME\""
 }
 
+detect_fs_uuid() {
+	UUID=$($BLKID -o value -c $NULL -s UUID "$VOLUME" 2>$NULL)
+
+	test -n "$UUID"
+}
+
+# example:
+# 'btrfs fi show' output :
+# 	devid    3 size 1.00GB used 894.50MB path /dev/sdb
+# need:		 ^
+btrfs_parse_devid() {
+	local a devid b
+	while read -r a devid b;
+	do
+		echo -n "$devid";
+	done;
+}
+
+btrfs_get_devid() {
+	local STR_IFS=$IFS
+
+	TMP=$(lsblk -Pdno MAJ:MIN $VOLUME)
+	TMP=${TMP##*=\"}
+	MAJOR=${TMP%%:*}
+	MINOR=${TMP##*:}
+	MINOR=${MINOR%%\"*}
+
+	test -n "$MAJOR" -a -n "$MINOR" || error "Can't get \"$VOLUME\" major:minor numbers"
+
+	IFS=$READ_IFS
+	test -e "$PROCPARTITIONS" || error "Can't read $PROCPARTITIONS"
+	while read -r RMAJOR RMINOR RSIZE RDEV;
+	do
+		if [ "$RMAJOR" = "$MAJOR" -a "$RMINOR" = "$MINOR" ] ; then
+			BTRFS_DEVICE=$RDEV
+			break;
+		fi
+	done <$PROCPARTITIONS
+
+	test -z $BTRFS_DEVICE || $BTRFS filesystem show "$UUID" 2> $NULL | $GREP "/dev/$BTRFS_DEVICE" | btrfs_parse_devid
+	IFS=$STR_IFS
+}
+
+
+# The whole btrfs volume (subvolid=0) is identified by UUID.
+# Also there can be multiple subvolumes mounted as well
+# identified by same UUID
+get_first_mounted_subvolume() {
+	local TMP
+	local STR_IFS=$IFS
+	IFS=$READ_IFS
+
+	read -r TMP<<EOF
+$($FINDMNT -nuP -o TARGET,UUID 2>$NULL | $GREP "$UUID")
+EOF
+
+	TMP=${TMP##*TARGET=\"}
+	TMP=${TMP%%\"*}
+	echo -n $TMP
+
+	IFS=$STR_IFS
+}
+
+detect_mounted_findmnt() {
+	MOUNTED=$(get_first_mounted_subvolume)
+
+	verbose "found $MOUNTED"
+	test -n "$MOUNTED"
+}
+
 # check if the given device is already mounted and where
 # FIXME: resolve swap usage and device stacking
-detect_mounted()  {
-	test -e "$PROCMOUNTS" || error "Cannot detect mounted device \"$VOLUME\""
+# $1 - VOLUME
+# $2 - RVOLUME
+detect_mounted_classic() {
+	test -e "$PROCMOUNTS" || error "Cannot detect mounted device \"$1\""
 
-	MOUNTED=$("$GREP" "^$VOLUME[ \t]" "$PROCMOUNTS")
+	MOUNTED=$("$GREP" "^$1[ \t]" "$PROCMOUNTS")
 
 	# for empty string try again with real volume name
-	test -z "$MOUNTED" && MOUNTED=$("$GREP" "^$RVOLUME[ \t]" "$PROCMOUNTS")
+	test -z "$MOUNTED" && MOUNTED=$("$GREP" "^$2[ \t]" "$PROCMOUNTS")
 
 	# cut device name prefix and trim everything past mountpoint
 	# echo translates \040 to spaces
@@ -213,8 +298,8 @@ detect_mounted()  {
 
 	# for systems with different device names - check also mount output
 	if test -z "$MOUNTED" ; then
-		MOUNTED=$(LANG=C "$MOUNT" | "$GREP" "^$VOLUME[ \t]")
-		test -z "$MOUNTED" && MOUNTED=$(LANG=C "$MOUNT" | "$GREP" "^$RVOLUME[ \t]")
+		MOUNTED=$(LANG=C "$MOUNT" | "$GREP" "^$1[ \t]")
+		test -z "$MOUNTED" && MOUNTED=$(LANG=C "$MOUNT" | "$GREP" "^$2[ \t]")
 		MOUNTED=${MOUNTED##* on }
 		MOUNTED=${MOUNTED% type *} # allow type in the mount name
 	fi
@@ -222,6 +307,17 @@ detect_mounted()  {
 	test -n "$MOUNTED"
 }
 
+
+detect_mounted() {
+	if test "$FSTYPE" = "btrfs" ; then
+		detect_fs_uuid || verbose "Can't get fs UUID from \"$VOLUME\" volume"
+		test -n "$FINDMNT" || error "Need 'findmnt' utility to work with btrfs filesystem"
+		detect_mounted_findmnt
+	else
+		detect_mounted_classic "$VOLUME" "$RVOLUME"
+	fi
+}
+
 # get the full size of device in bytes
 detect_device_size() {
 	# check if blockdev supports getsize64
@@ -366,6 +462,31 @@ resize_xfs() {
 	fi
 }
 
+
+################################
+# Resize Btrfs filesystem
+# - mounted for upsize/downsize
+# - need to find btrfs devid
+################################
+resize_btrfs() {
+	detect_mounted
+	MOUNTPOINT=$MOUNTED
+	if [ -z "$MOUNTED" ]; then
+		MOUNTPOINT=$TEMPDIR
+		temp_mount || error "Cannot mount Btrfs filesystem"
+	fi
+
+	# need to find btrfs devid to resize right btrfs volume
+	BTRFS_DEVID=$(btrfs_get_devid)
+	test -n "$BTRFS_DEVID" || error "Cannot find btrfs devid for \"$VOLUME ($RVOLUME)\""
+
+	# btrfs tool doesn't decode units above GiB
+	decode_size "$1" 1
+
+	verbose "Resizing \"$VOLUME\" btrfs filesystem to $NEWSIZE bytes (btrfs devid: $BTRFS_DEVID)"
+	dry "$BTRFS" filesystem resize $BTRFS_DEVID:$NEWSIZE $MOUNTPOINT
+}
+
 ####################
 # Resize filesystem
 ####################
@@ -382,6 +503,7 @@ resize() {
 	  "ext3"|"ext2"|"ext4") resize_ext $NEWSIZE ;;
 	  "reiserfs") resize_reiser $NEWSIZE ;;
 	  "xfs") resize_xfs $NEWSIZE ;;
+	  "btrfs") resize_btrfs $NEWSIZE ;;
 	  *) error "Filesystem \"$FSTYPE\" on device \"$VOLUME\" is not supported by this tool" ;;
 	esac || error "Resize $FSTYPE failed"
 	cleanup 0
@@ -431,6 +553,7 @@ check() {
 
 	case "$FSTYPE" in
 	  "xfs") dry "$XFS_CHECK" "$VOLUME" ;;
+	  "btrfs") dry "$BTRFS_CHECK" "$VOLUME" ;;
 	  *)    # check if executed from interactive shell environment
 		case "$-" in
 		  *i*) dry "$FSCK" $YES $FORCE "$VOLUME" ;;
@@ -453,6 +576,7 @@ test -n "$TUNE_EXT" -a -n "$RESIZE_EXT" -a -n "$TUNE_REISER" -a -n "$RESIZE_REIS
   -a -n "$TUNE_XFS" -a -n "$RESIZE_XFS" -a -n "$MOUNT" -a -n "$UMOUNT" -a -n "$MKDIR" \
   -a -n "$RMDIR" -a -n "$BLOCKDEV" -a -n "$BLKID" -a -n "$GREP" -a -n "$READLINK" \
   -a -n "$DATE" -a -n "$FSCK" -a -n "$XFS_CHECK" -a -n "$LVM" \
+  -a -n "$BTRFS" -a -n "$BTRFS_CHECK" -a -z "$FINDMNT" \
   || error "Required command definitions in the script are missing!"
 
 "$LVM" version >"$NULL" 2>&1 || error "Could not run lvm binary \"$LVM\""
@@ -462,11 +586,12 @@ test "$TEST64BIT" -eq 1000000000000000 || error "Shell does not handle 64bit ari
 $(echo Y | "$GREP" Y >"$NULL") || error "Grep does not work properly"
 test $("$DATE" -u -d"Jan 01 00:00:01 1970" +%s) -eq 1 || error "Date translation does not work"
 
-
 if [ "$#" -eq 0 ] ; then
 	tool_usage
 fi
 
+check_findmnt
+
 while [ "$#" -ne 0 ]
 do
 	 case "$1" in
Signature · 1 line
1.8.0
#2 lore raw

PATCH 2/3 - minor change in fsadm man page

In reply to Ondrej Kozina
---
 man/fsadm.8.in | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
man/fsadm.8.in +4 -2
index 46be1ff..17874db 100644
@@ -16,7 +16,7 @@ fsadm \- utility to resize or check filesystem on a device
 .SH DESCRIPTION
 fsadm utility checks or resizes the filesystem on a device.
 It tries to use the same API for 
-.IR ext2 ", " ext3 ", " ext4 ", " ReiserFS " and " XFS
+.IR ext2 ", " ext3 ", " ext4 ", " ReiserFS ", " XFS " and "btrfs
 filesystem.
 .SH OPTIONS
 .TP
@@ -78,4 +78,6 @@ Defaults to "/dev" and must be an absolute path.
 .BR resize_reiserfs (8),
 .BR xfs_info (8),
 .BR xfs_growfs (8),
-.BR xfs_check (8)
+.BR xfs_check (8),
+.BR btrfs (8),
+.BR btrfsck (8)
Signature · 1 line
1.8.0
#3 lore raw

PATCH 3/3 - add simple test for fsadm btrfs suport

In reply to Ondrej Kozina
---
 test/shell/fsadm-btrfs.sh | 156 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 156 insertions(+)
 create mode 100644 test/shell/fsadm-btrfs.sh
test/shell/fsadm-btrfs.sh new file +156 -0
new file mode 100644
index 0000000..f3333ee
@@ -0,0 +1,156 @@
+#!/bin/sh
+# Copyright (C) 2012 Red Hat, Inc. All rights reserved.
+#
+# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+test_description='Exercise fsadm btrfs resize'
+
+. lib/test
+
+aux prepare_vg 1 1024
+
+which mkfs.btrfs || check_btrfs=${check_btrfs:-mkfs.btrfs}
+which btrfsck	 || check_btrfs=${check_btrfs:-btrfsck}
+which btrfs	 || check_btrfs=${check_btrfs:-btrfs}
+
+vg_lv=$vg/$lv1
+vg_lv2=$vg/${lv1}bar
+vg_lv3=$vg/${lv1}bar2
+dev_vg_lv="$DM_DEV_DIR/$vg_lv"
+dev_vg_lv2="$DM_DEV_DIR/$vg_lv2"
+dev_vg_lv3="$DM_DEV_DIR/$vg_lv3"
+mount_dir="mnt"
+mount_space_dir="mnt space dir"
+# for recursive call
+export LVM_BINARY=$(which lvm)
+
+test ! -d "$mount_dir" && mkdir "$mount_dir"
+test ! -d "$mount_space_dir" && mkdir "$mount_space_dir"
+
+cleanup_mounted_and_teardown()
+{
+	umount "$mount_dir" || true
+	umount "$mount_space_dir" || true
+	aux teardown
+}
+
+check_missing_btrfs()
+{
+	eval local t=$check_btrfs
+	test -z "$t" && return 0
+	test "$t" = skip && return 1
+	# trick for warning test
+	echo "WARNING: fsadm test skipped btrfs tests, $t tool is missing"
+	return 1
+}
+
+fscheck_btrfs() {
+	btrfsck "$1"
+}
+
+lvcreate -n $lv1 -L300M $vg
+lvcreate -n ${lv1}bar -L300M $vg
+lvcreate -n ${lv1}bar2 -L300M $vg
+trap 'cleanup_mounted_and_teardown' EXIT
+
+single_device_test() {
+	mkfs.btrfs -m single "$dev_vg_lv"
+	mkfs.btrfs -m single "$dev_vg_lv2"
+	# 256 MB is minimal fs size btrfs is resizable to
+	# you can grow fs from 30MB->256MB
+	# but you can't grow from 30MB->180MB
+	fsadm --lvresize resize $vg_lv 256M
+	fsadm --lvresize resize $vg_lv2 256M
+
+	not fsadm -y --lvresize resize $vg_lv 200M
+	lvresize -L+10M -r $vg_lv
+	lvreduce -L256M -r $vg_lv
+	fscheck_btrfs $dev_vg_lv
+
+	mount "$dev_vg_lv" "$mount_dir"
+	mount "$dev_vg_lv2" "$mount_space_dir"
+
+	not fsadm -y --lvresize resize $vg_lv 200M
+	lvresize -L+10M -r $vg_lv
+	lvreduce -L256M -r $vg_lv
+	umount "$mount_dir"
+	fscheck_btrfs $dev_vg_lv
+
+	not fsadm -y --lvresize resize $vg_lv2 200M
+	lvresize -L+10M -r $vg_lv2
+	lvreduce -L256M -r $vg_lv2
+	umount "$mount_space_dir"
+	fscheck_btrfs $dev_vg_lv2
+}
+
+multiple_devices_test() {
+	mkfs.btrfs -m single -d single "$dev_vg_lv" "$dev_vg_lv2" "$dev_vg_lv3"
+
+	fsadm -y --lvresize resize $vg_lv 256M
+	fsadm -y --lvresize resize $vg_lv2 256M
+	fsadm -y --lvresize resize $vg_lv3 256M
+
+	# check if fsadm is able to get/resize btrfs on
+	# the right device
+	lvresize -L+150M $vg_lv
+	fsadm -y resize $vg_lv 300M
+	not fsadm -y resize $vg_lv2 300M
+	not fsadm -y resize $vg_lv3 300M
+	lvreduce -y -L256M -r $vg_lv
+
+	lvresize -L+150M $vg_lv2
+	fsadm -y resize $vg_lv2 300M
+	not fsadm -y resize $vg_lv3 300M
+	not fsadm -y resize $vg_lv 300M
+	lvreduce -y -L256M -r $vg_lv2
+
+	lvresize -L+150M $vg_lv3
+	fsadm -y resize $vg_lv3 300M
+	not fsadm -y resize $vg_lv2 300M
+	not fsadm -y resize $vg_lv 300M
+	lvreduce -y -L256M -r $vg_lv3
+	fscheck_btrfs $dev_vg_lv
+
+	# repeat with mounted fs
+	mount "$dev_vg_lv" "$mount_dir"
+	mount "$dev_vg_lv2" "$mount_space_dir"
+
+	lvresize -L300M $vg_lv
+	fsadm -y resize $vg_lv 300M
+	not fsadm -y resize $vg_lv2 300M
+	not fsadm -y resize $vg_lv3 300M
+	lvreduce -y -L256M -r $vg_lv
+
+	lvresize -L300M $vg_lv2
+	fsadm -y resize $vg_lv2 300M
+	not fsadm -y resize $vg_lv3 300M
+	not fsadm -y resize $vg_lv 300M
+	lvreduce -y -L256M -r $vg_lv2
+
+	lvresize -L300M $vg_lv3
+	fsadm -y resize $vg_lv3 300M
+	not fsadm -y resize $vg_lv2 300M
+	not fsadm -y resize $vg_lv 300M
+	lvreduce -y -L256M -r $vg_lv3
+
+	umount "$mount_dir"
+	umount "$mount_space_dir"
+	fscheck_btrfs $dev_vg_lv
+
+	lvresize -nf -L300M $vg_lv
+	lvresize -nf -L300M $vg_lv2
+}
+
+if check_missing_btrfs; then
+
+	single_device_test
+	multiple_devices_test
+fi
+
+vgremove -ff $vg
Signature · 1 line
1.8.0