All of lore.kernel.org
 help / color / mirror / Atom feed
From: Lukas Czerner <lczerner@redhat.com>
To: zkabelac@redhat.com
Cc: Lukas Czerner <lczerner@redhat.com>,
	dchinner@redhat.com, rwheeler@redhat.com, linux-lvm@redhat.com
Subject: [linux-lvm] [PATCH 03/35] fsadm: Add "list" command
Date: Wed, 21 Sep 2011 18:45:22 +0200	[thread overview]
Message-ID: <1316623554-28975-4-git-send-email-lczerner@redhat.com> (raw)
In-Reply-To: <1316623554-28975-1-git-send-email-lczerner@redhat.com>

List command provides the functionality of listing useful information
about the logical volumes, file systems and devices.

Signed-off-by: Lukas Czerner <lczerner@redhat.com>
---
 scripts/fsadm.sh |  231 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 229 insertions(+), 2 deletions(-)

diff --git a/scripts/fsadm.sh b/scripts/fsadm.sh
index 2895371..803c6a5 100755
--- a/scripts/fsadm.sh
+++ b/scripts/fsadm.sh
@@ -17,7 +17,8 @@
 # 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, bc, df
+#   xfs_db
 #
 # ext2/ext3/ext4: resize2fs, tune2fs
 # reiserfs: resize_reiserfs, reiserfstune
@@ -54,6 +55,8 @@ READLINK=readlink
 READLINK_E="-e"
 FSCK=fsck
 XFS_CHECK=xfs_check
+BC=bc
+DF=df
 
 # user may override lvm location by setting LVM_BINARY
 LVM=${LVM_BINARY:-lvm}
@@ -74,6 +77,8 @@ MOUNTPOINT=
 MOUNTED=
 REMOUNT=
 PROCMOUNTS="/proc/mounts"
+PROCDEVICES="/proc/devices"
+PROCPARTITIONS="/proc/partitions"
 NULL="$DM_DEV_DIR/null"
 
 MAX_VGS=999
@@ -202,7 +207,6 @@ detect_fs() {
 	FSTYPE=$("$BLKID" -c "$NULL" -s TYPE "$VOLUME") || error "Cannot get FSTYPE of \"$VOLUME\""
 	FSTYPE=${FSTYPE##*TYPE=\"} # cut quotation marks
 	FSTYPE=${FSTYPE%%\"*}
-	verbose "\"$FSTYPE\" filesystem found on \"$VOLUME\""
 }
 
 # check if the given device is already mounted and where
@@ -413,6 +417,7 @@ generic_make_fs() {
 resize() {
 	NEWSIZE=$2
 	detect_fs "$1"
+	verbose "\"$FSTYPE\" filesystem found on \"$VOLUME\""
 	is_natural $NEWSIZE
 	[ $? -ne 1 ] && error "$NEWSIZE is not valid number for file system size"
 	detect_device_size
@@ -565,6 +570,226 @@ destroy() {
 	dry $LVM lvremove $FORCE $device
 }
 
+float_math() {
+	if [ $# -le 0 ]; then
+		return
+	fi
+	result=$(LANG=C echo "scale=2; $@" | $BC -q 2> /dev/null)
+	echo $result
+}
+
+#####################################
+# Convet the size into human readable
+# form. size in KiB expected
+#####################################
+humanize_size() {
+	size=$1
+	count=0
+	while [ ${size%%.*} -ge 1024 ] && [ $count -lt 7 ]; do
+		size=$(float_math $size/1024)
+		count=$(($count+1))
+	done
+	case $count in
+		0) unit="KiB" ;;
+		1) unit="MiB" ;;
+		2) unit="GiB" ;;
+		3) unit="TiB" ;;
+		4) unit="PiB" ;;
+		5) unit="EiB" ;;
+		6) unit="ZiB" ;;
+		7) unit="YiB" ;;
+		*) unit="???" ;;
+	esac
+	echo "$size $unit"
+}
+
+get_ext_size() {
+	IFS=$NL
+	for i in $(LANG=C $TUNE_EXT -l "$VOLUME"); do
+		case "$i" in
+			"Block size"*) bsize=${i##*  } ;;
+			"Block count"*) bcount=${i##*  } ;;
+			"Reserved block count"*) rbcount=${i##*  } ;;
+			"Free blocks"*) fbcount=${i##*  } ;;
+		esac
+	done
+
+	bsize=$(($bsize/1024))
+	total=$(($bcount*$bsize))
+	TOTAL=$(humanize_size $total)
+	used=$((($bcount-$fbcount)*$bsize))
+	USED=$(humanize_size $used)
+	free=$((($fbcount-$rbcount)*$bsize))
+	FREE=$(humanize_size $free)
+	IFS=$OLD_IFS
+}
+
+get_xfs_size() {
+	IFS=$NL
+	if [ -z $MOUNTED ]; then
+
+		for i in $(LANG=C xfs_db -c 'sb' -c 'print blocksize fdblocks dblocks logblocks agcount' $VOLUME); do
+			case "$i" in
+				"blocksize ="*) bsize=${i##* } ;;
+				"fdblocks ="*) fbcount=${i##* } ;;
+				"dblocks ="*) bcount=${i##* } ;;
+				"logblocks ="*) lbcount=${i##* } ;;
+				"agcount ="*) agcount=${i##* } ;;
+			esac
+		done
+		bsize=$(($bsize/1024))
+		bcount=$(($bcount-$lbcount))
+		fbcount=$(($fbcount-(4+(4*$agcount))))
+
+		total=$(($bcount*$bsize))
+		TOTAL=$(humanize_size $total)
+		used=$((($bcount-$fbcount)*$bsize))
+		USED=$(humanize_size $used)
+		free=$(($fbcount*$bsize))
+		FREE=$(humanize_size $free)
+		return
+	fi
+
+	line=$($DF -k $VOLUME | grep -e "$MOUNTED$" | sed -e 's/  */ /g')
+	line=${line#* }
+	total=$(echo $line | cut -d' ' -f1)
+	TOTAL=$(humanize_size $total)
+	free=$(echo $line | cut -d' ' -f3)
+	FREE=$(humanize_size $free)
+	used=$(echo $line | cut -d' ' -f2)
+	USED=$(humanize_size $used)
+	IFS=$OLD_IFS
+}
+
+detect_fs_size() {
+	case $FSTYPE in
+		ext[234]) get_ext_size ;;
+		xfs) get_xfs_size ;;
+		*) error "Filesystem $FSTYM is not supported by $TOOL"
+	esac
+}
+
+list_filesystems() {
+	IFS=$NL
+	format="%-20s%-8s%-13s%-13s%-13s%s\n"
+	header=$(printf $format "Volume" "Type" "Free" "Used" "Total" "Mount point")
+	separator=""
+	for i in $(seq ${#header}); do
+		separator+="-"
+	done
+	echo $separator
+	echo $header
+	echo $separator
+	IFS=$NL
+	c=0
+	for line in $($LVM lvs -o lv_path,lv_size --noheadings --separator ' ' --nosuffix --units k 2> /dev/null); do
+		line=$(echo $line | sed -e 's/^ *\//\//')
+		volume=$(echo $line | cut -d' ' -f1)
+		detect_fs $volume
+		detect_mounted
+		detect_fs_size
+		printf $format $volume $FSTYPE $FREE $USED $TOTAL $MOUNTED
+		c=$((c+1))
+	done
+	if [ $c -eq 0 ]; then
+		echo " No file systems suitable for managing by $TOOL found."
+	fi
+	echo $separator
+	IFS=$OLD_IFS
+}
+
+list_devices() {
+	IFS=$NL
+
+	format="%-13s%-13s%-13s%-13s%s\n"
+	header=$(printf $format "Device" "Free" "Used" "Total" "Group")
+	separator=""
+	for i in $(seq ${#header}); do
+		separator+="-"
+	done
+	echo $separator
+	echo $header
+	echo $separator
+	c=0
+	for line in $(LANG=C $LVM pvs -o pv_name,vg_name,pv_size,pv_free,pv_used --separator ' ' --noheadings --nosuffix --units k); do
+		line=$(echo $line | sed -e 's/^ *\//\//')
+		device=$(echo $line | cut -d' ' -f1)
+		group=$(echo $line | cut -d' ' -f2)
+		total=$(echo $line | cut -d' ' -f3)
+		total=$(humanize_size ${total%%.*})
+		free=$(echo $line | cut -d' ' -f4)
+		free=$(humanize_size ${free%%.*})
+		used=$(echo $line | cut -d' ' -f5)
+		used=$(humanize_size ${used%%.*})
+
+		printf $format $device $free $used $total $group
+		c=$((c+1))
+	done
+	if [ $c -eq 0 ]; then
+		echo " No devices found in the pool."
+	fi
+	echo $separator
+	IFS=$OLD_IFS
+}
+
+list_pool() {
+	IFS=$NL
+
+	format="%-10s%-9s%-13s%-13s%s\n"
+	header=$(printf $format "Group" "Devices" "Free" "Used" "Total")
+	separator=""
+	for i in $(seq ${#header}); do
+		separator+="-"
+	done
+	echo $separator
+	echo $header
+	echo $separator
+	c=0
+	for line in $(LANG=C $LVM vgs -o vg_name,pv_count,vg_size,vg_free --separator ' ' --noheadings --nosuffix --units k); do
+		line=$(echo $line | sed -e 's/^ *//')
+		group=$(echo $line | cut -d' ' -f1)
+		devices=$(echo $line | cut -d' ' -f2)
+		total=$(echo $line | cut -d' ' -f3)
+		free=$(echo $line | cut -d' ' -f4)
+
+		used=$((${total%%.*}-${free%%.*}))
+		used=$(humanize_size ${used%%.*})
+		total=$(humanize_size ${total%%.*})
+		free=$(humanize_size ${free%%.*})
+		printf $format $group $devices $total $free $used
+		c=$((c+1))
+	done
+	if [ $c -eq 0 ]; then
+		echo " Pool is empty."
+	fi
+	echo $separator
+	IFS=$OLD_IFS
+}
+
+#############################
+# List the available storage
+# and file systems
+#############################
+list() {
+	for i in $@; do
+		case $i in
+			"filesystems" | "fs") list_filesystems ;;
+			"devices" | "dev") list_devices ;;
+			"pool") list_pool ;;
+			*) error "Wrong option $i. (see: $TOOL --help)"
+		esac
+	done
+
+	if [ $# -eq 0 ]; then
+		list_devices
+		echo ""
+		list_pool
+		echo ""
+		list_filesystems
+	fi
+}
+
+
 ####################################
 # Calclulate diff between two dates
 #  LANG=C input is expected the
@@ -579,6 +804,7 @@ diff_dates() {
 ###################
 check() {
 	detect_fs "$1"
+	verbose "\"$FSTYPE\" filesystem found on \"$VOLUME\""
 	if detect_mounted ; then
 		verbose "Skipping filesystem check for device \"$VOLUME\" as the filesystem is mounted on $MOUNTED";
 		cleanup 3
@@ -660,6 +886,7 @@ do
 	  "resize") COMMAND=$1; shift; ARGS=$@; break ;;
 	  "create") COMMAND=$1; shift; ARGS=$@; break ;;
 	  "destroy") COMMAND=$1; shift; ARGS=$@; break ;;
+	  "list") COMMAND=$1; shift; ARGS=$@; break ;;
 	  *) error "Wrong argument \"$1\". (see: $TOOL --help)"
 	esac
 	shift
-- 
1.7.4.4

  parent reply	other threads:[~2011-09-21 16:45 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-21 16:45 [linux-lvm] [RFC][PATCH 00/35] fsadm update Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 01/35] fsadm: Add "create" command Lukas Czerner
2011-09-21 19:00   ` Stephane Chazelas
2011-09-22  9:28     ` Lukas Czerner
2011-09-22 10:01       ` Zdenek Kabelac
2011-09-22 10:27         ` Lukas Czerner
2011-09-22 10:56       ` Stephane Chazelas
2011-09-21 16:45 ` [linux-lvm] [PATCH 02/35] fsadm: Add "destroy" command Lukas Czerner
2011-09-21 16:45 ` Lukas Czerner [this message]
2011-09-21 16:45 ` [linux-lvm] [PATCH 04/35] fsadm: Make "create" command more vg aware Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 05/35] fsadm: Teach "destroy" command to take more arguments Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 06/35] fsadm: Simple cleanup and comment update Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 07/35] fsadm: Create "add" command Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 08/35] fsadm: Update "list" command for better alignment Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 09/35] fsadm: Specify number of stripes when no device is given Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 10/35] fsadm: Print type of the volume in filesystem listing Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 11/35] fsadm: Add "remove" command Lukas Czerner
2011-09-22 10:06   ` Zdenek Kabelac
2011-09-22 10:36     ` Lukas Czerner
2011-09-22 10:43       ` Zdenek Kabelac
2011-09-22 10:52         ` Lukas Czerner
2011-09-22 11:41           ` Zdenek Kabelac
2011-09-21 16:45 ` [linux-lvm] [PATCH 12/35] fsadm: Try to avoid calling LVM in the loops Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 13/35] fsadm: Merge "destroy" and "remove" into one command Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 14/35] fsadm: Allow to remove all volume groups Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 15/35] fsadm: Make all internal math in kilobytes Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 16/35] fsadm: Use warn for warnings in list command Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 17/35] fsadm: Handle resize if there is no file system Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 18/35] fsadm: Fsck extN before resize only if it is not mounted Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 19/35] fsadm: Align numbers to the decimal point Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 20/35] fsadm: Add simple configuration file Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 21/35] fsadm: Use DEFAULT_POOL when creating volume group Lukas Czerner
2011-09-22 10:09   ` Zdenek Kabelac
2011-09-21 16:45 ` [linux-lvm] [PATCH 22/35] fsadm: Add LVOL_PREFIX configuration option Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 23/35] fsadm: Only use readlink if link is provided Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 24/35] fsadm: Remove unnecessary modification of PATH variable Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 25/35] fsadm: Allow to specify size without "size=" prefix in "resize" Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 26/35] fsadm: Allow to specify lv in vg/lv format Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 27/35] fsadm: error out when no size is provided in resize Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 28/35] fsadm: Umount ext2 file system prior resize Lukas Czerner
2011-09-22 10:28   ` Zdenek Kabelac
2011-09-21 16:45 ` [linux-lvm] [PATCH 29/35] lvresize: Specify --resize-fs-only when going to use fsadm resize Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 30/35] test: add helper to compute aligned lv size Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 31/35] fsadm: Add help for new commands and update man page Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 32/35] fsadm: Update authorship of the fsadm Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 33/35] test: Add test for fsadm add command Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 34/35] test: Add test for fsadm create command Lukas Czerner
2011-09-21 16:45 ` [linux-lvm] [PATCH 35/35] test: Add test for fsadm resize command Lukas Czerner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1316623554-28975-4-git-send-email-lczerner@redhat.com \
    --to=lczerner@redhat.com \
    --cc=dchinner@redhat.com \
    --cc=linux-lvm@redhat.com \
    --cc=rwheeler@redhat.com \
    --cc=zkabelac@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.