Mount & list content of raw disk image (KVM)

Posted on Thu 05 April 2018 in Administracja • [1 min read]

Sometimes I need to list content of raw disk image I made from original computer and converted to KVM.

This is very simple in Debian or Ubuntu. I made script to do it but it works only with one image and 1st partition because that’s what I need. Feel free to expand it:

#!/bin/bash
apt-get install kpartx
FILE=`ls -1 \*.raw\`
DUPA=`kpartx -av $FILE \| head -n1 \| cut -d" " -f3\`
if [ ! -x /mnt/$DUPA ]; then mkdir /mnt/$DUPA ; fi
sleep 5
mount /dev/mapper/$DUPA /mnt/$DUPA
ls /mnt/$DUPA
umount /mnt/$DUPA
kpartx -dv $FILE

I'm using kpartx tool which creates mappings of partitions to loop device. Then I can mount loop device on directory and list its content.