Restore files on RPi homelab server
I lost connection to my Raspberry Pi homelab server and then I found it wouldn't
respond to SSH. After connecting a debug screen, I discovered a message about
corrupted libapparmor library.
Since the RPi uses aarch64 architecture, I wasn't able to easily chroot from my
x86 PC, so I had to restore affected files directly from .deb packages to the
RPi microsd card.
This guide explains how to recover a corrupted Debian system on Raspberry Pi by manually extracting and replacing system files. So let's go!
Summary
- RPi server stopped responding
- Screen showed
libapparmorcorruption error - Cannot chroot because RPi is aarch64, PC is on x86
- Restored broken library from
.debdirectly to microsd - Booted again, but got kernel panic
- Restored kernel modules from
.debto microsd - Booted successfully
- Installed
debsumsto verify packages integrity - Reinstalled other broken packages via apt
- System fully restored
Solve the first error
These are the step-by-step instructions:
-
First, identify the microsd card partition and mount it:
mkdir /tmp/rootfs sudo mount /dev/sdXN /tmp/rootfsNotes:
- Find the correct device with
lsblkorblkid - Replace
sdXNwith that partition (eg.sdb2)
- Find the correct device with
-
Identify the corrupted file by checking error messages from the boot screen, which in my case mentioned about issues with
libapparmorlibrary -
Get the right
.debpackage according to Debian system version:curl -o libapparmor1_4.1.0-1_arm64.deb \ http://http.us.debian.org/debian/pool/main/a/apparmor/libapparmor1_4.1.0-1_arm64.debNote: check
/etc/os-releaseon your system for the correct version -
Extract
data.tar.xzfrom package contents:ar x libapparmor1_4.1.0-1_arm64.debNote: it can be
data.tar.gzfor older packages -
Extract data contents
-
For modern
.xzcompressed archives:tar -xJf data.tar.xz -
For older
.gzcompressed archives:tar -xzf data.tar.gz
-
-
Verify if the library is corrupted by comparing the extracted file with the one on the mounted microsd card partition:
cmp -s \ usr/lib/aarch64-linux-gnu/libapparmor.so.1.24.2 \ /tmp/rootfs/lib/aarch64-linux-gnu/libapparmor.so.1.24.2 \ && echo "MATCH" || echo "MISMATCH"Note: it says
MATCHif files are identical, orMISMATCHif they differ -
Replace the library if there is a mismatch
cp \ usr/lib/aarch64-linux-gnu/libapparmor.so.1.24.2 \ /tmp/rootfs/lib/aarch64-linux-gnu/libapparmor.so.1.24.2 -
Run the
cmpcommand again to confirm the files now match -
Fix permissions if needed
sudo chmod 644 /tmp/rootfs/lib/aarch64-linux-gnu/libapparmor.so.1.24.2
Kernel recovery
After fixing the library, I booted the microsd card but got a kernel panic, which indicated kernel modules were also corrupted.
So these steps were the next to be applied:
-
Download and extract kernel package
curl -o linux-image-6.12.62+rpt-rpi-v8_6.12.62-1+rpt1_arm64.deb https://archive.raspberrypi.com/debian/pool/main/l/linux/linux-image-6.12.62+rpt-rpi-v8_6.12.62-1+rpt1_arm64.deb ar x linux-image-6.12.62+rpt-rpi-v8_6.12.62-1+rpt1_arm64.deb tar -xJf data.tar.xzNote: Use the kernel version matching your system
-
Compare boot files between the extracted package and mounted microsd fat32 boot partition:
cmp -s \ boot/vmlinuz-6.12.62+rpt-rpi-v8 \ /tmp/rootfs/boot/vmlinuz-6.12.62+rpt-rpi-v8 \ && echo "MATCH" || echo "MISMATCH" cmp -s \ boot/config-6.12.62+rpt-rpi-v8 \ /tmp/rootfs/boot/config-6.12.62+rpt-rpi-v8 \ && echo "MATCH" || echo "MISMATCH" cmp -s \ boot/System.map-6.12.62+rpt-rpi-v8 \ /tmp/rootfs/boot/System.map-6.12.62+rpt-rpi-v8 \ && echo "MATCH" || echo "MISMATCH" -
Generate checksums for modules from the extracted package:
cd usr/lib/modules/6.12.62+rpt-rpi-v8/ find . -type f -exec sha256sum {} \; | sort > /tmp/hashes_orig.txt -
Generate checksums for modules on the microsd card:
cd /tmp/rootfs/usr/lib/modules/6.12.62+rpt-rpi-v8/ find . -type f -exec sha256sum {} \; | sort > /tmp/hashes_card.txtNote: being inside is needed to have same output
-
Compare the two checksums lists:
diff -u /tmp/hashes_orig.txt /tmp/hashes_card.txt -
If there are differences then replace affected modules by copying the entire modules directory or at least overriding the broken files:
sudo cp -r \ usr/lib/modules/6.12.62+rpt-rpi-v8/* \ /tmp/rootfs/usr/lib/modules/6.12.62+rpt-rpi-v8/
Verify and repair packages
Once the system booted, in order to verify package integrity I got some tools:
-
Install debsums
sudo apt update sudo apt install debsums -
Run a silent check to list only corrupted files:
sudo debsums -s -
For each corrupted package do a reinstall, example:
sudo apt install --reinstall \ libacl1 libassuan9 libcamera0.6 libcc1-0 libcrypt-dev \ libxmuu1 rpi-usb-gadget vim-commonNote: replace with the packages shown by
debsums -
Verify integrity again
sudo debsums -sNote: if no output, then all packages are intact
Conclusion
The system is now fully restored!
This process saved me from re-imaging the microsd card and losing all configuration.
It seems I must use an UPS next time >.<
Happy hacking! 🐱