diff --git a/README.md b/README.md index 0b49087..e644336 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ This serves to demonstrates how to: 2. Partition and prepare UEFI/GPT disks for a minimal OSTree host system 3. Generate OSTree repository in a empty filesystem 4. Integrate OSTree with GRUB2 bootloader +5. Upgrade existing installation with a rootfs image # Usage @@ -44,3 +45,11 @@ This serves to demonstrates how to: $ chmod +x ostree.sh $ sudo OSTREE_DEV_SCSI=scsi-360022480c22be84f8a61b39bbaed612f ./ostree.sh install ``` + +5. **Upgrade existing installation:** + + While booted into a OSTree system, use: + + ```console + $ sudo ./ostree.sh upgrade + ``` diff --git a/ostree.sh b/ostree.sh index 3ee3cc7..12d1de8 100644 --- a/ostree.sh +++ b/ostree.sh @@ -3,20 +3,32 @@ set -x set -u set -e -# [ENVIRONMENT]: REQUIRED -# OSTREE_DEV_SCSI= +# [ENVIRONMENT]: OPTIONS +function ENV_OPTS_CREATE { + # Required + # - OSTREE_DEV_SCSI (for install) -# [ENVIRONMENT]: CONFIGURABLE -export OSTREE_DEV_DISK=/dev/disk/by-id/${OSTREE_DEV_SCSI} -export OSTREE_DEV_BOOT=${OSTREE_DEV_DISK}-part1 -export OSTREE_DEV_HOME=${OSTREE_DEV_DISK}-part2 -export OSTREE_DEV_ROOT=${OSTREE_DEV_DISK}-part3 -export OSTREE_SYS_ROOT=/mnt -export OSTREE_SYS_BUILD=/tmp/rootfs + # Configurable + export OSTREE_DEV_DISK=${OSTREE_DEV_DISK:=/dev/disk/by-id/${OSTREE_DEV_SCSI}} + export OSTREE_DEV_BOOT=${OSTREE_DEV_BOOT:=${OSTREE_DEV_DISK}-part1} + export OSTREE_DEV_HOME=${OSTREE_DEV_HOME:=${OSTREE_DEV_DISK}-part2} + export OSTREE_DEV_ROOT=${OSTREE_DEV_ROOT:=${OSTREE_DEV_DISK}-part3} + export OSTREE_SYS_ROOT=${OSTREE_SYS_ROOT:=/mnt} + export OSTREE_SYS_BUILD=${OSTREE_SYS_BUILD:=/tmp/rootfs} +} + +# [ENVIRONMENT]: DEPENDENCIES +# | Todo: add persistent Pacman cache +function ENV_DEPS_CREATE { + # Skip in OSTree as filesystem is read-only + if ! grep -q ostree /proc/cmdline; then + pacman --noconfirm --needed -S $@ + fi +} # [DISK]: PARTITIONING (GPT+UEFI) function DISK_CREATE_LAYOUT { - pacman --noconfirm --needed -S parted + ENV_DEPS_CREATE parted umount --lazy --recursive ${OSTREE_SYS_ROOT} || : parted -a optimal -s ${OSTREE_DEV_DISK} -- \ mklabel gpt \ @@ -28,7 +40,7 @@ function DISK_CREATE_LAYOUT { # [DISK]: FILESYSTEM (ESP+EXT4) function DISK_CREATE_FORMAT { - pacman --noconfirm --needed -S dosfstools e2fsprogs + ENV_DEPS_CREATE dosfstools e2fsprogs mkfs.vfat -n SYS_BOOT -F 32 ${OSTREE_DEV_BOOT} mkfs.ext4 -L SYS_ROOT -F ${OSTREE_DEV_ROOT} mkfs.ext4 -L SYS_HOME -F ${OSTREE_DEV_HOME} @@ -42,7 +54,7 @@ function DISK_CREATE_MOUNTS { # [OSTREE]: INITIALIZATION function OSTREE_CREATE_REPO { - pacman --noconfirm --needed -S ostree wget which + ENV_DEPS_CREATE ostree wget which ostree admin init-fs --sysroot=${OSTREE_SYS_ROOT} --modern ${OSTREE_SYS_ROOT} ostree admin os-init --sysroot=${OSTREE_SYS_ROOT} archlinux ostree init --repo=${OSTREE_SYS_ROOT}/ostree/repo --mode=bare @@ -50,13 +62,12 @@ function OSTREE_CREATE_REPO { } # [OSTREE]: CONTAINER -# | Todo: add persistent Pacman cache # | Todo: delete /etc in Contailerfile (https://github.com/containers/podman/issues/20001) # | Todo: use tar format (`podman build -f Containerfile -o dest=${OSTREE_SYS_BUILD}.tar,type=tar`) function OSTREE_CREATE_IMAGE { # Add support for overlay storage driver in LiveCD if [[ $(df --output=fstype / | tail -n 1) = "overlay" ]]; then - pacman --noconfirm --needed -S "fuse-overlayfs" + ENV_DEPS_CREATE fuse-overlayfs export TMPDIR="/tmp/podman" PODMAN_ARGS=( --root ${TMPDIR}/storage @@ -65,7 +76,7 @@ function OSTREE_CREATE_IMAGE { fi # Create rootfs directory (workaround: `podman build --output local` doesn't preserve ownership) - pacman --noconfirm --needed -S podman + ENV_DEPS_CREATE podman podman ${PODMAN_ARGS[@]} build -f Containerfile -t rootfs rm -rf ${OSTREE_SYS_BUILD} mkdir ${OSTREE_SYS_BUILD} @@ -86,7 +97,7 @@ function OSTREE_DEPLOY_IMAGE { function BOOTLOADER_CREATE { grub-install --target=x86_64-efi --efi-directory=${OSTREE_SYS_ROOT}/boot/efi --removable --boot-directory=${OSTREE_SYS_ROOT}/boot/efi/EFI --bootloader-id=archlinux ${OSTREE_DEV_BOOT} - export OSTREE_SYS_PATH=$(ls -d ${OSTREE_SYS_ROOT}/ostree/deploy/archlinux/deploy/*|head -n 1) + export OSTREE_SYS_PATH=$(ls -d ${OSTREE_SYS_ROOT}/ostree/deploy/archlinux/deploy/* | head -n 1) rm -rfv ${OSTREE_SYS_PATH}/boot/* mount --mkdir --rbind ${OSTREE_SYS_ROOT}/boot ${OSTREE_SYS_PATH}/boot @@ -98,13 +109,36 @@ function BOOTLOADER_CREATE { umount -R ${OSTREE_SYS_ROOT} } -# Tasks to perform -DISK_CREATE_LAYOUT -DISK_CREATE_FORMAT -DISK_CREATE_MOUNTS +# [CLI]: TASKS FINECONTROL +case ${1:-} in + "install") + ENV_OPTS_CREATE + + DISK_CREATE_LAYOUT + DISK_CREATE_FORMAT + DISK_CREATE_MOUNTS + + OSTREE_CREATE_REPO + OSTREE_CREATE_IMAGE + OSTREE_DEPLOY_IMAGE + + BOOTLOADER_CREATE + ;; + + "upgrade") + if [[ ! -d "/ostree" ]]; then + exit 0 + fi + + export OSTREE_DEV_SCSI= + export OSTREE_SYS_ROOT=/ + ENV_OPTS_CREATE -OSTREE_CREATE_REPO -OSTREE_CREATE_IMAGE -OSTREE_DEPLOY_IMAGE + OSTREE_CREATE_IMAGE + OSTREE_DEPLOY_IMAGE + ;; -BOOTLOADER_CREATE + *) + echo "Usage: ostree.sh [install|upgrade]" + ;; +esac