Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add general ignition-ostree module with rootfs replacement #184

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash
set -euo pipefail

rootdisk=/dev/disk/by-label/root
rootmnt=/sysroot
tmproot=/run/ignition-ostree-rootfs
saved_rootmnt=${tmproot}/orig-sysroot
memdev=/dev/ram0

# Note that save & restore run in private mount namespaces, hence why we don't
# bother unmounting things here.

case "${1:-}" in
detect)
# This is obviously crude; perhaps in the future we could change ignition's `fetch`
# stage to write out a file if the rootfs is being replaced or so. But eh, it
# works for now.
wipes_rootfs=$(jq '.storage?.filesystems? // [] | map(select(.label == "root" and .wipeFilesystem == true)) | length' < /run/ignition.json)
if [ "${wipes_rootfs}" = "0" ]; then
exit 0
fi
echo "Detected rootfs replacement in fetched Ignition config: /run/ignition.json"
mkdir "${tmproot}"
;;
save)
size=$(lsblk -bn -o SIZE "${rootdisk}")
sizekbs="$(($size / 1024 + 1))"
modprobe --first-time brd rd_nr=1 rd_size="$sizekbs" max_part=1
echo "Moving rootfs to RAM..."
dd "if=${rootdisk}" "of=${memdev}" bs=8M
echo "Moved rootfs to RAM, pending redeployment: ${memdev}"
;;
restore)
mount "$rootdisk" $rootmnt
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside: I am still leaning towards "rootfs reprovisioning must add a root= kernel argument" (UUID is a sane default, but it doesn't have to be that). That way we avoid all races with changing the meaning of /dev/disk/by-label/root.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think I agree with this. I feel like we should spend some time to write up some of the complex root device options we want to support (e.g. at least starting with multipath, RAID, and LUKS) and see how it fits into one cohesive picture.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done in #503.

echo "Restoring rootfs from RAM..."
mkdir "${saved_rootmnt}"
mount "${memdev}" "${saved_rootmnt}"
rsync -aXHA "${saved_rootmnt}/" "${rootmnt}"
umount $saved_rootmnt
rmmod brd
rm -rf "${tmproot}"
;;
*)
echo "Unsupported operation: ${1:-}" 1>&2; exit 1
;;
esac
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[Unit]
Description=Ignition OSTree: detect rootfs replacement
DefaultDependencies=false
After=ignition-fetch.service
Before=ignition-disks.service
Before=initrd-root-fs.target
Before=sysroot.mount
ConditionKernelCommandLine=ostree

# This stage requires udevd to detect disks
Requires=systemd-udevd.service
After=systemd-udevd.service

[Service]
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=/run/ignition.env
ExecStart=/usr/libexec/ignition-ostree-dracut-rootfs detect
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[Unit]
Description=Ignition OSTree: restore rootfs
DefaultDependencies=false
After=ignition-disks.service
Before=ignition-ostree-growfs.service
Before=ignition-ostree-mount-firstboot-sysroot.service

ConditionKernelCommandLine=ostree
ConditionPathIsDirectory=/run/ignition-ostree-rootfs

[Service]
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=/run/ignition.env
# So we can transiently mount sysroot
MountFlags=slave
ExecStart=/usr/libexec/ignition-ostree-dracut-rootfs restore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[Unit]
Description=Ignition OSTree: save rootfs
DefaultDependencies=false
After=ignition-ostree-rootfs-detect.service
Before=ignition-disks.service
ConditionKernelCommandLine=ostree
ConditionPathIsDirectory=/run/ignition-ostree-rootfs

[Service]
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=/run/ignition.env
# So we can transiently mount sysroot
MountFlags=slave
ExecStart=/usr/libexec/ignition-ostree-dracut-rootfs save
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ install() {
rm \
sed \
sfdisk \
sgdisk
sgdisk \
rsync

for x in mount populate; do
install_ignition_unit ignition-ostree-${x}-var.service
Expand All @@ -65,6 +66,12 @@ install() {
inst_simple "$moddir/multipath-generator" \
"$systemdutildir/system-generators/multipath-generator"

inst_multiple jq chattr getfattr lsmod dd modprobe
inst_script "$moddir/ignition-ostree-dracut-rootfs.sh" "/usr/libexec/ignition-ostree-dracut-rootfs"
for x in detect save restore; do
install_ignition_unit ignition-ostree-rootfs-${x}.service
done

# Disk support
install_ignition_unit ignition-ostree-mount-firstboot-sysroot.service diskful
install_ignition_unit ignition-ostree-mount-subsequent-sysroot.service diskful-subsequent
Expand Down