Fixing broken filesystem inside .img file with multiple partitions.
published on 13.02.2026 23:11

If you ever end up breaking partition inside disk image for your VM this is how you will fix that:
In Debian and Ubuntu you can find these tools in "util-linux" and "mount" packages.

1.) Mount your disk image:
losetup --find --show /where/ever/is/yourdisk.img

2.) List all to see if dev is attached and what number it has:
losetup -a

3.) Once you have your number attach the "drive" partitions, "(#)" is your "drive" number:
partx --update /dev/loop(#)

4.) List all partitions in your attached "drive", replace "(#)":
ls -al /dev/loop(#)*

5.) Begin to fix it (here is example for ext4), replace "(#)" with number of drive and partition:
e2fsck -p -c -f -v /dev/loop(#)p(#)

6.) Detach partition mappings, replace "(#)" with "drive number":
partx --delete /dev/loop(#)

7.) Detach the drive it self, replace "(#)" with "drive number":
losetup --detach /dev/loop(#)

You are done.