Skip to content

Instantly share code, notes, and snippets.

@xezpeleta
Last active February 21, 2018 13:18
Show Gist options
  • Save xezpeleta/7689681 to your computer and use it in GitHub Desktop.
Save xezpeleta/7689681 to your computer and use it in GitHub Desktop.
Proxmox VM Backup ./mount_vm.sh <VMID> ./umount_vm.sh <VMID>
#! /bin/sh
if [ $# -eq 0 ]
then
echo "No arguments supplied"
exit 1
fi
if [ -z "$1" ]
then
echo "No arguments supplied"
exit 2
else
if [ "$1" -lt 100 ];then
echo "VMID must be greater than 100"
exit 3
fi
fi
if [ -z "$2" ]
then
# Default partition id = 1
PARTID=1
else
if [ "$2" -gt 0 ];then
PARTID=$2
else
echo "Wrong partition number"
exit 4
fi
fi
VMID=$1
IMG="/var/lib/vz/images/$VMID/vm-$VMID-disk-1.raw"
MOUNTPOINT="/mnt/$VMID"
if [ -f $IMG ]
then
echo "Image file: $IMG"
else
echo "File doesn't exist: $IMG"
echo "Are you sure that VM $VMID has a KVM raw file?"
exit 5
fi
if mountpoint -q "$MOUNTPOINT"
then
echo "File already mounted!"
exit 6
fi
# Available loopback device:
LOOPDEV=`losetup -f`
echo "Loop device: $LOOPDEV"
# Associate loopback with our VM image
losetup $LOOPDEV $IMG
echo "Device $LOOPDEV mapped to the VM image"
echo "Create partition devices:"
kpartx -av $LOOPDEV
PART="/dev/mapper/"`kpartx -l "$LOOPDEV"|sed -n ''$PARTID'p'|awk -F: '{ print $1 }'`
if [ -r $PART ]
then
echo "Mounting partition $PARTID: $PART"
if [ -d /mnt/$VMID ]
then
mount -r $PART $MOUNTPOINT
echo "$MOUNTPOINT mounted!"
else
echo "Mountpoint doesn't exist: $MOUNTPOINT"
kpartx -dv $LOOPDEV
sleep 1
losetup -d $LOOPDEV
exit 7
fi
else
echo "Partition device doesn't exist: $PART"
kpartx -dv $LOOPDEV
sleep 1
losetup -d $LOOPDEV
exit 8
fi
#! /bin/sh
if [ $# -eq 0 ]
then
echo "No arguments supplied"
exit 1
fi
if [ -z "$1" ]
then
echo "No arguments supplied"
exit 2
else
if [ "$1" -lt 100 ];then
echo "VMID must be greater than 100"
exit 3
fi
fi
VMID=$1
#IMG="/var/lib/vz/images/$VMID/vm-$VMID-disk-1.raw"
MOUNTPOINT="/mnt/$VMID"
LOOPDEV=`losetup -a|grep "$VMID"|awk -F: '{ print $1 }'`
#if [ -f $IMG ]
#then
# echo "Umounting file: $IMG"
#else
# echo "File doesn't exist: $IMG"
# echo "Are you sure that VM $VMID has a KVM raw file?"
# exit 4
#fi
if mountpoint -q "$MOUNTPOINT"
then
echo "Unmounting VMID_$VMID"
umount "$MOUNTPOINT"
kpartx -dv "$LOOPDEV"
sleep 1
losetup -d "$LOOPDEV"
else
echo "Not mounted"
exit 4
fi
@xezpeleta
Copy link
Author

El script "mount_vm.sh" sirve para acceder al contenido de una VM desde el servidor de Proxmox.

./mount_vm.sh []

El parámetro PARTID es opcional. Si no se especifica, se montará la primera partición del disco. Las particiones se montan en modo lectura.

El script "umount_vm.sh" desmonta esas particiones y restablece la situación inicial.

Por mejorar: si algo falla en el proceso, echar marcha atrás

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment