Skip to content

Instantly share code, notes, and snippets.

@mskarbek
Last active June 29, 2019 01:21
Show Gist options
  • Save mskarbek/a0701fe21d6918ebaa872a3f58e63036 to your computer and use it in GitHub Desktop.
Save mskarbek/a0701fe21d6918ebaa872a3f58e63036 to your computer and use it in GitHub Desktop.

Revisions

  1. mskarbek revised this gist Jun 16, 2018. 1 changed file with 20 additions and 14 deletions.
    34 changes: 20 additions & 14 deletions install_on_zfs.sh
    Original file line number Diff line number Diff line change
    @@ -2,14 +2,14 @@

    set -ex

    if [ -z ${POOL_NAME} ]
    if [ -z ${POOL_DISK} ]
    then
    POOL_NAME="system"
    POOL_DISK="/dev/vda"
    fi

    if [ -z ${POOL_DISK} ]
    if [ -z ${POOL_NAME} ]
    then
    POOL_DISK="/dev/vda"
    POOL_NAME="system"
    fi

    if [ -z ${HOST_NAME} ]
    @@ -116,7 +116,10 @@ then
    fi
    sed -i 's/root:\*:/root::/' /run/${POOL_NAME}/etc/shadow

    curl -L -o /run/${POOL_NAME}/etc/yum.repos.d/fedora-zfs.repo http://repo.skarbek.name/fedora-zfs/fedora-zfs.repo
    curl -L -o /run/${POOL_NAME}/etc/yum.repos.d/fedora-zfs.repo http://repo.commandcentral.space/fedora-zfs/fedora-zfs.repo

    sed -i '0,/metalink=/s/metalink=/baseurl=http:\/\/10\.171\.37\.120\/$releasever\/release\/\n#metalink=/' /run/${POOL_NAME}/etc/yum.repos.d/fedora.repo
    sed -i '0,/metalink=/s/metalink=/baseurl=http:\/\/10\.171\.37\.120\/$releasever\/updates\/\n#metalink=/' /run/${POOL_NAME}/etc/yum.repos.d/fedora-updates.repo

    dnf -y --installroot=/run/${POOL_NAME} --nodocs --nogpg --releasever=28 \
    install \
    @@ -142,16 +145,19 @@ dnf -y --installroot=/run/${POOL_NAME} clean all

    sync

    MACHINE_ID=$(cat /run/${POOL_NAME}/etc/machine-id)
    chroot /run/system setfiles -v -F -e /boot -e /dev -e /proc -e /sys /etc/selinux/targeted/contexts/files/file_contexts /

    cat << EOF > /run/${POOL_NAME}/boot/loader/entries/${MACHINE_ID}-${KERNEL_VERSION}-selinux.conf
    title Fedora 28 (SELinux autorelabel)
    version ${KERNEL_VERSION}
    machine-id ${MACHINE_ID}
    linux /vmlinuz-${KERNEL_VERSION}
    initrd /initramfs-${KERNEL_VERSION}.img
    options root=ZFS=${POOL_NAME}/rootfs/fedora28 enforcing=0 autorelabel=1 quiet
    EOF
    chroot /run/system setfiles -v -F /etc/selinux/targeted/contexts/files/file_contexts /home
    chroot /run/system setfiles -v -F /etc/selinux/targeted/contexts/files/file_contexts /root
    chroot /run/system setfiles -v -F /etc/selinux/targeted/contexts/files/file_contexts /var/cache
    chroot /run/system setfiles -v -F /etc/selinux/targeted/contexts/files/file_contexts /var/lib/flatpak
    chroot /run/system setfiles -v -F /etc/selinux/targeted/contexts/files/file_contexts /var/lib/docker
    chroot /run/system setfiles -v -F /etc/selinux/targeted/contexts/files/file_contexts /var/lib/containers
    chroot /run/system setfiles -v -F /etc/selinux/targeted/contexts/files/file_contexts /var/log
    chroot /run/system setfiles -v -F /etc/selinux/targeted/contexts/files/file_contexts /var/spool
    chroot /run/system setfiles -v -F /etc/selinux/targeted/contexts/files/file_contexts /var/tmp

    MACHINE_ID=$(cat /run/${POOL_NAME}/etc/machine-id)

    cat << EOF > /run/${POOL_NAME}/boot/loader/entries/${MACHINE_ID}-${KERNEL_VERSION}.conf
    title Fedora 28
  2. mskarbek created this gist Jun 16, 2018.
    172 changes: 172 additions & 0 deletions install_on_zfs.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,172 @@
    #!/bin/bash

    set -ex

    if [ -z ${POOL_NAME} ]
    then
    POOL_NAME="system"
    fi

    if [ -z ${POOL_DISK} ]
    then
    POOL_DISK="/dev/vda"
    fi

    if [ -z ${HOST_NAME} ]
    then
    HOST_NAME="zfshost"
    fi

    BOOT_PARTUUID=$(cat /proc/sys/kernel/random/uuid)
    POOL_PARTUUID=$(cat /proc/sys/kernel/random/uuid)

    sgdisk -Z ${POOL_DISK}
    sgdisk -n 1:0:+1024M -t 1:ef00 -u 1:${BOOT_PARTUUID} ${POOL_DISK}
    sgdisk -n 2:0:-8M -t 2:bf01 -u 2:${POOL_PARTUUID} ${POOL_DISK}

    sleep 1

    BOOT_PART=$(readlink -f /dev/disk/by-partuuid/${BOOT_PARTUUID})
    mkfs.vfat -n EFI -F32 ${BOOT_PART}

    zpool create -f \
    -o ashift=12 \
    -O mountpoint=/ \
    -O canmount=off \
    -O compression=lz4 \
    -O atime=off \
    -O checksum=sha256 \
    -O utf8only=on \
    -O xattr=sa \
    -O acltype=posixacl \
    -R /run/${POOL_NAME} \
    ${POOL_NAME} \
    /dev/disk/by-partuuid/${POOL_PARTUUID}

    sleep 1

    zfs create -o mountpoint=none ${POOL_NAME}/rootfs
    zfs create -o mountpoint=/ ${POOL_NAME}/rootfs/fedora28
    zfs create -o setuid=off ${POOL_NAME}/home
    zfs create -o mountpoint=/root ${POOL_NAME}/home/root
    zfs create -o canmount=off -o setuid=off -o exec=off ${POOL_NAME}/var
    zfs create -o com.sun:auto-snapshot=false ${POOL_NAME}/var/cache
    zfs create -o canmount=off ${POOL_NAME}/var/lib
    zfs create ${POOL_NAME}/var/lib/flatpak
    zfs create ${POOL_NAME}/var/lib/docker
    zfs create ${POOL_NAME}/var/lib/containers
    zfs create ${POOL_NAME}/var/log
    zfs create ${POOL_NAME}/var/spool
    zfs create -o com.sun:auto-snapshot=false -o exec=on ${POOL_NAME}/var/tmp

    mkdir -p /run/${POOL_NAME}/{boot,dev,etc,etc/rpm,proc,run,sys,tmp}

    mount ${BOOT_PART} /run/${POOL_NAME}/boot
    mount --bind /dev /run/${POOL_NAME}/dev
    mount --bind /proc /run/${POOL_NAME}/proc
    mount --bind /sys /run/${POOL_NAME}/sys
    mount -t tmpfs tmpfs /run/${POOL_NAME}/run
    mount -t tmpfs tmpfs /run/${POOL_NAME}/tmp

    touch /run/${POOL_NAME}/etc/fstab
    echo "PARTUUID=${BOOT_PARTUUID} /boot vfat umask=0077,shortname=winnt,x-initrd.mount 0 2" > /run/${POOL_NAME}/etc/fstab

    touch /run/${POOL_NAME}/etc/rpm/macros.image-language-conf
    echo "%_install_langs C:en:en_US:en_US.UTF-8" > /run/${POOL_NAME}/etc/rpm/macros.image-language-conf

    dnf -y --installroot=/run/${POOL_NAME} --nodocs --nogpg --releasever=28 \
    --disablerepo='*' --enablerepo=fedora --enablerepo=updates \
    install \
    bash-completion \
    binutils \
    cryptsetup \
    dbus-broker \
    dnf \
    dosfstools \
    dracut \
    fedora-release \
    gdisk \
    glibc-common \
    glibc-langpack-en \
    glibc-minimal-langpack \
    iproute \
    iputils \
    mbuffer \
    less \
    passwd \
    procps-ng \
    psmisc \
    rootfiles \
    rpm \
    selinux-policy-targeted \
    sudo \
    systemd \
    vim-minimal

    echo "deltarpm=0" >> /run/${POOL_NAME}/etc/dnf/dnf.conf

    chroot /run/${POOL_NAME} bootctl install --no-variables

    chroot /run/${POOL_NAME} systemctl enable systemd-resolved systemd-networkd dbus-broker
    chroot /run/${POOL_NAME} ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf
    echo -e "[Match]\nName=en*\n\n[Network]\nDHCP=yes\n" > /run/${POOL_NAME}/etc/systemd/network/10-dhcp.network
    if [ -n ${USB_STORAGE} ]
    then
    echo "add_drivers+=usb-storage" > /run/${POOL_NAME}/etc/dracut.conf.d/usb.conf
    fi
    sed -i 's/root:\*:/root::/' /run/${POOL_NAME}/etc/shadow

    curl -L -o /run/${POOL_NAME}/etc/yum.repos.d/fedora-zfs.repo http://repo.skarbek.name/fedora-zfs/fedora-zfs.repo

    dnf -y --installroot=/run/${POOL_NAME} --nodocs --nogpg --releasever=28 \
    install \
    kernel \
    kernel-core \
    kernel-modules \
    kernel-modules-extra

    dnf -y --installroot=/run/${POOL_NAME} --nodocs --nogpg --releasever=28 \
    install zfs-dracut

    chroot /run/${POOL_NAME} systemctl preset zfs-import-cache zfs-import-scan zfs-import.target zfs-mount zfs-share zfs-zed zfs.target

    KERNEL_VERSION=$(chroot /run/${POOL_NAME} bash -c "rpm -q kernel|sed 's/kernel-//'")

    zpool set cachefile=/run/${POOL_NAME}/etc/zfs/zpool.cache ${POOL_NAME}

    echo ${HOST_NAME} > /run/${POOL_NAME}/etc/hostname

    chroot /run/${POOL_NAME} dracut -fv --kver ${KERNEL_VERSION}

    dnf -y --installroot=/run/${POOL_NAME} clean all

    sync

    MACHINE_ID=$(cat /run/${POOL_NAME}/etc/machine-id)

    cat << EOF > /run/${POOL_NAME}/boot/loader/entries/${MACHINE_ID}-${KERNEL_VERSION}-selinux.conf
    title Fedora 28 (SELinux autorelabel)
    version ${KERNEL_VERSION}
    machine-id ${MACHINE_ID}
    linux /vmlinuz-${KERNEL_VERSION}
    initrd /initramfs-${KERNEL_VERSION}.img
    options root=ZFS=${POOL_NAME}/rootfs/fedora28 enforcing=0 autorelabel=1 quiet
    EOF

    cat << EOF > /run/${POOL_NAME}/boot/loader/entries/${MACHINE_ID}-${KERNEL_VERSION}.conf
    title Fedora 28
    version ${KERNEL_VERSION}
    machine-id ${MACHINE_ID}
    linux /vmlinuz-${KERNEL_VERSION}
    initrd /initramfs-${KERNEL_VERSION}.img
    options root=ZFS=${POOL_NAME}/rootfs/fedora28 quiet
    EOF

    echo "timeout 3" > /run/${POOL_NAME}/boot/loader/loader.conf
    echo "default ${MACHINE_ID}-*" >> /run/${POOL_NAME}/boot/loader/loader.conf

    umount {/run/${POOL_NAME}/boot,/run/${POOL_NAME}/dev,/run/${POOL_NAME}/proc,/run/${POOL_NAME}/sys,/run/${POOL_NAME}/run,/run/${POOL_NAME}/tmp}

    zfs snap -r ${POOL_NAME}@clean

    zpool export ${POOL_NAME}