What if your grub loader on a virtual machine is broken – or you need to repair or resize a filesystem in one of your virtual machines?
You want to check the storage of your virtual machines on your virtualization server.
- shutdown the virtual machine (or virsh destroy $machine) to make sure nothing else is using the file system
- Check for the next available loopback device on your virtualization server: losetup -f (let’s asume /dev/loop0 for the next steps)
- Now attach the device (disk) from your virtual machine as loopback device on your server: losetup /dev/loop0 /dev/mapper/$DEVICE
- Use kpartx to discover and device-map the partitions: kpartx -av /dev/loop0
- Check the partitioning: fdisk -l /dev/loop0
- Extend the partitioning, if you like/need (see the man pages for fdisk and resize2fs, if needed)
- Run fsck on the filesystem: fsck -vc /dev/mapper/loop0p2
- Mount the filesystem into your local system: mount /dev/mapper/loop0p2 /mnt
- Do whatever you want there (or chroot into the filesystem after bind-mounting /proc /sys and /dev for example)
- Detach the device from your local system: umount /mnt
- Delete the partition mapping for the device: kpartx -dv /dev/loop0
- Detach the loop device from your local system: losetup -d /dev/loop0