Skip to content

Commit 951d5d7

Browse files
committed
boot: Add systemd unit to cleanup sysroot automatically
This unit makes use of the `--auto` option of `ostree admin cleanup` to cleanup the sysroot when the `/sysroot/.cleanup` file is left behind when finalizing a staged deployment. The purpose of this service is to restore pruning of deleted deployments when a staged deployment is written out during shutdown of the previous boot. The unit is optional as the cleanup can be run at another time or not at all. Cleaning the sysroot will prune the repo, and this can be a slow and IO intensive operation. To keep the system from blocking, the default `Before` dependency on `multi-user.target` has been removed. Since the service will then run in the background, the IO scheduling class has been lowered to `idle` to keep the system's primary tasks more responsive.
1 parent 85dc35d commit 951d5d7

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

Makefile-boot.am

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ systemdsystemunit_DATA = src/boot/ostree-prepare-root.service \
4040
src/boot/ostree-remount.service \
4141
src/boot/ostree-finalize-staged.service \
4242
src/boot/ostree-finalize-staged.path \
43+
src/boot/ostree-auto-cleanup.service \
4344
$(NULL)
4445
systemdtmpfilesdir = $(prefix)/lib/tmpfiles.d
4546
dist_systemdtmpfiles_DATA = src/boot/ostree-tmpfiles.conf
@@ -68,6 +69,7 @@ EXTRA_DIST += src/boot/dracut/module-setup.sh \
6869
src/boot/ostree-finalize-staged.path \
6970
src/boot/ostree-remount.service \
7071
src/boot/ostree-finalize-staged.service \
72+
src/boot/ostree-auto-cleanup.service \
7173
src/boot/grub2/grub2-15_ostree \
7274
src/boot/grub2/ostree-grub-generator \
7375
$(NULL)

src/boot/ostree-auto-cleanup.service

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright © 2022 Endless OS Foundation LLC
2+
#
3+
# This library is free software; you can redistribute it and/or
4+
# modify it under the terms of the GNU Lesser General Public
5+
# License as published by the Free Software Foundation; either
6+
# version 2 of the License, or (at your option) any later version.
7+
#
8+
# This library is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
# Lesser General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU Lesser General Public
14+
# License along with this library. If not, see <https://www.gnu.org/licenses/>.
15+
16+
[Unit]
17+
Description=OSTree Automatic Sysroot Cleanup
18+
Documentation=man:ostree-admin-cleanup(1)
19+
ConditionPathExists=/sysroot/.cleanup
20+
21+
# We want this to be triggered by multi-user.target but not block it via
22+
# the default After added to target units since pruning the repo can be
23+
# slow. See the Default Dependencies sections in systemd.service(5) and
24+
# systemd.target(5).
25+
DefaultDependencies=no
26+
Requires=sysinit.target
27+
After=sysinit.target basic.target
28+
Conflicts=shutdown.target
29+
Before=shutdown.target
30+
31+
[Service]
32+
Type=oneshot
33+
RemainAfterExit=yes
34+
ExecStop=/usr/bin/ostree admin cleanup --auto
35+
ProtectSystem=strict
36+
ReadWritePaths=/sysroot /boot
37+
38+
# This will be allowed to run in the background, so try to make it less
39+
# disruptive while it prunes the repo.
40+
IOSchedulingClass=idle
41+
42+
[Install]
43+
WantedBy=multi-user.target

src/libostree/ostree-impl-system-generator.c

+2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ require_internal_units (const char *normal_dir,
134134
return FALSE;
135135
if (symlinkat (SYSTEM_DATA_UNIT_PATH "/ostree-finalize-staged.path", normal_dir_dfd, "multi-user.target.wants/ostree-finalize-staged.path") < 0)
136136
return glnx_throw_errno_prefix (error, "symlinkat");
137+
if (symlinkat (SYSTEM_DATA_UNIT_PATH "/ostree-auto-cleanup.service", normal_dir_dfd, "multi-user.target.wants/ostree-auto-cleanup.service") < 0)
138+
return glnx_throw_errno_prefix (error, "symlinkat");
137139

138140
return TRUE;
139141
#else

tests/kolainst/destructive/staged-deploy.sh

+7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ case "${AUTOPKGTEST_REBOOT_MARK:-}" in
1010
"")
1111
# Test our generator
1212
test -f /run/systemd/generator/multi-user.target.wants/ostree-finalize-staged.path
13+
test -f /run/systemd/generator/multi-user.target.wants/ostree-auto-cleanup.service
1314
test -f /run/systemd/generator/local-fs.target.requires/ostree-remount.service
1415

1516
cat >/etc/systemd/system/sock-to-ignore.socket << 'EOF'
@@ -77,6 +78,12 @@ EOF
7778
rm -f svc.txt
7879
# And there should not be a staged deployment
7980
test '!' -f /run/ostree/staged-deployment
81+
# Check that auto cleanup ran
82+
assert_not_has_file /sysroot/.cleanup
83+
journalctl -b 0 -u ostree-auto-cleanup.service > svc.txt
84+
assert_file_has_content svc.txt 'Starting ostree-auto-cleanup.service'
85+
assert_not_file_has_content svc.txt 'No cleanup needed.'
86+
rm -f svc.txt
8087

8188
# Upgrade with staging
8289
test '!' -f /run/ostree/staged-deployment

0 commit comments

Comments
 (0)