Skip to content

Instantly share code, notes, and snippets.

@dlangille
Last active December 21, 2018 09:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlangille/95c47fcad400f2e78e29 to your computer and use it in GitHub Desktop.
Save dlangille/95c47fcad400f2e78e29 to your computer and use it in GitHub Desktop.
ZFS root install
# Based on http://www.aisecure.net/2012/01/16/rootzfs/ and
# @vermaden's guide on the forums
# set your disks here
DISKS="ada0 ada1"
# where will we fetch the install from?
FTPURL="ftp://ftp.freebsd.org/pub/FreeBSD/releases/amd64/10.1-RELEASE"
# the hostname this computer will have:
HOSTNAME="tallboy.example.org"
# the primary IP address for this server
IP="10.5.0.1"
# the netmask for this server
NETMASK="255.255.255.248"
# the default gateway for this server i.e. defaultrouter
GATEWAY="10.0.0.1"
gmirror load
gmirror stop swap
NUM=-1
for I in ${DISKS}; do
NUM=$(($NUM + 1))
gpart destroy -F ${I}
gpart create -s gpt ${I}
gpart add -a4k -s512k -t freebsd-boot -l bootcode${NUM} ${I}
# create our swap partition
gpart add -a4k -b1m -s 8g -t freebsd-swap -l swap${I} ${I}
#
# note: not using all the disk, on purpose, adjust this size for your HDD
#
gpart add -s 455G -t freebsd-zfs -l disk${NUM} ${I}
gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 ${I}
gnop create -S 4096 /dev/gpt/disk${NUM}
done
# create our gmirror for swap
gmirror label -F -h -b round-robin swap /dev/gpt/swap*
# create our main pool, based on those nop... which gets us well aligned.
zpool create -f -O mountpoint=none \
-O canmount=off \
-O atime=off \
-O checksum=fletcher4 \
-o cachefile=/tmp/zpool.cache \
zroot mirror /dev/gpt/disk*.nop
zpool export zroot
# now, drop those nops....
NUM=-1
for I in ${DISKS}; do
NUM=$(($NUM + 1))
gnop destroy /dev/gpt/disk${NUM}.nop
done
# import again (in previous scripts, this was altroot=/mnt
zpool import -o altroot=/mnt -o cachefile=/tmp/zpool.cache zroot
zfs set mountpoint=none zroot
zfs set checksum=fletcher4 zroot
zfs set atime=off zroot
zfs create -o mountpoint=none zroot/bootenv
zfs create -o mountpoint=/ zroot/bootenv/default
zfs create -o mountpoint=/tmp -o compression=lz4 -o setuid=off zroot/tmp
zfs create -o mountpoint=/usr -o canmount=off zroot/usr
zfs create zroot/usr/local
zfs create -o setuid=off zroot/usr/home
zfs create -o compression=lz4 -o setuid=off zroot/usr/ports
zfs create -o compression=off -o exec=off -o setuid=off zroot/usr/ports/distfiles
zfs create -o compression=off -o exec=off -o setuid=off zroot/usr/ports/packages
zfs create -o compression=lz4 -o exec=off -o setuid=off zroot/usr/src
zfs create zroot/usr/obj
zfs create -o mountpoint=/var zroot/var
zfs create -o compression=lz4 -o exec=off -o setuid=off zroot/var/crash
zfs create -o exec=off -o setuid=off zroot/var/db
zfs create -o compression=lz4 -o exec=on -o setuid=off zroot/var/db/pkg
zfs create -o readonly=on -o exec=off -o setuid=off zroot/var/empty
zfs create -o compression=lz4 -o exec=off -o setuid=off zroot/var/log
zfs create -o compression=lz4 -o exec=off -o setuid=off zroot/var/mail
zfs create -o exec=off -o setuid=off zroot/var/run
zfs create -o compression=lz4 -o exec=on -o setuid=off zroot/var/tmp
#
# We are in /mnt because that's what we set the mount point to...
#
chmod 1777 /mnt/tmp
chmod 1777 /mnt/var/tmp
# add this symlink
cd /mnt ; ln -s usr/home home
# do the install
echo starting the fetch and install
cd /mnt/tmp
export DESTDIR=/mnt
for file in base.txz kernel.txz doc.txz
do
echo fetching ${file}
fetch ${FTPURL}/${file}
echo extratcting ${file}
cat ${file} | tar --unlink -xpJf - -C ${DESTDIR:-/}
rm ${file}
done
echo finished with fetch and install
cp /tmp/zpool.cache /mnt/boot/zfs/zpool.cache
# overwrite the /etc/fstab file and direct everything to ZFS
cat << EOF > /mnt/etc/fstab
/dev/mirror/swap none swap sw 0 0
EOF
cat << EOF >> /mnt/boot/loader.conf
geom_mirror_load="YES"
zfs_load="YES"
vfs.root.mountfrom="zfs:zroot/bootenv/default"
EOF
# start sshd
cat << EOF >> /mnt/etc/rc.conf
zfs_enable="YES"
hostname="${HOSTNAME}"
ifconfig_em1="inet ${IP} netmask ${NETMASK}"
defaultrouter="${GATEWAY}"
sshd_enable="YES"
ntpd_enable="YES"
EOF
zpool set bootfs=zroot/bootenv/default zroot
zfs umount -a
zfs set mountpoint=/zroot zroot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment