Skip to contentSkip to conversation

common: new function to get real device path name and basename

PATCH Superseded Eryu Guan 3 participants 4 messages Apr 22, 2014 – Apr 25, 2014

lore
Share & tools
lore

Eryu Guan

author
#0 lore raw

PATCH 1/1 common: new function to get real device path name and basename

If TEST_DEV or SCRATCH_DEV is symlink(mostly a lvm lv), a simple basename is not enough, symlink should be followed.

This task is common enough, so introduce new helper functions and replace all readlink calls in

ext4/305
generic/009
generic/019
generic/285
generic/312

---
 common/rc         | 16 ++++++++++++++++
 tests/ext4/305    |  2 +-
 tests/generic/009 |  2 +-
 tests/generic/019 |  4 +---
 tests/generic/285 |  2 +-
 tests/generic/312 |  2 +-
 6 files changed, 21 insertions(+), 7 deletions(-)
common/rc +16 -0
index acf419b..68b040d 100644
@@ -2302,6 +2302,22 @@ init_rc()
 	export XFS_IO_PROG="$XFS_IO_PROG -F"
 }
 
+# get real device path name by following link
+_real_dev()
+{
+	local _dev=$1
+	if [ -b "$_dev" ] && [ -L "$_dev" ]; then
+		_dev=`readlink -f "$_dev"`
+	fi
+	echo $_dev
+}
+
+# basename of a device
+_basename_dev()
+{
+	echo `basename $(_real_dev $1)`
+}
+
 init_rc
 
 ################################################################################
tests/ext4/305 +1 -1
index eee461a..645f5c8 100755
@@ -49,7 +49,7 @@ _require_scratch
 rm -f $seqres.full
 echo "Silence is golden"
 
-DEV_BASENAME=$(basename $(readlink -f $SCRATCH_DEV))
+DEV_BASENAME=$(_basename_dev $SCRATCH_DEV)
 echo "Start test on device $SCRATCH_DEV, basename $DEV_BASENAME" >$seqres.full
 _scratch_mkfs >>$seqres.full 2>&1
 
tests/generic/009 +1 -1
index b7b0b3f..0886594 100644
@@ -50,7 +50,7 @@ testfile=$TEST_DIR/009.$$
 
 # Disable extent zeroing for ext4 as that change where holes are created
 if [ "$FSTYP" = "ext4" ]; then
-	DEV=`basename $TEST_DEV`
+	DEV=`_basename_dev $TEST_DEV`
 	echo 0 >/sys/fs/ext4/$DEV/extent_max_zeroout_kb
 fi
 
tests/generic/019 +1 -3
index 1208c49..8c37a31 100755
@@ -41,9 +41,7 @@ _need_to_be_root
 _require_scratch
 _require_fail_make_request
 
-# TODO: Function are common enough to be moved to common/blkdev
-SCRATCH_REAL_DEV=`readlink -f $SCRATCH_DEV`
-SCRATCH_BDEV=`basename $SCRATCH_REAL_DEV`
+SCRATCH_BDEV=`_basename_dev $SCRATCH_DEV`
 
 allow_fail_make_request()
 {
tests/generic/285 +1 -1
index 8078b1c..8826023 100755
@@ -48,7 +48,7 @@ BASE_TEST_FILE=$TEST_DIR/seek_sanity_testfile
 
 # Disable extent zeroing for ext4 as that change where holes are created
 if [ "$FSTYP" = "ext4" ]; then
-	DEV=`basename $TEST_DEV`
+	DEV=`_basename_dev $TEST_DEV`
 	echo 0 >/sys/fs/ext4/$DEV/extent_max_zeroout_kb
 fi
 
tests/generic/312 +1 -1
index eaec43c..a81b0df 100755
@@ -52,7 +52,7 @@ _require_scratch
 # 5G in byte
 fssize=$((2**30 * 5))
 required_blocks=$(($fssize / 1024))
-dev_blocks=$(grep $(basename $(readlink -f $SCRATCH_DEV)) /proc/partitions | $AWK_PROG '{print $3}')
+dev_blocks=$(grep $(_basename_dev $SCRATCH_DEV) /proc/partitions | $AWK_PROG '{print $3}')
 if [ $required_blocks -gt $dev_blocks ];then
 	_notrun "this test requires \$SCRATCH_DEV has ${fssize}B space"
 fi
Signature · 6 lines
1.9.0

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#1 lore raw
Quoted 35 lines · Eryu Guan

If TEST_DEV or SCRATCH_DEV is symlink(mostly a lvm lv), a simple basename is not enough, symlink should be followed.

This task is common enough, so introduce new helper functions and replace all readlink calls in

ext4/305
generic/009
generic/019
generic/285
generic/312

---
 common/rc         | 16 ++++++++++++++++
 tests/ext4/305    |  2 +-
 tests/generic/009 |  2 +-
 tests/generic/019 |  4 +---
 tests/generic/285 |  2 +-
 tests/generic/312 |  2 +-
 6 files changed, 21 insertions(+), 7 deletions(-)
common/rc +5 -0
index acf419b..68b040d 100644
@@ -2302,6 +2302,22 @@ init_rc()
 	export XFS_IO_PROG="$XFS_IO_PROG -F"
 }
 
+# get real device path name by following link
+_real_dev()
+{
+	local _dev=$1
+	if [ -b "$_dev" ] && [ -L "$_dev" ]; then

common/rc init_rc() · line 2319

Quoted 10 lines
common/rc +10 -0
+		_dev=`readlink -f "$_dev"`
+	fi
+	echo $_dev
+}
+
+# basename of a device
+_basename_dev()
+{
+	echo `basename $(_real_dev $1)`
+}

I don't really like that function name. It describes the implementation, not what the function actually returns. i.e. we're getting the -short- device names here, basename is the implemenation used to turn a full device name to a short device name...

Cheers,

Dave

Signature · 7 lines
Dave Chinner
[email protected]

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
author
#2 lore raw

PATCH v2 1/1 common: new function to get real device path name and basename

If TEST_DEV or SCRATCH_DEV is symlink(mostly a lvm lv), a simple basename is not enough, symlink should be followed.

This task is common enough, so introduce new helper functions and replace all readlink calls in

ext4/305
generic/009
generic/019
generic/285
generic/312

---
v2: rename _basename_dev to _short_dev as Dave suggested

 common/rc         | 16 ++++++++++++++++
 tests/ext4/305    |  2 +-
 tests/generic/009 |  2 +-
 tests/generic/019 |  4 +---
 tests/generic/285 |  2 +-
 tests/generic/312 |  2 +-
 6 files changed, 21 insertions(+), 7 deletions(-)
common/rc +16 -0
index acf419b..f16328d 100644
@@ -2302,6 +2302,22 @@ init_rc()
 	export XFS_IO_PROG="$XFS_IO_PROG -F"
 }
 
+# get real device path name by following link
+_real_dev()
+{
+	local _dev=$1
+	if [ -b "$_dev" ] && [ -L "$_dev" ]; then
+		_dev=`readlink -f "$_dev"`
+	fi
+	echo $_dev
+}
+
+# basename of a device
+_short_dev()
+{
+	echo `basename $(_real_dev $1)`
+}
+
 init_rc
 
 ################################################################################
tests/ext4/305 +1 -1
index eee461a..860d0a6 100755
@@ -49,7 +49,7 @@ _require_scratch
 rm -f $seqres.full
 echo "Silence is golden"
 
-DEV_BASENAME=$(basename $(readlink -f $SCRATCH_DEV))
+DEV_BASENAME=$(_short_dev $SCRATCH_DEV)
 echo "Start test on device $SCRATCH_DEV, basename $DEV_BASENAME" >$seqres.full
 _scratch_mkfs >>$seqres.full 2>&1
 
tests/generic/009 +1 -1
index b7b0b3f..aa662b2 100644
@@ -50,7 +50,7 @@ testfile=$TEST_DIR/009.$$
 
 # Disable extent zeroing for ext4 as that change where holes are created
 if [ "$FSTYP" = "ext4" ]; then
-	DEV=`basename $TEST_DEV`
+	DEV=`_short_dev $TEST_DEV`
 	echo 0 >/sys/fs/ext4/$DEV/extent_max_zeroout_kb
 fi
 
tests/generic/019 +1 -3
index 1208c49..7a019ad 100755
@@ -41,9 +41,7 @@ _need_to_be_root
 _require_scratch
 _require_fail_make_request
 
-# TODO: Function are common enough to be moved to common/blkdev
-SCRATCH_REAL_DEV=`readlink -f $SCRATCH_DEV`
-SCRATCH_BDEV=`basename $SCRATCH_REAL_DEV`
+SCRATCH_BDEV=`_short_dev $SCRATCH_DEV`
 
 allow_fail_make_request()
 {
tests/generic/285 +1 -1
index 8078b1c..ac34d34 100755
@@ -48,7 +48,7 @@ BASE_TEST_FILE=$TEST_DIR/seek_sanity_testfile
 
 # Disable extent zeroing for ext4 as that change where holes are created
 if [ "$FSTYP" = "ext4" ]; then
-	DEV=`basename $TEST_DEV`
+	DEV=`_short_dev $TEST_DEV`
 	echo 0 >/sys/fs/ext4/$DEV/extent_max_zeroout_kb
 fi
 
tests/generic/312 +1 -1
index eaec43c..eb6cec9 100755
@@ -52,7 +52,7 @@ _require_scratch
 # 5G in byte
 fssize=$((2**30 * 5))
 required_blocks=$(($fssize / 1024))
-dev_blocks=$(grep $(basename $(readlink -f $SCRATCH_DEV)) /proc/partitions | $AWK_PROG '{print $3}')
+dev_blocks=$(grep $(_short_dev $SCRATCH_DEV) /proc/partitions | $AWK_PROG '{print $3}')
 if [ $required_blocks -gt $dev_blocks ];then
 	_notrun "this test requires \$SCRATCH_DEV has ${fssize}B space"
 fi
Signature · 6 lines
1.9.0

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs