Fix stage_env admission for mounted unjournaled ext4; release 1.0.2
This commit is contained in:
parent
025cf13214
commit
c957b4fef7
|
|
@ -2,7 +2,7 @@
|
||||||
"schema": 1,
|
"schema": 1,
|
||||||
"stages": {
|
"stages": {
|
||||||
"stage_env": {
|
"stage_env": {
|
||||||
"version": "1.0.1"
|
"version": "1.0.2"
|
||||||
},
|
},
|
||||||
"stage_wifi": {
|
"stage_wifi": {
|
||||||
"version": "1.0.0"
|
"version": "1.0.0"
|
||||||
|
|
|
||||||
|
|
@ -83,3 +83,25 @@ Example shape:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Stage release 1.0.2: mounted unjournaled ext4
|
||||||
|
|
||||||
|
The admission rule above is refined for a verified read-write ext4 mount of
|
||||||
|
exactly the userdata block device. Linux clears `EXT4_VALID_FS` while mounting
|
||||||
|
an unjournaled filesystem read-write, so `Filesystem state: not clean` alone
|
||||||
|
on this live mount does not establish corruption. See the kernel's
|
||||||
|
[ext4_setup_super implementation](https://kernel.googlesource.com/pub/scm/linux/kernel/git/stable/linux-stable.git/+/refs/tags/v5.10.247/fs/ext4/super.c#2704).
|
||||||
|
|
||||||
|
For this case only, with no recorded filesystem errors, stage_env now arms the
|
||||||
|
existing offline preparation and reports protection pending reboot. It does
|
||||||
|
not claim that the live filesystem has passed an integrity check. An absent
|
||||||
|
`FS Error count` field is accepted as before: tune2fs can omit this field when
|
||||||
|
no errors are recorded; it is not presented as measured health telemetry.
|
||||||
|
Read-only mounts, explicit errors, missing metadata, mismatched devices and
|
||||||
|
unclean unmounted filesystems remain blocked.
|
||||||
|
|
||||||
|
The boot helper still refuses mounted devices and requires `e2fsck -pf` to
|
||||||
|
return 0 or 1 before creating a journal. Other exit codes preserve the marker,
|
||||||
|
stop migration and prevent the dependent userdata mount from proceeding.
|
||||||
|
Journal protection becomes active only after successful offline preparation;
|
||||||
|
installation success alone does not verify physical storage health.
|
||||||
|
|
|
||||||
|
|
@ -452,7 +452,7 @@ ensure_userdata_fstab_policy() {
|
||||||
}
|
}
|
||||||
|
|
||||||
configure_userdata_storage_contract() {
|
configure_userdata_storage_contract() {
|
||||||
local device features state error_count metadata mount_options
|
local device features state error_count metadata mount_options mount_source mount_type
|
||||||
local marker="/var/lib/iomatex/storage/enable-userdata-journal"
|
local marker="/var/lib/iomatex/storage/enable-userdata-journal"
|
||||||
|
|
||||||
install -D -m 0755 -o root -g root \
|
install -D -m 0755 -o root -g root \
|
||||||
|
|
@ -500,6 +500,7 @@ EOF
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! metadata="$(tune2fs -l "$device" 2>/dev/null)"; then
|
if ! metadata="$(tune2fs -l "$device" 2>/dev/null)"; then
|
||||||
|
rm -f "$marker"
|
||||||
log "ERROR: cannot inspect /userdata filesystem; storage protection is not verified"
|
log "ERROR: cannot inspect /userdata filesystem; storage protection is not verified"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
@ -511,17 +512,30 @@ EOF
|
||||||
log "ERROR: /userdata requires offline backup and repair (state=${state:-unknown}, errors=${error_count:-unknown}, mount=${mount_options:-unmounted}); stage_env cannot complete"
|
log "ERROR: /userdata requires offline backup and repair (state=${state:-unknown}, errors=${error_count:-unknown}, mount=${mount_options:-unmounted}); stage_env cannot complete"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
features="$(printf '%s\n' "$metadata" | awk -F: '/^Filesystem features:/{print $2}')"
|
features="$(printf '%s\n' "$metadata" | awk -F: '/^Filesystem features:/{sub(/^[[:space:]]+/, "", $2); print $2}')"
|
||||||
|
if [ -z "$features" ] || [[ "$state" != "clean" && "$state" != "not clean" ]]; then
|
||||||
|
rm -f "$marker"
|
||||||
|
log "ERROR: incomplete or unsupported /userdata metadata; journal migration was not armed"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
if printf '%s\n' "$features" | grep -qw has_journal; then
|
if printf '%s\n' "$features" | grep -qw has_journal; then
|
||||||
rm -f "$marker"
|
rm -f "$marker"
|
||||||
log "Userdata ext4 journal already enabled"
|
log "Userdata ext4 journal already enabled"
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$state" = "clean" ] && [ "${error_count:-0}" = "0" ]; then
|
# Without a journal, ext4 clears EXT4_VALID_FS while mounted read-write.
|
||||||
|
# This is not proof of corruption. Only queue the existing OFFLINE check;
|
||||||
|
# never run e2fsck or create a journal on the mounted filesystem.
|
||||||
|
mount_source="$(findmnt -n -o SOURCE --mountpoint /userdata 2>/dev/null || true)"
|
||||||
|
mount_source="$(readlink -f "$mount_source" 2>/dev/null || true)"
|
||||||
|
mount_type="$(findmnt -n -o FSTYPE --mountpoint /userdata 2>/dev/null || true)"
|
||||||
|
if [ "$state" = "clean" ] || { [ "$state" = "not clean" ] \
|
||||||
|
&& [[ ",$mount_options," == *,rw,* ]] \
|
||||||
|
&& [ "$mount_source" = "$device" ] && [ "$mount_type" = "ext4" ]; }; then
|
||||||
: >"$marker"
|
: >"$marker"
|
||||||
chmod 0600 "$marker"
|
chmod 0600 "$marker"
|
||||||
log "Clean unjournaled /userdata detected; safe offline journal migration armed for next boot"
|
log "Unjournaled /userdata (state=$state, mount=${mount_options:-unmounted}); offline filesystem check and journal migration armed for next boot"
|
||||||
log "Storage journal protection is pending: reboot is required to activate it"
|
log "Storage journal protection is pending: reboot is required to activate it"
|
||||||
else
|
else
|
||||||
rm -f "$marker"
|
rm -f "$marker"
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,7 @@ class FilesystemHealthTest(unittest.TestCase):
|
||||||
|
|
||||||
|
|
||||||
class StorageInstallerTest(unittest.TestCase):
|
class StorageInstallerTest(unittest.TestCase):
|
||||||
def run_contract(self, features, state, options, errors="0"):
|
def run_contract(self, features, state, options, errors="0", mount_source="/dev/test", mount_type="ext4"):
|
||||||
source = MODULE_PATH.with_name("stage_env_install_no_pulse.sh").read_text()
|
source = MODULE_PATH.with_name("stage_env_install_no_pulse.sh").read_text()
|
||||||
body = "configure_userdata_storage_contract() {" + source.split(
|
body = "configure_userdata_storage_contract() {" + source.split(
|
||||||
"configure_userdata_storage_contract() {", 1)[1].split(
|
"configure_userdata_storage_contract() {", 1)[1].split(
|
||||||
|
|
@ -70,15 +70,16 @@ SCRIPT_DIR=/unused
|
||||||
log() { echo "$*"; }
|
log() { echo "$*"; }
|
||||||
install() { :; }
|
install() { :; }
|
||||||
write_if_changed() { cat >/dev/null; }
|
write_if_changed() { cat >/dev/null; }
|
||||||
readlink() { echo /dev/test; }
|
readlink() { if [[ "$2" == /dev/disk/by-partlabel/userdata ]]; then echo /dev/test; else printf '%s\\n' "$2"; fi; }
|
||||||
[() { if [[ "$1" == '-b' ]]; then return 0; fi; if [[ "$1 $2" == '! -b' ]]; then return 1; fi; builtin [ "$@"; }
|
[() { if [[ "$1" == '-b' ]]; then return 0; fi; if [[ "$1 $2" == '! -b' ]]; then return 1; fi; builtin [ "$@"; }
|
||||||
tune2fs() { printf 'Filesystem features: %s\\nFilesystem state: %s\\nFS Error count: %s\\n' "$FEATURES" "$STATE" "$ERRORS"; }
|
tune2fs() { printf 'Filesystem features: %s\\nFilesystem state: %s\\nFS Error count: %s\\n' "$FEATURES" "$STATE" "$ERRORS"; }
|
||||||
findmnt() { echo "$OPTIONS"; }
|
findmnt() { case "$3" in OPTIONS) echo "$OPTIONS";; SOURCE) echo "$MOUNT_SOURCE";; FSTYPE) echo "$MOUNT_TYPE";; *) return 1;; esac; }
|
||||||
""" + body + "\nconfigure_userdata_storage_contract\n"
|
""" + body + "\nconfigure_userdata_storage_contract\n"
|
||||||
import os
|
import os
|
||||||
result = subprocess.run(["bash", "-c", script], text=True, capture_output=True,
|
result = subprocess.run(["bash", "-c", script], text=True, capture_output=True,
|
||||||
env={**os.environ, "FEATURES": features, "STATE": state,
|
env={**os.environ, "FEATURES": features, "STATE": state,
|
||||||
"OPTIONS": options, "ERRORS": errors})
|
"OPTIONS": options, "ERRORS": errors,
|
||||||
|
"MOUNT_SOURCE": mount_source, "MOUNT_TYPE": mount_type})
|
||||||
marker = pathlib.Path(directory + "/var/lib/iomatex/storage/enable-userdata-journal")
|
marker = pathlib.Path(directory + "/var/lib/iomatex/storage/enable-userdata-journal")
|
||||||
return result.returncode, marker.exists(), result.stdout + result.stderr
|
return result.returncode, marker.exists(), result.stdout + result.stderr
|
||||||
|
|
||||||
|
|
@ -101,10 +102,98 @@ findmnt() { echo "$OPTIONS"; }
|
||||||
self.assertNotEqual(0, rc, output)
|
self.assertNotEqual(0, rc, output)
|
||||||
self.assertFalse(armed)
|
self.assertFalse(armed)
|
||||||
|
|
||||||
def test_unclean_unjournaled_disk_does_not_report_success(self):
|
def test_mounted_unjournaled_disk_queues_offline_check(self):
|
||||||
rc, armed, output = self.run_contract("extent", "not clean", "rw")
|
# tune2fs omits FS Error count on some devices with no recorded errors.
|
||||||
|
for errors in ("0", ""):
|
||||||
|
rc, armed, output = self.run_contract("extent", "not clean", "rw,relatime", errors)
|
||||||
|
self.assertEqual(0, rc, output)
|
||||||
|
self.assertTrue(armed)
|
||||||
|
self.assertIn("pending", output)
|
||||||
|
|
||||||
|
def test_unclean_disk_requires_verified_rw_ext4_mount_of_same_device(self):
|
||||||
|
for options, source, fs_type in [("", "", ""), ("ro", "/dev/test", "ext4"),
|
||||||
|
("rw", "/dev/other", "ext4"), ("rw", "/dev/test", "ext2")]:
|
||||||
|
with self.subTest(options=options, source=source, fs_type=fs_type):
|
||||||
|
rc, armed, output = self.run_contract("extent", "not clean", options,
|
||||||
|
mount_source=source, mount_type=fs_type)
|
||||||
|
self.assertNotEqual(0, rc, output)
|
||||||
|
self.assertFalse(armed)
|
||||||
|
|
||||||
|
def test_mounted_unjournaled_disk_with_errors_still_fails(self):
|
||||||
|
for state, errors in [("not clean with errors", "0"), ("not clean", "4")]:
|
||||||
|
rc, armed, output = self.run_contract("extent", state, "rw", errors)
|
||||||
|
self.assertNotEqual(0, rc, output)
|
||||||
|
self.assertFalse(armed)
|
||||||
|
|
||||||
|
def test_incomplete_metadata_does_not_arm_migration(self):
|
||||||
|
for features, state in [("", "clean"), ("extent", ""), ("has_journal", "unknown")]:
|
||||||
|
rc, armed, output = self.run_contract(features, state, "rw")
|
||||||
|
self.assertNotEqual(0, rc, output)
|
||||||
|
self.assertFalse(armed)
|
||||||
|
|
||||||
|
|
||||||
|
class OfflinePreparationTest(unittest.TestCase):
|
||||||
|
def run_preparation(self, fsck_rc=0, mounted=False):
|
||||||
|
import os
|
||||||
|
body = MODULE_PATH.with_name("iomatex-userdata-prepare").read_text()
|
||||||
|
with tempfile.TemporaryDirectory() as directory:
|
||||||
|
root = pathlib.Path(directory)
|
||||||
|
marker = root / "marker"
|
||||||
|
marker.touch()
|
||||||
|
fstab = root / "fstab"
|
||||||
|
fstab.write_text("/dev/test /userdata ext4 defaults 0 0\n")
|
||||||
|
events = root / "events"
|
||||||
|
body = body.replace('/var/lib/iomatex/storage/enable-userdata-journal', str(marker))
|
||||||
|
body = body.replace('/etc/fstab', str(fstab))
|
||||||
|
mocks = r'''
|
||||||
|
[() { if [[ "$1" == '-b' ]]; then return 0; fi; builtin [ "$@"; }
|
||||||
|
readlink() { echo /dev/test; }
|
||||||
|
findmnt() { [[ "$MOUNTED" == 1 ]]; }
|
||||||
|
mountpoint() { [[ "$MOUNTED" == 1 ]]; }
|
||||||
|
blkid() { echo ext4; }
|
||||||
|
e2fsck() { echo fsck >>"$EVENTS"; return "$FSCK_RC"; }
|
||||||
|
tune2fs() {
|
||||||
|
if [[ "$1" == '-j' ]]; then
|
||||||
|
echo journal >>"$EVENTS"
|
||||||
|
elif [[ -f "$EVENTS" ]] && grep -qx journal "$EVENTS"; then
|
||||||
|
echo 'Filesystem features: has_journal extent'
|
||||||
|
else
|
||||||
|
echo 'Filesystem features: extent'
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
sync() { :; }
|
||||||
|
install() { cp "${@: -2:1}" "${@: -1}"; }
|
||||||
|
'''
|
||||||
|
result = subprocess.run(["bash", "-c", mocks + body], text=True, capture_output=True,
|
||||||
|
env={**os.environ, "FSCK_RC": str(fsck_rc),
|
||||||
|
"MOUNTED": "1" if mounted else "0", "EVENTS": str(events)})
|
||||||
|
return (result.returncode, marker.exists(),
|
||||||
|
events.read_text().splitlines() if events.exists() else [],
|
||||||
|
fstab.read_text(), result.stdout + result.stderr)
|
||||||
|
|
||||||
|
def test_mounted_filesystem_is_never_repaired(self):
|
||||||
|
rc, marker, events, fstab, output = self.run_preparation(mounted=True)
|
||||||
self.assertNotEqual(0, rc, output)
|
self.assertNotEqual(0, rc, output)
|
||||||
self.assertFalse(armed)
|
self.assertTrue(marker)
|
||||||
|
self.assertEqual([], events)
|
||||||
|
self.assertTrue(fstab.rstrip().endswith("0 0"))
|
||||||
|
|
||||||
|
def test_failed_or_reboot_required_fsck_stops_before_journal_creation(self):
|
||||||
|
for fsck_rc in (2, 4, 8, 16):
|
||||||
|
with self.subTest(fsck_rc=fsck_rc):
|
||||||
|
rc, marker, events, fstab, output = self.run_preparation(fsck_rc)
|
||||||
|
self.assertNotEqual(0, rc, output)
|
||||||
|
self.assertTrue(marker)
|
||||||
|
self.assertEqual(["fsck"], events)
|
||||||
|
self.assertTrue(fstab.rstrip().endswith("0 0"))
|
||||||
|
|
||||||
|
def test_successful_offline_check_creates_journal_and_enables_boot_checks(self):
|
||||||
|
for fsck_rc in (0, 1):
|
||||||
|
rc, marker, events, fstab, output = self.run_preparation(fsck_rc)
|
||||||
|
self.assertEqual(0, rc, output)
|
||||||
|
self.assertFalse(marker)
|
||||||
|
self.assertEqual(["fsck", "journal"], events)
|
||||||
|
self.assertTrue(fstab.rstrip().endswith("0 2"))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,3 @@
|
||||||
{
|
{
|
||||||
"version": "1.0.1"
|
"version": "1.0.2"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue