HOWTO Clone a Machine

From Research
Revision as of 17:24, 17 November 2009 by Gordp (talk | contribs)
Jump to navigation Jump to search

Using standard Linux tools, this guide explains how to copy a working Linux machine onto a brand new machine, in less than an hour. It is also very useful to re-create a machine which is undergoing a disk-(subsystem) upgrade, such as migrating to RAID or a different level of RAID.

Server/Golden Machine

First, create an image of a good working system or use an existing image, if you have one. At Research we have images stored at musashi:/export/mirror/image/fresh-deployable-gentoo-images

  • Follow these steps, as root:
livecd root # cd /
livecd root # mount /boot
livecd root # tar cpzf hostname.tgz / --exclude=/hostname.tgz --exclude=/proc --exclude=/lost+found --exclude=/mnt --exclude=/sys --exclude=/tmp
  • This will create a host image of about 700 MB for a minimal server; as much as 22GB for a loaded workstation. Forgetting to mount /boot will mean your target machine won't have any kernel, nor grub.conf! You'll need a rescue-CD to create grub.conf, and you'll have to compile a fresh kernel.

Client/To Be Imaged Machine

Boot the machine you want the image deployed on with a minimal Gentoo CD. Mount that hard drive and partition it appropriately.

livecd root # fdisk /dev/sda
/dev/sda1	/boot	500M     <make this partition bootable>
/dev/sda2	swap	8000M
/dev/sda3	/	30000M     <or, the entire remainder of the disk>

Add filesystems

livecd root # mke2fs /dev/sda1
livecd root # mkswap /dev/sda2
livecd root # mke2fs -j /dev/sda3


Mount drives:

livecd root # mount /dev/sda3 /mnt/gentoo
livecd root # mkdir /mnt/gentoo/tmp
livecd root # mkdir /mnt/gentoo/boot
livecd root # mkdir /mnt/gentoo/proc
livecd root # mount /dev/sda1 /mnt/gentoo/boot
livecd root # swapon /dev/sda2
livecd root # mount -t proc none /mnt/gentoo/proc

Copy over the hostname.tgz file onto this machine.

livecd root # scp root@hostname:/hostname.tgz /mnt/gentoo

Extract it into the root of the new machine /mnt/gentoo

livecd root # cd /mnt/gentoo
livecd root # tar -xzf hostname.tgz

Chroot into the new environment.

livecd root # chroot /mnt/gentoo /bin/bash
livecd root # env-update && source /etc/profile

Verify /boot/grub/grub.conf and /etc/fstab

Setup grub on the hard drive:

livecd root # grep -v rootfs /proc/mounts > /etc/mtab
livecd root # grub-install /dev/hda

Finish Configuring the machine:

  • Set a root password.
  • Set the hostname.
  • Static ip address.
  • Reset log files.
  • Check /etc/make.conf

Make system dirs:

livecd root # mkdir -p /mnt/gentoo-distfiles
livecd root # mkdir -p /sys

Exit and umount drives, remove Gentoo CD, reboot the machine.