Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Backups, Scripts

Solaris boot disk copy using dd

Submitted by on November 22, 2005 – 6:40 pm 6 Comments

The following Korn shell script will make a bootable copy of the boot disk on a Solaris system. The script uses dd and requires that the source disk and destination disk have the same geometry.

#!/bin/ksh

####  Set some variables  ####
HOST=`hostname`
ROOT_USR=server_dudes
MAIL_SUB="Root disk copy on $HOST"

####  Set some flags  ####
MAIL_MSG=0
RETURN=0
LOOP=""
DD_DEVICE="0"           # for DD_DEVICE, 0 = do not issue the dd command to
                        # duplicate the partition configuration of SRC_DRV to
                        # DEST_DRV; 1 = issue dd command - assumes the SRC_DRV
                        # and DEST_DRV are identical disks
EDIT_VFSTAB=0           # for EDIT_VFSTAB, 0 = edit the vfstab file on
                        # DEST_DRV so system will boot off of that drive in
                        # that scsi location;  1 = don't edit the vfstab file
                        # (disks locations are swapped on those systems to
                        # boot off alternate)

#--------------------------------
# Define the source and destination devices for each system; set flags
#--------------------------------
if [ $HOST = "bobby" ]
then
  SRC_DRV="c1t1d0s"
  DEST_DRV="c1t2d0s"
  DD_DEVICE="1"
fi

#--------------------------------
# Define a couple of procedures
#--------------------------------
####  Send completion messages  ####
mail_msg() {
  case $MAIL_MSG in
        0) MESSAGE="Root copy successfully completed on `hostname`" ;;
        1) MESSAGE="Error copying $mp partition" ;;
        2) MESSAGE="Unable to remove root copy mount point. Still mounted." ;;
        3) MESSAGE="Destination Drive in use. Copy aborted. See /var/tmp/DEST_DRV" ;;
  esac
/usr/ucb/mail -s "$MAIL_SUB" $ROOT_USR << END
$MESSAGE
END
}

####  Exit code on completion ####
get_out() {
  exit $RETURN
}

#--------------------------------
# Check that none of the DEST_DRV partitions are mounted. Unmount if true.
# Check it twice in case two different temp mount points were used.
# If DEST_DRV still in use, record, abort and notify.
#--------------------------------
for count in 1 2
do
    for i in `df -kl | grep $DEST_DRV | awk '{print $6}' | sort -r`
    do
      umount $i
    done
done

df -kl | grep $DEST_DRV > /var/tmp/DEST_DRV

if [ $? -eq 0 ]
then
  MAIL_MSG=3 ; RETURN=3
  mail_msg
  get_out
fi

#--------------------------------
# Duplicate the partition information if disk geometries match:
#--------------------------------
if [ $DD_DEVICE -eq 1 ]
then
  echo "Running dd to create partition layout ..."
  # dd if=/dev/rdsk/${SRC_DRV}2 of=/dev/rdsk/${DEST_DRV}2 bs=1024k
  prtvtoc /dev/rdsk/${SRC_DRV}2 | fmthard -s - /dev/rdsk/${DEST_DRV}2
fi

#--------------------------------
# create the temporary alt root mount point if it doesn't exist
#--------------------------------
if [ ! -d /newroot ]
then
  echo "Creating mount point for root disk copy ..."
  mkdir /newroot
fi

#--------------------------------
# get the filesystems on the root drive & duplicate on the alt drive
#--------------------------------
for i in `df -kl | grep $SRC_DRV | grep ^/dev | awk '{print (substr($1,length($1),1))"."(substr($6,2))}' | sort`
do
  part=`echo $i | awk -F. '{print $1}'`              # disk slice to copy
  mp=`echo $i | awk -F. '{print $2}'`                # mount point
  echo "making filesystem for /newroot/$mp ..."
  echo y | newfs /dev/rdsk/$DEST_DRV$part
  if [ "$mp" != "" -a ! -d /newroot/$mp ]
  then
    mkdir /newroot/$mp
  fi
  mount /dev/dsk/$DEST_DRV$part /newroot/$mp
  echo "copying files for /newroot/$mp ..."
  cd /$mp ; find . -mount ! -type s | cpio -pmud /newroot/$mp
  # for some reason, the /dev/fd directory is not being transferred
  # so do it manually
  if [ "$mp" = "" -o "$mp" = "/" ]
  then
    mkdir /newroot/dev/fd
    chmod 555 /newroot/dev/fd
    touch /newroot/reconfigure  # we have to populate the new /dev/fd
  fi
  if [ $? != 0 ]
  then
    MAIL_MSG=1 ; RETURN=1
    mail_msg
    get_out
  fi
done

#--------------------------------
# install a boot block so this drive can boot
#--------------------------------
installboot /usr/platform/`uname -i`/lib/fs/ufs/bootblk /dev/rdsk/${DEST_DRV}0
mkdir /newroot/tmp ; chmod 777 /newroot/tmp
mkdir /newroot/proc

#--------------------------------
# if geometries are different we have to use the new disk in it's
# current location to boot from, so edit the vfstab file
#--------------------------------
if [ $EDIT_VFSTAB -eq 0 ]
then
  sed s/$SRC_DRV/$DEST_DRV/g /etc/vfstab > /newroot/etc/vfstab
fi

#--------------------------------
# make the directory structure on the alternate root
#--------------------------------
for LOOP in `df -k | grep $SRC_DRV | awk '{print $6}' | sort -r`
do
  if [ "x$LOOP" != "x" ]
  then
    mkdir -p /newroot$LOOP
    if [ $? != 0 ]
    then
      mkdir /newroot$LOOP
    fi
  fi
done

#--------------------------------
# create mount points for any additional filesystems
#--------------------------------
for other_mount in `df -kl | grep -v $DEST_DRV | grep -v $SRC_DRV | grep ^/dev | awk '{print $6}'`
do
  if [ "$other_mount" ]
  then
    mkdir -p /newroot$other_mount
  fi
done

#--------------------------------
# unmount all the alt root filesystems & delete the temporary mount point
#--------------------------------
for i in `df -kl | grep -v vx | grep newroot | awk '{print $6}' | sort -r`
do
  umount $i
done

df -kl | grep newroot > /dev/null 2>&1

if [ $? -ne 0 ]
then
  rmdir /newroot
else
  MAIL_MSG=2 ; RETURN=2
  mail_msg
  get_out
fi

mail_msg
get_out

 

Print Friendly, PDF & Email

6 Comments »

  • nmlpc says:

    I want to install my network adapter, while running a backtrack 2 boot disk. I tried saving it to a USB stick, but couldn’t figure out how to access the drive, I’m very new to Backtrack.

  • Jason says:

    So i just thought a little definition might help.
    This “shell” term might refer to alot of different meanings on a computer.

    Here’s just an example .
    What actually does this mean when its refering to the “Shell”

    QUOTE
    A F0 entry corresponds to the Shell= statement, under the [Boot] section, of the System.ini file. The Shell= statement in the system.ini file is used to designate what program would act as the shell for the operating system. UNQUOTE

    I know this might be a little hard to explain in a few paragraphs, but is it possible to define this a little
    to explain what it means by the word or the term “shell”

  • krow147 says:

    i googled it but i didnt find any….. it would be very helpful if there is because im too lazy to insert shapes in word

  • timq3dimensionscom says:

    Write the script so that it will do the following, no matter in which directory it is run. (To test your script while you’re developing it, you’ll need to create some files to test it with in the “unixtest” directory. However, when we check your work, we’ll test it with different files in a different directory.) Suppose that your script is being run in a directory D:

    * Cat every file (in D) whose name starts with “test” into a file (in D) called “bigtest” (you’ll have to redirect output). (Hint: You might want to review the “Wildcards” section in the “Unix Shell Commands” lesson.)

    * Copy every line of a file (in D) called “dogfile.txt” that has the word “dog” (lower-case letters only) in it into a file (in D) called “doglines.txt”.

    * Write every line containing the word “delete” produced by “man mail” into a file called “delete”

    My test directory is: /home/unixtest
    input directory is same
    What I have so far:

    #!/bin/csh -f

    set DIR=”/home/unixtest”

    cp $DIR/test* > $DIR/bigtest.txt
    grep dog $DIR/dogfile.txt > $DIR/doglines.txt
    man mail | grep delete > $DIR/delete

    The delete one is working but the other two files it produces ‘bigtest’ and ‘doglines’ do not have results in them.

    perhaps I have the wrong syntax somewhere?
    I am using the C-cash syntax
    with:
    #!/bin/csh -f
    at the beginning

  • che-che says:

    For example, $? returns the success/failure state of the last executed unix commands.

    What other symbols can be used (apart from the question mark) along with the dollar sign (except when it is used for retrieving environment variables). Is the behaviour same between different flavours of unix, like LINUX, Solaris etc.

  • Superman says:

    I need to learn the basics such as;

    chmod
    ls
    rmdir
    Redirection (‘>’, ‘<‘, ), pipes (‘|’) and filters (grep, egrep, cat, wc)

    Does anyone know where I can find out what these all mean? A website etc. Thanx.

Leave a Reply

%d bloggers like this: