Problem with disk numbering in my ZFS creation script

THe last system I set up with ZFS, I used a script. Today, when trying a system with different device types, I encountered a limitation of that script. I need to fix it.

DISKS="ada0 ada1 da0 da1 da2 da3 da4 da5 da6 da7"

gmirror load
gmirror stop swap

for I in ${DISKS}; do
        NUM=$( echo ${I} | tr -c -d '0-9' )
        gpart destroy -F ${I}
        gpart create -s gpt ${I}
        gpart add -b 34 -s 94 -t freebsd-boot -l bootcode${NUM} ${I}

        gpart add -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 -t freebsd-zfs -s 2784G -l disk${NUM} ${I}
        gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 ${I}
        gnop create -S 4096 /dev/gpt/disk${NUM}
done

In the above script, NUM takes on the value of what digits appear in the string of I. Given that DISKS contains both ada0 and da0, two disks will get the same value for $I. That’s a problem.

I need a better value for $I.

I think this will work better:

DISKS="da0 da1 da2 da3 da4 da5 da6 da7 ada0 ada1"

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 -b 34 -s 94 -t freebsd-boot -l bootcode${NUM} ${I}

        gpart add -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 -t freebsd-zfs -s 2784G -l disk${NUM} ${I}
        gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 ${I}
        gnop create -S 4096 /dev/gpt/disk${NUM}
done

Of note:

  1. I moved ada0 and ada1 to the end of the list; this way da0..da7 will be disk1..disk7, less confusion
  2. NUM starts off at -1
  3. That expression for incrementing NUM works on /bin/sh on FreeBSD.
Website Pin Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google StumbleUpon Premium Responsive

Leave a Comment

Scroll to Top