Novell Home

Imaging Linux Systems with Rsync

Novell Cool Solutions: Feature
By Bart Whiteley

Digg This - Slashdot This

Posted: 5 Dec 2003
 

How to use Rsync to Build Fresh Installations for Development and Testing

Rsync is a powerful tool with many applications. These instructions describe a method of using rsync to restore a system from an image stored on a different partition of the same disk. Multiple master images can be stored on a single disk (provided it is large enough). This can be very useful for development and testing. For example, the image could be a fresh Linux install. If you are testing a linux product install, once you are done testing a build you can rollback the test image to its original state in a matter of seconds. This much more efficient than reinstalling. Although the example in this document only includes a single system, the same principles can easily be extended to store master images on a file server, and to image multiple systems from a single file server.

These instructions are for setting up a single machine that can be booted into Redhat Advanced Server (RAS), United Linux (UL), or any other distributions. You can also use these guidelines to configure more or less masters and sandboxes to meet your needs. Rsync is the key to this simplified setup.

Pre-Install Tasks

Partition your hard disk with a swap partition and 5 Linux partitions.

To do this:

  1. Start the Red Hat 9 (or what ever you plan to use for your Utility partition) installation.


  2. Drop to a command-line (CTRL-ALT-F2) and run FDISK on the drive (typically /dev/hda, but it may vary dependant on your configuration).

Partitioning

You should plan the partitioning scheme prior to beginning this process to make it easier.

Here are some guidelines for the partitions:

  1. In FDISK create the swap partition and write it to the partition table. This will insure that it is hda1.
  2. Create the Utility partition (hda2). These both should be primary partitions.
  3. Create an extended partition that is as large as your 2 masters and 2 sandboxes combined (20+GB). The extended partition will be hda3. You can then create the masters and sandboxes as logical partitions within the extended and they will be numbered hda5-hda8.

/dev/hda1 = swap - as per typical Linux recommendation 2X your RAM)
/dev/hda2 = "Utility" - Since this will be a working partition, make it whatever size needed for your planned installation
/dev/hda5 = RedHat AS (sandbox) - At least 5GB but more if possible
/dev/hda6 = RedHat AS (master) - At least 4-4.5GB to allow for a full server install
/dev/hda7 = UL or SUSE (sandbox) - At least 5GB but more if possible
/dev/hda8 = UL or SUSE (master) - At least 4-4.5GB to allow for a full server install

The utility partition handles imaging. Sandbox partitions are used for installations and testing. The master partitions hold the pristine installs of the OSs and are used to restore the sandbox to its original state.

Utility Partition Installation
Start by installing any Linux system on /dev/hda2. It's nice if this system uses grub as its default bootloader and most all distributions use grub these days. Be sure to install grub to the Master Boot Record (MBR) of /dev/hda. You will want to do an install that will provide the functionality you need if you plan to use this for more than just running rsync. After completing the install, boot to this installation and test it to make certain everything installed correctly.

Install First Sandbox
Next install RedHat AS into /dev/hda5. Do not install grub to the MBR. Install it to the boot sector of /dev/hda5. If you do install grub to the MBR, it's not the end of the world, but you'll need to learn grub to fix the problem. You should do an "all packages" install here also to insure that everything you might need is available. A full install is less than 4 GB but you should install whatever components you want to test with.

Install the Second Sandbox
Install United Linux or SUSE Server in /dev/hda7. Take care not to overwrite the MBR here also. Follow the same guidelines as for the first sandbox.

Use the Sandbox Installs to Setup the Masters
In this section you will format the master partitions and use rsync to put all of the files on them. These instructions assume the above layout and installations. If you change anything above you must edit sync.sh to reflect those changes.

  1. Boot into the utility partition.
  2. Format /dev/hda6 using the following command: mke2fs /dev/hda6
  3. Format /dev/hda8 using the following command: mke2fs /dev/hda8
  4. After putting sync.sh (below) in an easy to find and use place run the following:
    /wherever/sync.sh ras sandbox2master
    This command copies everything for the sandbox partition to the master. This sets the master up as a fresh install which can be used to restore the sandbox to that state.
  5. Now use sync.sh to do the same for the other sandbox/master combination using:
    /wherever/sync.sh ul sandbox2master

Configure Grub to Allow Booting to the Utility or Either of the Sandboxes

  1. Boot to the Utility partition if not already there.
  2. Load /boot/grub/menu.lst into a text editor.
  3. Mount hda5 with the following command: mount /dev/hda5 /mnt/hda5. You may need to mkdir /mnt/hda5.
  4. Navigate to /hda5/boot/grub and copy the boot information from menu.lst into the file opened in step 2 above as the second entry. (See sample file attached). Be careful not to change any of the default boot options or you might render your system non-bootable. Leave the first boot option as is!
  5. Leaving the file from step 2 open Mount hda7 with the following command: mount /dev/hda7 /mnt/hda7. You may need to mkdir /mnt/hda7.
  6. Navigate to /hda7/boot/grub and copy the boot information from menu.lst into the file opened in step 2 above as the third entry.
  7. Now restart the machine and test each grub option to make certain it works as expected.

Using the Setup
You can now use the sandboxes and restore them to their original state using sync.sh from the Utility partition. Once you've finished working in a sandbox and wish to restore it to its original state, boot into utility and run: /wherever/sync.sh master2sandbox. Since rsync only changes the differences, "re-imaging" usually only takes 1 or 2 minutes.

Additional notes

  • If you want to be able to boot into a "master" system, you'll have to update the /etc/fstab on that system after you copy it from the sandbox the first time. Note that the sync.sh script --excludes /etc/fstab, so it doesn't get overwritten.
  • If you need to make changes to the master (like adding more distribution packages, or applying updated from the distribution vendor), there are two ways to do this. Either boot into it (after changing its /etc/fstab), or boot into "utility", mount it, then chroot into it.
  • Redhat has some goofy LABEL in its kernel parameters and its /etc/fstab. Just change this to the actual device (/dev/hda5 or /dev/hda6). If you don't do this, you'll be sorry. You'll think you're in /dev/hda6. 'mount', 'df', and other utilities will say that you are in /dev/hda6, but really you're in /dev/hda5.
  • The script can be easily adapted to have the masters housed on a different server. After you install the test box, boot it into the Utility partition; mount the sandbox, then rsync the whole sandbox to a directory on the other file server. Hint: add '-e ssh -z' to the rsync parameters, and use scp syntax for the remote path. Now you can modify the sync.sy .sh. script to sync the sandbox to a master on another box. (After adding '-e ssh' to the rsync parameters in the script and adjusting the remote path.) I've done this where I have several systems (RH, SUSE, Turbo, mandrake, etc.) all in separate dirs on a file server. Then I can image test machines to be any distribution in a matter of minutes. You can manage the master image (to update packages, etc), simply by logging into the file server as root and 'chroot'ing to the dir containing the distribution you want to manage.
  • You might want to do something similar on your development box. Suppose you run a development configuration and hate reboots. So install a test box with RAS or UL or what ever you need for development. Then you just need to rsync that system to a directory on your development box. You can then chroot into that dir, and you will be running RAS or UL. Then you can build on that platform, without ever booting out of your development box.
  • You'll want to copy /etc/ssh/*host* from utility to all the other partitions. Using this method, you can ssh to the box no matter what system it is running, without getting ssh key mismatch errors.
  • If you don't have room for a utility partition you can boot into knoppix or some other CD-ROM based "rescue" system to do the rsync.

This information is offered as a way to get you started using this utility to be more productive using multiple Linux distribution installations. As you use this tool, you will very likely find new and more innovative ways of tweaking this. Please let us know what you discover or create based on this information.

Program Listing 1
sync.sh

#!/bin/sh

Usage()
{
	echo "Usage: $0  "
	exit 1
}

if [ "x$1" = "xul" ]; then
	if [ "x$2" = "xmaster2sandbox" ]; then
		SOURCE=hda8
		DEST=hda7
	elif [ "x$2" = "xsandbox2master" ]; then
		SOURCE=hda7
		DEST=hda8
	else
		Usage
	fi
elif [ "x$1" = "xras" ]; then
	if [ "x$2" = "xmaster2sandbox" ]; then
		SOURCE=hda6
		DEST=hda5
	elif [ "x$2" = "xsandbox2master" ]; then
		SOURCE=hda5
		DEST=hda6
	else
		Usage
	fi
else
	Usage
fi

umount /dev/$SOURCE /dev/$DEST

fsck /dev/$DEST

mount /dev/$SOURCE /mnt/$SOURCE
mount /dev/$DEST /mnt/$DEST

rsync -av --delete --exclude /etc/fstab /mnt/$SOURCE/* /mnt/$DEST/

umount /dev/$SOURCE /dev/$DEST


Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions. www.webwiseone.com

© 2014 Novell