diff mbox series

package/skeleton-systemd: systemd-ify /var on a read-only rootfs

Message ID 30214_1656937450_62C2DBEA_30214_425_1_ebc1b4a9ba79f014485eb6c21d96956d9ebc3f5d.1656937448.git.yann.morin@orange.com
State Changes Requested
Headers show
Series package/skeleton-systemd: systemd-ify /var on a read-only rootfs | expand

Commit Message

Yann E. MORIN July 4, 2022, 12:24 p.m. UTC
From: "Yann E. MORIN" <yann.morin@orange.com>

When the rootfs is not remounted read-write (thus assuming a read-only
rootfs like squashfs), we create a tmpfiles.d factory for /var.

However, we register those in /etc/tmpfiles.d/, but /etc could also be
a tmpfs (for full state-less systems, or easy factory-reset, see [0]).

So, we move our var factory to /usr/lib/tmpfiles.d/, which is also the
lcoation where systemd itself places its own tmpfiles, and where we
already put all our other tmpfiles (see audit, avahi, cryptsetup, dhcp,
lighttpd, nfs-utils, quagga, samba4, swupdate) and our handling of
systemd's catalog files too. We also rename the file to a better name,
so that it is obvious it is generated by us (systemd already installs a
var.conf of its own, so we want to avoid name clashing).

Additionally, since /etc may be empty, we might not have an fstab
available to actually mount /var as a tmpfs. So, we register /var as aa
systemd mount, so that we can also have the /var factory populated and
functional even when /etc is empty. The var.mount unit is heavily
modelled after systemd's own tmp.mount one, so we carry the same license
for that file (in case that may apply). We add an explicit reverse
dependency to systemd-tmpfiles-setup.service, to ensure /var is mounted
before we try to populate it.

Even though we could have split the two changes in two patches, one for
moving the tmpfiles unit away from /etc, and one for adding a systemd
mount unit, the two really make sense together as part of having an
empty /etc, so we made that a single change.

[0] http://0pointer.de/blog/projects/stateless.html

Signed-off-by: Yann E. MORIN <yann.morin@orange.com>
Cc: Norbert Lange <nolange79@gmail.com>
Cc: Romain Naour <romain.naour@smile.fr>
Cc: Jérémy Rosen <jeremy.rosen@smile.fr>
---
 .../skeleton-init-systemd.mk                   |  7 ++++---
 package/skeleton-init-systemd/var.mount        | 18 ++++++++++++++++++
 2 files changed, 22 insertions(+), 3 deletions(-)
 create mode 100644 package/skeleton-init-systemd/var.mount

Comments

Yann E. MORIN Oct. 15, 2022, 9:27 p.m. UTC | #1
Yann, All,

On 2022-07-04 14:24 +0200, yann.morin@orange.com spake thusly:
> From: "Yann E. MORIN" <yann.morin@orange.com>
[--SNIP--]
> Even though we could have split the two changes in two patches, one for
> moving the tmpfiles unit away from /etc, and one for adding a systemd
> mount unit, the two really make sense together as part of having an
> empty /etc, so we made that a single change.

Yet, you should have known it would been a bit more easier to review the
two as separate changes, so this is eaxactly what I did in that series:
    https://lore.kernel.org/buildroot/cover.1665868987.git.yann.morin.1998@free.fr/T/#mea2407e6384ddf35d618c720ed9c6bf8462fcfd2

Regards,
Yann E. MORIN.
diff mbox series

Patch

diff --git a/package/skeleton-init-systemd/skeleton-init-systemd.mk b/package/skeleton-init-systemd/skeleton-init-systemd.mk
index 795a171809..970951d553 100644
--- a/package/skeleton-init-systemd/skeleton-init-systemd.mk
+++ b/package/skeleton-init-systemd/skeleton-init-systemd.mk
@@ -29,15 +29,14 @@  else
 # a real (but empty) directory, and the "factory files" will be copied
 # back there by the tmpfiles.d mechanism.
 define SKELETON_INIT_SYSTEMD_ROOT_RO_OR_RW
-	mkdir -p $(TARGET_DIR)/etc/systemd/tmpfiles.d
 	echo "/dev/root / auto ro 0 1" >$(TARGET_DIR)/etc/fstab
-	echo "tmpfs /var tmpfs mode=1777 0 0" >>$(TARGET_DIR)/etc/fstab
 endef
 
 define SKELETON_INIT_SYSTEMD_PRE_ROOTFS_VAR
 	rm -rf $(TARGET_DIR)/usr/share/factory/var
 	mv $(TARGET_DIR)/var $(TARGET_DIR)/usr/share/factory/var
 	mkdir -p $(TARGET_DIR)/var
+	mkdir -p $(TARGET_DIR)/usr/lib/tmpfiles.d
 	for i in $(TARGET_DIR)/usr/share/factory/var/* \
 		 $(TARGET_DIR)/usr/share/factory/var/lib/* \
 		 $(TARGET_DIR)/usr/share/factory/var/lib/systemd/*; do \
@@ -51,7 +50,9 @@  define SKELETON_INIT_SYSTEMD_PRE_ROOTFS_VAR
 			printf "C! %s - - - -\n" "$${j}" \
 			|| exit 1; \
 		fi; \
-	done >$(TARGET_DIR)/etc/tmpfiles.d/var-factory.conf
+	done >$(TARGET_DIR)/usr/lib/tmpfiles.d/buildroot-factory.conf
+	$(INSTALL) -D -m 0644 $(SKELETON_INIT_SYSTEMD_PKGDIR)/var.mount \
+		$(TARGET_DIR)/usr/lib/systemd/system/var.mount
 endef
 SKELETON_INIT_SYSTEMD_ROOTFS_PRE_CMD_HOOKS += SKELETON_INIT_SYSTEMD_PRE_ROOTFS_VAR
 
diff --git a/package/skeleton-init-systemd/var.mount b/package/skeleton-init-systemd/var.mount
new file mode 100644
index 0000000000..6b165dff6d
--- /dev/null
+++ b/package/skeleton-init-systemd/var.mount
@@ -0,0 +1,18 @@ 
+# SPDX-License-Identifier: LGPL-2.1-or-later
+# Modelled after systemd's tmp.mount
+
+[Unit]
+Description=Buildroot /var tmpfs
+DefaultDependencies=no
+Conflicts=umount.target
+Before=basic.target local-fs.target umount.target systemd-tmpfiles-setup.service
+After=swap.target
+
+[Mount]
+What=tmpfs
+Where=/var
+Type=tmpfs
+Options=mode=1777,strictatime,nosuid,nodev,size=50%%,nr_inodes=1m
+
+[Install]
+WantedBy=basic.target