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

Fixes and improvements based on experience #4

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
78 changes: 49 additions & 29 deletions debian-stretch-zfs-root.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,30 @@
# with this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
#

### Static settings
set -e

ZPOOL=tank
TARGETDIST=stretch
### Static settings, overridable by environment variables

PARTBIOS=1
PARTEFI=2
PARTZFS=3
ZPOOL=${ZPOOL:-tank}
TARGETDIST=${TARGETDIST:-stretch}

SIZESWAP=2G
SIZETMP=3G
SIZEVARTMP=3G
SIZESWAP=${SIZESWAP:-2G}
SIZETMP=${SIZETMP:-3G}
SIZEVARTMP=${SIZEVARTMP:-3G}

GRUBPKG=${GRUBPKG:-grub-pc}
#GRUBPKG=grub-efi-amd64 # INCOMPLETE NOT TESTED

test -n "$ETHDEV" || ETHDEV=$(ip -o link show|grep -v ' lo:'|head -n 1|cut -d: -f2|sed -e 's/ //g')

PARTBIOS=${PARTBIOS:-1}
PARTEFI=${PARTEFI:-2}
PARTZFS=${PARTZFS:-3}

ENABLE_POSIXACL=${ENABLE_POSIXACL:-no}

# NEWHOST is also used for hostname of the new system, if set (if unset, is
# taken from freshly generated hostid)

### User settings

Expand All @@ -62,9 +74,9 @@ fi

while read -r DISK; do
if [ -z "${BYID[$DISK]}" ]; then
DISKS+=("$DISK")
ZFSPARTITIONS+=("$DISK$PARTZFS")
EFIPARTITIONS+=("$DISK$PARTEFI")
DISKS+=("/dev/$DISK")
ZFSPARTITIONS+=("/dev/$DISK$PARTZFS")
EFIPARTITIONS+=("/dev/$DISK$PARTEFI")
else
DISKS+=("${BYID[$DISK]}")
ZFSPARTITIONS+=("${BYID[$DISK]}-part$PARTZFS")
Expand Down Expand Up @@ -101,7 +113,7 @@ case "$RAIDLEVEL" in
RAIDDEF+=" mirror"
fi
RAIDDEF+=" $ZFSPARTITION"
((I++))
((I++)) || true
done
;;
*)
Expand Down Expand Up @@ -132,19 +144,25 @@ case $DEBRELEASE in
8*)
echo "deb http://http.debian.net/debian/ jessie-backports main contrib non-free" >/etc/apt/sources.list.d/jessie-backports.list
test -f /var/lib/apt/lists/http.debian.net_debian_dists_jessie-backports_InRelease || apt-get update
test -d /usr/share/doc/zfs-dkms || DEBIAN_FRONTEND=noninteractive apt-get install --yes gdisk debootstrap dosfstools zfs-dkms/jessie-backports
if [ ! -d /usr/share/doc/zfs-dkms ]; then NEED_PACKAGES+=(zfs-dkms/jessie-backports); fi
;;

9*)
echo "deb http://deb.debian.org/debian/ stretch contrib non-free" >/etc/apt/sources.list.d/contrib-non-free.list
test -f /var/lib/apt/lists/deb.debian.org_debian_dists_stretch_non-free_binary-amd64_Packages || apt-get update
test -d /usr/share/doc/zfs-dkms || DEBIAN_FRONTEND=noninteractive apt-get install --yes gdisk debootstrap dosfstools zfs-dkms
if [ ! -d /usr/share/doc/zfs-dkms ]; then NEED_PACKAGES+=(zfs-dkms); fi
;;
*)
echo "Unsupported Debian Live CD release" >&2
exit 1
;;
esac
if [ ! -f /sbin/zpool ]; then NEED_PACKAGES+=(zfsutils-linux); fi
if [ ! -f /usr/sbin/debootstrap ]; then NEED_PACKAGES+=(debootstrap); fi
if [ ! -f /sbin/sgdisk ]; then NEED_PACKAGES+=(gdisk); fi
if [ ! -f /sbin/mkdosfs ]; then NEED_PACKAGES+=(dosfstools); fi
echo "Need packages: ${NEED_PACKAGES[@]}"
if [ -n "${NEED_PACKAGES[*]}" ]; then DEBIAN_FRONTEND=noninteractive apt-get install --yes "${NEED_PACKAGES[@]}"; fi

modprobe zfs
if [ $? -ne 0 ] ; then
Expand All @@ -168,7 +186,7 @@ sleep 2
# Workaround for Debian's grub, especially grub-probe, not supporting all ZFS features
# Using "-d" to disable all features, and selectivly enable features later (but NOT 'hole_birth' and 'embedded_data')
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=776676
zpool create -f -o ashift=12 -d -o altroot=/target -O atime=off -O mountpoint=none $ZPOOL $RAIDDEF
zpool create -f -o ashift=12 -d -o altroot=/target -o autoexpand=on -O atime=off -O mountpoint=none $ZPOOL $RAIDDEF
if [ $? -ne 0 ] ; then
echo "Unable to create zpool '$ZPOOL'" >&2
exit 1
Expand All @@ -178,15 +196,18 @@ for ZFSFEATURE in async_destroy empty_bpobj lz4_compress spacemap_histogram enab
done
zfs set compression=lz4 $ZPOOL
# The two properties below improve performance but reduce compatibility with non-Linux ZFS implementations
# Commented out by default
#zfs set xattr=sa $ZPOOL
#zfs set acltype=posixacl $ZPOOL
case "$ENABLE_POSIXACL" in
y*)
zfs set xattr=sa $ZPOOL
zfs set acltype=posixacl $ZPOOL
;;
esac

zfs create $ZPOOL/ROOT
zfs create -o mountpoint=/ $ZPOOL/ROOT/debian-$TARGETDIST
zpool set bootfs=$ZPOOL/ROOT/debian-$TARGETDIST $ZPOOL

zfs create -o mountpoint=/tmp -o setuid=off -o exec=off -o quota=$SIZETMP $ZPOOL/tmp
zfs create -o mountpoint=/tmp -o setuid=off -o exec=off -o devices=off -o com.sun:auto-snapshot=false -o quota=$SIZETMP $ZPOOL/tmp
chmod 1777 /target/tmp

# /var needs to be mounted via fstab, the ZFS mount script runs too late during boot
Expand All @@ -201,6 +222,8 @@ mount -t zfs $ZPOOL/var/tmp /target/var/tmp
chmod 1777 /target/var/tmp

zfs create -V $SIZESWAP -b $(getconf PAGESIZE) -o primarycache=metadata -o com.sun:auto-snapshot=false -o logbias=throughput -o sync=always $ZPOOL/swap
# sometimes needed to wait for /dev/zvol/$ZPOOL/swap to appear
sleep 2
mkswap -f /dev/zvol/$ZPOOL/swap

zpool status
Expand All @@ -218,12 +241,12 @@ for EFIPARTITION in "${EFIPARTITIONS[@]}"; do
mkdir -pv /mnt/efi-$I
mount $EFIPARTITION /mnt/efi-$I
fi
((I++))
((I++)) || true
done

debootstrap --include=openssh-server,locales,joe,rsync,sharutils,psmisc,htop,patch,less $TARGETDIST /target http://http.debian.net/debian/

NEWHOST=debian-$(hostid)
test -n NEWHOST || NEWHOST=debian-$(hostid)}
echo $NEWHOST >/target/etc/hostname
sed -i "1s/^/127.0.1.1\t$NEWHOST\n/" /target/etc/hosts

Expand Down Expand Up @@ -255,9 +278,6 @@ chroot /target /usr/sbin/locale-gen
perl -i -pe 's/main$/main contrib non-free/' /target/etc/apt/sources.list
chroot /target /usr/bin/apt-get update

GRUBPKG=grub-pc
#GRUBPKG=grub-efi-amd64 # INCOMPLETE NOT TESTED

chroot /target /usr/bin/apt-get install --yes linux-image-amd64 grub2-common $GRUBPKG zfs-initramfs zfs-dkms
grep -q zfs /target/etc/default/grub || perl -i -pe 's/quiet/boot=zfs quiet/' /target/etc/default/grub
chroot /target /usr/sbin/update-grub
Expand Down Expand Up @@ -285,10 +305,10 @@ if [ -d /proc/acpi ]; then
chroot /target service acpid stop
fi

ETHDEV=$(udevadm info -e | grep "ID_NET_NAME_PATH=" | head -n1 | cut -d= -f2)
test -n "$ETHDEV" || ETHDEV=enp0s1
echo -e "\nauto $ETHDEV\niface $ETHDEV inet dhcp\n" >>/target/etc/network/interfaces
echo -e "nameserver 8.8.8.8\nnameserver 8.8.4.4" >> /target/etc/resolv.conf
if [ -n "$ETHDEV" ]; then
echo -e "\nauto $ETHDEV\niface $ETHDEV inet dhcp\n" >>/target/etc/network/interfaces
echo -e "nameserver 8.8.8.8\nnameserver 8.8.4.4" >> /target/etc/resolv.conf
fi

chroot /target /usr/bin/passwd
chroot /target /usr/sbin/dpkg-reconfigure tzdata
Expand Down