Skip to content

Instantly share code, notes, and snippets.

@ejrydhfs
Created April 12, 2025 04:16
Show Gist options
  • Save ejrydhfs/12b29ac2f86c12b29b5f7c2ff59fbb88 to your computer and use it in GitHub Desktop.
Save ejrydhfs/12b29ac2f86c12b29b5f7c2ff59fbb88 to your computer and use it in GitHub Desktop.
How to repair a fat32 drive on linux
1. Use ddrescue to create an image of the drive and create a disk image. You will need to mount the disk image later,
2. Mount the disk image as a loop device. This will trick the software we will use, TestDisk, into thinking it's a real device.
3. Use TestDisk and follow the documentation here to repair the FAT boot sector:
https://www.cgsecurity.org/testdisk_doc/repairing_filesystem.html
4. unmount the loop device, and create a copy of your disk image.
5. Increase the size of the file from the command line using the truncate (yes, the truncate command) which can also increase the size of files by using it like this: truncate -s +N where N is the number of bytes you want to add.
6. Take the copy of the disk image, mount it as a loop device, and run gparted from the terminal by typing this on the terminal: "gparted /dev/loopN" where the capital N is the number of the loop device.
7. once you have gparted running, try to increase the size of the FAT partition just below the size of the file.
8. gparted will fail. now run dostools
9. run photorec
10. run fsck
11. turn the disk image into a vhd by using https://docs.oracle.com/en/virtualization/virtualbox/6.0/user/vboxmanage-convertfromraw.html
and run chkdsk from the file before gparted, after dostools, and before gparted while deleting the files it recovered
12, From the vhd windows, you can then try to perform stuff using photorec again or fsck
as we can see, there are steps where several procedures are possible. each procedure requires the steps above to be done in a sequence so we'll call each of these sequences a series of operations.
at each step you may want to create a new copy of the disk image, if you don't have enough space to do this you may want to create a master disk image, make a copy of the master which we'll call a secondary copy, and then make copies of the secondary copy, one for every series of operations that involves a different way of modifying data on the disk image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment