Skip to content

Instantly share code, notes, and snippets.

@kevinmeziere
Last active December 28, 2015 01:49
Show Gist options
  • Save kevinmeziere/7423257 to your computer and use it in GitHub Desktop.
Save kevinmeziere/7423257 to your computer and use it in GitHub Desktop.
FreeBSD 9.2 Install ZFS w/ Mirror
#!/bin/sh
#
# Calomel.org
# https://calomel.org/zfs_freebsd_root_install.html
# FreeBSD 9.2-RELEASE ZFS Root Install script
# Original file: zfs.sh @ Version 0.15
echo "# remove any old partitions on destination drives"
umount zroot
umount /mnt
zpool destroy zroot
gpart delete -i 3 ada0
gpart delete -i 2 ada0
gpart delete -i 1 ada0
gpart destroy -F ada0
gpart delete -i 3 ada1
gpart delete -i 2 ada1
gpart delete -i 1 ada1
gpart destroy -F ada1
echo ""
echo "# Create the boot and zfs data partitions"
gpart create -s gpt ada0
gpart create -s gpt ada1
gpart add -b 34 -s 94 -t freebsd-boot ada0
gpart add -b 34 -s 94 -t freebsd-boot ada1
gpart add -t freebsd-zfs -l disk0 ada0
gpart add -t freebsd-zfs -l disk1 ada1
gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 ada0
gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 ada1
echo ""
echo "# Align the Disks for 4K and create the pool"
gnop create -S 4096 /dev/gpt/disk0 /dev/gpt/disk1
zpool create -f -o altroot=/mnt -o cachefile=/var/tmp/zpool.cache zroot /dev/gpt/disk0.nop
zpool export zroot
gnop destroy /dev/gpt/disk0.nop
zpool import -o altroot=/mnt -o cachefile=/var/tmp/zpool.cache zroot
echo ""
echo "# Set the bootfs property and set options"
zpool set bootfs=zroot zroot
zpool set listsnapshots=on zroot
zpool set autoreplace=on zroot
zpool set autoexpand=on zroot
zfs set checksum=fletcher4 zroot
zfs set atime=off zroot
zfs set copies=2 zroot
# FreeBSD 9.2 and 10 Compression, ZFS v5 pool v5000
zpool set feature@lz4_compress=enabled zroot
zfs set compression=lz4 zroot
echo ""
echo "# Add swap space and apply options"
zfs create -V 1G zroot/swap
zfs set org.freebsd:swap=on zroot/swap
zfs set checksum=off zroot/swap
zfs set copies=1 zroot/swap
echo ""
echo "# Create a symlink to /home and fix some permissions"
cd /mnt/zroot ; ln -s usr/home home
echo ""
echo "# Install FreeBSD OS from *.txz memstick."
echo "# This will take a few minutes..."
cd /usr/freebsd-dist
export DESTDIR=/mnt/zroot
# Option 1: only install a 64bit os without 32bit libs
for file in base.txz kernel.txz doc.txz ports.txz src.txz;
# Option 2: also include 32bit compatable libs
#for file in base.txz lib32.txz kernel.txz doc.txz ports.txz src.txz;
do (cat $file | tar --unlink -xpJf - -C ${DESTDIR:-/}); done
echo ""
echo "# Copy zpool.cache to install disk."
cp /var/tmp/zpool.cache /mnt/zroot/boot/zfs/zpool.cache
echo ""
echo "# Setup ZFS root mount and boot"
echo 'zfs_enable="YES"' >> /mnt/zroot/etc/rc.conf
echo 'zfs_load="YES"' >> /mnt/zroot/boot/loader.conf
echo 'vfs.root.mountfrom="zfs:zroot"' >> /mnt/zroot/boot/loader.conf
echo ""
echo "# enable networking, allow ssh and stop syslog from listening."
echo 'hostname="FreeBSDzfs"' >> /mnt/zroot/etc/rc.conf
echo 'ifconfig_igb0="dhcp lladdr 00:11:22:33:44:55"' >> /mnt/zroot/etc/rc.conf
echo '#ifconfig_igb0="inet 192.168.0.150 netmask 255.255.255.0 lladdr 00:11:22:33:44:55"' >> /mnt/zroot/etc/rc.conf
echo '#defaultrouter="192.168.0.1"' >> /mnt/zroot/etc/rc.conf
echo 'sshd_enable="YES"' >> /mnt/zroot/etc/rc.conf
echo 'syslogd_flags="-ss"' >> /mnt/zroot/etc/rc.conf
echo ""
echo "# sshd, disable remote root logins."
echo 'PermitRootLogin no' >> /mnt/zroot/etc/ssh/sshd_config
echo 'PermitEmptyPasswords no' >> /mnt/zroot/etc/ssh/sshd_config
echo ""
echo "# /etc/rc.conf disable sendmail"
echo 'dumpdev="NO"' >> /mnt/zroot/etc/rc.conf
echo 'sendmail_enable="NO"' >> /mnt/zroot/etc/rc.conf
echo 'sendmail_submit_enable="NO"' >> /mnt/zroot/etc/rc.conf
echo 'sendmail_outbound_enable="NO"' >> /mnt/zroot/etc/rc.conf
echo 'sendmail_msp_queue_enable="NO"' >> /mnt/zroot/etc/rc.conf
echo ""
echo "# touch the /etc/fstab else freebsd will not boot properly"
touch /mnt/zroot/etc/fstab
sync
echo ""
echo "# Syncing... Install Done."
sync
#### EOF ####
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment