Manual Mirroring

Peter Baer Galvin

This month's column includes a handy script for performing a manual mirroring of your root disk drive. Also included is a way to recover a failed disk, a book review, a security hint, and some follow-up information about last month's column.

In last month's column (Reliable and Practical Root Disk Mirroring), a complex solution to "perfect disk mirroring" was presented. That solution included a combination of Solstice Disk Suite and Veritas Volume Manager. Disksuite was used to mirror the root disk, and Volume Manager was given a small slice of root for rootdg to keep it happy and left to manage the remaining disks.

There are times when a simple disk copy is more appropriate than a RAID-1 set. There are also times when both, together, are the best solution. For example, a workstation might need its second disk for scratch or work space, but may on occasion need its root disk copied (for example, before an operating system upgrade, as a backup copy). On the other extreme, a "must never fail" server of an extra-paranoid systems administrator can provide the most uptime by having RAID-1 of the root disk, and a nightly static copy of the same disk.

This static copy can protect you when RAID-1 cannot. For example, a new systems administrator may make a mistake on the root disk, deleting files or making inappropriate edits to system files. RAID-1 will happily, immediately, replicate the mistake to the mirror copy. With a static nightly copy, undoing the mistake is fairly trivial. Likewise, a security incident that effected the root disk could be undone by booting from the previous night's copy.

There are many, many scripts floating around that help perform this manual mirror. The script presented below is the simplest and most effective that I have found. It performs the manual root disk copy that was mentioned last month. By periodically firing it off from cron, it can be used as a fail safe, even in cases where other methods are used as well. It can also be used ad hoc when a snapshot of the current root disk is needed. For example, you could run the script before attempting a serious patch installation or even operating system upgrade. Should you be unhappy with the result of the system changes, you could reverse the process to reset the root disk to pre-change state.

Do not use this script without customizing it or it could have a negative impact on your system's functionality. Be sure to test it in your environment and on your systems before counting on it. Watch the script the first time it runs on each system to be sure the execution is clean. For example, start it with sh -xf to watch each line execute. And before first use, be sure to back up the target system.

This script assumes that it is called /opt/local/etc/rootcopy. Fundamentally, it performs the following steps (words in capital letters are variables in the script):

The fmthard command is very useful for duplicating the partitioning from one disk to another. Unfortunately, it has sometimes failed to copy the partition information in testing (saying that Partition 0 not aligned on cylinder boundary). If this is the case when you run it, you'll need to manually duplicate the partition to your copy disk before you run the script and remove the fmthard command from the script.

Without further ado, the script:

#!/bin/sh
# Original Script written by Constantin Ionescu
# Modified by Carlo Cosolo
# Modified by Peter Baer Galvin
# Modified by John West
# Use and distribute freely
 
# Define variables for use in the script
# ! Important, these must be set correctly !
 
# The root disk to duplicate (leave off slice numbers and path)
SRC=c0t0d0
 
# The empty disk to duplicate it to (leave off slice numbers and path)
DEST=c0t1d0
 
# The directory to mount destination partitions on while duplicating
MOUNTDIR=/dup_0
 
# The file name of this script, to rename it on the destination to avoid execution
SCRIPT=/opt/local/etc/rootcopy
 
# The slices that should be copied
SLICES="s0 s3 s4 s6 s7"
 
echo ====================================
echo Disk Copy script started `date`
echo
 
# Make sure the mount point for duplicate partitions exists
if [ ! -d $MOUNTDIR ]; then
  mkdir $MOUNTDIR
  chmod 700 $MOUNTDIR
fi
 
# Partition the duplicate disk, make filesystems, make it bootable
prtvtoc /dev/rdsk/${SRC}s2 > /tmp/vtoc
fmthard -s /tmp/vtoc /dev/rdsk/${DEST}s2
installboot /usr/platform/`uname -i`/lib/fs/ufs/bootblk /dev/rdsk/${DEST}s0
 
# Modify the following loop to handle any special cases
for fs in $SLICES
do
  newfs /dev/dsk/${DEST}${fs} < /dev/null; mount /dev/dsk/${DEST}${fs} ${MOUNTDIR};
  ufsdump 0f - /dev/dsk/${SRC}${fs}|(cd ${MOUNTDIR}; ufsrestore rf -);
  if [ $fs = "s0" ]; then
    sed 's/${SRC}/${DEST}/g' /etc/vfstab > ${MOUNTDIR}/etc/vfstab;
    mv ${MOUNTDIR}/${SCRIPT} ${MOUNTDIR}/${SCRIPT}.DONTRUN;
  fi
  umount ${MOUNTDIR}
done
 
echo
echo Disk Copy script ended `date`
echo ====================================
echo

Thanks to my coworkers John West and Kyle Oliver for their help on writing and testing this script.