Saturday, 3 March 2012

FreeBSD with ZFS

During the past few weeks I had a few installs of FreeBSD and PC BSD. Since everything regarding these installs were well documented, standard install went without a hitch. Installed x11, gnome, kde the lot... All the same as debian; but, I learned about some new things. For instance,  I never knew what Scroll-lock is for cos I didn't need it. But the knowledge I gained made be want more...wait....

Problems started to appear when my laptop could not properly go in to a sleep/resume cycle, which is mentioned as normal in the freebsd handbook. not content at all... But figured that I can debug ACPI if I compile the "kernel". ha ha .... On another note, I wanted small fonts in the consoles like I had in debian and ubuntu. Again documentation concludes that I need to do a "kernel" compile. great ha... This is starting to give me a bit of a chill ("kernel compile"). In linux I was used not to touch anything with "kernal" in the title or description, let alone compile it... Hmm... freebsd handbook chapter 9 talks in detail about that and seems to be a smooth ride... Let me see... And, I wanted ZFS instead of UFS; apparently, for no good reason.

Found some help pages and blogs and forums
1. This is how to partition MBR for freebsd 8 (official).
2. This is how to partition GPT for freebsd 9 in complete command line.
3. There is a video on how to partition GPT for freebsd 9 using part command line and part standard installer.

However I wanted to install in an MBR partion using freebsd 9. The problem is freebsd 9 uses a new installer making parts of official institutions in (1) invalid. With a little merging of (1),(2) and referring to (3) all the time, I managed to come up with something that really worked. Here's how.

  1. Boot from CD/DVD/USB and start installer as normal. Proceed until you reach partitioning. 
  2. Drop to "Shell" partitioning.
  3. In mine the disk was labelled 'ada0'. 'ada0s3' was the linux partitions and  'ada0s1' was simply the freebsd partition made during the previous installations. 
  4. Create freebsd zfs and swap partitions. Unlike UFS partitions, no need to create separate ones for /, /usr, /var. Even swap can reside inside the zfs but was warned against that in (1). So,
    gpart add -s 135G -t freebsd-zfs ada0s1
    gpart add -t freebsd-swap ada0s1
    
    My BSD partition was 145GB. So 135 for data and 10GB rest for swap.
  5. Then create a ZFS pool named 'zroot'. If for anything it fails add -f flag.
    zpool create zroot /dev/ada0s1a
  6. Then create the boot properties, checksum and mount points.
    zpool set bootfs=zroot zroot
    zfs set checksum=fletcher4 zroot
    zfs set mountpoint=/mnt zroot
    
  7. Now install ZFS Boot
    zpool export zroot
    dd if=/boot/zfsboot of=/dev/ad0s1 count=1
    dd if=/boot/zfsboot of=/dev/ad0s1a skip=1 seek=1024
    zpool import -o cachefile=/var/tmp/zpool.cache zroot
    
    if anything goes wrong refer to (1).
  8. Create the file system
    zfs create zroot/usr
    zfs create zroot/usr/home
    zfs create zroot/var
    
    Create other file system parts. Now the syntax is
    zfs create (-o PROPERTY=VALUE)* MOUNT
    
    eg. for compression(ON), exec(ON) and mount (zroot/tmp) type
    zfs create -o compression=on -o exec=on zroot/tmp
    
    Mountcompressionexecsetuid
    zroot/tmpononoff
    zroot/usr/portslzjboff
    zroot/usr/ports/distfilesoffoffoff
    zroot/usr/ports/packagesoffoffoff
    zroot/usr/srclzjboffoff
    zroot/var/crashlzjboffoff
    zroot/var/dboffoff
    zroot/var/db/pkglzjbonoff
    zroot/var/emptyoffoff
    zroot/var/loglzjb off off
    zroot/var/mailgzip offoff
    zroot/var/runoff off
    zroot/var/tmplzjb on off
  9. Add symbolic links to /home and set permissions for tmp
    chmod 1777 /mnt/tmp
    cd /mnt ; ln -s usr/home home
    chmod 1777 /mnt/var/tmp
    
  10. Exit to standard installation.
    exit
  11. Continue as normal and finally exit from the installation. and select LiveCD instead of Reboot.
  12. Login as 'root' without password.
  13. Copy zpool cache to file system.
    cp /var/tmp/zpool.cache /mnt/boot/zfs/zpool.cache
    
  14. Edit /etc/rc.conf, /boot/loader.conf.
    echo 'zfs_enable="YES"' >> /mnt/etc/rc.conf
    echo 'zfs_load="YES"' >> /mnt/boot/loader.conf
    echo 'vfs.root.mountfrom="zfs:zroot"' >> /mnt/boot/loader.conf
    
  15. register swap space in fstab
    echo '/dev/ada0s1b  none  swap  sw  0  0' >> /mnt/etc/fstab
    
  16. Unmount everything and mount again
    zfs set readonly=on zroot/var/empty
    zfs umount -af
    zfs set mountpoint=legacy zroot
    zfs set mountpoint=/tmp zroot/tmp
    zfs set mountpoint=/usr zroot/usr
    zfs set mountpoint=/var zroot/var
    
  17. Reboot.
    reboot now

And it worked...
Next... kernel compiles....