├── .gitignore ├── test ├── layout-bios-nobootpart.sfdisk ├── layout-uefi-nobootpart.sfdisk ├── mkinitcpio.conf ├── layout-bios-withbootpart.sfdisk ├── layout-uefi-withbootpart.sfdisk ├── build_rootfs.sh ├── run ├── start_vm └── install_from_live ├── examples ├── mkinitcpio.conf ├── Containerfile.minimal ├── build_rootfs_minimal.sh ├── build_rootfs_full.sh └── Containerfile.full ├── lib ├── build_rootfs_post_install ├── build_aur_packages ├── common.sh └── commit_rootfs ├── share ├── ostree-0-integration.conf └── Containerfile.builder ├── README.md ├── arch-ostree └── COPYING /.gitignore: -------------------------------------------------------------------------------- 1 | /cache* 2 | /rootfs 3 | /deployment 4 | /test/cache* 5 | -------------------------------------------------------------------------------- /test/layout-bios-nobootpart.sfdisk: -------------------------------------------------------------------------------- 1 | label: dos 2 | 3 | size=9G, type="linux", name="root" 4 | -------------------------------------------------------------------------------- /examples/mkinitcpio.conf: -------------------------------------------------------------------------------- 1 | HOOKS=(base systemd ostree autodetect modconf kms keyboard keymap consolefont block filesystems fsck) 2 | -------------------------------------------------------------------------------- /test/layout-uefi-nobootpart.sfdisk: -------------------------------------------------------------------------------- 1 | label: gpt 2 | 3 | size=1G, type="uefi", name="efi" 4 | size=9G, type="linux", name="root" 5 | -------------------------------------------------------------------------------- /test/mkinitcpio.conf: -------------------------------------------------------------------------------- 1 | HOOKS=(base systemd ostree autodetect modconf kms keyboard sd-vconsole block sd-encrypt filesystems fsck) 2 | -------------------------------------------------------------------------------- /test/layout-bios-withbootpart.sfdisk: -------------------------------------------------------------------------------- 1 | label: dos 2 | 3 | size=1G, type="linux", name="boot" 4 | size=8G, type="linux", name="root" 5 | -------------------------------------------------------------------------------- /test/layout-uefi-withbootpart.sfdisk: -------------------------------------------------------------------------------- 1 | label: gpt 2 | 3 | size=1G, type="uefi", name="efi" 4 | size=1G, type="linux", name="boot" 5 | size=8G, type="linux", name="root" 6 | -------------------------------------------------------------------------------- /lib/build_rootfs_post_install: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -euo pipefail 4 | 5 | # shellcheck source=SCRIPTDIR/common.sh 6 | source /tmp/common.sh 7 | # shellcheck disable=SC1091 8 | source /tmp/script 9 | 10 | if have_function post_install_early; then 11 | post_install_early 12 | fi 13 | 14 | if [ -d /tmp/aur ]; then 15 | pacman --noconfirm -U /tmp/aur/*; 16 | fi 17 | 18 | if have_function post_install; then 19 | post_install 20 | fi 21 | -------------------------------------------------------------------------------- /lib/build_aur_packages: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -euo pipefail 4 | 5 | build_aur_package() { 6 | local name="$1" 7 | local url="https://aur.archlinux.org/$name.git" 8 | local dir="/home/builder/pkg" 9 | 10 | git clone "$url" "$dir" 11 | chown -R builder:builder "$dir" 12 | 13 | pushd "$dir" 14 | sudo -u builder makepkg -s --noconfirm 15 | cp ./*.pkg.tar.zst /tmp/aur/ 16 | popd 17 | 18 | rm -rf "$dir" 19 | } 20 | 21 | pacman --noconfirm -Syu base-devel git sudo 22 | 23 | useradd -m builder 24 | echo 'builder ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/builder 25 | 26 | for package in "$@"; do 27 | build_aur_package "$package" 28 | done 29 | -------------------------------------------------------------------------------- /share/ostree-0-integration.conf: -------------------------------------------------------------------------------- 1 | d /var/log/journal 0755 root root - 2 | L /var/home - - - - ../sysroot/home 3 | #d /var/opt 0755 root root - 4 | d /var/srv 0755 root root - 5 | d /var/roothome 0700 root root - 6 | d /var/usrlocal 0755 root root - 7 | d /var/usrlocal/bin 0755 root root - 8 | d /var/usrlocal/etc 0755 root root - 9 | d /var/usrlocal/games 0755 root root - 10 | d /var/usrlocal/include 0755 root root - 11 | d /var/usrlocal/lib 0755 root root - 12 | d /var/usrlocal/man 0755 root root - 13 | d /var/usrlocal/sbin 0755 root root - 14 | d /var/usrlocal/share 0755 root root - 15 | d /var/usrlocal/src 0755 root root - 16 | d /var/mnt 0755 root root - 17 | d /run/media 0755 root root - 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # archlinux-ostree 2 | 3 | A tool for building an archlinux OSTree tree and deploying it. 4 | 5 | If you just want to know how to use this, check out the 6 | [guide on the Arch wiki](https://wiki.archlinux.org/title/User:M1cha/Install_Arch_Linux_inside_OSTree). 7 | 8 | ## Thanks 9 | - [GrabbenD](https://github.com/GrabbenD) for making this work with mainline 10 | GRUB2 and for making this work without Fedora. 11 | - [ostreedev](https://github.com/ostreedev) for the awesome OSTree project. 12 | - [Arch Linux](https://archlinux.org) for an awesome Linux distro. 13 | - [containers](https://github.com/containers) for podman. 14 | 15 | ## References 16 | - [OSTree documentation](https://github.com/containers) 17 | -------------------------------------------------------------------------------- /test/build_rootfs.sh: -------------------------------------------------------------------------------- 1 | packages=( 2 | base 3 | linux 4 | intel-ucode 5 | amd-ucode 6 | 7 | efibootmgr 8 | grub 9 | ostree 10 | which 11 | 12 | btrfs-progs 13 | openssh 14 | networkmanager 15 | ) 16 | aur_packages=( 17 | yay-bin 18 | ) 19 | 20 | prepare() { 21 | install -d "$rootfs/etc" 22 | install -m 0644 mkinitcpio.conf "$rootfs/etc/" 23 | } 24 | 25 | post_install_early() { 26 | echo 'Server = https://geo.mirror.pkgbuild.com/$repo/os/$arch' > /etc/pacman.d/mirrorlist 27 | pacman-key --init 28 | pacman-key --populate 29 | } 30 | 31 | post_install() { 32 | mkdir /efi 33 | 34 | ln -sf /usr/share/zoneinfo/UTC /etc/localtime 35 | sed -i 's/^#\(en_US.UTF-8 UTF-8\)/\1/' /etc/locale.gen 36 | 37 | locale-gen 38 | systemctl enable NetworkManager.service 39 | systemctl enable sshd.service 40 | systemctl enable systemd-timesyncd.service 41 | } 42 | -------------------------------------------------------------------------------- /examples/Containerfile.minimal: -------------------------------------------------------------------------------- 1 | FROM localhost/archlinux-ostree-builder AS builder 2 | 3 | # We need the ostree hook. 4 | RUN install -d /mnt/etc 5 | COPY mkinitcpio.conf /mnt/etc/ 6 | 7 | # Install packages. 8 | RUN pacstrap -c -G -M /mnt \ 9 | base \ 10 | linux \ 11 | intel-ucode \ 12 | amd-ucode \ 13 | efibootmgr \ 14 | grub \ 15 | ostree \ 16 | which 17 | 18 | # Turn the pacstrapped rootfs into a container image. 19 | FROM scratch 20 | COPY --from=builder /mnt / 21 | 22 | # The rootfs can't be modified and systemd can't create them implicitly. 23 | # That's why we have to create them as part of the rootfs. 24 | RUN mkdir /efi 25 | 26 | # Normal post installation steps. 27 | RUN ln -sf /usr/share/zoneinfo/UTC /etc/localtime 28 | RUN sed -i 's/^#\(en_US.UTF-8 UTF-8\)/\1/' /etc/locale.gen 29 | RUN locale-gen 30 | RUN systemctl enable systemd-timesyncd.service 31 | -------------------------------------------------------------------------------- /examples/build_rootfs_minimal.sh: -------------------------------------------------------------------------------- 1 | packages=( 2 | # The basics as usual. 3 | base 4 | # We need a kernel, of course. 5 | linux 6 | # Microcode updates for multiple CPUs, so this rootfs can work on all 7 | # of them. 8 | intel-ucode 9 | amd-ucode 10 | 11 | # All of these are needed to be able to use `ostree deploy` with GRUB. 12 | efibootmgr 13 | grub 14 | ostree 15 | which 16 | ) 17 | 18 | prepare() { 19 | # We need the ostree hook. 20 | install -d "$rootfs/etc" 21 | install -m 0644 mkinitcpio.conf "$rootfs/etc/" 22 | } 23 | 24 | post_install() { 25 | # The rootfs can't be modified and systemd can't create them implicitly. 26 | # That's why we have to create them as part of the rootfs. 27 | mkdir /efi 28 | 29 | ln -sf /usr/share/zoneinfo/UTC /etc/localtime 30 | sed -i 's/^#\(en_US.UTF-8 UTF-8\)/\1/' /etc/locale.gen 31 | 32 | locale-gen 33 | systemctl enable systemd-timesyncd.service 34 | } 35 | -------------------------------------------------------------------------------- /examples/build_rootfs_full.sh: -------------------------------------------------------------------------------- 1 | packages=( 2 | # The basics as usual. 3 | base 4 | # We need a kernel, of course. 5 | linux 6 | # Microcode updates for multiple CPUs, so this rootfs can work on all 7 | # of them. 8 | intel-ucode 9 | amd-ucode 10 | 11 | # All of these are needed to be able to use `ostree deploy` with GRUB. 12 | efibootmgr 13 | grub 14 | ostree 15 | which 16 | ) 17 | aur_packages=( 18 | yay-bin 19 | ) 20 | 21 | prepare() { 22 | # We need the ostree hook. 23 | install -d "$rootfs/etc" 24 | install -m 0644 mkinitcpio.conf "$rootfs/etc/" 25 | } 26 | 27 | # This is needed to be able to install AUR packages. 28 | post_install_early() { 29 | echo 'Server = https://geo.mirror.pkgbuild.com/$repo/os/$arch' > /etc/pacman.d/mirrorlist 30 | pacman-key --init 31 | pacman-key --populate 32 | } 33 | 34 | post_install() { 35 | # The rootfs can't be modified and systemd can't create them implicitly. 36 | # That's why we have to create them as part of the rootfs. 37 | mkdir /efi 38 | 39 | ln -sf /usr/share/zoneinfo/UTC /etc/localtime 40 | sed -i 's/^#\(en_US.UTF-8 UTF-8\)/\1/' /etc/locale.gen 41 | 42 | locale-gen 43 | systemctl enable systemd-timesyncd.service 44 | } 45 | -------------------------------------------------------------------------------- /test/run: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -xeuo pipefail 4 | 5 | git_dir="$(git rev-parse --show-toplevel)" 6 | test_dir="$git_dir/test" 7 | cache_dir="$test_dir/cache" 8 | userhost="root@127.0.0.1" 9 | port="60022" 10 | ssh_key="$cache_dir/id_rsa" 11 | ssh_opts=( 12 | -i "$ssh_key" 13 | -o StrictHostKeyChecking=no 14 | -o UserKnownHostsFile=/dev/null 15 | ) 16 | 17 | source "$git_dir/lib/common.sh" 18 | 19 | exec_ssh() { 20 | ssh \ 21 | "${ssh_opts[@]}" \ 22 | -t \ 23 | -p "$port" \ 24 | "$userhost" \ 25 | "$@" 26 | } 27 | 28 | cp_ssh() { 29 | from=("${@:1:$#-1}") 30 | to="${@:$#}" 31 | 32 | scp \ 33 | "${ssh_opts[@]}" \ 34 | -r \ 35 | -P "$port" \ 36 | "${from[@]}" \ 37 | "$userhost:$to" 38 | } 39 | 40 | exec_ssh rm -rf /root/arch-ostree 41 | exec_ssh mkdir /root/arch-ostree 42 | cp_ssh \ 43 | "$git_dir/arch-ostree" \ 44 | "$git_dir/lib" \ 45 | "$git_dir/share" \ 46 | /root/arch-ostree/ 47 | 48 | exec_ssh mkdir /root/arch-ostree/test 49 | cp_ssh \ 50 | "$git_dir/test/build_rootfs.sh" \ 51 | "$git_dir/test/install_from_live" \ 52 | "$git_dir"/test/layout-*.sfdisk \ 53 | "$git_dir/test/mkinitcpio.conf" \ 54 | /root/arch-ostree/test/ 55 | 56 | cp_ssh "${ssh_key}.pub" /tmp/ 57 | 58 | exec_ssh /root/arch-ostree/test/install_from_live "$@" 59 | msg "Successful" 60 | -------------------------------------------------------------------------------- /examples/Containerfile.full: -------------------------------------------------------------------------------- 1 | FROM localhost/archlinux-ostree-builder AS builder 2 | 3 | # We need the ostree hook. 4 | RUN install -d /mnt/etc 5 | COPY mkinitcpio.conf /mnt/etc/ 6 | 7 | # Install packages. 8 | RUN pacstrap -c -G -M /mnt \ 9 | base \ 10 | linux \ 11 | intel-ucode \ 12 | amd-ucode \ 13 | efibootmgr \ 14 | grub \ 15 | ostree \ 16 | which 17 | 18 | # Build AUR packages: Install dependencies and create user. 19 | RUN pacman --noconfirm -Syu base-devel git sudo 20 | RUN useradd -m builder 21 | 22 | # Build AUR packages: Build 23 | USER builder 24 | RUN git clone https://aur.archlinux.org/yay-bin.git /home/builder/yay-bin 25 | RUN cd /home/builder/yay-bin && makepkg -s --noconfirm 26 | USER root 27 | 28 | # Build AUR packages: Copy packages to a common place. 29 | RUN mkdir /aur 30 | RUN cp /home/builder/yay-bin/*.tar.zst /aur/ 31 | 32 | # Turn the pacstrapped rootfs into a container image. 33 | FROM scratch 34 | COPY --from=builder /mnt / 35 | 36 | # The rootfs can't be modified and systemd can't create them implicitly. 37 | # That's why we have to create them as part of the rootfs. 38 | RUN mkdir /efi 39 | 40 | # Normal post installation steps. 41 | RUN ln -sf /usr/share/zoneinfo/UTC /etc/localtime 42 | RUN sed -i 's/^#\(en_US.UTF-8 UTF-8\)/\1/' /etc/locale.gen 43 | RUN locale-gen 44 | RUN systemctl enable systemd-timesyncd.service 45 | 46 | # Install built AUR packages. 47 | COPY --from=builder /aur /aur 48 | RUN echo 'Server = https://geo.mirror.pkgbuild.com/$repo/os/$arch' > /etc/pacman.d/mirrorlist 49 | RUN pacman-key --init 50 | RUN pacman-key --populate 51 | RUN pacman --noconfirm -U /aur/* 52 | RUN rm -r /aur 53 | -------------------------------------------------------------------------------- /share/Containerfile.builder: -------------------------------------------------------------------------------- 1 | FROM docker.io/alpine:3.21 AS builder 2 | 3 | RUN apk add --no-cache coreutils gnupg tar wget zstd 4 | 5 | WORKDIR /tmp 6 | 7 | RUN wget http://mirror.cmt.de/archlinux/iso/latest/b2sums.txt 8 | RUN wget http://mirror.cmt.de/archlinux/iso/latest/sha256sums.txt 9 | RUN wget http://mirror.cmt.de/archlinux/iso/latest/archlinux-bootstrap-x86_64.tar.zst 10 | RUN wget http://mirror.cmt.de/archlinux/iso/latest/archlinux-bootstrap-x86_64.tar.zst.sig 11 | RUN gpg --auto-key-locate clear,wkd -v --locate-external-key pierre@archlinux.org 12 | 13 | # This might be pedantic given that the signature matches, but why not. 14 | RUN b2sum --ignore-missing -c b2sums.txt 15 | RUN sha256sum --ignore-missing -c sha256sums.txt 16 | RUN gpg --keyserver-options auto-key-retrieve --verify archlinux-bootstrap-x86_64.tar.zst.sig archlinux-bootstrap-x86_64.tar.zst 17 | 18 | WORKDIR / 19 | 20 | RUN mkdir /rootfs 21 | RUN tar xf /tmp/archlinux-bootstrap-x86_64.tar.zst --numeric-owner -C /rootfs 22 | 23 | FROM scratch 24 | COPY --from=builder /rootfs/root.x86_64 / 25 | 26 | # The bootstrap image is very minimal and we still have to setup pacman. 27 | RUN pacman-key --init 28 | RUN pacman-key --populate 29 | RUN echo 'Server = https://geo.mirror.pkgbuild.com/$repo/os/$arch' > /etc/pacman.d/mirrorlist 30 | 31 | # This allows us to use this image for committing as well. 32 | RUN pacman --noconfirm -Syu grub ostree rsync 33 | 34 | # This allows using this container to make a deployment. 35 | RUN ln -s sysroot/ostree /ostree 36 | 37 | # This allows using pacstrap -N in a rootless container. 38 | RUN echo 'root:1000:5000' > /etc/subuid 39 | RUN echo 'root:1000:5000' > /etc/subgid 40 | -------------------------------------------------------------------------------- /lib/common.sh: -------------------------------------------------------------------------------- 1 | 2 | # Source: https://github.com/archlinux/arch-install-scripts/blob/4802e1197e43e300e8fc545b7b2a0358ad2f920b/common#L75 3 | # shellcheck disable=SC2059 # $1 and $2 can contain the printf modifiers 4 | out() { printf "$1 $2\n" "${@:3}"; } 5 | error() { out "====> ERROR:" "$@"; } >&2 6 | warning() { out "====> WARNING:" "$@"; } >&2 7 | msg() { out "====>" "$@"; } 8 | die() { error "$@"; exit 1; } 9 | 10 | arg_to_varname() { 11 | name="${1:2}" 12 | echo "${name//-/_}" 13 | } 14 | 15 | # Source: https://gitlab.archlinux.org/pacman/pacman/-/blob/v6.0.2/scripts/libmakepkg/util/pkgbuild.sh.in?ref_type=tags#L29 16 | have_function() { 17 | declare -f "$1" >/dev/null 18 | } 19 | 20 | # Source: https://github.com/archlinux/arch-install-scripts/blob/4802e1197e43e300e8fc545b7b2a0358ad2f920b/common#L183 21 | # This outputs code for declaring all variables to stdout. For example, if 22 | # FOO=BAR, then running 23 | # declare -p FOO 24 | # will result in the output 25 | # declare -- FOO="bar" 26 | # This function may be used to re-declare all currently used variables and 27 | # functions in a new shell. 28 | declare_all() { 29 | # Remove read-only variables to avoid warnings. Unfortunately, declare +r -p 30 | # doesn't work like it looks like it should (declaring only read-write 31 | # variables). However, declare -rp will print out read-only variables, which 32 | # we can then use to remove those definitions. 33 | declare -p | grep -Fvf <(declare -rp) 34 | # Then declare functions 35 | declare -pf 36 | } 37 | 38 | # Source: https://stackoverflow.com/questions/1527049/how-can-i-join-elements-of-a-bash-array-into-a-delimited-string/17841619#17841619 39 | join_by() { 40 | local IFS="$1" 41 | shift 42 | echo "$*" 43 | } 44 | -------------------------------------------------------------------------------- /lib/commit_rootfs: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -euo pipefail 4 | 5 | rootfs="/mnt" 6 | 7 | # shellcheck source=SCRIPTDIR/common.sh 8 | source /opt/lib/common.sh 9 | 10 | move_from_var() { 11 | # This database must not be deleted, so we can modify it temporarily 12 | # after running `ostree admin unlock`. 13 | sed -i \ 14 | -e 's|^#\(DBPath\s*=\s*\).*|\1/usr/lib/pacman|g' \ 15 | -e 's|^#\(IgnoreGroup\s*=\s*\).*|\1modified|g' \ 16 | "$rootfs/etc/pacman.conf" 17 | mv "$rootfs/var/lib/pacman" "$rootfs/usr/lib/" 18 | } 19 | 20 | move_to_var() { 21 | mv "$rootfs/home" "$rootfs/var/" 22 | ln -s var/home "$rootfs/home" 23 | 24 | mv "$rootfs/mnt" "$rootfs/var/" 25 | ln -s var/mnt "$rootfs/mnt" 26 | 27 | # This is recommended by ostree but I don't see a good reason for it. 28 | # rmdir "$rootfs/var/opt" 29 | # mv "$rootfs/opt" "$rootfs/var/" 30 | # ln -s var/opt "$rootfs/opt" 31 | 32 | mv "$rootfs/root" "$rootfs/var/roothome" 33 | ln -s var/roothome "$rootfs/root" 34 | 35 | rm -r "${rootfs:?}/usr/local" 36 | ln -s ../var/usrlocal "$rootfs/usr/local" 37 | 38 | mv "$rootfs/srv" "$rootfs/var/srv" 39 | ln -s var/srv "$rootfs/srv" 40 | 41 | cp "/opt/share/ostree-0-integration.conf" "$rootfs/usr/lib/tmpfiles.d/" 42 | } 43 | 44 | clean_rootfs() { 45 | # They'll be unused. 46 | rm -r "${rootfs:?}/var/"* 47 | 48 | # pacman leaves behind sockets which are not supported by ostree. 49 | find "$rootfs" -type s -exec rm {} \; 50 | } 51 | 52 | # ostree expects the initramfs in a different path. 53 | # Also, we need to prepend microcode updates. 54 | create_initramfs() { 55 | kmod_dirs=() 56 | while IFS= read -r -u3 -d $'\0' file; do 57 | kmod_dirs+=("$file") 58 | done 3< <(find "$rootfs/usr/lib/modules" -mindepth 1 -maxdepth 1 -type d -print0) 59 | 60 | ucode_images=() 61 | while IFS= read -r -u3 -d $'\0' file; do 62 | ucode_images+=("$file") 63 | done 3< <(find "$rootfs/boot" -mindepth 1 -maxdepth 1 -type f -name '*-ucode.img' -print0) 64 | 65 | num_kernels=${#kmod_dirs[@]} 66 | if [ "$num_kernels" -eq 0 ]; then 67 | die "No kernel found" 68 | fi 69 | if [ "$num_kernels" -ne 1 ]; then 70 | die "Multiple kernels found" 71 | fi 72 | 73 | if [ ${#ucode_images[@]} -eq 0 ]; then 74 | warning "No microcode updates found" 75 | fi 76 | 77 | kmod_dir="${kmod_dirs[0]}" 78 | kernel_pkg="$(cat "$kmod_dir/pkgbase")" 79 | 80 | cat \ 81 | "${ucode_images[@]}" \ 82 | "$rootfs/boot/initramfs-${kernel_pkg}.img" \ 83 | > "$kmod_dir/initramfs.img" 84 | } 85 | 86 | ostreeify() { 87 | # Those are required so the ostree tools can use them. 88 | mkdir "$rootfs/sysroot" 89 | ln -s sysroot/ostree "$rootfs/ostree" 90 | 91 | # etc is handled by ostree and expected to be in /usr. 92 | mv "$rootfs/etc" "$rootfs/usr/" 93 | 94 | # This is not needed. We only need the directory as a mountpoint. 95 | rm -r "${rootfs:?}/boot" 96 | mkdir "$rootfs/boot" 97 | } 98 | 99 | lower_fstype="$(stat -f -c '%T' /mnt-lower)" 100 | if [ "$lower_fstype" = "overlayfs" ]; then 101 | echo "overlay detected, use rsync." >&2 102 | rsync -a /mnt-lower/ /mnt 103 | else 104 | # Create a temporary overlay FS so none of our rootfs changes are permanent. 105 | mkdir /overlay/{upper,work} 106 | mount \ 107 | -t overlay \ 108 | -o 'lowerdir=/mnt-lower,upperdir=/overlay/upper,workdir=/overlay/work' \ 109 | overlay /mnt 110 | fi 111 | 112 | move_from_var 113 | move_to_var 114 | clean_rootfs 115 | create_initramfs 116 | ostreeify 117 | 118 | ostree commit --repo /sysroot/ostree/repo --tree=dir=/mnt "$@" 119 | -------------------------------------------------------------------------------- /test/start_vm: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -xeuo pipefail 4 | 5 | git_dir="$(git rev-parse --show-toplevel)" 6 | source "$git_dir/lib/common.sh" 7 | 8 | test_dir="$git_dir/test" 9 | cache_dir="$test_dir/cache" 10 | disk_file="$cache_dir/disk.qcow2" 11 | mirror="https://geo.mirror.pkgbuild.com/iso" 12 | iso="$cache_dir/arch.iso" 13 | iso_unverified="$cache_dir/arch-unverified.iso" 14 | ovmf_vars="$cache_dir/OVMF_VARS.4m.fd" 15 | ssh_key="$cache_dir/id_rsa" 16 | cloud_init_iso="$cache_dir/cloud-init.iso" 17 | with_cdrom=true 18 | with_uefi=true 19 | long_opts=( 20 | with-cdrom: 21 | with-uefi: 22 | ) 23 | 24 | download_arch_iso() { 25 | if [ ! -f "$iso" ]; then 26 | if [ ! -f "$iso_unverified" ]; then 27 | curl \ 28 | -Lo "$iso_unverified" \ 29 | "$mirror/latest/archlinux-x86_64.iso" 30 | fi 31 | curl \ 32 | -Lo "$iso_unverified.sig" \ 33 | "$mirror/latest/archlinux-x86_64.iso.sig" 34 | 35 | sq --force wkd get pierre@archlinux.org -o "$cache_dir/release-key.pgp" 36 | sq verify \ 37 | --signer-file "$cache_dir/release-key.pgp" \ 38 | --detached "$iso_unverified.sig" \ 39 | "$iso_unverified" 40 | 41 | mv "$iso_unverified" "$iso" 42 | fi 43 | } 44 | 45 | create_cloud_init_iso() { 46 | echo ''> "$cache_dir/meta-data" 47 | 48 | cat < "$cache_dir/user-data" 49 | #cloud-config 50 | users: 51 | - name: root 52 | ssh_authorized_keys: 53 | - $(cat "$ssh_key.pub") 54 | EOF 55 | 56 | cat < "$cache_dir/network-config" 57 | version: 2 58 | config: disabled 59 | EOF 60 | 61 | xorrisofs \ 62 | -output "$cloud_init_iso" \ 63 | -volid CIDATA \ 64 | -joliet \ 65 | -rational-rock \ 66 | "$cache_dir/meta-data" \ 67 | "$cache_dir/user-data" \ 68 | "$cache_dir/network-config" 69 | } 70 | 71 | if ! temp=$(getopt -o '' --long "$(join_by , "${long_opts[@]}")" -- "$@"); then 72 | die "Invalid arguments" 73 | fi 74 | 75 | eval set -- "$temp" 76 | while true; do 77 | case "$1" in 78 | '--with-cdrom'|\ 79 | '--with-uefi') 80 | name="$(arg_to_varname "$1")" 81 | 82 | case "$2" in 83 | 'true'|'1'|'yes') 84 | eval "$name=true" 85 | ;; 86 | 'false'|'0'|'no') 87 | eval "$name=false" 88 | ;; 89 | *) 90 | die "Unsupported bool value: $2" 91 | ;; 92 | esac 93 | 94 | shift 2 95 | continue 96 | ;; 97 | '--') 98 | shift 99 | break 100 | ;; 101 | *) 102 | die "BUG: Unexpected argument: $1" 103 | ;; 104 | esac 105 | done 106 | 107 | mkdir -p "$cache_dir" 108 | 109 | if [ ! -f "$ssh_key" ]; then 110 | ssh-keygen -t rsa -N "" -C "test" -f "$ssh_key" 111 | fi 112 | 113 | 114 | if [ ! -f "$disk_file" ]; then 115 | qemu-img create -f qcow2 "$disk_file" 10G 116 | fi 117 | 118 | cloud_init_args=() 119 | if [ $with_cdrom = true ]; then 120 | download_arch_iso 121 | create_cloud_init_iso 122 | cloud_init_args+=( 123 | -cdrom "$iso" 124 | -drive file="$cloud_init_iso,format=raw" 125 | ) 126 | fi 127 | 128 | extra_args=() 129 | if [ $with_uefi = true ]; then 130 | if [ ! -f "$ovmf_vars" ]; then 131 | cp /usr/share/edk2/x64/OVMF_VARS.4m.fd "$ovmf_vars" 132 | fi 133 | 134 | extra_args+=( 135 | -drive "if=pflash,format=raw,readonly=on,file=/usr/share/edk2/x64/OVMF_CODE.4m.fd" 136 | -drive "if=pflash,format=raw,file=$ovmf_vars" 137 | ) 138 | fi 139 | 140 | qemu-system-x86_64 \ 141 | "${cloud_init_args[@]}" \ 142 | -drive file="$disk_file,format=qcow2" \ 143 | -enable-kvm \ 144 | -cpu host \ 145 | -m 4G \ 146 | "${extra_args[@]}" \ 147 | -nic user,hostfwd=tcp::60022-:22 148 | -------------------------------------------------------------------------------- /test/install_from_live: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -xeuo pipefail 4 | 5 | export PATH="/root/arch-ostree:$PATH" 6 | 7 | # shellcheck source=/lib/common.sh 8 | source /root/arch-ostree/lib/common.sh 9 | 10 | disk="/dev/disk/by-id/ata-QEMU_HARDDISK_QM00002" 11 | supported_part_schemes=( 12 | plain 13 | lukspart 14 | ) 15 | supported_boot_modes=( 16 | bios 17 | uefi 18 | ) 19 | long_opts=( 20 | part-scheme: 21 | with-boot-part: 22 | ) 23 | part_scheme=plain 24 | if [ -f /sys/firmware/efi/fw_platform_size ]; then 25 | boot_mode=uefi 26 | else 27 | boot_mode=bios 28 | fi 29 | with_boot_part=true 30 | tasks=( 31 | cleanup 32 | increase_cowspace 33 | pacman_refresh 34 | prepare_disk 35 | mount 36 | prepare_live_env 37 | init_ostree 38 | build_builder_container 39 | build_rootfs 40 | commit_rootfs 41 | create_deployment_env 42 | grub_install 43 | deploy 44 | install_ssh_key 45 | write_fstab 46 | ) 47 | 48 | task_cleanup() { 49 | umount -R /mnt || true 50 | cryptsetup close root || true 51 | } 52 | 53 | task_increase_cowspace() { 54 | mount -o remount,size=2G /run/archiso/cowspace 55 | } 56 | 57 | task_pacman_refresh() { 58 | pacman --noconfirm -Sy 59 | } 60 | 61 | task_prepare_disk() { 62 | if [ $with_boot_part = true ]; then 63 | bootpart_suffix="-withbootpart" 64 | else 65 | bootpart_suffix="-nobootpart" 66 | fi 67 | 68 | sfdisk -w always "$disk" \ 69 | < "/root/arch-ostree/test/layout-${boot_mode}${bootpart_suffix}.sfdisk" 70 | sleep 1 71 | 72 | if [ "$boot_mode" = "uefi" ]; then 73 | mkfs.vfat -F32 "$part_efi" 74 | fi 75 | if [ $with_boot_part = true ]; then 76 | mkfs.ext4 "$part_boot" 77 | fi 78 | 79 | case "$part_scheme" in 80 | plain) 81 | mkfs.ext4 "$part_root" 82 | ;; 83 | lukspart) 84 | echo -n test | cryptsetup -v luksFormat "$part_root" - 85 | echo -n test | cryptsetup open "$part_root" root -d - 86 | mkfs.ext4 /dev/mapper/root 87 | ;; 88 | esac 89 | } 90 | 91 | task_mount() { 92 | case "$part_scheme" in 93 | plain) 94 | mount "$part_root" /mnt 95 | ;; 96 | lukspart) 97 | if [ ! -e /dev/mapper/root ]; then 98 | echo -n test | cryptsetup open "$part_root" root -d - 99 | fi 100 | 101 | mount /dev/mapper/root /mnt 102 | ;; 103 | esac 104 | 105 | if [ $with_boot_part = true ]; then 106 | mkdir -p /mnt/boot 107 | mount "$part_boot" /mnt/boot 108 | fi 109 | 110 | if [ "$boot_mode" = "uefi" ]; then 111 | mkdir -p /mnt/efi 112 | mount "$part_efi" /mnt/efi 113 | fi 114 | } 115 | 116 | task_prepare_live_env() { 117 | arch-ostree prepare_live_env 118 | } 119 | 120 | task_init_ostree() { 121 | ostree admin init-fs --sysroot /mnt --modern /mnt 122 | ostree admin stateroot-init --sysroot /mnt archlinux 123 | ostree config --repo /mnt/ostree/repo set sysroot.bootprefix 1 124 | } 125 | 126 | task_build_builder_container() { 127 | arch-ostree build_builder_container 128 | } 129 | 130 | task_build_rootfs() { 131 | arch-ostree \ 132 | --aur-dir /mnt/setup/aur \ 133 | --pacman-cache /mnt/setup/pacman_cache \ 134 | --rootfs-dir /mnt/setup/rootfs \ 135 | build_rootfs_directory \ 136 | /root/arch-ostree/test/build_rootfs.sh 137 | } 138 | 139 | task_commit_rootfs() { 140 | arch-ostree \ 141 | --rootfs-dir /mnt/setup/rootfs \ 142 | --ostree-repo /mnt/ostree/repo \ 143 | commit_rootfs_directory \ 144 | -- -v -b test 145 | } 146 | 147 | task_create_deployment_env() { 148 | rm -rf /mnt/setup/deployment 149 | ostree checkout \ 150 | --repo /mnt/ostree/repo \ 151 | --require-hardlinks \ 152 | test /mnt/setup/deployment 153 | } 154 | 155 | deploy_cmd() { 156 | arch-ostree \ 157 | --deploy-env-dir /mnt/setup/deployment \ 158 | --sysroot-dir /mnt \ 159 | deploy_env -- "$@" 160 | } 161 | 162 | task_grub_install() { 163 | case "$boot_mode" in 164 | bios) 165 | deploy_cmd grub-install --target i386-pc "$disk" 166 | ;; 167 | uefi) 168 | deploy_cmd grub-install \ 169 | --target x86_64-efi \ 170 | --efi-directory /efi \ 171 | --bootloader-id=GRUB 172 | ;; 173 | esac 174 | 175 | ln -sf ../loader/grub.cfg /mnt/boot/grub/grub.cfg 176 | } 177 | 178 | task_deploy() { 179 | rootuuid="$(blkid -o value -s UUID "$part_root")" 180 | kargs=() 181 | 182 | case "$part_scheme" in 183 | plain) 184 | kargs+=( 185 | --karg=root=UUID="$rootuuid" 186 | ) 187 | ;; 188 | lukspart) 189 | kargs+=( 190 | --karg="rd.luks.name=$rootuuid=root" 191 | --karg=root=/dev/mapper/root 192 | ) 193 | ;; 194 | esac 195 | 196 | deploy_cmd ostree admin deploy \ 197 | --os=archlinux \ 198 | --no-merge \ 199 | --karg-none \ 200 | "${kargs[@]}" \ 201 | --karg=rw \ 202 | test 203 | } 204 | 205 | task_install_ssh_key() { 206 | install -d /mnt/ostree/deploy/archlinux/var/roothome/.ssh 207 | install \ 208 | -m 0644 \ 209 | "/tmp/id_rsa.pub" \ 210 | /mnt/ostree/deploy/archlinux/var/roothome/.ssh/authorized_keys 211 | } 212 | 213 | task_write_fstab() { 214 | genfstab -U /mnt >> /mnt/ostree/deploy/archlinux/deploy/*/etc/fstab 215 | } 216 | 217 | task_host_shell() { 218 | bash 219 | } 220 | 221 | task_deploy_shell() { 222 | deploy_cmd "${args[@]}" 223 | } 224 | 225 | task_poweroff() { 226 | systemctl poweroff 227 | } 228 | 229 | task_group_prepare_existing() { 230 | task_cleanup 231 | task_increase_cowspace 232 | task_pacman_refresh 233 | task_mount 234 | task_prepare_live_env 235 | } 236 | 237 | if ! temp=$(getopt -o '+' --long "$(join_by , "${long_opts[@]}")" -- "$@"); then 238 | die "Invalid arguments" 239 | fi 240 | 241 | eval set -- "$temp" 242 | while true; do 243 | case "$1" in 244 | '--part-scheme') 245 | name="$(arg_to_varname "$1")" 246 | printf -v "$name" "%s" "$2" 247 | shift 2 248 | continue 249 | ;; 250 | '--with-boot-part') 251 | name="$(arg_to_varname "$1")" 252 | 253 | case "$2" in 254 | 'true'|'1'|'yes') 255 | eval "$name=true" 256 | ;; 257 | 'false'|'0'|'no') 258 | eval "$name=false" 259 | ;; 260 | *) 261 | die "Unsupported bool value: $2" 262 | ;; 263 | esac 264 | 265 | shift 2 266 | continue 267 | ;; 268 | '--') 269 | shift 270 | break 271 | ;; 272 | *) 273 | die "BUG: Unexpected argument: $1" 274 | ;; 275 | esac 276 | done 277 | 278 | if [ $# -ne 0 ]; then 279 | tasks=() 280 | 281 | for arg in "$@"; do 282 | shift 283 | 284 | if [ "$arg" = "--" ]; then 285 | break 286 | fi 287 | 288 | tasks+=("$arg") 289 | done 290 | fi 291 | 292 | args=("$@") 293 | 294 | for task in "${tasks[@]}"; do 295 | if ! have_function "task_$task"; then 296 | die "Unsupported task: $task" 297 | fi 298 | done 299 | 300 | if [[ ! ${supported_part_schemes[*]} =~ $part_scheme ]]; then 301 | die "Unsupported partition scheme: $part_scheme" 302 | fi 303 | 304 | if [[ ! ${supported_boot_modes[*]} =~ $boot_mode ]]; then 305 | die "Unsupported boot_mode: $boot_mode" 306 | fi 307 | 308 | case "$boot_mode" in 309 | bios) 310 | if [ $with_boot_part = true ]; then 311 | part_boot="$disk-part1" 312 | part_root="$disk-part2" 313 | else 314 | part_root="$disk-part1" 315 | fi 316 | ;; 317 | uefi) 318 | part_efi="/dev/disk/by-partlabel/efi" 319 | if [ $with_boot_part = true ]; then 320 | part_boot="/dev/disk/by-partlabel/boot" 321 | fi 322 | part_root="/dev/disk/by-partlabel/root" 323 | ;; 324 | esac 325 | 326 | for task in "${tasks[@]}"; do 327 | "task_$task" 328 | done 329 | -------------------------------------------------------------------------------- /arch-ostree: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # shellcheck disable=SC2154 3 | 4 | set -euo pipefail 5 | 6 | scriptdir="$(dirname "$(readlink -f "$0")")" 7 | lib_dir="$scriptdir/lib" 8 | share_dir="$scriptdir/share" 9 | 10 | # shellcheck source=lib/common.sh 11 | source "$lib_dir/common.sh" 12 | 13 | builder_tag="archlinux-ostree-builder" 14 | rootfs_tag="archlinux-ostree-rootfs" 15 | ostree_repo="/sysroot/ostree/repo" 16 | podman_build_cache=false 17 | 18 | usage() { 19 | cat < is a value of 0, false, no, 1, true or yes. 23 | 24 | COMMON OPTIONS: 25 | --aur-dir 26 | Path to the directory where built AUR packages are stored. 27 | --builder-tag 28 | Name of the tag to use for the pacstrap container. 29 | Default: archlinux-ostree-builder 30 | --deploy-env-dir 31 | Path to the directory that's used as the deployment environment. 32 | --ostree-repo 33 | Path to the ostree repository. 34 | Default: /sysroot/ostree/repo 35 | --podman-build-cache 36 | Enable/Disable podman build cache. Boolean. 37 | Default: no. 38 | --pacman-cache 39 | Path to a persistent pacman package cache. This controls both podmans 40 | --no-cache option and buildahs BUILDAH_LAYERS environment variable. 41 | Default: undefined, thus the pacman cache is disabled. 42 | --sysroot-dir 43 | Path to the directory where your rootfs and all your partitions 44 | (like boot or efi) are mounted. 45 | --help 46 | Print this help message 47 | 48 | ROOTFS OPTIONS: 49 | --rootfs-dir 50 | Path to the directory where the rootfs will be created in. 51 | 52 | CONTAINER OPTIONS: 53 | --rootfs-containerfile 54 | Path to the Containerfile used for building the rootfs container. 55 | --rootfs-tag 56 | Name of the tag to use for the rootfs container. 57 | Default: archlinux-ostree-rootfs 58 | 59 | COMMON COMMANDS: 60 | prepare_live_env 61 | Prepare currently booted live ISO environment for setting up a new 62 | ostree installation. 63 | build_builder_container 64 | (Re-)Build pacstrap container that's used by the other commands and tag 65 | it with --builder-tag. 66 | deploy_env -- [arg...] 67 | Spawn a shell with a deployment at --deploy-env-dir as the rootfs. 68 | This can be used to install the bootloader and create your first 69 | deployment from the arch live ISO. 70 | The shell runs inside a chroot that looks similar to a booted ostree 71 | environment. 72 | 73 | CONTAINER COMMANDS: 74 | build_rootfs_container 75 | Build rootfs container from --rootfs-containerfile and tag it with 76 | --rootfs-tag. 77 | commit_rootfs_container -- [arg...] 78 | Commit --rootfs-tag container to --ostree-repo. This tool provides 79 | --repo and --tree-dir to "ostree commit". Everything else 80 | (like branch name) can and has to be provided by you. 81 | enter_rootfs_container -- [arg...] 82 | Enter --rootfs-tag container. All changes will be lost. 83 | 84 | ROOTFS COMMANDS: 85 | build_rootfs_directory 86 | Builds a rootfs directory at --rootfs-dir using the script-file at 87 | . See BUILD_SCRIPT_FORMAT for details on the format of that file. 88 | is deleted recursively before building the new rootfs. 89 | commit_rootfs_directory -- [arg...] 90 | Commit --rootfs-dir container to --ostree-repo. This tool provides 91 | --repo and --tree-dir to ostree commit. Everything else 92 | (like branch name) can and has to be provided by you. 93 | enter_rootfs_directory -- [arg...] 94 | Enter --rootfs-dir. All changes will be lost. 95 | pacstrap_rootfs_directory [pkg...] 96 | Run pacstrap on --rootfs-dir. If the directory exists, it will NOT be 97 | deleted. [pkg...] will be passed to pacstrap and must contain the 98 | packages that shall be installed. 99 | 100 | BUILD_SCRIPT FORMAT: 101 | A build script is a shell-script that's sourced by another shell-script. 102 | It can be used to define everything that's needed to build your rootfs in a 103 | single file. 104 | This works similar to PKGBUILD in that it has to define certain variables 105 | and functions. The script can be sourced multiple times and into different 106 | environments(like containers). So don't expect any variables or files to 107 | stay around in between function calls. 108 | 109 | VARIABLES: 110 | packages 111 | A bash array that contains the packages you want to install using 112 | pacstrap. 113 | aur_packages 114 | A bash array that contains the packages you want to install from the 115 | AUR. They are built inside a clean --builder-tag container, so you 116 | don't need to worry about leaving behind trash in your actual rootfs. 117 | Currently, they are build manually using makepkg, so you have to make 118 | sure to include dependent packages that are only available in the AUR 119 | as well. The packages are built in the order they are defined. 120 | 121 | FUNCTIONS: 122 | prepare 123 | This is called after creating the rootfs directory, but before calling 124 | pacstrap. It can be used to copy files like mkinitcpio.conf that will 125 | be already be used by pacstrap. 126 | It's called from the environment that arch-ostree runs in. The path to 127 | the rootfs directory is stored in the variable "rootfs". 128 | post_install_early 129 | This is called after pacstrap inside a container that uses the 130 | pacstrapped directory as it's rootfs. This can be used to write the 131 | mirrorlist or init and populate the pacman keys which may be needed if 132 | you want to install AUR packages(which are installed after this step). 133 | post_install 134 | Called after both post_install_early and installing AUR packages. This 135 | can be used for common post installation steps like generating locales 136 | or enabling systemd services. 137 | EOF 138 | } 139 | 140 | prepare_live_env() { 141 | msg "Install required packages" 142 | pacman \ 143 | --needed \ 144 | --noconfirm \ 145 | -S \ 146 | ostree \ 147 | podman 148 | 149 | msg "Patch storage.conf" 150 | sed -i \ 151 | -e 's|^\(graphroot\s*=\s*\).*|\1"/mnt/setup/container-storage"|g' \ 152 | /etc/containers/storage.conf 153 | 154 | msg "Patch containers.conf" 155 | sed -i \ 156 | -e 's|^# \(image_copy_tmp_dir\s*=\s*\).*|\1"/mnt/setup/container-tmp"|g' \ 157 | /etc/containers/containers.conf 158 | 159 | mkdir -p "/mnt/setup/container-tmp" 160 | } 161 | 162 | pacstrap_rootfs_directory() { 163 | # Without the increased pids-limit, pacstrap may fail at verifying 164 | # package integrity. 165 | podman run \ 166 | --pids-limit 4096 \ 167 | --cap-add sys_admin \ 168 | --cap-add mknod \ 169 | --security-opt apparmor=unconfined \ 170 | --net=host \ 171 | "${pacman_cache_args[@]}" \ 172 | -v "$rootfs_dir:/mnt" \ 173 | --rm -it \ 174 | "localhost/$builder_tag" \ 175 | pacstrap -c -G -M /mnt "$@" 176 | } 177 | 178 | deploy_env() { 179 | # We want to mount everything that the user has mounted at 180 | # `sysroot_dir`(usually /mnt). This gives us all of the bootloader 181 | # partitions. 182 | findmnt_output="$(findmnt \ 183 | --all \ 184 | --real \ 185 | --submounts \ 186 | --mountpoint "$sysroot_dir" \ 187 | --list \ 188 | --noheadings \ 189 | --output TARGET \ 190 | | tail -n +2)" 191 | 192 | mkdir -p "$deploy_env_dir/etc" 193 | 194 | mount -o ro,bind "$deploy_env_dir" "$deploy_env_dir" 195 | mount -o ro,bind "$deploy_env_dir/usr/etc" "$deploy_env_dir/etc" 196 | 197 | for path in /run /tmp /var; do 198 | mount -t tmpfs tmpfs "${deploy_env_dir}${path}" 199 | done 200 | 201 | for path in home mnt roothome usrlocal srv opt; do 202 | mkdir "$deploy_env_dir/var/$path" 203 | done 204 | 205 | for path in /dev /proc /sys; do 206 | mount --rbind "$path" "${deploy_env_dir}${path}" 207 | done 208 | 209 | mount -o rw,bind "$sysroot_dir" "${deploy_env_dir}/sysroot" 210 | 211 | mounted_boot=false 212 | if [ -n "$findmnt_output" ]; then 213 | while read -r source; do 214 | target="$(realpath --relative-to="$sysroot_dir" "$source")" 215 | mount -o bind "$source" "${deploy_env_dir}/$target" 216 | 217 | if [ "$target" = "boot" ]; then 218 | mounted_boot=true 219 | fi 220 | done <<< "$findmnt_output" 221 | fi 222 | 223 | # In case there is no separate boot partition we need to 224 | # bind-mount the boot directory so a `grub-install` within the 225 | # deploy env installs to the actual boot directory outside of 226 | # the temporary deployment rootfs. 227 | if [ -e "$sysroot_dir/boot" ] && [ $mounted_boot = false ]; then 228 | mount --mkdir -o bind "$sysroot_dir/boot" "$deploy_env_dir/boot" 229 | fi 230 | 231 | chroot "$deploy_env_dir" "${args[@]}" 232 | } 233 | 234 | build_rootfs_directory() { 235 | rootfs="$rootfs_dir" 236 | build_script="$(realpath "$1")" 237 | build_scriptdir=$(dirname "$build_script") 238 | 239 | # shellcheck disable=SC1090 240 | source "$build_script" 241 | 242 | post_install_args=() 243 | # shellcheck disable=SC2236 # This doesn't work with -n 244 | if [ ! -z "${aur_packages+x}" ]; then 245 | msg "Build AUR packages" 246 | 247 | if [ -d "$aur_dir" ]; then 248 | rm -r "$aur_dir" 249 | fi 250 | mkdir "$aur_dir" 251 | 252 | podman run \ 253 | "${pacman_cache_args[@]}" \ 254 | --net=host \ 255 | --rm -it \ 256 | -v "$aur_dir:/tmp/aur" \ 257 | -v "$lib_dir/build_aur_packages:/tmp/script" \ 258 | "$builder_tag" \ 259 | /tmp/script \ 260 | "${aur_packages[@]}" 261 | 262 | post_install_args+=(-v "$aur_dir:/tmp/aur") 263 | fi 264 | 265 | rm -rf "$rootfs" 266 | if [ -e "$rootfs" ]; then 267 | die "Failed to delete rootfs directory" 268 | fi 269 | 270 | if have_function prepare; then 271 | msg "Prepare" 272 | pushd "$build_scriptdir" 273 | prepare 274 | popd 275 | fi 276 | 277 | msg "Pacstrap" 278 | pacstrap_rootfs_directory "${packages[@]}" 279 | 280 | msg "Post-install" 281 | podman run \ 282 | "${post_install_args[@]}" \ 283 | --net=host \ 284 | --rm -it \ 285 | --tmpfs /tmp \ 286 | --tmpfs /run \ 287 | -v "$build_scriptdir:/tmp/work" \ 288 | -v "$build_script:/tmp/script" \ 289 | -v "$lib_dir/common.sh:/tmp/common.sh" \ 290 | -v "$lib_dir/build_rootfs_post_install:/tmp/entrypoint" \ 291 | -w /tmp/work \ 292 | --entrypoint /tmp/entrypoint \ 293 | --rootfs "$rootfs" 294 | } 295 | 296 | long_opts=( 297 | aur-dir: 298 | builder-tag: 299 | deploy-env-dir: 300 | help 301 | ostree-repo: 302 | pacman-cache: 303 | podman-build-cache: 304 | rootfs-containerfile: 305 | rootfs-dir: 306 | rootfs-tag: 307 | sysroot-dir: 308 | ) 309 | if ! temp=$(getopt -o '' --long "$(join_by , "${long_opts[@]}")" -- "$@"); then 310 | die "Invalid arguments" 311 | fi 312 | 313 | eval set -- "$temp" 314 | while true; do 315 | case "$1" in 316 | '--builder-tag'|\ 317 | '--rootfs-tag') 318 | name="$(arg_to_varname "$1")" 319 | printf -v "$name" "%s" "$2" 320 | shift 2 321 | continue 322 | ;; 323 | '--aur-dir'|\ 324 | '--deploy-env-dir'|\ 325 | '--ostree-repo'|\ 326 | '--pacman-cache'|\ 327 | '--rootfs-containerfile'|\ 328 | '--rootfs-dir'|\ 329 | '--sysroot-dir') 330 | name="$(arg_to_varname "$1")" 331 | value="$(realpath "$2")" 332 | printf -v "$name" "%s" "$value" 333 | shift 2 334 | continue 335 | ;; 336 | '--podman-build-cache') 337 | name="$(arg_to_varname "$1")" 338 | 339 | case "$2" in 340 | 'true'|'1'|'yes') 341 | eval "$name=true" 342 | ;; 343 | 'false'|'0'|'no') 344 | eval "$name=false" 345 | ;; 346 | *) 347 | die "Unsupported bool value: $2" 348 | ;; 349 | esac 350 | 351 | shift 2 352 | continue 353 | ;; 354 | '--help') 355 | usage 356 | exit $(( $# ? 0 : 1 )) 357 | ;; 358 | '--') 359 | shift 360 | break 361 | ;; 362 | *) 363 | die "BUG: Unexpected argument: $1" 364 | ;; 365 | esac 366 | done 367 | 368 | if [ -z ${1+x} ]; then 369 | die "Missing command argument" 370 | fi 371 | command="${1}" 372 | shift 1 373 | 374 | pacman_cache_args=() 375 | # shellcheck disable=SC2236 # This doesn't work with -n 376 | if [ ! -z ${pacman_cache+x} ]; then 377 | mkdir -p "$pacman_cache" 378 | pacman_cache_args=( 379 | -v "$pacman_cache:/var/cache/pacman/pkg" 380 | ) 381 | fi 382 | 383 | podman_build_cache_args=() 384 | if [ "$podman_build_cache" = false ]; then 385 | podman_build_cache_args+=(--no-cache) 386 | export BUILDAH_LAYERS=false 387 | fi 388 | 389 | case "$command" in 390 | 'prepare_live_env') 391 | prepare_live_env 392 | ;; 393 | 'build_builder_container') 394 | podman build \ 395 | --net=host \ 396 | "${podman_build_cache_args[@]}" \ 397 | -f "$share_dir/Containerfile.builder" \ 398 | -t "$builder_tag" 399 | ;; 400 | 401 | 'build_rootfs_container') 402 | podman build \ 403 | --net=host \ 404 | "${podman_build_cache_args[@]}" \ 405 | "${pacman_cache_args[@]}" \ 406 | --cap-add sys_admin \ 407 | --cap-add mknod \ 408 | -f "$rootfs_containerfile" \ 409 | -t "$rootfs_tag" 410 | ;; 411 | 'enter_rootfs_container') 412 | podman run \ 413 | --net=host \ 414 | --rm -it \ 415 | "localhost/$rootfs_tag" \ 416 | "$@" 417 | ;; 418 | 'commit_rootfs_container') 419 | podman run \ 420 | --net=host \ 421 | --cap-add sys_admin \ 422 | --security-opt apparmor=unconfined \ 423 | --mount "type=image,src=localhost/$rootfs_tag,dst=/mnt-lower" \ 424 | --mount "type=bind,src=$ostree_repo,dst=/sysroot/ostree/repo" \ 425 | -v "$lib_dir:/opt/lib:ro" \ 426 | -v "$share_dir:/opt/share:ro" \ 427 | --rm -it \ 428 | "localhost/$builder_tag" \ 429 | /opt/lib/commit_rootfs "$@" 430 | ;; 431 | 432 | 'pacstrap_rootfs_directory') 433 | if [ -d "$rootfs_dir" ]; then 434 | warning "rootfs directory exists already." 435 | else 436 | mkdir -p "$rootfs_dir" 437 | fi 438 | 439 | pacstrap_rootfs_directory "$@" 440 | ;; 441 | 'build_rootfs_directory') 442 | build_rootfs_directory "$@" 443 | ;; 444 | 'enter_rootfs_directory') 445 | podman run \ 446 | --net=host \ 447 | --rm -it \ 448 | --rootfs "$rootfs_dir:O" \ 449 | /bin/bash "$@" 450 | ;; 451 | 'commit_rootfs_directory') 452 | podman run \ 453 | --net=host \ 454 | --cap-add sys_admin \ 455 | --security-opt apparmor=unconfined \ 456 | --mount "type=bind,src=$rootfs_dir,dst=/mnt-lower,ro" \ 457 | --mount "type=bind,src=$ostree_repo,dst=/sysroot/ostree/repo" \ 458 | -v "$lib_dir:/opt/lib:ro" \ 459 | -v "$share_dir:/opt/share:ro" \ 460 | --tmpfs /overlay \ 461 | --rm -it \ 462 | "localhost/$builder_tag" \ 463 | /opt/lib/commit_rootfs "$@" 464 | ;; 465 | 'deploy_env') 466 | args=("$@") 467 | unshare -m bash -c "set -euo pipefail; $(declare_all); deploy_env" 468 | ;; 469 | *) 470 | die "Unsupported command: ${command}" 471 | ;; 472 | esac 473 | 474 | msg "Successful" 475 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | --------------------------------------------------------------------------------