A spare-machine Gitea forge, hardened, backed up and with CI, as a template. Personal values are placeholders listed in the README.
212 lines
9.2 KiB
Markdown
212 lines
9.2 KiB
Markdown
---
|
||
type: Guide
|
||
purpose: Repair the NTFS backup drive without reformatting it.
|
||
answers:
|
||
- "How is a dirty or damaged NTFS backup drive repaired?"
|
||
cites:
|
||
- scripts/chkdsk-vm.sh
|
||
---
|
||
|
||
# Repairing a Dirty NTFS Backup Drive
|
||
|
||
The backup drive is NTFS and also holds non-backup data, so it is never reformatted. Linux
|
||
can clear its dirty flag and fix the `$MFT` mirror (`ntfsfix`), but **has no
|
||
full NTFS repair** — real damage needs Windows `chkdsk`. With no Windows
|
||
machine, `chkdsk` runs from the official Windows 11 ISO's Setup command prompt
|
||
inside a VM on a separate Linux PC (not the server), with the backup drive passed through over
|
||
USB. Nothing gets installed and no licence is needed.
|
||
|
||
## Triage — find your rung, start there
|
||
|
||
Symptom first appears as a mount failure on mithlond:
|
||
|
||
```
|
||
mount: /mnt/backup: wrong fs type, bad option, bad superblock on /dev/sdb1 ...
|
||
ERROR: BACKUP is dirty — run by hand: ntfsfix -n /dev/disk/by-label/BACKUP, then ntfsfix -d
|
||
```
|
||
|
||
Then read the kernel log — it says which rung you are on:
|
||
|
||
```bash
|
||
journalctl -k --since "-1h" --no-pager | grep -iE 'ntfs3|sdb|I/O'
|
||
```
|
||
|
||
| Kernel log / `ntfsfix -n` says | Meaning | Go to |
|
||
|---|---|---|
|
||
| `volume is dirty and "force" flag is not set!` only; `ntfsfix -n` ends `processed successfully` with no mismatch | Flag left by an unsafe eject or Windows Fast Startup; structures fine | [Rung 1](#rung-1--clear-the-dirty-flag) |
|
||
| `ntfsfix -n`: `$MFTMirr does not match $MFT (record 3)` | Unplugged while mounted; the `$Volume` record's mirror missed a write | [Rung 2](#rung-2--fix-the-mft-mirror) |
|
||
| `Mark volume as dirty due to NTFS errors`, or the flag comes back right after rung 1/2 | Real damage (index/record) in something written at unplug time | [Rung 3](#rung-3--chkdsk-via-a-windows-setup-vm) |
|
||
| `failed to read volume at offset`, `Synchronize Cache failed`, USB resets **while plugged in** | Cable, port or drive hardware | Swap cable/port; check SMART: `sudo smartctl -a /dev/sdb` (`smartmontools`) |
|
||
|
||
Never `mount -o force` and never reformat. Every rung below runs only with
|
||
nothing using the backup drive: `findmnt /mnt/backup` prints nothing, and
|
||
`systemctl is-active gitea-backup` is not `activating`.
|
||
|
||
Always address the drive as **`/dev/disk/by-label/BACKUP`**, not `/dev/sdb1` — a
|
||
mistyped `sda1` can hit the server's own `/boot` (ntfsfix refuses only because it
|
||
is mounted).
|
||
|
||
## Rung 1 — clear the dirty flag
|
||
|
||
On mithlond (`ntfsfix` is in `ntfsprogs`, not `ntfs-3g`):
|
||
|
||
```bash
|
||
sudo ntfsfix -n /dev/disk/by-label/BACKUP # dry run — must end "processed successfully", no "does not match"
|
||
sudo ntfsfix -d /dev/disk/by-label/BACKUP # clears the flag, empties the journal
|
||
sudo gitea-backup check # mounts, reads every restic pack back
|
||
```
|
||
|
||
## Rung 2 — fix the MFT mirror
|
||
|
||
`ntfsfix -n` printing `$MFTMirr does not match $MFT (record 3)` followed by
|
||
`Remount failed: Input/output error` is expected in dry-run mode — nothing was
|
||
written yet, so the simulated remount still sees the mismatch. Record 3 is
|
||
`$Volume`, which holds the dirty flag itself; `ntfsfix` copies the good `$MFT`
|
||
record over the mirror, which is the designed fix.
|
||
|
||
```bash
|
||
sudo ntfsfix -d /dev/disk/by-label/BACKUP # "Correcting differences in $MFTMirr record 3...OK"
|
||
sudo ntfsfix -n /dev/disk/by-label/BACKUP # re-check: no mismatch, "processed successfully"
|
||
sudo gitea-backup check
|
||
```
|
||
|
||
**If the volume is dirty again straight after this `check`** (kernel log:
|
||
`Mark volume as dirty due to NTFS errors`), ntfs3 hit damage that `ntfsfix`
|
||
cannot touch → rung 3. Do not loop on `ntfsfix -d`: every rw mount of a
|
||
damaged volume risks the other data on the backup drive.
|
||
|
||
## Rung 3 — chkdsk via a Windows Setup VM
|
||
|
||
On a **separate Linux PC** with KVM (the commands assume Arch; adapt if yours
|
||
differs). Not on the server: little RAM, old CPU, live services.
|
||
|
||
Do not use Windows XP's `chkdsk /f` on the backup drive: it predates NTFS features newer
|
||
Windows writes and can "repair" them away. XP also cannot see GPT disks.
|
||
|
||
### 1. QEMU
|
||
|
||
```bash
|
||
sudo pacman -S qemu-desktop
|
||
ls -l /dev/kvm # must exist
|
||
```
|
||
|
||
### 2. The Windows 11 ISO
|
||
|
||
Microsoft issues a per-session link valid 24 h — there is no fixed URL.
|
||
|
||
1. Open <https://www.microsoft.com/software-download/windows11>.
|
||
2. *Download Windows 11 Disk Image (ISO) for x64 devices* → **Windows 11
|
||
(multi-edition ISO for x64 devices)** → **Download Now**.
|
||
3. Pick a language → **Confirm** → right-click **64-bit Download** → copy link.
|
||
4. Download (quotes required — the link contains `&`; `-C -` resumes):
|
||
|
||
```bash
|
||
mkdir -p vm && curl -L -C - -o vm/Win11_x64.iso '<pasted link>' # from the repo root
|
||
```
|
||
|
||
About 7–9 GB. `vm/` is gitignored, so the ISO stays out of git but inside the
|
||
repo; keep it for next time — rung 3 needs no new download while it is there.
|
||
|
||
### 3. Plug the backup drive into the PC
|
||
|
||
```bash
|
||
lsusb # find your drive; its ID goes in DRIVE_USB
|
||
findmnt -S LABEL=BACKUP || echo "not mounted — good"
|
||
```
|
||
|
||
If the desktop auto-mounted it, unmount it first (`udisksctl unmount -b
|
||
/dev/disk/by-label/BACKUP`) — the VM must have the drive to itself.
|
||
|
||
### 4. Boot the ISO with the backup drive passed through
|
||
|
||
Use the script — the long QEMU line is fragile to paste (a paste once
|
||
broke after `qemu-system-x86_64`, which then booted an empty VM:
|
||
"No bootable device", then an iPXE prompt):
|
||
|
||
```bash
|
||
scripts/chkdsk-vm.sh # from the repo root; uses the newest vm/Win*11*.iso
|
||
scripts/chkdsk-vm.sh path/to/Win11.iso # or an explicit ISO
|
||
```
|
||
|
||
Run it **as yourself, not with `sudo`** — root cannot open a window on the
|
||
Wayland desktop (`Authorization required, but no authorization protocol
|
||
specified`). It refuses if a QEMU is already running, the backup drive is not plugged in,
|
||
or the backup drive is mounted on the PC; asks for sudo once to `setfacl` the backup drive's USB
|
||
node (`/dev/kvm` needs nothing); boots **UEFI** (OVMF, package `edk2-ovmf`,
|
||
q35) with the ISO and the backup drive, no network. The VM's UEFI variable store is kept
|
||
in `vm/OVMF_VARS.4m.fd`.
|
||
|
||
What you see:
|
||
|
||
1. TianoCore logo, then **"Press any key to boot from CD or DVD…"** for ~5 s
|
||
— press Enter repeatedly from the moment the window opens.
|
||
2. Missed it → a boot menu (*EFI Firmware Setup / UEFI QEMU DVD-ROM / EFI
|
||
Internal Shell*): pick **UEFI QEMU DVD-ROM**, Enter, and press Enter
|
||
repeatedly again. At a `Shell>` prompt instead: type `exit`.
|
||
3. Windows logo, then (1–2 min, 9 GB ISO) the Setup language screen.
|
||
|
||
Stop QEMU (only when chkdsk is **not** running): close the window, Ctrl+C in
|
||
its terminal, or `pkill -x qemu-system-x86_64`. Check `pgrep -a qemu`; the
|
||
args after `qemu-system-x86_64` show what it was really started with.
|
||
|
||
### 5. chkdsk from the Setup command prompt
|
||
|
||
At the first Setup screen (language) press **Shift+F10**:
|
||
|
||
```
|
||
diskpart
|
||
list volume ← the backup drive: ~1863 GB, NTFS, label "BACKUP" — note its letter
|
||
exit
|
||
chkdsk X: /f ← X = that letter
|
||
```
|
||
|
||
Answer **Y** if it offers to convert lost chains to files. `/f` checks
|
||
metadata, not every sector: minutes to ~1 h over USB passthrough, and the
|
||
percentage stalls then jumps (stage 2, indexes). **Never close the window
|
||
mid-run.** Wait for the summary; photograph or note it (record in the [log](#incident-log)).
|
||
A good finish reads `… No further action is required` and `0 KB in bad
|
||
sectors`. `Unable to obtain a handle to the event log` is harmless — Setup has
|
||
no event log service, so chkdsk cannot file its report there.
|
||
|
||
If `list volume` shows no ~1863 GB volume, the passthrough failed — check
|
||
`lsusb` IDs on the host, the `setfacl` grant, and that nothing on the host
|
||
mounted it.
|
||
|
||
Then close the QEMU window (or `wpeutil shutdown`). The backup drive was never mounted on
|
||
the PC, so unplugging is safe.
|
||
|
||
### 6. Back on mithlond
|
||
|
||
One at a time; stop and investigate if any fails:
|
||
|
||
```bash
|
||
sudo gitea-backup check # "no errors were found"
|
||
sudo gitea-backup restore-test # "RESTORE TEST PASSED"
|
||
sudo gitea-backup snapshots # a snapshot from a run cut off mid-write may be missing or flagged
|
||
```
|
||
|
||
If `check` blames one snapshot (e.g. the one written at unplug time), drop it
|
||
and prune: `sudo sh -c '. /etc/gitea-backup/env; export RESTIC_REPOSITORY
|
||
RESTIC_PASSWORD_FILE; mount -t ntfs3 /dev/disk/by-label/BACKUP /mnt/backup &&
|
||
restic forget <ID> --prune; cd /; umount /mnt/backup'`, then `check` again.
|
||
|
||
Also open a few of your non-backup files on the backup drive (from the PC) to confirm
|
||
they survived.
|
||
|
||
## Avoiding a repeat
|
||
|
||
- **Never unplug the backup drive while it is mounted.** `findmnt /mnt/backup` must print
|
||
nothing. The nightly run mounts it only ~1–2 min around 03:30.
|
||
- To test the failure mail, unplug **before** starting a run, never during.
|
||
- Eject from Windows/the PC safely too; Windows Fast Startup leaves the flag.
|
||
- Longer term: a small **ext4 partition on the backup drive** for the restic repo would
|
||
make this repairable on the server with `fsck`, no Windows. `ntfsresize`
|
||
(`ntfsprogs`) / GParted can shrink NTFS from Linux — but only once the
|
||
volume is consistent, i.e. after a clean rung 3.
|
||
|
||
## Incident log
|
||
|
||
| Date | What happened | Rung | Outcome |
|
||
|---|---|---|---|
|
||
| YYYY-MM-DD | e.g. drive pulled mid-run; `$MFTMirr` mismatch | 2 | e.g. `ntfsfix -d`, `check` clean |
|