Ignore:
Timestamp:
09/09/13 11:51:53 (21 months ago)
Author:
Kris Moore <kris@…>
Branches:
master, 9.2-release, releng/10.0, releng/10.0.1, releng/10.0.2, releng/10.0.3, releng/10.1, releng/10.1.1, releng/10.1.2
Children:
d15dbfc, 4f9718b
Parents:
26599e2
Message:

Add bunch of new functionality to lpreserver

Replace "mirror" with "zpool" sub-cmd, which will allow you to
attach / detach / online / offline / list various zpool drives

  • Important notes *

When attaching a new disk to a zpool, the drive will
be wiped and re-formatted with a 'freebsd-zfs' file-system of the same
size as the first-disk in th zpool array.

If the first disk has a swap partition it will be created also,
but not activated, since advanced users can handle that as the see fit.

Last but not least, GRUB will be stamped on the new disk, making it
bootable should another drive in the array go bad.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src-sh/lpreserver/backend/functions.sh

    r1996f85 r59f367e  
    376376} 
    377377 
    378 add_mirror_disk() { 
     378add_zpool_disk() { 
    379379   pool="$1" 
    380380   disk="$2" 
     381   disk="`echo $disk | sed 's|/dev/||g'`" 
    381382 
    382383   if [ -z "$pool" ] ; then 
     
    387388   if [ -z "$disk" ] ; then 
    388389      exit_err "No disk specified" 
     390      exit 0 
     391   fi 
     392 
     393   if [ ! -e "/dev/$disk" ] ; then 
     394      exit_err "No such device: $disk" 
    389395      exit 0 
    390396   fi 
     
    394400   if [ $? -ne 0 ] ; then exit_err "Invalid pool: $pool"; fi 
    395401 
    396    # Make sure zpool isn't raid 
    397    zpool list -H -v ${pool} | grep -q "raid" 
    398    if [ $? -eq 0 ] ; then exit_err "Cannot mirror a raidz pool!" ; fi 
     402   # Cleanup the target disk 
     403   echo "Deleting all partitions on: $disk" 
     404   rc_nohalt "gpart destroy -F $disk" >/dev/null 2>/dev/null 
     405   rc_nohalt "dd if=/dev/zero of=/dev/${disk} bs=1m count=1" >/dev/null 2>/dev/null 
     406   rc_nohalt "dd if=/dev/zero of=/dev/${disk} bs=1m oseek=`diskinfo /dev/${disk} | awk '{print int($3 / (1024*1024)) - 4;}'`" >/dev/null 2>/dev/null 
    399407 
    400408   # Grab the first disk in the pool 
    401    mDisk=`zpool list -H -v | grep -v "^$pool" | awk '{print $1}' | grep -v "^mirror" | head -n 1` 
     409   mDisk=`zpool list -H -v | grep -v "^$pool" | awk '{print $1}' | grep -v "^mirror" | grep -v "^raidz" | head -n 1` 
     410 
     411   # Is this MBR or GPT? 
     412   echo $mDisk | grep -q 's[0-4][a-z]$' 
     413   if [ $? -eq 0 ] ; then 
     414      # MBR 
     415      type="MBR" 
     416      # Strip off the "a-z" 
     417      rDiskDev=`echo $mDisk | rev | cut -c 2- | rev` 
     418   else 
     419      # GPT 
     420      type="GPT" 
     421      # Strip off the "p[1-9]" 
     422      rDiskDev=`echo $mDisk | rev | cut -c 3- | rev` 
     423   fi 
     424 
     425   # Make sure this disk has a layout we can read 
     426   gpart show $rDiskDev >/dev/null 2>/dev/null 
     427   if [ $? -ne 0 ] ; then  
     428      exit_err "failed to get disk device layout $rDiskDev" 
     429   fi 
     430 
     431   # Get the size of "freebsd-zfs & freebsd-swap" 
     432   sSize=`gpart show ${rDiskDev} | grep freebsd-swap | cut -d "(" -f 2 | cut -d ")" -f 1` 
     433   zSize=`gpart show ${rDiskDev} | grep freebsd-zfs | cut -d "(" -f 2 | cut -d ")" -f 1` 
     434    
     435   echo "Creating new partitions on $disk" 
     436   if [ "$type" = "MBR" ] ; then 
     437      # Create the new MBR layout 
     438      rc_halt_s "gpart create -s MBR -f active $disk" 
     439      rc_halt_s "gpart add -a 4k -t freebsd $disk"       
     440      rc_halt_s "gpart set -a active -i 1 $disk" 
     441      rc_halt_s "gpart create -s BSD ${disk}s1" 
     442      rc_halt_s "gpart add -t freebsd-zfs -s $zSize ${disk}s1" 
     443      if [ -n "$sSize" ] ; then 
     444        rc_halt_s "gpart add -t freebsd-swap -s $sSize ${disk}s1" 
     445      fi 
     446      aDev="${disk}s1a" 
     447   else 
     448      # Creating a GPT disk 
     449      rc_halt_s "gpart create -s GPT $disk" 
     450      rc_halt_s "gpart add -b 34 -s 1M -t bios-boot $disk" 
     451      rc_halt_s "gpart add -t freebsd-zfs -s $zSize ${disk}" 
     452      if [ -n "$sSize" ] ; then 
     453        rc_halt_s "gpart add -t freebsd-swap -s $sSize ${disk}" 
     454      fi 
     455      aDev="${disk}p2" 
     456   fi 
    402457 
    403458   # Now we can insert the target disk 
    404    zpool attach $pool $mDisk $disk 
    405    if [ $? -ne 0 ] ; then 
    406       exit_err "Failed attaching $disk" 
    407    fi 
    408  
    409    echo "Added $disk to zpool $pool. Resilver will begin automatically." 
     459   echo "Attaching to zpool: $aDev" 
     460   rc_halt_s "zpool attach $pool $mDisk $aDev" 
     461 
     462   # Lastly we need to stamp GRUB 
     463   echo "Stamping GRUB on: $disk" 
     464   rc_halt_s "grub-install --force /dev/${disk}" 
     465 
     466   echo "Added $disk ($aDev) to zpool $pool. Resilver will begin automatically." 
    410467   exit 0 
    411468} 
    412469 
    413 list_mirror_disks() { 
     470list_zpool_disks() { 
    414471   pool="$1" 
    415472 
     
    423480   if [ $? -ne 0 ] ; then exit_err "Invalid pool: $pool"; fi 
    424481 
    425    # Make sure zpool isn't raid 
    426    zpool list -H -v ${pool} | grep -q "raid" 
    427    if [ $? -eq 0 ] ; then exit_err "Pool: $pool is raidz!" ; fi 
    428  
    429    zpool list -H -v | grep -v "^$pool" | awk '{print $1}' | grep -v "^mirror" | tail +2 > /tmp/.mList.$$ 
    430  
    431    while read line 
    432    do 
    433       echo "$line" | grep -q -e "spare" -e "log" -e "cache" 
    434       if [ $? -eq 0 ] ; then break ; fi 
    435  
    436       echo "$line"   
    437    done < /tmp/.mList.$$ 
    438    rm /tmp/.mList.$$ 
    439 } 
    440  
    441 rem_mirror_disk() { 
     482   zpool list -H -v $pool 
     483} 
     484 
     485rem_zpool_disk() { 
    442486   pool="$1" 
    443487   disk="$2" 
     
    456500   zpool status $pool >/dev/null 2>/dev/null 
    457501   if [ $? -ne 0 ] ; then exit_err "Invalid pool: $pool"; fi 
    458  
    459    # Make sure zpool isn't raid 
    460    zpool list -H -v ${pool} | grep -q "raid" 
    461    if [ $? -eq 0 ] ; then exit_err "Cannot remove disks from a raidz pool!" ; fi 
    462502 
    463503   zpool detach $pool $disk 
     
    468508   exit 0 
    469509} 
     510 
     511offline_zpool_disk() { 
     512   pool="$1" 
     513   disk="$2" 
     514 
     515   if [ -z "$pool" ] ; then 
     516      exit_err "No pool specified" 
     517      exit 0 
     518   fi 
     519 
     520   if [ -z "$disk" ] ; then 
     521      exit_err "No disk specified" 
     522      exit 0 
     523   fi 
     524 
     525   # Check if pool exists 
     526   zpool status $pool >/dev/null 2>/dev/null 
     527   if [ $? -ne 0 ] ; then exit_err "Invalid pool: $pool"; fi 
     528 
     529   zpool offline $pool $disk 
     530   exit $? 
     531} 
     532 
     533online_zpool_disk() { 
     534   pool="$1" 
     535   disk="$2" 
     536 
     537   if [ -z "$pool" ] ; then 
     538      exit_err "No pool specified" 
     539      exit 0 
     540   fi 
     541 
     542   if [ -z "$disk" ] ; then 
     543      exit_err "No disk specified" 
     544      exit 0 
     545   fi 
     546 
     547   # Check if pool exists 
     548   zpool status $pool >/dev/null 2>/dev/null 
     549   if [ $? -ne 0 ] ; then exit_err "Invalid pool: $pool"; fi 
     550 
     551   zpool online $pool $disk 
     552   exit $? 
     553} 
Note: See TracChangeset for help on using the changeset viewer.