Last active
November 20, 2016 22:15
-
-
Save ebirn/5090565 to your computer and use it in GitHub Desktop.
shellcript to mount/unmount VM images containing LVM volumes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
NAME=$1 | |
if [ "$NAME" == "" ]; then | |
echo "usage: $0 <vmname>" | |
exit 1; | |
fi | |
MYSELF=$( hostname -s ) | |
if [ "$MYSELF" == "$NAME" ]; then | |
echo "BAD IDEA: cannot mount $NAME image inside it's VM. try on another host" | |
exit 1; | |
fi | |
# path to image file | |
IMGFILE="/backup/host/$NAME/$NAME.raw" | |
# loopback mount | |
echo -n "mount loop device: " | |
losetup -f $IMGFILE | |
LOOPDEV=$( losetup -j $IMGFILE | awk 'BEGIN {FS=":" } ; { print $1; }' | head -1 ) | |
echo "$LOOPDEV" | |
echo "adding device maps" | |
kpartx -av $LOOPDEV | |
echo "activate volume group vg_$NAME" | |
vgscan | |
vgchange -ay vg_$NAME | |
MOUNTPOINT="/mnt/$NAME" | |
echo "mount image to $MOUNTPOINT" | |
mkdir -p $MOUNTPOINT | |
mount -oro /dev/vg_$NAME/lv_root $MOUNTPOINT | |
echo "" | |
echo "press ENTER to reverse process and unmount" | |
echo "do not forget to cease all file access to mountpoint first!" | |
echo "" | |
read | |
echo "unmounting..." | |
umount $MOUNTPOINT | |
echo "disable vg" | |
# disable volume group | |
vgchange -an "vg_$NAME" | |
echo "remove device maps" | |
# unmap partitions | |
kpartx -dv $LOOPDEV | |
echo "detach loopback" | |
losetup -d $LOOPDEV | |
echo "done." |
probably can be simplified with the partscan option of losetup
losetup --partscan
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
one might need to adopt the path to the image file, line #18