Skip to content

Commit 753acbb

Browse files
committed
Support a system partition that is initially mounted read-write
In mininit-syspart, if the system partition was initially mounted read-write and no update was performed, it would still be read-write when starting the rootfs init. For consistency with mininit-initramfs, we will remount it read-only. If the system partition was initially mounted read-write, the remount before updates can be skipped. Elevate the update log messages from DEBUG to INFO level, so they will show up in the log by default.
1 parent 9d3be92 commit 753acbb

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

mininit.c

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,43 +42,50 @@ int create_mount_point(const char *path)
4242

4343
void perform_updates(bool is_backup)
4444
{
45+
bool boot_ro = access(".", W_OK) && errno == EROFS;
4546
bool update_modules = !access("update_m.bin", R_OK);
4647
bool update_rootfs = !access("update_r.bin", R_OK);
47-
if (!update_modules && !update_rootfs) return;
4848

49-
DEBUG("Remounting boot device read-write\n");
50-
if (mount(NULL, ".", NULL, MS_REMOUNT | MS_NOATIME, NULL)) {
51-
ERROR("Unable to remount boot device read-write: %d\n", errno);
52-
return;
53-
}
49+
if (update_modules || update_rootfs) {
50+
if (boot_ro) {
51+
INFO("remounting boot device read-write\n");
52+
if (mount(NULL, ".", NULL, MS_REMOUNT | MS_NOATIME, NULL)) {
53+
ERROR("Unable to remount boot device read-write: %d\n", errno);
54+
return;
55+
}
56+
boot_ro = false;
57+
}
5458

55-
if (update_modules) {
56-
DEBUG("Modules update found\n");
57-
rename("modules.squashfs", "modules.squashfs.bak");
58-
rename("modules.squashfs.sha1", "modules.squashfs.bak.sha1");
59-
rename("update_m.bin", "modules.squashfs");
60-
rename("update_m.bin.sha1", "modules.squashfs.sha1");
61-
}
59+
if (update_modules) {
60+
INFO("performing modules update\n");
61+
rename("modules.squashfs", "modules.squashfs.bak");
62+
rename("modules.squashfs.sha1", "modules.squashfs.bak.sha1");
63+
rename("update_m.bin", "modules.squashfs");
64+
rename("update_m.bin.sha1", "modules.squashfs.sha1");
65+
}
6266

63-
if (update_rootfs) {
64-
DEBUG("RootFS update found\n");
67+
if (update_rootfs) {
68+
INFO("performing rootfs update\n");
6569

66-
/* If rootfs_bak was not passed, or the backup is not available,
67-
* make a backup of the current rootfs before the update */
68-
if (!is_backup || access(ROOTFS_BACKUP, F_OK)) {
69-
rename(ROOTFS_CURRENT, ROOTFS_BACKUP);
70-
rename(ROOTFS_CURRENT ".sha1", ROOTFS_BACKUP ".sha1");
70+
/* If rootfs_bak was not passed, or the backup is not available,
71+
* make a backup of the current rootfs before the update */
72+
if (!is_backup || access(ROOTFS_BACKUP, F_OK)) {
73+
rename(ROOTFS_CURRENT, ROOTFS_BACKUP);
74+
rename(ROOTFS_CURRENT ".sha1", ROOTFS_BACKUP ".sha1");
75+
}
76+
77+
rename(ROOTFS_UPDATE, ROOTFS_CURRENT);
78+
rename(ROOTFS_UPDATE ".sha1", ROOTFS_CURRENT ".sha1");
7179
}
7280

73-
rename(ROOTFS_UPDATE, ROOTFS_CURRENT);
74-
rename(ROOTFS_UPDATE ".sha1", ROOTFS_CURRENT ".sha1");
81+
sync();
7582
}
7683

77-
sync();
78-
79-
DEBUG("Remounting boot device read-only\n");
80-
if (mount(NULL, ".", NULL, MS_REMOUNT | MS_RDONLY, NULL)) {
81-
ERROR("Unable to remount boot device read-only: %d\n", errno);
84+
if (!boot_ro) {
85+
INFO("remounting boot device read-only\n");
86+
if (mount(NULL, ".", NULL, MS_REMOUNT | MS_RDONLY, NULL)) {
87+
ERROR("Unable to remount boot device read-only: %d\n", errno);
88+
}
8289
}
8390
}
8491

0 commit comments

Comments
 (0)