freebsd-helpers/newzfs.sh

247 lines
6.8 KiB
Bash
Raw Permalink Normal View History

2017-04-19 02:28:55 +00:00
set -E
2017-04-19 04:28:03 +00:00
constructionsite="/mnt"
fstab="newfstab"
2017-04-19 02:28:55 +00:00
devices=$@
if [ "$devices" = "" ]
then
echo "No devices to format passed."
exit 1
fi
echo "This will make absolutely destructive changes to these devices: $devices"
2017-04-19 04:28:03 +00:00
echo "Make sure that you currently do not have zpools of the names \"root\" and \"boot\"."
echo "Make sure that you currently do not have gmirrors of the names \"var\" and \"down\"."
echo "The new system will be put together at $constructionsite. Make sure it's free."
2017-04-19 02:28:55 +00:00
echo "Type in 'yes' to continue."
read optin
if [ "$optin" = "yes" ] # yay for sh! miss a few spaces and kill your system, why not?
then
2017-04-19 04:28:03 +00:00
#echo "# device mountpoint fstype options dump pass" > $fstab
2017-04-24 02:43:18 +00:00
echo "tmpfs /tmp tmpfs rw,mode=777 0 0" >> $fstab
2017-04-24 00:38:00 +00:00
echo 'geom_eli_load="YES"' >> loader.conf
2017-04-24 02:24:58 +00:00
echo 'geom_mirror_load="YES"' >> loader.conf
2017-04-24 00:38:00 +00:00
echo 'aesni_load="YES"' >> loader.conf
echo 'zfs_load="YES"' >> loader.conf
echo 'tmpfs_load="YES"' >> loader.conf
echo 'coretemp_load="YES"' >> loader.conf
echo 'beastie_disable="YES"' >> loader.conf
echo 'kern.vty="vt"' >> loader.conf
2017-04-19 02:28:55 +00:00
echo ""
2017-04-23 22:28:42 +00:00
echo "Activating gmirror."
gmirror load
echo ""
2017-04-19 05:47:21 +00:00
echo "Generating secret key."
key_path="/tmp/disk.key"
dd if=/dev/random of=$key_path bs=4096 count=1
echo ""
2017-04-19 02:28:55 +00:00
i=0 # iterated, used to get different labels per device
for device in $devices
do
i=`expr $i + 1`
echo "Handling device $device."
echo ""
echo "Destroying current partition layout."
if ! gpart destroy -F $device; then
echo "Can't (even forcibly) destroy layout of device $device, aborting."
exit 1
fi
echo ""
echo "Creating new GPT partition layout."
gpart create -s gpt $device
echo ""
echo "Adding gptzfsboot partition."
2017-04-19 04:28:03 +00:00
gpart add -s 94 -t freebsd-boot -l gptzfsboot-$device $device
2017-04-19 02:28:55 +00:00
gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 $device
echo ""
echo "Adding boot partition."
2017-04-19 04:28:03 +00:00
gpart add -s 2G -t freebsd-zfs -l boot-$device $device
2017-04-19 02:28:55 +00:00
echo ""
echo "Adding swap partition."
2017-04-19 04:28:03 +00:00
gpart add -s 2G -t freebsd-swap -l swap-$device $device
2017-04-19 02:28:55 +00:00
echo ""
echo "Adding var partition."
2017-04-19 04:28:03 +00:00
gpart add -s 20G -t freebsd-ufs -l var-$device $device
2017-04-19 02:28:55 +00:00
echo ""
echo "Adding down partition."
2017-04-19 04:28:03 +00:00
gpart add -s 100G -t freebsd-ufs -l down-$device $device
2017-04-19 02:28:55 +00:00
echo ""
echo "Adding root partition using rest of space."
2017-04-19 04:28:03 +00:00
gpart add -t freebsd-zfs -l root-$device $device
2017-04-19 02:28:55 +00:00
echo ""
if [ ! -d "/tmp/boot" ]
then
mkdir /tmp/boot
fi
2017-04-19 05:47:21 +00:00
if [ $i -eq 1 ]
then
2017-04-19 06:26:44 +00:00
echo "Creating boot zpool…"
2017-04-19 05:47:21 +00:00
zpool create -fm /zboot -o altroot=/tmp/boot boot gpt/boot-$device
else
2017-04-19 06:26:44 +00:00
echo "Attaching to boot zpool…"
2017-04-19 05:47:21 +00:00
zpool attach boot gpt/boot-$device
2017-04-19 05:50:21 +00:00
fi
2017-04-19 02:28:55 +00:00
2017-04-19 06:26:44 +00:00
mkdir /tmp/boot/boot
2017-04-19 02:28:55 +00:00
echo ""
echo "Creating geli containers for all partitions to be crypted."
2017-04-23 22:13:45 +00:00
# root using CBC instead of XTS because zfs already does extensive checksum magics
geli init -b -e AES-CBC -l 256 -K $key_path -s 4096 gpt/root-$device
2017-04-24 00:57:55 +00:00
echo "geli_root-${device}_keyfile0_load=\"YES\"" >> loader.conf
echo "geli_root-${device}_keyfile0_type=\"gpt/root-$device:geli_keyfile0\"" >> loader.conf
echo "geli_root-${device}_keyfile0_name=\"/boot/disk.key\"" >> loader.conf
2017-04-24 00:38:00 +00:00
#tocrypt="gpt/var-$device gpt/down-$device gpt/swap-$device"
tocrypt="var down swap"
for name in $tocrypt
2017-04-19 02:28:55 +00:00
do
2017-04-24 00:38:00 +00:00
geli init -b -e AES-XTS -l 256 -K $key_path -s 4096 gpt/$name-$device
2017-04-24 00:49:10 +00:00
echo "geli_$name-${device}_keyfile0_load=\"YES\"" >> loader.conf
echo "geli_$name-${device}_keyfile0_type=\"gpt/$name-$device:geli_keyfile0\"" >> loader.conf
echo "geli_$name-${device}_keyfile0_name=\"/boot/disk.key\"" >> loader.conf
2017-04-19 02:28:55 +00:00
done
echo ""
2017-04-24 00:38:00 +00:00
echo "/dev/gpt/swap-$device.eli none swap sw 0 0" >> $fstab
2017-04-19 02:28:55 +00:00
echo "Attaching geli containers."
2017-04-23 22:21:52 +00:00
geli attach -k $key_path gpt/root-$device
2017-04-24 00:38:00 +00:00
for name in $tocrypt
2017-04-19 02:28:55 +00:00
do
2017-04-24 00:38:00 +00:00
geli attach -k $key_path gpt/$name-$device
2017-04-19 02:28:55 +00:00
done
echo ""
2017-04-19 05:47:21 +00:00
if [ $i -eq 1 ]
2017-04-19 04:28:03 +00:00
then
echo "Creating root zpool…"
2017-04-19 05:30:10 +00:00
zpool create -fm / -o altroot=$constructionsite root gpt/root-$device.eli
2017-04-24 02:24:58 +00:00
echo 'vfs.root.mountfrom="zfs:root"' >> loader.conf
2017-04-19 04:28:03 +00:00
echo ""
echo "Creating var gmirror…"
gmirror label -v var /dev/gpt/var-$device.eli
echo "Creating UFS on var gmirror…"
2017-04-19 05:54:52 +00:00
newfs -U /dev/mirror/var
2017-04-19 04:28:03 +00:00
echo "/dev/mirror/var /var ufs rw 0 2" >> $fstab
echo ""
echo "Creating down gmirror…"
gmirror label -v down /dev/gpt/down-$device.eli
echo "Creating UFS on down gmirror…"
2017-04-19 05:54:52 +00:00
newfs -U /dev/mirror/down
2017-04-19 04:28:03 +00:00
echo "/dev/mirror/down /media/down ufs rw 0 2" >> $fstab
echo ""
else
echo "Attaching to boot zpool…"
2017-04-19 05:47:21 +00:00
zpool attach boot gpt/boot-$device
2017-04-19 04:28:03 +00:00
echo ""
echo "Attaching to root zpool…"
2017-04-19 05:47:21 +00:00
zpool attach root gpt/root-$device.eli
2017-04-19 04:28:03 +00:00
echo ""
echo "Adding to var gmirror…"
gmirror insert var /dev/gpt/var-$device.eli
echo ""
echo "Adding to down gmirror…"
gmirror insert down /dev/gpt/down-$device.eli
echo ""
fi
2017-04-19 05:47:21 +00:00
done
2017-04-19 04:28:03 +00:00
2017-04-19 05:30:10 +00:00
2017-04-19 06:26:44 +00:00
echo "Exporting boot zpool…"
2017-04-19 05:47:21 +00:00
zpool export boot
2017-04-19 06:26:44 +00:00
echo "Re-importing boot zpool at $constructionsite"
2017-04-23 22:17:25 +00:00
zpool import -o altroot=$constructionsite boot
2017-04-19 05:47:21 +00:00
echo ""
2017-04-19 05:30:10 +00:00
2017-04-19 05:47:21 +00:00
echo "Mounting var…"
2017-04-24 00:38:00 +00:00
mkdir $constructionsite/var
mount /dev/mirror/var $constructionsite/var
2017-04-19 05:30:10 +00:00
2017-04-19 05:47:21 +00:00
echo "Mounting down…"
2017-04-24 00:38:00 +00:00
mkdir -p $constructionsite/media/down
mount /dev/mirror/down $constructionsite/media/down
2017-04-23 22:28:42 +00:00
echo ""
2017-04-19 05:30:10 +00:00
2017-04-23 22:28:42 +00:00
echo "Preparing boot…"
2017-04-24 00:38:00 +00:00
mkdir $constructionsite/zboot/boot
2017-04-23 22:28:42 +00:00
echo ""
2017-04-19 04:28:03 +00:00
2017-04-24 00:38:00 +00:00
2017-04-19 05:47:21 +00:00
echo "Disk setup done. Press enter to continue."
read x
echo "Extracting kernel…"
tar -C $constructionsite -xvf kernel.txz
2017-04-19 04:28:03 +00:00
2017-04-19 05:47:21 +00:00
echo "Extracting base system…"
tar -C $constructionsite -xvf base.txz
2017-04-24 00:38:00 +00:00
2017-04-24 01:15:17 +00:00
echo "Copying key…"
cp $key_path $constructionsite/boot/
2017-04-24 02:29:22 +00:00
echo ""
2017-04-24 01:15:17 +00:00
2017-04-24 00:38:00 +00:00
echo "Creating loader.conf…"
cat loader.conf >> $constructionsite/boot/loader.conf
2017-04-24 02:29:22 +00:00
echo ""
2017-04-19 05:47:21 +00:00
echo "Creating fstab…"
cat $fstab >> $constructionsite/etc/fstab
2017-04-24 02:29:22 +00:00
echo ""
echo "Moving/symlinking boot…"
mv $constructionsite/boot $constructionsite/zboot/
cd $constructionsite
2017-04-24 02:33:22 +00:00
ln -s zboot/boot boot
2017-04-19 05:47:21 +00:00
echo "Maybbe it werk now? D:"
2017-04-19 04:28:03 +00:00
2017-04-19 02:28:55 +00:00
else
echo "That wasn't 'yes': '$optin'"
fi