9c306a
#!/bin/bash
9c306a
# Update an existing system to use a read only sysroot
9c306a
# and https://bugzilla.redhat.com/show_bug.cgi?id=2060976
9c306a
 
9c306a
set -euo pipefail
9c306a
 
9c306a
main() {
9c306a
    # Used to condition execution of this unit at the systemd level
9c306a
    local -r stamp_file="/var/lib/.ostree-readonly-sysroot"
9c306a
 
9c306a
    if [[ -f "${stamp_file}" ]]; then
9c306a
        exit 0
9c306a
    fi
9c306a
 
9c306a
    local -r ostree_sysroot_readonly="$(ostree config --repo=/sysroot/ostree/repo get "sysroot.readonly" &> /dev/null || echo "false")"
9c306a
    if [[ "${ostree_sysroot_readonly}" == "true" ]]; then
9c306a
        # Nothing to do
9c306a
        touch "${stamp_file}"
9c306a
        exit 0
9c306a
    fi
9c306a
 
9c306a
    local -r boot_entries="$(ls -A /boot/loader/entries/ | wc -l)"
9c306a
 
9c306a
    # Ensure that we can read BLS entries to avoid touching systems where /boot
9c306a
    # is not mounted
9c306a
    if [[ "${boot_entries}" -eq 0 ]]; then
9c306a
        echo "No BLS entry found: Maybe /boot is not mounted?" 1>&2
9c306a
        echo "This is unexpected thus no migration will be performed" 1>&2
9c306a
        touch "${stamp_file}"
9c306a
        exit 0
9c306a
    fi
9c306a
 
9c306a
    # Check if any existing deployment is still missing the rw karg
9c306a
    local rw_kargs_found=0
9c306a
    local count=0
9c306a
    for f in "/boot/loader/entries/"*; do
9c306a
        count="$(grep -c "^options .* rw" "${f}" || true)"
9c306a
        if [[ "${count}" -ge 1 ]]; then
9c306a
            rw_kargs_found=$((rw_kargs_found + 1))
9c306a
        fi
9c306a
    done
9c306a
 
9c306a
    # Some deployments are still missing the rw karg. Let's try to update them
9c306a
    if [[ "${boot_entries}" -ne "${rw_kargs_found}" ]]; then
9c306a
        ostree admin kargs edit-in-place --append-if-missing=rw || \
9c306a
            echo "Failed to edit kargs in place with ostree" 1>&2
9c306a
    fi
9c306a
 
9c306a
    # Re-check if any existing deployment is still missing the rw karg
9c306a
    rw_kargs_found=0
9c306a
    count=0
9c306a
    for f in "/boot/loader/entries/"*; do
9c306a
        count="$(grep -c "^options .* rw" "${f}" || true)"
9c306a
        if [[ "${count}" -ge 1 ]]; then
9c306a
            rw_kargs_found=$((rw_kargs_found + 1))
9c306a
        fi
9c306a
    done
9c306a
    unset count
9c306a
 
9c306a
    # If all deployments are good, then we can set the sysroot.readonly option
9c306a
    # in the ostree repo config
9c306a
    if [[ "${boot_entries}" -eq "${rw_kargs_found}" ]]; then
9c306a
        echo "Setting up the sysroot.readonly option in the ostree repo config"
9c306a
        ostree config --repo=/sysroot/ostree/repo set "sysroot.readonly" "true"
9c306a
        touch "${stamp_file}"
9c306a
        exit 0
9c306a
    fi
9c306a
 
9c306a
    # If anything else before failed, we will retry on next boot
9c306a
    echo "Will retry next boot" 1>&2
9c306a
    exit 0
9c306a
}
9c306a
 
9c306a
main "${@}"