Cloning or duplicating a running system using dump/restore

Would you like to share some of your solutions for certain problems? Tips or tricks? Post here. All new topics are automatically moderated.

Cloning or duplicating a running system using dump/restore

Postby fbsd1 » 26 Feb 2010, 10:26

[SIZE="3"]Cloning or duplicating a running system
using dump/restore.[/SIZE]


The dump command will be issued from a host running in multiple-user-mode. The host is a default configuration. IE: [FILE]/[/FILE] as “a” partition, [FILE]/var[/FILE] as “d” partition, [FILE]/tmp[/FILE] as “e” partition and [FILE]/usr[/FILE] as “f” partition. We will dump from the hard drive where the running system is installed to a second hard drive cabled to the motherboard or an external USB cabled hard drive or a USB memory stick of sufficient size. Whatever the target media is, all the data currently on it will be destroyed by this procedure. Even the MBR (Master Boot Record) will be recreated.

For this example [FILE]/dev/ad0[/FILE] is the hard drive where the live system is running.

The target is an USB flash stick [FILE]/dev/da0[/FILE].

The following is the sequence of commands used:

* Collect live file system sizes and save
[CMD=" "]df -h > liveFSsizes[/CMD]

Code: Select all
cat liveFSsizes

Filesystem     Size    Used   Avail Capacity  Mounted on
/dev/ad0s1a    496M    169M    287M    37%    /
devfs          1.0K    1.0K      0B   100%    /dev
/dev/ad0s1d    1.5G     26M    1.4G     2%    /var
/dev/ad0s1e    496M     10K    456M     0%    /tmp
/dev/ad0s1f     15G    410M     13G     3%    /usr

* Collect live Slice Partition sizes and save
Bsdlabel lists the live Slice Partition sizes to a file. This file will be used later to allocate the slice partitions to the target.

[CMD=" "]bsdlabel ad0s1 > liveSPsizes[/CMD]

Code: Select all
cat liveSPsizes

# /dev/ad0s1:
8 partitions:
#        size   offset    fstype   [fsize bsize bps/cpg]
  a:  1048576        0    4.2BSD        0     0     0
  b:  2291584  1048576      swap
  c: 39862305        0    unused        0     0         
  d:  3241984  3340160    4.2BSD        0     0     0
  e:  1048576  6582144    4.2BSD        0     0     0
  f: 32231585  7630720    4.2BSD        0     0     0

If you want to increase or decrease the partition allocation space sizes edit the liveSPsizes.usb file. I have edited this changing partition sizes so it will fit on a 2GB USB stick. You can see from the df –h output that the default sizes sysinstall calculates leaves a lot of free space in the partitions.

Code: Select all
# /dev/da0s1: 2GB USB flash drive stick
8 partitions:
#        size   offset    fstype   [fsize bsize bps/cpg]
  a:     300M        *    4.2BSD        0     0     0   
  b:     200M        *      swap                       
  c:        *        *    unused        0     0         
  d:      50M        *    4.2BSD        0     0     0   
  e:     100M        *    4.2BSD        0     0     0   
  f:        *        *    4.2BSD        0     0     0   

* Zero out the target MBR destroying all the data on the target.

[CMD=" "]dd if=/dev/zero of=/dev/da0 count=2[/CMD]

* fdisk the target with a new MBR.

[CMD=" "]fdisk -BI /dev/da0[/CMD]

The [FILE]–B[/FILE] means Reinitialize the boot code contained is sector 0 of the disk
Default from [FILE]/boot/mbr[/FILE]
The ‘I’ means initialize sector 0 slice table for one slice covering the entire disk.

You will get 2 messages.
Code: Select all
Fdisk: invalid fdisk partition table found
Fdisk: Class not found

Disregard these messages, they are the result of zeroing out the old MBR.

* Label the target:

[CMD=" "]bsdlabel -B -w da0s1[/CMD]

The [FILE]–B[/FILE] means bootstrap code will be read from /boot/boot & written to the disk
The [FILE]–w[/FILE] means write a standard label

* Allocate the partitions
* Restore the partition sizes as defined in the file liveSPsizes

[CMD=" "]bsdlabel -R -w da0s1 liveSPsizes[/CMD]

* Format all the new empty file system on the target.

Code: Select all
newfs –U /dev/da0s1a               # /   
newfs -U /dev/da0s1d               # /var
newfs -U /dev/da0s1e               # /tmp
newfs -U /dev/da0s1f                # /usr

* Mount target file system ‘a’ / and clone

Code: Select all
mount /dev/da0s1a /mnt
cd /mnt
dump -0Lauf - /dev/ad1s1a  | restore -rf -

[FILE]-0[/FILE] means do full dump,
[FILE]-L[/FILE] means take snapshot because source is live file system,
[FILE]-a[/FILE] means enforce writing until a end-of media is reached
[FILE]-u[/FILE] means update the /etc/dumpdates file with the results
[FILE]-f -[/FILE] means standard output is where the dumped data is to be written.

You can think of 'standard output' as a un-named virtual file or buffer. If there was no pipe | to the restore program the dumped data would roll across the terminal screen.

The restore program, on the other side of the | pipe, usually reads from the system's tape drive. But in this case, it reads from standard input as the [FILE]-f –[/FILE] command line option indicates. It restores the data to where the working directory is currently positioned. In the example the current working directory is [FILE]/mnt[/FILE] where the target file system. ‘a’ is mounted The [FILE]–r [/FILE]flag means rebuild the file system. The restored file system will be of the same size as the one dumped including its free space it you did not change the content of the (liveSPsizes) file. In reality what we have with the above dump command is as dump writes a block of data to standard output its immediately handed to restores standard input and written to the target. The above snippet of code would have to be duplicated for each file system you wanted to dump.

The following (fbsd2clone) script employs the sample code snippet detailed above.

Code: Select all
# This script will use dump/restore to clone your running
# system to another motherboard cabled hard drive or
# USB cabled hard drive or USB flash stick.
# This is run as root.
# Change these device unit pre-fixs as needed
#     ad0 is the live file system
#     da0 is the target

echo "Collect live file system sizes and save"
df -h  > liveFSsizes
cat liveFSsizes

echo "  "
echo "  "
echo "Collect live Slice Partition sizes and save"
bsdlabel ad0s1 > liveSPsizes
cat liveSPsizes
echo "  "
echo "  "
cat liveSPsizes.usb

# At this point you can edit the liveSPsizes files and
# increase or decrease the file system partition sizes
# man bsdlabel for details

echo "  "
echo "  "
echo "Prepare the target"
dd if=/dev/zero of=/dev/da0 count=4
fdisk -BI /dev/da0
bsdlabel -B -w da0s1
bsdlabel -R da0s1 liveSPsizes.usb
newfs -U /dev/da0s1a
newfs -U /dev/da0s1d
newfs -U /dev/da0s1e
newfs -U /dev/da0s1f

echo "  "
echo "  "
echo "Mount target file system 'a' / and clone"
mount /dev/da0s1a /mnt
cd /mnt
dump -0Lauf - /dev/ad0s1a  | restore -rf -

echo "  "
echo "  "
echo "Mount target file system 'd' /var and clone"
mount /dev/da0s1d /mnt/var
cd /mnt/var 
dump -0Lauf - /dev/ad0s1d  | restore -rf -

echo "  "
echo "  "
echo "Mount target file system 'e' /tmp and clone"
mount /dev/da0s1e /mnt/tmp
cd /mnt/tmp
dump -0Lauf - /dev/ad0s1e  | restore -rf -

echo "  "
echo "  "
echo "Mount target file system 'f'/usr  and clone"
mount /dev/da0s1f /mnt/usr
cd /mnt/usr
dump -0Lauf - /dev/ad0s1f  | restore -rf -

echo "  "
echo "  "
echo "Clean up"
cd /root
umount /mnt/usr
umount /mnt/tmp
umount /mnt/var
umount /mnt

echo " Script completed"
Posts: 213
Joined: 26 Feb 2010, 09:43
Location: Angeles City, Philippines


Postby philipan » 02 Feb 2011, 11:21

* Restore the partition sizes as defined in the file liveSPsizes/liveSPsizes.usb

should be
Code: Select all
bsdlabel -R da0s1 liveSPsizes

instead of
Code: Select all
bsdlabel -R -w da0s1 liveSPsizes

However, it is correct in the script.
Thanks! Extremely useful!
Junior Member
Posts: 1
Joined: 02 Feb 2011, 10:36
Location: Augsburg, Germany

Postby synonymous » 17 Feb 2011, 03:53

Does this method work with a LiveCD as well?
I booted from a FreeBSD variant LiveCD, and then at the shell prompt created a 700MB slice on the 20GB HDD, partitioned it, and mounted the first partition (/dev/ad0s1a) to /mnt/tmp. However, I am unable to "cd" to /mnt/tmp. Is it safe to presume that this method works only for pre-installed OS?
I am trying to dump the LiveCD filesystem footprint (that is loaded in RAM) to the slice, and then modify the grub menu to boot to that partition. Maybe I am misunderstanding something very basic - can you please enlighten?
Junior Member
Posts: 4
Joined: 17 Feb 2011, 02:13

Postby Bashar » 26 Jan 2012, 16:59

No need todo anything to [FILE]/dev/da0s1b[/FILE]. I believe it should be the swap partition, no?
Junior Member
Posts: 1
Joined: 25 Oct 2010, 02:47

Postby J65nko » 09 Feb 2012, 01:17

Indeed, you don't have to format/newfs a swap partition.
Posts: 422
Joined: 17 Nov 2008, 00:06
Location: Budel, Netherlands

Return to Howtos & FAQs (Moderated)

Who is online

Users browsing this forum: No registered users and 1 guest