Clone Disk Images to Save Time and Network Usage

I have installed Ubuntu over a dozen times in the last week - a couple on bare metal and many more as guests under Xen. I have gotten pretty good at navigating the menus and options, but it still takes time to fill in fields and click “Continue”. And the data usage according to my ISP…

There has to be a better way.

That better way is to copy an already-installed system’s disk and use that for subsequent guests/instances.

Assumptions

Below are a couple of assumptions that the rest of the post rely upon.

  • an already installed “template” system that has been shutdown (powered off)
  • the template and target systems are virtual machines
  • the template and target will use the same disk sizes
  • (optional) network settings use DHCP to prevent IP collisions
  • (optional) needed/wanted packages are already installed on the template instance

The template instance - the instance that all other instances are based upon - should have as much installed and updated as possible. This will reduce time and network usage in the long run.

For example, if the function of the instances is to run nginx, then nginx should be installed before continuing.

Duplicate Those Disks

NOTE: The template instance must be shutdown (powered off)! Otherwise there is a risk of data loss!

In the examples below, the template installation is mesos01 and is being used to create mesos02 and mesos03.

Create New Disks

Create disks for the new instances. The steps below use LVM; your tools may differ.

Run lvcreate to create new, blank volumes for the new instances. The size (-L 200G) matches exactly with the template instance (mesos01).

1 cdcd@xen01:~$ sudo lvcreate -L 200G -n mesos02 /dev/xen01-vg
2   Logical volume "mesos02" created.
3 cdcd@xen01:~$ sudo lvcreate -L 200G -n mesos03 /dev/xen01-vg
4   Logical volume "mesos03" created.
5 cdcd@xen01:~$ sudo lvs
6   LV        VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
7   mesos01   xen01-vg -wi-ao---- 200.00g                                                    
8   mesos02   xen01-vg -wi-ao---- 200.00g                                                    
9   mesos03   xen01-vg -wi-ao---- 200.00g

Copy Disks

Use the dd tool to copy the template instance’s disk to the new volumes. The if argument is the input; of is the output target. In context for this exercise, if is the template instance, of is a new instance.

1 cdcd@xen01:~$ sudo dd if=/dev/xen01-vg/mesos01 of=/dev/xen01-vg/mesos02 bs=10M
2 20480+0 records in
3 20480+0 records out
4 214748364800 bytes (215 GB) copied, 3215.04 s, 66.8 MB/s

Do It In One Go

Here is a quick for loop to do it all at once.

1 for i in 02 03; do
2   name="mesos${i}"
3   new_lv="/dev/xen01-vg/${name}"
4   sudo lvcreate -L 200G -n $name /dev/xen01-vg
5   sudo dd if=/dev/xen01-vg/mesos01 of=$new_lv bs=1M
6 done

Make Them Unique

At this point, all of the cloned instances will have the same network settings and hostname. Actually, they have the same everything – including SSH credentials and host keys!

Fix Hostnames

Hostnames can be changed by editing /etc/hostname and should be changed to suit the particular instance. Do not forget about /etc/hosts!

Reboot to ensure the change takes effect.

Reset OpenSSH Keys

It is a bad idea to have multiple hosts using the same SSH keys. The process below resets the host keys for a debian-based system.

Steps:

  1. Delete current keys (ssh_host_*)
  2. Regenerate keys (dkpkg-reconfigure)
  3. Restart SSH server (systemctl restart ssh)
 1 cdcd@mesos03:~$ sudo rm -rf /etc/ssh/ssh_host_*
 2 cdcd@mesos03:~$ sudo dpkg-reconfigure openssh-server
 3 Creating SSH2 RSA key; this may take some time ...
 4 2048 db:34:fd:9f:99:11:0f:9c:a6:06:18:7a:3c:da:c3:db /etc/ssh/ssh_host_rsa_key.pub (RSA)
 5 Creating SSH2 DSA key; this may take some time ...
 6 1024 7d:97:35:e9:32:1f:ac:9d:79:3f:a7:ac:f7:e0:6c:27 /etc/ssh/ssh_host_dsa_key.pub (DSA)
 7 Creating SSH2 ECDSA key; this may take some time ...
 8 256 8e:1f:5a:7d:36:84:8e:15:d3:1c:7f:0f:4a:4b:91:ff /etc/ssh/ssh_host_ecdsa_key.pub (ECDSA)
 9 Creating SSH2 ED25519 key; this may take some time ...
10 256 a8:89:8e:8b:59:8e:96:8a:27:c4:54:bc:a5:78:3b:24 /etc/ssh/ssh_host_ed25519_key.pub (ED25519)
11 cdcd@mesos03:~$ sudo systemctl restart ssh