The microSD card: what is saved, the clock, and repairing corruption
The Pi has no hard disk. It boots from your microSD card, and the card is the disk: the operating system, everything you install and all your files live on it. Take the card home, bring it back, and your work is still there. This page covers what that means in practice and how to repair the card when something gets damaged.
What is saved and what is not
| Saved on the card (still there next lab) | Not saved (gone after shutdown or on another Pi) |
|---|---|
| Installed software: Apache, MariaDB, PHP, phpMyAdmin | The date and time (see below) |
Config files you edited in /etc (apache2.conf etc.) | Programs you were running: Flask, Python scripts, Thonny |
Your MariaDB databases and users (/var/lib/mysql) | Anything typed in nano or Thonny but not saved |
Your files in /home/pi, including the env venv | The IP address (the network may give you a different one) |
| Wi-Fi networks you joined |
Services start by themselves; your programs do not. Apache and MariaDB are enabled services, so they start at every boot. Flask and your Python scripts only run while you run them: after a reboot, start app.py again.
systemctl is-enabled apache2 mariadb # "enabled" = starts at boot
Why the clock resets
A normal computer keeps time with a small battery on the motherboard. The lab Pis have no clock battery (the Pi 5 has a clock chip, but it needs an optional battery), and the lab network may block internet time. So at every boot the Pi does not know the real time. Raspberry Pi OS restores the time it saved at the last shutdown, so the clock is usually days or weeks behind.
What a wrong clock breaks:
| Symptom | Cause |
|---|---|
apt update: Release file ... is not valid yet | The Pi thinks it is in the past, so the package lists look like they come from the future. |
| Web pages: Your connection is not private / certificate errors | Certificates look not yet valid (or expired). |
Wrong SensorDate in MariaDB | The database stamps rows with the Pi's clock. |
| Clock skew detected / file has modification time in the future | Files were saved while the clock was wrong. Harmless once the clock is fixed. |
Fix: set the clock at the start of every lab, before apt or recording data:
sudo date -s "26 MAR 2026 14:55" # use the real date and time
date # check it
timedatectl # also shows the time zone and whether internet time is working
If the time is right but the hours are off, the time zone is wrong: sudo dpkg-reconfigure tzdata (Singapore is Asia → Singapore). See dpkg-reconfigure.
Rows saved while the clock was wrong keep the wrong time. For lab data it is easiest to delete them: DELETE FROM Data WHERE SensorDate < '2026-03-26'; (use today's date).
Avoiding corruption
The card is written to all the time. If power is cut while it is writing, the file being written (or the file system itself) can be damaged. Most corruption comes from:
-
Pulling the power or the card without shutting down. Always run
sudo shutdown nowand wait until the green light stops flashing. -
A weak power supply. The Pi 5 needs the official 27 W USB-C supply. A phone charger causes undervoltage: random crashes and damaged files. Check:
vcgencmd get_throttled # throttled=0x0 means power has been fine since bootAnything other than
0x0(for example0x50000) means the voltage dropped at some point. -
A full card. Programs fail half-way through writing. Check with
df -h /and keep some space free. -
An old or cheap card. Cards wear out. Errors that keep coming back after repairs mean the card is dying.
Signs of corruption and how to fix them
Work through these from the top. Before any repair, copy off what you cannot lose (your .py files and a database dump; see Back up your work).
"Read-only file system"
Saving, apt or sudo fails with Read-only file system. Linux found an error on the card and switched it to read-only to stop more damage. Check:
mount | grep " / " # "(ro," means read-only, "(rw," means normal
sudo dmesg | grep -i -E "ext4|mmc|i/o error" | tail
Fix: save what you can, then reboot. Raspberry Pi OS checks and repairs the file system at boot when it is marked as damaged. If it comes back read-only again, force a full check at the next boot:
sudo nano /boot/firmware/cmdline.txt
This file is one single line. Add fsck.mode=force at the end of that line (a space before it, no new line), save and reboot. The boot takes longer while it checks. Afterwards, remove fsck.mode=force again.
Repairing the card from another computer
If the Pi will not boot at all, repair the card in another Linux machine (another Pi with a USB card reader works):
lsblk # find the card, e.g. sda with sda1 and sda2
sudo umount /dev/sda1 /dev/sda2 # never check a mounted file system
sudo fsck.vfat -a /dev/sda1 # boot partition (small, FAT)
sudo e2fsck -f -y /dev/sda2 # main Linux partition (ext4)
Use the device names lsblk shows. Getting them wrong can damage the other computer's disk.
:::warning Windows Windows can only read the small boot partition. If Windows asks "You need to format the disk", click Cancel. Formatting erases everything on the card. :::
"dpkg was interrupted" or apt errors after a crash
An install was cut off half-way:
sudo dpkg --configure -a # finish interrupted installs
sudo apt --fix-broken install # fix missing dependencies
sudo apt install --reinstall apache2 # replace a program's damaged files
sudo dpkg --verify apache2 lists files that differ from the original package. Config files you edited on purpose will show up too; anything else in the list is likely damaged.
A config file is empty or full of garbage
After a power cut, a file you were editing can end up empty (0 bytes) or full of strange characters, and the service will not start. sudo apache2ctl configtest points at the broken file. To get the original back, move the broken file away and let apt put back the default:
sudo mv /etc/apache2/apache2.conf ~/apache2.conf.broken
sudo apt install --reinstall -o Dpkg::Options::="--force-confmiss" apache2
sudo systemctl restart apache2
This gives you the default file, so redo any lab changes you made to it (for example the phpMyAdmin Include line, see phpMyAdmin).
Python: "source code cannot contain null bytes"
Your .py file was being saved when the power went off and is now partly zeros. Open it in Thonny: if the code is still there, copy it into a new file and save it under a new name. If it is empty, rewrite it from the lab sheet (this is why backups matter).
A broken venv (pip or python errors from inside env) is easiest to rebuild:
cd /home/pi
rm -rf env
python3 -m venv env
source env/bin/activate
pip install pyserial pymysql flask
Then point Thonny at it again (Python venv and Thonny).
MariaDB will not start after a power cut
MariaDB usually repairs itself when it starts. If systemctl status mariadb shows failed:
df -h / # 1. is the card full? (100% = free space first)
sudo journalctl -u mariadb -n 50 --no-pager # 2. read why it stopped
Before trying anything else, back up the raw database files:
sudo systemctl stop mariadb
sudo cp -a /var/lib/mysql /home/pi/mysql-files-backup
If MariaDB starts but a table gives errors, check all tables:
sudo mariadb-check --all-databases
Last resort when MariaDB crashes at start with InnoDB errors: add innodb_force_recovery = 1 under [mysqld] in /etc/mysql/mariadb.conf.d/50-server.cnf, start MariaDB, dump your database straight away (sudo mariadb-dump MySensor > ~/mysensor.sql), then remove that line again. Never leave it in: it is only for getting your data out.
The card is full
Symptoms: you cannot save files, MariaDB stops, the desktop will not log in (it goes back to the login screen).
df -h / # how full?
sudo apt clean # downloaded installers
sudo journalctl --vacuum-size=50M # old system logs
du -h /home/pi | sort -h | tail -n 15 # your biggest folders
The Pi will not boot at all
No desktop, a black screen, or the Pi 5's diagnostic screen (with a QR code) on the monitor: the Pi cannot find or read the boot files on the card.
- Push the card in firmly and power up again with the official supply.
- Try repairing it from another computer.
- If that fails, copy your files off the card from another Linux computer (the files are in
/home/pion the second partition), then re-clone the card as in Lab 5a.
The card is dying
Errors that keep coming back after repairs, I/O error or mmc0 errors in sudo dmesg, or files changing by themselves: copy your work off now and move to a new card.
Back up your work
The card is the only copy of your work. Keep a copy somewhere else:
mariadb-dump -u Nypuser -p MySensor > ~/mysensor_backup.sql # the database (into a file)
tar czf ~/mywork.tar.gz ~/*.py ~/templates ~/static ~/mysensor_backup.sql # all your work in one file
Copy mywork.tar.gz to a USB stick, or to your laptop over the network:
scp pi@172.16.x.x:~/mywork.tar.gz . # run this on your laptop (Windows PowerShell works too)
To back up the whole card at home, use Win32 Disk Imager on Windows: put the card in a reader, choose a file name, click Read. Write puts the image back onto a card later.