From patchwork Mon May 15 15:37:11 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Wilck X-Patchwork-Id: 9727359 X-Patchwork-Delegate: christophe.varoqui@free.fr Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 72B8A6028A for ; Mon, 15 May 2017 15:39:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6C5D32848E for ; Mon, 15 May 2017 15:39:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 610702898D; Mon, 15 May 2017 15:39:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 9DFCC2848E for ; Mon, 15 May 2017 15:39:10 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4C099804ED; Mon, 15 May 2017 15:39:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 4C099804ED Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=suse.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dm-devel-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 4C099804ED Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0784D5DD61; Mon, 15 May 2017 15:39:09 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id A9FE04A48D; Mon, 15 May 2017 15:39:08 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v4FFcRHd007968 for ; Mon, 15 May 2017 11:38:27 -0400 Received: by smtp.corp.redhat.com (Postfix) id 1680D7ED9C; Mon, 15 May 2017 15:38:27 +0000 (UTC) Delivered-To: dm-devel@redhat.com Received: from mx1.redhat.com (ext-mx07.extmail.prod.ext.phx2.redhat.com [10.5.110.31]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 189437EBF3; Mon, 15 May 2017 15:38:25 +0000 (UTC) Received: from prv3-mh.provo.novell.com (prv3-mh.provo.novell.com [137.65.250.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6B0B0C04B924; Mon, 15 May 2017 15:38:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 6B0B0C04B924 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=suse.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=mwilck@suse.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 6B0B0C04B924 Received: from apollon.suse.de.de (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by prv3-mh.provo.novell.com with ESMTP (TLS encrypted); Mon, 15 May 2017 09:38:01 -0600 From: Martin Wilck To: Christophe Varoqui , Hannes Reinecke , Benjamin Marzinski Date: Mon, 15 May 2017 17:37:11 +0200 Message-Id: <20170515153722.11508-2-mwilck@suse.com> In-Reply-To: <20170515153722.11508-1-mwilck@suse.com> References: <20170515153722.11508-1-mwilck@suse.com> X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 203 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Mon, 15 May 2017 15:38:17 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Mon, 15 May 2017 15:38:17 +0000 (UTC) for IP:'137.65.250.26' DOMAIN:'prv3-mh.provo.novell.com' HELO:'prv3-mh.provo.novell.com' FROM:'mwilck@suse.com' RCPT:'' X-RedHat-Spam-Score: -0.301 (BAYES_80, RCVD_IN_DNSWL_MED, SPF_PASS) 137.65.250.26 prv3-mh.provo.novell.com 137.65.250.26 prv3-mh.provo.novell.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.31 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: dm-devel@redhat.com Cc: dm-devel@redhat.com Subject: [dm-devel] [PATCH v2 01/12] kpartx: test-kpartx: new unit test program X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Mon, 15 May 2017 15:39:10 +0000 (UTC) X-Virus-Scanned: ClamAV using ClamSMTP This is a unit test program for kpartx, in particular for deleting partitions. NOTE: This test program fails with current kpartx; full patch series needed to make it work. Signed-off-by: Martin Wilck --- kpartx/test-kpartx | 254 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 254 insertions(+) create mode 100755 kpartx/test-kpartx diff --git a/kpartx/test-kpartx b/kpartx/test-kpartx new file mode 100755 index 00000000..7c45cd14 --- /dev/null +++ b/kpartx/test-kpartx @@ -0,0 +1,254 @@ +#! /bin/bash + +# This is a unit test program for kpartx, in particular for deleting partitions. +# +# The rationale is the following: +# +# 1) kpartx should delete all mappings it created beforehand. +# 2) kpartx should handle partitions on dm devices and other devices +# (e.g. loop devices) equally well. +# 3) kpartx should only delete "partitions", which are single-target +# linear mappings into a block device. Other maps should not be touched. +# 4) kpartx should only delete mappings it created itself beforehand. +# In particular, it shouldn't delete LVM LVs, even if they are fully +# contained in the block device at hand and thus look like partitions +# in the first place. (For historical compatibility reasons, we allow +# such mappings to be deleted with the -f/--force flag). +# 5) DM map names may be changed, thus kpartx shouldn't rely on them to +# check whether a mapping is a partition of a particular device. It is +# legal for a partition of /dev/loop0 to be named "loop0". + +# Note: This program tries hard to clean up, but if tests fail, +# stale DM or loop devices may keep lurking around. + +# Set WORKDIR in environment to existing dir to for persistence +# WARNING: exisiting files will be truncated. +# If empty, test will be done in temporary dir +: ${WORKDIR:=} +# Set this environment variable to test an alternative kpartx executable +: ${KPARTX:=} +# Options to pass to kpartx always +: ${KPARTX_OPTS:=-s} +# Time to wait for device nodes to appear (microseconds) +# Waiting is only needed if "s" is not in $KPARTX_OPTS +: ${WAIT_US:=0} + +# IMPORTANT: The ERR trap is essential for this program to work correctly! +trap 'LINE=$LINENO; trap - ERR; echo "== error in $BASH_COMMAND on line $LINE ==" >&2; exit 1' ERR +trap 'cleanup' 0 + +CLEANUP=: +cleanup() { + trap - ERR + trap - 0 + if [[ $OK ]]; then + echo == all tests completed successfully == >&2 + else + echo == step $STEP failed == >&2 + fi + eval "$CLEANUP" &>/dev/null +} + +push_cleanup() { + CLEANUP="$@;$CLEANUP" +} + +pop_cleanup() { + # CAUTION: simplistic + CLEANUP=${CLEANUP#*;} +} + +step() { + STEP="$@" + echo == Test step: $STEP == >&2 +} + +mk_partitions() { + parted -s $1 mklabel msdos + parted -s -- $1 mkpart prim ext2 1MiB -1s +} + +step preparation + +[[ $UID -eq 0 ]] +[[ $KPARTX ]] || { + if [[ -x $PWD/kpartx/kpartx ]]; then + KPARTX=$PWD/kpartx/kpartx + else + KPARTX=$(which kpartx) + fi +} +[[ $KPARTX ]] + +FILE1=kpartx1 +FILE2=kpartx2 +FILE3=kpartx3 +SIZE=$((1024*1024*1024)) # use bytes as units here +SECTSIZ=512 +OFFS=32 # offset of linear mapping into dev, sectors +VG=kpvg # volume group name +LV=kplv # logical vol name +LVMCONF='devices { filter = [ "a|/dev/loop.*|", r".*" ] }' + +OK= + +[[ $WORKDIR ]] || { + WORKDIR=$(mktemp -d /tmp/kpartx-XXXXXX) + push_cleanup 'rm -rf $WORKDIR' +} + +push_cleanup "cd $PWD" +cd "$WORKDIR" + +step "create loop devices" +truncate -s $SIZE $FILE1 +truncate -s $SIZE $FILE2 +truncate -s $SIZE $FILE3 + +LO1=$(losetup -f $FILE1 --show) +push_cleanup 'losetup -d $LO1' +LO2=$(losetup -f $FILE2 --show) +push_cleanup 'losetup -d $LO2' +LO3=$(losetup -f $FILE3 --show) +push_cleanup 'losetup -d $LO3' + +[[ $LO1 && $LO2 && $LO3 && -b $LO1 && -b $LO2 && -b $LO3 ]] +DEV1=$(stat -c "%t:%T" $LO1) +DEV2=$(stat -c "%t:%T" $LO2) +DEV3=$(stat -c "%t:%T" $LO3) + +usleep $WAIT_US + +step "create DM devices (spans)" +# Create two linear mappings spanning two loopdevs. +# One of them gets a pathological name colliding with +# the loop device name. +# These mappings must not be removed by kpartx. +# They also serve as DM devices to test partition removal on those. + +TABLE="\ +0 $((SIZE/SECTSIZ-OFFS)) linear $DEV1 $OFFS +$((SIZE/SECTSIZ-OFFS)) $((SIZE/SECTSIZ-OFFS)) linear $DEV2 $OFFS" + +SPAN1=kpt +SPAN2=$(basename $LO2) +dmsetup create $SPAN1 <<<"$TABLE" +push_cleanup 'dmsetup remove -f $SPAN1' + +dmsetup create $SPAN2 <<<"$TABLE" +push_cleanup 'dmsetup remove -f $SPAN2' + +usleep $WAIT_US +[[ -b /dev/mapper/$SPAN1 ]] +[[ -b /dev/mapper/$SPAN2 ]] + +step "create vg on $LO3" +# On the 3rd loop device, we create a VG and an LV +# The LV should not be removed by kpartx. +pvcreate --config "$LVMCONF" -f $LO3 +vgcreate --config "$LVMCONF" $VG $LO3 +push_cleanup 'vgremove --config "$LVMCONF" -f $VG' +lvcreate --config "$LVMCONF" -L $((SIZE/2))B -n $LV $VG +push_cleanup 'lvremove --config "$LVMCONF" -f $VG/$LV' +usleep $WAIT_US + +[[ -b /dev/mapper/$VG-$LV ]] + +# dmsetup table /dev/mapper/$VG-$LV +# dmsetup info /dev/mapper/$VG-$LV + +step "create partitions on loop devices" + +mk_partitions $LO1 +mk_partitions $LO2 + +# Test invocation of kpartx with regular file here +LO2P1=/dev/mapper/$(basename $LO2)-foo1 +$KPARTX $KPARTX_OPTS -a -p -foo $FILE2 +push_cleanup 'dmsetup remove -f $(basename $LO2P1)' + +LO1P1=/dev/mapper/$(basename $LO1)-eggs1 +$KPARTX $KPARTX_OPTS -a -p -eggs $LO1 +push_cleanup 'dmsetup remove -f $(basename $LO1P1)' + +usleep $WAIT_US +[[ -b $LO1P1 ]] +[[ -b $LO2P1 ]] + +# dmsetup info $LO2P1 + +# Set pathological name for partition on $LO1 (same as loop device itself) +dmsetup rename $(basename $LO1P1) $(basename $LO1) +LO1P1=/dev/mapper/$(basename $LO1) +pop_cleanup +push_cleanup 'dmsetup remove -f $(basename $LO1P1)' + +# dmsetup info $LO1P1 + +step "create partitions on DM devices" +mk_partitions /dev/mapper/$SPAN2 + +$KPARTX $KPARTX_OPTS -a -p -bar /dev/mapper/$SPAN2 +SPAN2P1=/dev/mapper/${SPAN2}-bar1 + +# udev rules may have created partition mappings without UUIDs +# which aren't removed by default (if system standard kpartx doesn't +# set the UUID). Remove them using -f +push_cleanup '$KPARTX $KPARTX_OPTS -f -d /dev/mapper/$SPAN2' +push_cleanup 'dmsetup remove -f $(basename $SPAN2P1)' + +$KPARTX $KPARTX_OPTS -a -p -spam /dev/mapper/$SPAN1 +SPAN1P1=/dev/mapper/${SPAN1}-spam1 +# see above +push_cleanup '$KPARTX $KPARTX_OPTS -f -d /dev/mapper/$SPAN1' +push_cleanup 'dmsetup remove -f $(basename $SPAN1P1)' + +usleep $WAIT_US +[[ -b $SPAN2P1 ]] +[[ -b $SPAN1P1 ]] + +step "delete partitions on DM devices" +$KPARTX $KPARTX_OPTS -d /dev/mapper/$SPAN1 >&2 +usleep $WAIT_US + +[[ -b $SPAN2P1 ]] +[[ -b $LO1P1 ]] +[[ -b $LO2P1 ]] +[[ ! -b $SPAN1P1 ]] + +$KPARTX $KPARTX_OPTS -d /dev/mapper/$SPAN2 +usleep $WAIT_US + +[[ -b $LO1P1 ]] +[[ -b $LO2P1 ]] +[[ ! -b $SPAN2P1 ]] + +step "delete partitions on loop devices" + +$KPARTX $KPARTX_OPTS -d $LO3 + +# This will also delete the loop device +$KPARTX $KPARTX_OPTS -d $FILE2 +$KPARTX $KPARTX_OPTS -d $LO1 +usleep $WAIT_US + +# ls -l /dev/mapper +[[ ! -b $LO1P1 ]] +pop_cleanup +[[ ! -b $LO2P1 ]] +pop_cleanup +# spans should not have been removed +[[ -b /dev/mapper/$SPAN1 ]] +[[ -b /dev/mapper/$SPAN2 ]] +# LVs neither +[[ -b /dev/mapper/$VG-$LV ]] + +step "delete partitions on $LO3 with -f" + +$KPARTX $KPARTX_OPTS -f -d $LO3 +# -d -f should delete the LV, too +[[ ! -b /dev/mapper/$VG-$LV ]] +[[ -b /dev/mapper/$SPAN1 ]] +[[ -b /dev/mapper/$SPAN2 ]] + +OK=yes