X Tutup
Skip to content

Commit 8ea48df

Browse files
committed
update-done: add minimal tool to manage system updates for /etc and /var, if /usr has changed
In order to support offline updates to /usr, we need to be able to run certain tasks on next boot-up to bring /etc and /var in line with the updated /usr. Hence, let's devise a mechanism how we can detect whether /etc or /var are not up-to-date with /usr anymore: we keep "touch files" in /etc/.updated and /var/.updated that are mtime-compared with /usr. This means: Whenever the vendor OS tree in /usr is updated, and any services that shall be executed at next boot shall be triggered, it is sufficient to update the mtime of /usr itself. At next boot, if /etc/.updated and/or /var/.updated is older than than /usr (or missing), we know we have to run the update tools once. After that is completed we need to update the mtime of these files to the one of /usr, to keep track that we made the necessary updates, and won't repeat them on next reboot. A subsequent commit adds a new ConditionNeedsUpdate= condition that allows checking on boot whether /etc or /var are outdated and need updating. This is an early step to allow booting up with an empty /etc, with automatic rebuilding of the necessary cache files or user databases therein, as well as supporting later updates of /usr that then propagate to /etc and /var again.
1 parent dc92e62 commit 8ea48df

File tree

6 files changed

+148
-4
lines changed

6 files changed

+148
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
/systemd-tty-ask-password-agent
110110
/systemd-uaccess
111111
/systemd-udevd
112+
/systemd-update-done
112113
/systemd-update-utmp
113114
/systemd-user-sessions
114115
/systemd-vconsole-setup

Makefile.am

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,8 @@ rootlibexec_PROGRAMS = \
361361
systemd-sysctl \
362362
systemd-sleep \
363363
systemd-bus-proxyd \
364-
systemd-socket-proxyd
364+
systemd-socket-proxyd \
365+
systemd-update-done
365366

366367
systemgenerator_PROGRAMS = \
367368
systemd-getty-generator \
@@ -495,7 +496,8 @@ nodist_systemunit_DATA = \
495496
units/initrd-cleanup.service \
496497
units/initrd-udevadm-cleanup-db.service \
497498
units/initrd-switch-root.service \
498-
units/systemd-nspawn@.service
499+
units/systemd-nspawn@.service \
500+
units/systemd-update-done.service
499501

500502
dist_userunit_DATA = \
501503
units/user/basic.target \
@@ -538,7 +540,8 @@ EXTRA_DIST += \
538540
units/initrd-cleanup.service.in \
539541
units/initrd-udevadm-cleanup-db.service.in \
540542
units/initrd-switch-root.service.in \
541-
units/systemd-nspawn@.service.in
543+
units/systemd-nspawn@.service.in \
544+
units/systemd-update-done.service.in
542545

543546
CLEANFILES += \
544547
units/console-shell.service.m4 \
@@ -1640,6 +1643,14 @@ systemd_update_utmp_LDADD = \
16401643
libsystemd-shared.la \
16411644
$(AUDIT_LIBS)
16421645

1646+
# ------------------------------------------------------------------------------
1647+
systemd_update_done_SOURCES = \
1648+
src/update-done/update-done.c
1649+
1650+
systemd_update_done_LDADD = \
1651+
libsystemd-internal.la \
1652+
libsystemd-shared.la
1653+
16431654
# ------------------------------------------------------------------------------
16441655
systemd_shutdownd_SOURCES = \
16451656
src/shutdownd/shutdownd.c
@@ -5100,14 +5111,19 @@ RUNLEVEL4_TARGET_WANTS += \
51005111
RUNLEVEL5_TARGET_WANTS += \
51015112
systemd-update-utmp-runlevel.service
51025113
endif
5114+
51035115
SYSINIT_TARGET_WANTS += \
5104-
systemd-update-utmp.service
5116+
systemd-update-utmp.service \
5117+
systemd-update-done.service
5118+
51055119
LOCAL_FS_TARGET_WANTS += \
51065120
systemd-remount-fs.service \
51075121
tmp.mount
5122+
51085123
MULTI_USER_TARGET_WANTS += \
51095124
getty.target \
51105125
systemd-ask-password-wall.path
5126+
51115127
SYSINIT_TARGET_WANTS += \
51125128
dev-hugepages.mount \
51135129
dev-mqueue.mount \

src/update-done/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../Makefile

src/update-done/update-done.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2+
3+
/***
4+
This file is part of systemd.
5+
6+
Copyright 2014 Lennart Poettering
7+
8+
systemd is free software; you can redistribute it and/or modify it
9+
under the terms of the GNU Lesser General Public License as published by
10+
the Free Software Foundation; either version 2.1 of the License, or
11+
(at your option) any later version.
12+
13+
systemd is distributed in the hope that it will be useful, but
14+
WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
Lesser General Public License for more details.
17+
18+
You should have received a copy of the GNU Lesser General Public License
19+
along with systemd; If not, see <http://www.gnu.org/licenses/>.
20+
***/
21+
22+
#include "util.h"
23+
24+
static int apply_timestamp(const char *path, struct timespec *ts) {
25+
struct timespec twice[2];
26+
struct stat st;
27+
28+
assert(path);
29+
assert(ts);
30+
31+
if (stat(path, &st) >= 0) {
32+
/* Is the timestamp file already newer than the OS? If so, there's nothing to do. */
33+
if (st.st_mtim.tv_sec > ts->tv_sec ||
34+
(st.st_mtim.tv_sec == ts->tv_sec && st.st_mtim.tv_nsec >= ts->tv_nsec))
35+
return 0;
36+
37+
/* It is older? Then let's update it */
38+
twice[0] = *ts;
39+
twice[1] = *ts;
40+
41+
if (utimensat(AT_FDCWD, path, twice, AT_SYMLINK_NOFOLLOW) < 0) {
42+
43+
if (errno == EROFS) {
44+
log_debug("Can't update timestamp file %s, file system is read-only.", path);
45+
return 0;
46+
}
47+
48+
log_error("Failed to update timestamp on %s: %m", path);
49+
return -errno;
50+
}
51+
52+
} else if (errno == ENOENT) {
53+
_cleanup_close_ int fd = -1;
54+
55+
/* The timestamp file doesn't exist yet? Then let's create it. */
56+
57+
fd = open(path, O_CREAT|O_EXCL|O_WRONLY|O_TRUNC|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0644);
58+
if (fd < 0) {
59+
60+
if (errno == EROFS) {
61+
log_debug("Can't create timestamp file %s, file system is read-only.", path);
62+
return 0;
63+
}
64+
65+
log_error("Failed to create timestamp file %s: %m", path);
66+
return -errno;
67+
}
68+
69+
twice[0] = *ts;
70+
twice[1] = *ts;
71+
72+
if (futimens(fd, twice) < 0) {
73+
log_error("Failed to update timestamp on %s: %m", path);
74+
return -errno;
75+
}
76+
} else {
77+
log_error("Failed to stat() timestamp file %s: %m", path);
78+
return -errno;
79+
}
80+
81+
return 0;
82+
}
83+
84+
int main(int argc, char *argv[]) {
85+
struct stat st;
86+
int r, q;
87+
88+
log_set_target(LOG_TARGET_AUTO);
89+
log_parse_environment();
90+
log_open();
91+
92+
if (stat("/usr", &st) < 0) {
93+
log_error("Failed to stat /usr: %m");
94+
return EXIT_FAILURE;
95+
}
96+
97+
r = apply_timestamp("/etc/.updated", &st.st_mtim);
98+
99+
q = apply_timestamp("/var/.updated", &st.st_mtim);
100+
if (q < 0 && r == 0)
101+
r = q;
102+
103+
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
104+
}

units/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
/systemd-udevd.service
6666
/systemd-update-utmp-runlevel.service
6767
/systemd-update-utmp.service
68+
/systemd-update-done.service
6869
/systemd-user-sessions.service
6970
/systemd-vconsole-setup.service
7071
/user@.service
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# This file is part of systemd.
2+
#
3+
# systemd is free software; you can redistribute it and/or modify it
4+
# under the terms of the GNU Lesser General Public License as published by
5+
# the Free Software Foundation; either version 2.1 of the License, or
6+
# (at your option) any later version.
7+
8+
[Unit]
9+
Description=Update is Completed
10+
Documentation=man:sysusers.d(5) man:systemd-sysusers(8)
11+
DefaultDependencies=no
12+
Conflicts=shutdown.target
13+
After=systemd-readahead-collect.service systemd-readahead-replay.service local-fs.target
14+
Before=sysinit.target shutdown.target
15+
RefuseManualStart=yes
16+
RefuseManualStop=yes
17+
18+
[Service]
19+
Type=oneshot
20+
RemainAfterExit=yes
21+
ExecStart=@rootlibexecdir@/systemd-update-done

0 commit comments

Comments
 (0)
X Tutup