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
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() {