Mount & list content of raw disk image (KVM)

Posted on Thu 05 April 2018 in Tips4Unices • 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:
1. 2#!/bin/bash
2. 4apt-get install kpartx
3. 6FILE=`ls -1 *.raw`
4. 8DUPA=`kpartx -av $FILE | head -n1 | cut -d" " -f3`
5. 10if [ ! -x /mnt/$DUPA ]; then mkdir /mnt/$DUPA ; fi
6. 12sleep 5
7. 14mount /dev/mapper/$DUPA /mnt/$DUPA
8. 16ls /mnt/$DUPA
9. 18umount /mnt/$DUPA
10. 20kpartx -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.