Thursday, February 16, 2017

Simple Mirroring and Striping with FreeBSD 10/11 with GMIRROR

This is just my installation note I prepared while installing the FreeBSD10. Initially the mirroring and installation was all confusing but once I got the knack I just love it and I could do that any number of times. So the steps are below

Boot the FreeBSD CD
Choose Shell options to drop you to a shell where we going to create GPT partitions by hand which gives us much better control over
Create the partions for the mirrors , we are going to use GPT partitions

Now create the gpt partitions in the first disk
#gpart create -s gpt ada0

Lets create needed partitions on the first disk, starting with boot partition on the first 64k section of the disk
create boot partition in first disk
# gpart add -s 64k -t freebsd-boot -l boot0 ada0

all other partitions as needed like root, swap, usr, var, home.
# gpart add -s 5G -t freebsd-ufs -l root0 ada0
# gpart add -s 4G -t freebsd-swap -l swap0 ada0
# gpart add -s 10G -t freebsd-ufs -l usr0 ada0
# gpart add -s 10G -t freebsd-ufs -l var0 ada0
# gpart add -s 10G -t freebsd-ufs -l home0 ada0

Once partition are created make the first disk bootable, with boot record to be installed in the boot partition.
#gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 ada0

Now create same gpt partition in the second disk
create boot partition in first disk
#gpart add -s 64k -t freebsd-boot -l boot1 ada1
other partitions as same as the first disk, you might notice only the change is in the last where ada0 is replaced with ada1

# gpart add -s 5G -t freebsd-ufs -l root1 ada1
# gpart add -s 4G -t freebsd-swap -l swap1 ada1
# gpart add -s 10G -t freebsd-ufs -l usr1 ada1
# gpart add -s 10G -t freebsd-ufs -l var1 ada1
# gpart add -s 10G -t freebsd-ufs -l home1 ada1

Now lets makes the second disk bootable by installing bootcode in the boot partition
# gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 ada1

Next step is creating the mirrors and with labels and formatting them with a filesystem
Create mirrored UFS filesystems (GPT) and Create filesystems.
Lets load the mirroring driver first
# kldload geom_mirror

Create the mirrors
#gmirror label boot /dev/gpt/boot0 /dev/gpt/boot1
#gmirror label swap /dev/gpt/swap0 /dev/gpt/swap1
#gmirror label root /dev/gpt/root0 /dev/gpt/root1
#gmirror label usr /dev/gpt/usr0 /dev/gpt/usr1
#gmirror label var /dev/gpt/var0 /dev/gpt/var1
#gmirror label home /dev/gpt/home0 /dev/gpt/home1

Now let us check the gmirror status
# gmirror status

Create filesystems in created mirrors , use -t for ssds to enable TRIM
#newfs -U -L -t root /dev/mirror/root
#newfs -U -L -t usr /dev/mirror/usr
#newfs -U -L -t var /dev/mirror/var
#newfs -U -L -t home /dev/mirror/home

In the above example please note that -t option with newfs is used because I am doing this on SSDs so I wanted to have TRIM enabled

Create temporary directories to be mounted along with root
#mkdir -p /mnt/usr
#mkdir -p /mnt/var
#mkdir -p /mnt/home

Now mount the root temporary

#mount /dev/mirror/root /mnt
#mount /dev/mirror/usr /mnt/usr
#mount /dev/mirror/var /mnt/var
#mount /dev/mirror/home /mnt/home

Now time to create fstab file for mount points . Create the fstab file for the mirrored partitions or you could come back to shell just before reboot the system and create the fstab file  in /mnt/etc
 If you miss this step  you will not be able to boot up the new system
#vi /tmp/bsdinstall_etc/fstab or best is vi /mnt/etc/fstab
# Device          Mountpoint      FStype  Options Dump    Pass#
/dev/mirror/swap  none            swap    sw      0       0
/dev/mirror/root  /               ufs     rw      1       1
/dev/mirror/usr   /usr   ufs     rw      2       2
/dev/mirror/var   /var   ufs     rw      2       2
/dev/mirror/home   /home   ufs     rw      2       2

exit for shell and continue the installation as usual without creating the partitions and just before rebooting goto the shell again and add the following to load the mirror driver before booting the system.

echo 'geom_mirror_load="YES"'  >  /mnt/boot/loader.conf

Thats all for mirroring, striping just varies in 2 steps
1. instead of mirror we stripe the partitions as below
this example shows striping for swap partion
gstripe label swap /dev/gpt/swap0 /dev/gpt/swap1

Then kindly change change boot loader to
echo 'geom_stripe_load="YES"' >  /mnt/boot/loader.conf

Thats all simple to get a mirroring and striping in FreeBSD 10/11
Enjoy