From ad6f95d2b6b9c0283aaded610c5e53794782625b Mon Sep 17 00:00:00 2001 From: Guido Trotter Date: Thu, 19 Feb 2009 13:42:05 +0000 Subject: [PATCH] More tweaks to kpartx output parsing Apparently kpartx likes to play with us, and different versions of kpartx will give the mapped partition different names. Sometimes dashes in the original name will be doubled, and sometimes a leading directory in /dev (eg. a volume group name) will be included in the name itself. In order to avoid that we just look in the output for anything which includes the string '-1' (which is the first partition, with the separator '-' which we forced kpartx to use with the -p- option). Moreover, to be sure we don't pass on wrong information, we check that our grep had some output, and that the created device actually exists after running kpartx -a, with the output redirected to /dev/null as otherwise sometimes it will echo a few lines which will be inserted in the function result, and thus break the actual result filesystem_dev variable. Reviewed-by: iustinp --- common.sh.in | 16 ++++++++++++---- 1 files changed, 12 insertions(+), 4 deletions(-) diff --git a/common.sh.in b/common.sh.in index d0a7593..167bb00 100644 --- a/common.sh.in +++ b/common.sh.in @@ -97,12 +97,20 @@ EOF map_disk0() { blockdev="$1" - block_base=`basename $blockdev` filesystem_dev_base=`kpartx -l -p- $blockdev | \ - grep -m 1 "^$block_base.*$blockdev" | \ + grep -m 1 -- "-1.*$blockdev" | \ awk '{print $1}'` - kpartx -a -p- $blockdev - echo "/dev/mapper/$filesystem_dev_base" + if [ -z "$filesystem_dev_base" ]; then + log_error "Cannot interpret kpartx output and get partition mapping" + exit 1 + fi + kpartx -a -p- $blockdev > /dev/null + filesystem_dev="/dev/mapper/$filesystem_dev_base" + if [ ! -b "$filesystem_dev" ]; then + log_error "Can't find kpartx mapped partition: $filesystem_dev" + exit 1 + fi + echo "$filesystem_dev" } unmap_disk0() { -- 1.7.2.5