From d1dd9c3794f6f642a78ef33a92975129bb361d5a Mon Sep 17 00:00:00 2001 From: CyberLeo Date: Fri, 31 Jul 2020 00:04:37 -0500 Subject: experimental/kpartx: new port --- experimental/kpartx/APKBUILD | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 experimental/kpartx/APKBUILD diff --git a/experimental/kpartx/APKBUILD b/experimental/kpartx/APKBUILD new file mode 100644 index 000000000..00a4c909c --- /dev/null +++ b/experimental/kpartx/APKBUILD @@ -0,0 +1,32 @@ +# Contributor: CyberLeo +# Maintainer: CyberLeo +pkgname=kpartx +pkgver=0.8.4 +pkgrel=0 +pkgdesc="Manipulates device mappings for the partitions of any block device; part of linux multipath-tools." +url="http://christophe.varoqui.free.fr/" +arch="all" +options="!check" # Requires root and loopback kernel module. +license="LGPL-2.0" +depends="" +makedepends="lvm2-dev" +subpackages="$pkgname-doc" +source="https://git.opensvc.com/?p=multipath-tools/.git;a=snapshot;h=refs/tags/$pkgver;sf=tgz;/multipath-tools-$pkgver.tar.gz" +builddir="$srcdir/multipath-tools-$pkgver" + +build() { + cd kpartx + make kpartx +} + +check() { + cd kpartx + ./test-kpartx +} + +package() { + cd kpartx + make DESTDIR="$pkgdir" install +} + +sha512sums="130308e61d6dce31085fc2763219f4df0f3ad9153e0e6e7a5a1c3c948a2305cff9413699025c28f9b81dd24d2a9263f9fa825253060e44232c3bb6600cd1f07f multipath-tools-0.8.4.tar.gz" -- cgit v1.2.3-70-g09d2 From 51a4428e485c9d45d8e5865c77d2d37c2f5d122e Mon Sep 17 00:00:00 2001 From: CyberLeo Date: Fri, 31 Jul 2020 02:45:19 -0500 Subject: experimental/kpartx: fix license, fix description, and attempt to fix checks --- experimental/kpartx/APKBUILD | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/experimental/kpartx/APKBUILD b/experimental/kpartx/APKBUILD index 00a4c909c..881c34ae7 100644 --- a/experimental/kpartx/APKBUILD +++ b/experimental/kpartx/APKBUILD @@ -3,29 +3,28 @@ pkgname=kpartx pkgver=0.8.4 pkgrel=0 -pkgdesc="Manipulates device mappings for the partitions of any block device; part of linux multipath-tools." +pkgdesc="Manipulates device mappings for the partitions of any block device." url="http://christophe.varoqui.free.fr/" arch="all" -options="!check" # Requires root and loopback kernel module. -license="LGPL-2.0" +options="checkroot" # Requires root and loopback kernel module. +license="LGPL-2.0-only" depends="" makedepends="lvm2-dev" +checkdepends="debianutils-which" subpackages="$pkgname-doc" source="https://git.opensvc.com/?p=multipath-tools/.git;a=snapshot;h=refs/tags/$pkgver;sf=tgz;/multipath-tools-$pkgver.tar.gz" -builddir="$srcdir/multipath-tools-$pkgver" +builddir="$srcdir/multipath-tools-$pkgver/kpartx" build() { - cd kpartx make kpartx } check() { - cd kpartx - ./test-kpartx + echo 'exec sleep "$(echo ${1} / 1000000.0 | bc)"' > usleep && chmod 755 usleep + PATH=$PATH:/sbin:/usr/sbin:$builddir ./test-kpartx } package() { - cd kpartx make DESTDIR="$pkgdir" install } -- cgit v1.2.3-70-g09d2 From 94d91a4910a24d65daa00a89af09bd4ab23133c6 Mon Sep 17 00:00:00 2001 From: CyberLeo Date: Sun, 2 Aug 2020 00:49:56 -0500 Subject: experimental/kpartx: facilitate testing --- experimental/kpartx/APKBUILD | 13 +- experimental/kpartx/test-kpartx | 395 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 402 insertions(+), 6 deletions(-) create mode 100755 experimental/kpartx/test-kpartx diff --git a/experimental/kpartx/APKBUILD b/experimental/kpartx/APKBUILD index 881c34ae7..e1e0feddb 100644 --- a/experimental/kpartx/APKBUILD +++ b/experimental/kpartx/APKBUILD @@ -6,13 +6,14 @@ pkgrel=0 pkgdesc="Manipulates device mappings for the partitions of any block device." url="http://christophe.varoqui.free.fr/" arch="all" -options="checkroot" # Requires root and loopback kernel module. +options="!check" # Requires root and loopback/dm-mod kernel modules. license="LGPL-2.0-only" depends="" makedepends="lvm2-dev" -checkdepends="debianutils-which" +checkdepends="debianutils-which parted" subpackages="$pkgname-doc" -source="https://git.opensvc.com/?p=multipath-tools/.git;a=snapshot;h=refs/tags/$pkgver;sf=tgz;/multipath-tools-$pkgver.tar.gz" +source="multipath-tools-$pkgver.tar.gz::https://git.opensvc.com/?p=multipath-tools/.git;a=snapshot;h=refs/tags/$pkgver;sf=tgz;/multipath-tools-$pkgver.tar.gz +test-kpartx" builddir="$srcdir/multipath-tools-$pkgver/kpartx" build() { @@ -20,12 +21,12 @@ build() { } check() { - echo 'exec sleep "$(echo ${1} / 1000000.0 | bc)"' > usleep && chmod 755 usleep - PATH=$PATH:/sbin:/usr/sbin:$builddir ./test-kpartx + sudo KPARTX="${builddir}/kpartx" WAIT_US=1000000 "${srcdir}/test-kpartx" } package() { make DESTDIR="$pkgdir" install } -sha512sums="130308e61d6dce31085fc2763219f4df0f3ad9153e0e6e7a5a1c3c948a2305cff9413699025c28f9b81dd24d2a9263f9fa825253060e44232c3bb6600cd1f07f multipath-tools-0.8.4.tar.gz" +sha512sums="130308e61d6dce31085fc2763219f4df0f3ad9153e0e6e7a5a1c3c948a2305cff9413699025c28f9b81dd24d2a9263f9fa825253060e44232c3bb6600cd1f07f multipath-tools-0.8.4.tar.gz +2e3702243bfc52df7353cc828fb03e3dff36eddbf77eb13140fd58d857c3d45990c679c535c00c569e5a5412ad3823717e264449c038ebf1726a4e7adc08e06e test-kpartx" diff --git a/experimental/kpartx/test-kpartx b/experimental/kpartx/test-kpartx new file mode 100755 index 000000000..93a5abdbd --- /dev/null +++ b/experimental/kpartx/test-kpartx @@ -0,0 +1,395 @@ +#! /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: existing 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; ls -la /dev/mapper; read a; exit 1' ERR +trap 'cleanup' 0 + +CLEANUP=: +cleanup() { + trap - ERR + trap - 0 + if [[ $OK ]]; then + echo == all ${ALL_PASS} tests completed successfully == >&2 + else + printf "== Final Results ==\n%s of %s failed!\n\n" "${ALL_FAIL}" "$(( ALL_PASS + ALL_FAIL ))" >&2 + fi + eval "$CLEANUP" &>/dev/null +} + +push_cleanup() { + CLEANUP="$@;$CLEANUP" +} + +pop_cleanup() { + # CAUTION: simplistic + CLEANUP=${CLEANUP#*;} +} + +step() { + STEP="$@" + + if [ $PASS -gt 0 -o $FAIL -gt 0 ] + then + printf "PASS: %s FAIL: %s\n\n" "${PASS}" "${FAIL}" + PASS=0 + FAIL=0 + fi + + echo == Test step: $STEP == >&2 +} + +mk_partitions() { + parted -s $1 mklabel msdos + parted -s -- $1 mkpart prim ext2 1MiB -1s +} + +wipe_ptable() { + dd if=/dev/zero of=$1 bs=1b count=1 +} + +usleep() { + if which usleep >/dev/null 2>&1 + then + env usleep "${1}" + else + sleep $( echo "${1}" / 1000000.0 | bc ) + fi +} + +invoke_kpartx() { + $KPARTX $KPARTX_OPTS "${@}" + usleep "${WAIT_US}" +} + +assert_exists() { + local result=0 + while [ "${1}" ] + do + if [ -b "$(readlink -f "${1}")" ] + then + PASS=$(( PASS + 1 )) + ALL_PASS=$(( ALL_PASS + 1 )) + else + FAIL=$(( FAIL + 1 )) + ALL_FAIL=$(( ALL_FAIL + 1 )) + echo "assert_exists: Device does not exist when it should: ${1}" + result=1 + fi + shift + done + return $result +} + +assert_unexists() { + local result=0 + while [ "${1}" ] + do + if [ ! -b "$(readlink -f "${1}")" ] + then + PASS=$(( PASS + 1 )) + ALL_PASS=$(( ALL_PASS + 1 )) + else + FAIL=$(( FAIL + 1 )) + ALL_FAIL=$(( ALL_FAIL + 1 )) + echo "assert_exists: Device exists when it should not: ${1}" + result=1 + fi + shift + done + return $result +} + +PASS=0 +ALL_PASS=0 +FAIL=0 +ALL_FAIL=0 + +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 +FILE4=kpartx4 + +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 +truncate -s $SIZE $FILE4 + +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' +LO4=$(losetup -f $FILE4 --show) +push_cleanup 'losetup -d $LO4' + +[[ $LO1 && $LO2 && $LO3 && $LO4 && -b $LO1 && -b $LO2 && -b $LO3 && -b $LO4 ]] +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' + +# This is a non-kpartx pseudo "partition" mapping +USER1=user1 +push_cleanup 'dmsetup remove -f $USER1' +dmsetup create $USER1 <&2 +usleep $WAIT_US + +assert_exists $SPAN2P1 $LO1P1 $LO2P1 +assert_unexists $SPAN1P1 + +invoke_kpartx -d /dev/mapper/$SPAN2 +usleep $WAIT_US + +assert_exists $LO1P1 $LO2P1 +assert_unexists $SPAN2P1 + +step "rename partitions on loop device" +invoke_kpartx -u -p -spam $LO2 +assert_unexists $LO2P1 +assert_exists ${LO2P1//-foo/-spam} + +step "rename partitions on loop device back" +invoke_kpartx -u -p -foo $LO2 +assert_exists $LO2P1 +assert_unexists ${LO2P1//-foo/-spam} + +step "rename partitions on loop device to default" +invoke_kpartx -u $LO2 +#read a +assert_unexists $LO2P1 +assert_exists ${LO2P1//-foo/p} +# $LO1 ends in a digit + +step "rename partitions on loop device back from default" +invoke_kpartx -u -p -foo $LO2 +assert_exists $LO2P1 +assert_unexists ${LO2P1//-foo/p} + +step "rename partitions on loop devices" +invoke_kpartx -u -p spam $LO2 + +step "delete partitions on loop devices" + +invoke_kpartx -d $LO3 + +# This will also delete the loop device +invoke_kpartx -d $FILE2 +invoke_kpartx -d $LO1 +usleep $WAIT_US + +# ls -l /dev/mapper +assert_unexists $LO1P1 +pop_cleanup +assert_unexists $LO2P1 +pop_cleanup +# spans should not have been removed +# LVs neither +assert_exists /dev/mapper/$SPAN1 /dev/mapper/$SPAN2 /dev/mapper/$USER1 /dev/mapper/$VG-$LV + +step "delete partitions on $LO3 with -f" + +invoke_kpartx -f -d $LO3 +# -d -f should delete the LV, too +assert_unexists /dev/mapper/$VG-$LV +assert_exists /dev/mapper/$SPAN1 /dev/mapper/$SPAN2 + +step "test kpartx creation/deletion on an image file with no existing loopdev" +losetup -d $LO4 + +OUTPUT=$(invoke_kpartx -v -a $FILE4 2>&1) +read loop dm < \ + <(sed -n 's/^add map \(loop[0-9]*\)p1 ([0-9]*:\([0-9]*\)).*$/\1 dm-\2/p' \ + <<<$OUTPUT) +[[ $dm && $loop ]] +push_cleanup "dmsetup remove -f /dev/$dm" +push_cleanup "losetup -d /dev/$loop" + +assert_exists /dev/mapper/${loop}p1 +invoke_kpartx -d $FILE4 +assert_unexists /dev/mapper/${loop}p1 +# /dev/$loop is _not_ automatically deleted +assert_exists /dev/${loop} + +[ $ALL_FAIL -eq 0 ] && OK=yes -- cgit v1.2.3-70-g09d2 From 4a177049e3d486da3f54d346d63ea80699c08b5b Mon Sep 17 00:00:00 2001 From: CyberLeo Date: Sun, 2 Aug 2020 01:17:18 -0500 Subject: user/kpartx: testing complete, promote out of experimental --- experimental/kpartx/APKBUILD | 32 ---- experimental/kpartx/test-kpartx | 395 ---------------------------------------- user/kpartx/APKBUILD | 32 ++++ user/kpartx/test-kpartx | 395 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 427 insertions(+), 427 deletions(-) delete mode 100644 experimental/kpartx/APKBUILD delete mode 100755 experimental/kpartx/test-kpartx create mode 100644 user/kpartx/APKBUILD create mode 100755 user/kpartx/test-kpartx diff --git a/experimental/kpartx/APKBUILD b/experimental/kpartx/APKBUILD deleted file mode 100644 index e1e0feddb..000000000 --- a/experimental/kpartx/APKBUILD +++ /dev/null @@ -1,32 +0,0 @@ -# Contributor: CyberLeo -# Maintainer: CyberLeo -pkgname=kpartx -pkgver=0.8.4 -pkgrel=0 -pkgdesc="Manipulates device mappings for the partitions of any block device." -url="http://christophe.varoqui.free.fr/" -arch="all" -options="!check" # Requires root and loopback/dm-mod kernel modules. -license="LGPL-2.0-only" -depends="" -makedepends="lvm2-dev" -checkdepends="debianutils-which parted" -subpackages="$pkgname-doc" -source="multipath-tools-$pkgver.tar.gz::https://git.opensvc.com/?p=multipath-tools/.git;a=snapshot;h=refs/tags/$pkgver;sf=tgz;/multipath-tools-$pkgver.tar.gz -test-kpartx" -builddir="$srcdir/multipath-tools-$pkgver/kpartx" - -build() { - make kpartx -} - -check() { - sudo KPARTX="${builddir}/kpartx" WAIT_US=1000000 "${srcdir}/test-kpartx" -} - -package() { - make DESTDIR="$pkgdir" install -} - -sha512sums="130308e61d6dce31085fc2763219f4df0f3ad9153e0e6e7a5a1c3c948a2305cff9413699025c28f9b81dd24d2a9263f9fa825253060e44232c3bb6600cd1f07f multipath-tools-0.8.4.tar.gz -2e3702243bfc52df7353cc828fb03e3dff36eddbf77eb13140fd58d857c3d45990c679c535c00c569e5a5412ad3823717e264449c038ebf1726a4e7adc08e06e test-kpartx" diff --git a/experimental/kpartx/test-kpartx b/experimental/kpartx/test-kpartx deleted file mode 100755 index 93a5abdbd..000000000 --- a/experimental/kpartx/test-kpartx +++ /dev/null @@ -1,395 +0,0 @@ -#! /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: existing 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; ls -la /dev/mapper; read a; exit 1' ERR -trap 'cleanup' 0 - -CLEANUP=: -cleanup() { - trap - ERR - trap - 0 - if [[ $OK ]]; then - echo == all ${ALL_PASS} tests completed successfully == >&2 - else - printf "== Final Results ==\n%s of %s failed!\n\n" "${ALL_FAIL}" "$(( ALL_PASS + ALL_FAIL ))" >&2 - fi - eval "$CLEANUP" &>/dev/null -} - -push_cleanup() { - CLEANUP="$@;$CLEANUP" -} - -pop_cleanup() { - # CAUTION: simplistic - CLEANUP=${CLEANUP#*;} -} - -step() { - STEP="$@" - - if [ $PASS -gt 0 -o $FAIL -gt 0 ] - then - printf "PASS: %s FAIL: %s\n\n" "${PASS}" "${FAIL}" - PASS=0 - FAIL=0 - fi - - echo == Test step: $STEP == >&2 -} - -mk_partitions() { - parted -s $1 mklabel msdos - parted -s -- $1 mkpart prim ext2 1MiB -1s -} - -wipe_ptable() { - dd if=/dev/zero of=$1 bs=1b count=1 -} - -usleep() { - if which usleep >/dev/null 2>&1 - then - env usleep "${1}" - else - sleep $( echo "${1}" / 1000000.0 | bc ) - fi -} - -invoke_kpartx() { - $KPARTX $KPARTX_OPTS "${@}" - usleep "${WAIT_US}" -} - -assert_exists() { - local result=0 - while [ "${1}" ] - do - if [ -b "$(readlink -f "${1}")" ] - then - PASS=$(( PASS + 1 )) - ALL_PASS=$(( ALL_PASS + 1 )) - else - FAIL=$(( FAIL + 1 )) - ALL_FAIL=$(( ALL_FAIL + 1 )) - echo "assert_exists: Device does not exist when it should: ${1}" - result=1 - fi - shift - done - return $result -} - -assert_unexists() { - local result=0 - while [ "${1}" ] - do - if [ ! -b "$(readlink -f "${1}")" ] - then - PASS=$(( PASS + 1 )) - ALL_PASS=$(( ALL_PASS + 1 )) - else - FAIL=$(( FAIL + 1 )) - ALL_FAIL=$(( ALL_FAIL + 1 )) - echo "assert_exists: Device exists when it should not: ${1}" - result=1 - fi - shift - done - return $result -} - -PASS=0 -ALL_PASS=0 -FAIL=0 -ALL_FAIL=0 - -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 -FILE4=kpartx4 - -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 -truncate -s $SIZE $FILE4 - -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' -LO4=$(losetup -f $FILE4 --show) -push_cleanup 'losetup -d $LO4' - -[[ $LO1 && $LO2 && $LO3 && $LO4 && -b $LO1 && -b $LO2 && -b $LO3 && -b $LO4 ]] -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' - -# This is a non-kpartx pseudo "partition" mapping -USER1=user1 -push_cleanup 'dmsetup remove -f $USER1' -dmsetup create $USER1 <&2 -usleep $WAIT_US - -assert_exists $SPAN2P1 $LO1P1 $LO2P1 -assert_unexists $SPAN1P1 - -invoke_kpartx -d /dev/mapper/$SPAN2 -usleep $WAIT_US - -assert_exists $LO1P1 $LO2P1 -assert_unexists $SPAN2P1 - -step "rename partitions on loop device" -invoke_kpartx -u -p -spam $LO2 -assert_unexists $LO2P1 -assert_exists ${LO2P1//-foo/-spam} - -step "rename partitions on loop device back" -invoke_kpartx -u -p -foo $LO2 -assert_exists $LO2P1 -assert_unexists ${LO2P1//-foo/-spam} - -step "rename partitions on loop device to default" -invoke_kpartx -u $LO2 -#read a -assert_unexists $LO2P1 -assert_exists ${LO2P1//-foo/p} -# $LO1 ends in a digit - -step "rename partitions on loop device back from default" -invoke_kpartx -u -p -foo $LO2 -assert_exists $LO2P1 -assert_unexists ${LO2P1//-foo/p} - -step "rename partitions on loop devices" -invoke_kpartx -u -p spam $LO2 - -step "delete partitions on loop devices" - -invoke_kpartx -d $LO3 - -# This will also delete the loop device -invoke_kpartx -d $FILE2 -invoke_kpartx -d $LO1 -usleep $WAIT_US - -# ls -l /dev/mapper -assert_unexists $LO1P1 -pop_cleanup -assert_unexists $LO2P1 -pop_cleanup -# spans should not have been removed -# LVs neither -assert_exists /dev/mapper/$SPAN1 /dev/mapper/$SPAN2 /dev/mapper/$USER1 /dev/mapper/$VG-$LV - -step "delete partitions on $LO3 with -f" - -invoke_kpartx -f -d $LO3 -# -d -f should delete the LV, too -assert_unexists /dev/mapper/$VG-$LV -assert_exists /dev/mapper/$SPAN1 /dev/mapper/$SPAN2 - -step "test kpartx creation/deletion on an image file with no existing loopdev" -losetup -d $LO4 - -OUTPUT=$(invoke_kpartx -v -a $FILE4 2>&1) -read loop dm < \ - <(sed -n 's/^add map \(loop[0-9]*\)p1 ([0-9]*:\([0-9]*\)).*$/\1 dm-\2/p' \ - <<<$OUTPUT) -[[ $dm && $loop ]] -push_cleanup "dmsetup remove -f /dev/$dm" -push_cleanup "losetup -d /dev/$loop" - -assert_exists /dev/mapper/${loop}p1 -invoke_kpartx -d $FILE4 -assert_unexists /dev/mapper/${loop}p1 -# /dev/$loop is _not_ automatically deleted -assert_exists /dev/${loop} - -[ $ALL_FAIL -eq 0 ] && OK=yes diff --git a/user/kpartx/APKBUILD b/user/kpartx/APKBUILD new file mode 100644 index 000000000..e1e0feddb --- /dev/null +++ b/user/kpartx/APKBUILD @@ -0,0 +1,32 @@ +# Contributor: CyberLeo +# Maintainer: CyberLeo +pkgname=kpartx +pkgver=0.8.4 +pkgrel=0 +pkgdesc="Manipulates device mappings for the partitions of any block device." +url="http://christophe.varoqui.free.fr/" +arch="all" +options="!check" # Requires root and loopback/dm-mod kernel modules. +license="LGPL-2.0-only" +depends="" +makedepends="lvm2-dev" +checkdepends="debianutils-which parted" +subpackages="$pkgname-doc" +source="multipath-tools-$pkgver.tar.gz::https://git.opensvc.com/?p=multipath-tools/.git;a=snapshot;h=refs/tags/$pkgver;sf=tgz;/multipath-tools-$pkgver.tar.gz +test-kpartx" +builddir="$srcdir/multipath-tools-$pkgver/kpartx" + +build() { + make kpartx +} + +check() { + sudo KPARTX="${builddir}/kpartx" WAIT_US=1000000 "${srcdir}/test-kpartx" +} + +package() { + make DESTDIR="$pkgdir" install +} + +sha512sums="130308e61d6dce31085fc2763219f4df0f3ad9153e0e6e7a5a1c3c948a2305cff9413699025c28f9b81dd24d2a9263f9fa825253060e44232c3bb6600cd1f07f multipath-tools-0.8.4.tar.gz +2e3702243bfc52df7353cc828fb03e3dff36eddbf77eb13140fd58d857c3d45990c679c535c00c569e5a5412ad3823717e264449c038ebf1726a4e7adc08e06e test-kpartx" diff --git a/user/kpartx/test-kpartx b/user/kpartx/test-kpartx new file mode 100755 index 000000000..93a5abdbd --- /dev/null +++ b/user/kpartx/test-kpartx @@ -0,0 +1,395 @@ +#! /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: existing 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; ls -la /dev/mapper; read a; exit 1' ERR +trap 'cleanup' 0 + +CLEANUP=: +cleanup() { + trap - ERR + trap - 0 + if [[ $OK ]]; then + echo == all ${ALL_PASS} tests completed successfully == >&2 + else + printf "== Final Results ==\n%s of %s failed!\n\n" "${ALL_FAIL}" "$(( ALL_PASS + ALL_FAIL ))" >&2 + fi + eval "$CLEANUP" &>/dev/null +} + +push_cleanup() { + CLEANUP="$@;$CLEANUP" +} + +pop_cleanup() { + # CAUTION: simplistic + CLEANUP=${CLEANUP#*;} +} + +step() { + STEP="$@" + + if [ $PASS -gt 0 -o $FAIL -gt 0 ] + then + printf "PASS: %s FAIL: %s\n\n" "${PASS}" "${FAIL}" + PASS=0 + FAIL=0 + fi + + echo == Test step: $STEP == >&2 +} + +mk_partitions() { + parted -s $1 mklabel msdos + parted -s -- $1 mkpart prim ext2 1MiB -1s +} + +wipe_ptable() { + dd if=/dev/zero of=$1 bs=1b count=1 +} + +usleep() { + if which usleep >/dev/null 2>&1 + then + env usleep "${1}" + else + sleep $( echo "${1}" / 1000000.0 | bc ) + fi +} + +invoke_kpartx() { + $KPARTX $KPARTX_OPTS "${@}" + usleep "${WAIT_US}" +} + +assert_exists() { + local result=0 + while [ "${1}" ] + do + if [ -b "$(readlink -f "${1}")" ] + then + PASS=$(( PASS + 1 )) + ALL_PASS=$(( ALL_PASS + 1 )) + else + FAIL=$(( FAIL + 1 )) + ALL_FAIL=$(( ALL_FAIL + 1 )) + echo "assert_exists: Device does not exist when it should: ${1}" + result=1 + fi + shift + done + return $result +} + +assert_unexists() { + local result=0 + while [ "${1}" ] + do + if [ ! -b "$(readlink -f "${1}")" ] + then + PASS=$(( PASS + 1 )) + ALL_PASS=$(( ALL_PASS + 1 )) + else + FAIL=$(( FAIL + 1 )) + ALL_FAIL=$(( ALL_FAIL + 1 )) + echo "assert_exists: Device exists when it should not: ${1}" + result=1 + fi + shift + done + return $result +} + +PASS=0 +ALL_PASS=0 +FAIL=0 +ALL_FAIL=0 + +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 +FILE4=kpartx4 + +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 +truncate -s $SIZE $FILE4 + +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' +LO4=$(losetup -f $FILE4 --show) +push_cleanup 'losetup -d $LO4' + +[[ $LO1 && $LO2 && $LO3 && $LO4 && -b $LO1 && -b $LO2 && -b $LO3 && -b $LO4 ]] +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' + +# This is a non-kpartx pseudo "partition" mapping +USER1=user1 +push_cleanup 'dmsetup remove -f $USER1' +dmsetup create $USER1 <&2 +usleep $WAIT_US + +assert_exists $SPAN2P1 $LO1P1 $LO2P1 +assert_unexists $SPAN1P1 + +invoke_kpartx -d /dev/mapper/$SPAN2 +usleep $WAIT_US + +assert_exists $LO1P1 $LO2P1 +assert_unexists $SPAN2P1 + +step "rename partitions on loop device" +invoke_kpartx -u -p -spam $LO2 +assert_unexists $LO2P1 +assert_exists ${LO2P1//-foo/-spam} + +step "rename partitions on loop device back" +invoke_kpartx -u -p -foo $LO2 +assert_exists $LO2P1 +assert_unexists ${LO2P1//-foo/-spam} + +step "rename partitions on loop device to default" +invoke_kpartx -u $LO2 +#read a +assert_unexists $LO2P1 +assert_exists ${LO2P1//-foo/p} +# $LO1 ends in a digit + +step "rename partitions on loop device back from default" +invoke_kpartx -u -p -foo $LO2 +assert_exists $LO2P1 +assert_unexists ${LO2P1//-foo/p} + +step "rename partitions on loop devices" +invoke_kpartx -u -p spam $LO2 + +step "delete partitions on loop devices" + +invoke_kpartx -d $LO3 + +# This will also delete the loop device +invoke_kpartx -d $FILE2 +invoke_kpartx -d $LO1 +usleep $WAIT_US + +# ls -l /dev/mapper +assert_unexists $LO1P1 +pop_cleanup +assert_unexists $LO2P1 +pop_cleanup +# spans should not have been removed +# LVs neither +assert_exists /dev/mapper/$SPAN1 /dev/mapper/$SPAN2 /dev/mapper/$USER1 /dev/mapper/$VG-$LV + +step "delete partitions on $LO3 with -f" + +invoke_kpartx -f -d $LO3 +# -d -f should delete the LV, too +assert_unexists /dev/mapper/$VG-$LV +assert_exists /dev/mapper/$SPAN1 /dev/mapper/$SPAN2 + +step "test kpartx creation/deletion on an image file with no existing loopdev" +losetup -d $LO4 + +OUTPUT=$(invoke_kpartx -v -a $FILE4 2>&1) +read loop dm < \ + <(sed -n 's/^add map \(loop[0-9]*\)p1 ([0-9]*:\([0-9]*\)).*$/\1 dm-\2/p' \ + <<<$OUTPUT) +[[ $dm && $loop ]] +push_cleanup "dmsetup remove -f /dev/$dm" +push_cleanup "losetup -d /dev/$loop" + +assert_exists /dev/mapper/${loop}p1 +invoke_kpartx -d $FILE4 +assert_unexists /dev/mapper/${loop}p1 +# /dev/$loop is _not_ automatically deleted +assert_exists /dev/${loop} + +[ $ALL_FAIL -eq 0 ] && OK=yes -- cgit v1.2.3-70-g09d2 From 27d39acb81eb8b8341a30b50337498a3cb002deb Mon Sep 17 00:00:00 2001 From: CyberLeo Date: Thu, 22 Jul 2021 21:48:07 -0500 Subject: user/kpartx: move to github and bump version --- user/kpartx/APKBUILD | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/user/kpartx/APKBUILD b/user/kpartx/APKBUILD index e1e0feddb..e5d464f77 100644 --- a/user/kpartx/APKBUILD +++ b/user/kpartx/APKBUILD @@ -1,7 +1,7 @@ # Contributor: CyberLeo # Maintainer: CyberLeo pkgname=kpartx -pkgver=0.8.4 +pkgver=0.8.6 pkgrel=0 pkgdesc="Manipulates device mappings for the partitions of any block device." url="http://christophe.varoqui.free.fr/" @@ -12,7 +12,7 @@ depends="" makedepends="lvm2-dev" checkdepends="debianutils-which parted" subpackages="$pkgname-doc" -source="multipath-tools-$pkgver.tar.gz::https://git.opensvc.com/?p=multipath-tools/.git;a=snapshot;h=refs/tags/$pkgver;sf=tgz;/multipath-tools-$pkgver.tar.gz +source="multipath-tools-$pkgver.tar.gz::https://github.com/opensvc/multipath-tools/archive/refs/tags/$pkgver.tar.gz test-kpartx" builddir="$srcdir/multipath-tools-$pkgver/kpartx" @@ -27,6 +27,5 @@ check() { package() { make DESTDIR="$pkgdir" install } - -sha512sums="130308e61d6dce31085fc2763219f4df0f3ad9153e0e6e7a5a1c3c948a2305cff9413699025c28f9b81dd24d2a9263f9fa825253060e44232c3bb6600cd1f07f multipath-tools-0.8.4.tar.gz +sha512sums="82e5b7307e599ba6b059679c3987a442fb5be4885f0a27c260a99a07cb336b88d48e314b4ec951944e0200e4731522d8da043d98fa566857ecc6d100791c0e38 multipath-tools-0.8.6.tar.gz 2e3702243bfc52df7353cc828fb03e3dff36eddbf77eb13140fd58d857c3d45990c679c535c00c569e5a5412ad3823717e264449c038ebf1726a4e7adc08e06e test-kpartx" -- cgit v1.2.3-70-g09d2