Rescuing Your Raspberry Pi: Recover Data from Corrupted SD Card with Guestfish
SD card corruption is a common and frustrating issue for Raspberry Pi users. Recently, my Raspberry Pi’s SD card became corrupted, causing the filesystem to go into read-only mode. The root filesystem (rootfs) was completely inaccessible, making it impossible to retrieve my data through conventional means. Fortunately, I was able to recover my files by creating an image backup of the SD card and extracting data using guestfish. This guide walks you through the process step by step.
Step 1: Create an Image Backup of the Corrupted SD Card
Before attempting any data recovery, it’s crucial to create a backup image of the corrupted SD card. This ensures that no further damage occurs to the original card and allows you to safely extract files from the image.
Be very careful when using the
ddcommand! Selecting the wrong device can lead to irreversible data loss.
Use the following command to create an image of the SD card:
1
sudo dd if=/dev/sdX of=~/raspberry_pi_backup.img bs=4M status=progress
- Replace
/dev/sdXwith the actual device name of your SD card. - The
bs=4Moption optimizes speed, whilestatus=progressprovides real-time progress updates.
Ensure you have enough disk space available to store the backup image before proceeding.
Step 2: Install Guestfish
To access and extract files from the disk image, install libguestfs-tools, which includes guestfish:
1
sudo apt-get install libguestfs-tools
Step 3: Open the Image with Guestfish
Run guestfish in read-only mode to prevent accidental modifications to the image:
1
sudo guestfish --ro -a ~/raspberry_pi_backup.img
This command opens an interactive guestfish prompt, allowing you to explore the disk image safely.
Step 4: List Devices and Mount the Partition
Within the guestfish prompt, execute the following commands:
-
Start the guestfish session:
1
run
-
List available devices:
1
list-devices
Example output:
1
/dev/sda
-
List partitions on the device:
1
list-partitions
Example output:
1 2
/dev/sda1 /dev/sda2
-
Mount the root filesystem partition (replace
/dev/sdaXwith the correct partition number):1
mount /dev/sdaX /
If you’re unsure which partition contains your data, try listing files with
ls /after mounting each partition.
Step 5: Navigate and Extract Files
Once the partition is mounted, list files and directories:
1
ls /
To copy files from the image to your host system, use the copy-out command:
1
copy-out /path/on/device /path/on/host
For example, to copy the entire home directory from the image to your local system:
1
copy-out /home/user ~/recovered_data/
Ensure you have enough storage space on your host system before copying large files.
Step 6: Exit Guestfish
After extracting your files, exit the guestfish session:
1
exit
If some files fail to copy, check their permissions and try copying them as root.
Conclusion
By following these steps, I successfully recovered my files from a corrupted Raspberry Pi SD card. guestfish is a powerful and efficient tool for accessing disk images, even when standard recovery methods fail.
Best Practices to Prevent Future Corruption
- Regularly back up your data to avoid unexpected losses.
- Use high-quality SD cards to minimize the risk of corruption.
- Shut down your Raspberry Pi properly to maintain filesystem integrity.
If you’re dealing with a similar issue, try this method before assuming your data is lost!