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

Recfactor immutable rootfs package and initrd creation #305

Merged
merged 4 commits into from
Jun 22, 2021
Merged
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
3 changes: 3 additions & 0 deletions packages/cos-setup/02-cos-setup-initramfs.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
install_items+=" /lib/systemd/system/cos-setup-initramfs.service /etc/systemd/system/initrd.target.requires/cos-setup-initramfs.service "
install_items+=" /lib/systemd/system/cos-setup-rootfs.service /etc/systemd/system/initrd-fs.target.requires/cos-setup-rootfs.service "
add_dracutmodules+=" network "
6 changes: 5 additions & 1 deletion packages/cos-setup/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ requires:
version: ">=0"
steps:
- mkdir -p /lib/systemd/system
- mkdir -p /etc/dracut.conf.d
- cp 02-cos-setup-initramfs.conf /etc/dracut.conf.d
- cp -rfv *.service /lib/systemd/system
- cp -rfv *.timer /lib/systemd/system
- cp -rfv cos-setup /usr/bin/
- chmod +x /usr/bin/cos-setup
- systemctl enable cos-setup-rootfs.service
- systemctl enable cos-setup-initramfs.service
- systemctl enable cos-setup-reconcile.timer
- systemctl enable cos-setup-fs.service
- systemctl enable cos-setup-boot.service
- systemctl enable cos-setup-network.service
- systemctl enable cos-setup-network.service
16 changes: 16 additions & 0 deletions packages/cos-setup/cos-setup-initramfs.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Unit]
Description=cOS system initramfs setup before swith root
DefaultDependencies=no
After=initrd-fs.target
Requires=initrd-fs.target
Before=initrd.target

[Service]
RootDirectory=/sysroot
MountAPIVFS=true
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/cos-setup initramfs

[Install]
RequiredBy=initrd.target
14 changes: 14 additions & 0 deletions packages/cos-setup/cos-setup-rootfs.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[Unit]
Description=cOS system early rootfs setup
DefaultDependencies=no
After=initrd-root-fs.target
Requires=initrd-root-fs.target
Conflicts=initrd-switch-root.target

[Service]
Type=oneshot
ExecStartPre=/usr/bin/ln -s /sysroot/system /system
ExecStart=/usr/bin/cos-setup rootfs

[Install]
RequiredBy=initrd-fs.target
2 changes: 1 addition & 1 deletion packages/cos-setup/definition.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: cos-setup
category: system
version: 0.2.11+3
version: "0.2.16"
requires:
- name: "yip"
category: "toolchain"
Expand Down
5 changes: 5 additions & 0 deletions packages/cos/50-cos-initrd.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
hostonly_cmdline="no"
hostonly="no"
compress="xz"
omit_dracutmodules+=" multipath "
add_dracutmodules+=" dmsquash-live "
7 changes: 7 additions & 0 deletions packages/cos/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ steps:
- sed -i 's/:PRETTY_NAME:/{{.Values.brand_name}} v{{.Values.version}}/g' setup.yaml
- yip setup.yaml

# Create initrd
- |
cp 50-cos-initrd.conf /etc/dracut.conf.d && \
kernel=$(ls /lib/modules | head -n1) && \
dracut -f "/boot/initrd-${kernel}" "${kernel}" && \
ln -sf "initrd-${kernel}" /boot/initrd
# See https://luet-lab.github.io/docs/docs/concepts/packages/specfile/#package-by-container-content
unpack: true

Expand Down
5 changes: 2 additions & 3 deletions packages/cos/collection.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
packages:
- name: "cos"
category: "system"
version: 0.5.3+3
version: 0.5.3+5
brand_name: "cOS"
labels:
autobump.revdeps: "true"
autobump.revbump_related: "recovery/cos-img recovery/cos-squash"
- name: "cos"
category: "recovery"
version: 0.5.3+3
version: 0.5.3+3
version: 0.5.3+5
brand_name: "cOS recovery"
labels:
autobump.revdeps: "true"
Expand Down
2 changes: 1 addition & 1 deletion packages/grub-config/config/bootargs.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ set kernel=/boot/vmlinuz
if [ -n "$recoverylabel" ]; then
set kernelcmd="console=tty1 root=live:LABEL=$recoverylabel rd.live.dir=/ rd.live.squashimg=$img panic=5"
else
set kernelcmd="console=tty1 root=LABEL=$label iso-scan/filename=$img panic=5 security=selinux selinux=1"
set kernelcmd="console=tty1 root=LABEL=$label cos-img/filename=$img panic=5 security=selinux selinux=1"
fi

set initramfs=/boot/initrd
2 changes: 1 addition & 1 deletion packages/grub-config/definition.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
name: "grub-config"
category: "system"
version: 0.0.6+2
version: "0.0.7"
1 change: 1 addition & 0 deletions packages/immutable-rootfs/02-cos-immutable-rootfs.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_dracutmodules+=" cos-immutable-rootfs "

This file was deleted.

99 changes: 87 additions & 12 deletions packages/immutable-rootfs/30cos-immutable-rootfs/cos-generator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,57 @@

type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh

cos_unit="cos-immutable-rootfs.service"
cos_layout="/run/cos/cos-layout.env"

# Disable the service unless we override it
mkdir -p "/run/systemd/system/${cos_unit}.d"

[ -z "${root}" ] && root=$(getarg root=)

root_perm="ro"
if getargbool 0 rd.cos.debug.rw; then
root_perm="rw"
cos_root_perm="ro"
if getargbool 0 rd.cos.debugrw; then
cos_root_perm="rw"
fi

oem_timeout=$(getarg rd.cos.oemtimeout=)
[ -z "${oem_timeout}" ] && oem_timeout="10"
cos_overlay=$(getarg rd.cos.overlay=)
[ -z "${cos_overlay}" ] && cos_overlay="tmpfs:20%"

GENERATOR_DIR="$2"
[ -z "$GENERATOR_DIR" ] && exit 1
[ -d "$GENERATOR_DIR" ] || mkdir "$GENERATOR_DIR"

dev=$(dev_unit_name /dev/disk/by-label/COS_OEM)
{
echo "[Unit]"
echo "DefaultDependencies=no"
echo "Before=cos-setup-rootfs.service"
echo "Conflicts=initrd-switch-root.target"
echo "[Mount]"
echo "Where=/oem"
echo "What=/dev/disk/by-label/COS_OEM"
echo "Options=rw,suid,dev,exec,noauto,nouser,async"
} > "$GENERATOR_DIR"/oem.mount

if [ ! -e "$GENERATOR_DIR/cos-setup-rootfs.service.wants/oem.mount" ]; then
mkdir -p "$GENERATOR_DIR"/cos-setup-rootfs.service.wants
ln -s "$GENERATOR_DIR"/oem.mount \
"$GENERATOR_DIR"/cos-setup-rootfs.service.wants/oem.mount
fi

mkdir -p "$GENERATOR_DIR/$dev.device.d"
{
echo "[Unit]"
echo "Before=initrd-root-fs.target"
echo "JobRunningTimeoutSec=${oem_timeout}"
} > "$GENERATOR_DIR/$dev.device.d/timeout.conf"

if [ ! -e "$GENERATOR_DIR/initrd-root-fs.target.wants/$dev.device" ]; then
mkdir -p "$GENERATOR_DIR"/initrd-root-fs.target.wants
ln -s "$GENERATOR_DIR"/"$dev".device \
"$GENERATOR_DIR"/initrd-root-fs.target.wants/"$dev".device
fi

case "${root}" in
Expand All @@ -23,20 +69,15 @@ esac

[ "${rootok}" != "1" ] && exit 0

GENERATOR_DIR="$2"
[ -z "$GENERATOR_DIR" ] && exit 1
[ -d "$GENERATOR_DIR" ] || mkdir "$GENERATOR_DIR"

dev="${root//-/\\x2d}"
dev="${_dev//\//-}"
dev=$(dev_unit_name "${root}")
{
echo "[Unit]"
echo "Before=initrd-root-fs.target"
echo "DefaultDependencies=no"
echo "[Mount]"
echo "Where=/sysroot"
echo "What=${root}"
echo "Options=${root_perm},suid,dev,exec,auto,nouser,async"
echo "Options=${cos_root_perm},suid,dev,exec,auto,nouser,async"
} > "$GENERATOR_DIR"/sysroot.mount

if [ ! -e "$GENERATOR_DIR/initrd-root-fs.target.requires/sysroot.mount" ]; then
Expand All @@ -48,6 +89,40 @@ fi
mkdir -p "$GENERATOR_DIR/$dev.device.d"
{
echo "[Unit]"
echo "JobTimeoutSec=3000"
echo "JobRunningTimeoutSec=3000"
echo "JobTimeoutSec=300"
echo "JobRunningTimeoutSec=300"
} > "$GENERATOR_DIR/$dev.device.d/timeout.conf"

case "${cos_overlay}" in
UUID=*) \
cos_overlay="block:/dev/disk/by-uuid/${cos_overlay#UUID=}"
;;
LABEL=*) \
cos_overlay="block:/dev/disk/by-label/${cos_overlay#LABEL=}"
;;
esac

cos_mounts=()
for mount in $(getargs rd.cos.mount=); do
case "${mount}" in
UUID=*) \
mount="/dev/disk/by-uuid/${mount#UUID=}"
;;
LABEL=*) \
mount="/dev/disk/by-label/${mount#LABEL=}"
;;
esac
cos_mounts+=("${mount}")
done

mkdir -p "${cos_layout%/*}"
#> "${cos_layout}"

{
echo "[Service]"
echo "Environment=\"cos_mounts=${cos_mounts[@]}\""
echo "Environment=\"cos_overlay=${cos_overlay}\""
echo "Environment=\"cos_root_perm=${cos_root_perm}\""
echo "Environment=\"root=${root}\""
echo "EnvironmentFile=${cos_layout}"
} > "/run/systemd/system/${cos_unit}.d/override.conf"
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Unit]
Description=cOS system immutable rootfs mounts
DefaultDependencies=no
After=initrd-root-fs.target cos-setup-rootfs.service
Requires=initrd-root-fs.target
Before=initrd-fs.target
Conflicts=initrd-switch-root.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStartPre=-/usr/bin/systemctl stop oem.mount
ExecStart=/sbin/cos-mount-layout
42 changes: 42 additions & 0 deletions packages/immutable-rootfs/30cos-immutable-rootfs/cos-loop-img.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

function doLoopMount {
local label

for label in "${dev_labels[@]}"; do
[ -e "/tmp/cosloop-${label}" ] && continue
[ -e "/dev/disk/by-label/${label}" ] || continue
> "/tmp/cosloop-${label}"
mount -t auto -o "${cos_root_perm}" "/dev/disk/by-label/${label}" "${cos_state}" || continue
if [ -f "${cos_state}/${cos_img}" ]; then
losetup -f "${cos_state}/${cos_img}"
exit 0
else
umount "${cos_state}"
fi
done
}

type getarg > /dev/null 2>&1 || . /lib/dracut-lib.sh

PATH=/usr/sbin:/usr/bin:/sbin:/bin

declare cos_img=$1
declare cos_root_perm="ro"
declare cos_state="/run/initramfs/cos-state"
declare dev_labels=("COS_STATE" "COS_RECOVERY")

[ -z "${cos_img}" ] && exit 1

if getargbool 0 rd.cos.debugrw; then
cos_root_perm="rw"
fi

ismounted "${cos_state}" && exit 0

mkdir -p "${cos_state}"

doLoopMount

rm -r "${cos_state}"
exit 1
Loading