Clone UEFI booted Debian Linux

At work I had the need to clone a debian box that boots via UEFI.

I have a remote boot setup, but you can also boot into a rescue CD.

Then clone the old disk to a Server. Here is the old layout:

parted /dev/sda print
Model: ATA ST980813ASG (scsi)
Disk /dev/sda: 80.0GB
Sector size (logical/physical): 512B/512B
Partition Table: gptMYSERVER

Number Start End Size File system Name Flags
 1 1049kB 538MB 537MB fat32 boot
 2 538MB 75.8GB 75.3GB ext4
 3 75.8GB 80.0GB 4184MB linux-swap(v1)

I copied the data via ssh:

mount /dev/sda1 /mnt
cd /mnt
time tar cvfj - . | ssh MYSERVER "cat > HDD-image/sda1.tar.bz2"
umount /mnt

mount /dev/sda2 /mnt
cd /mnt
time tar cvfj - . | ssh MYSERVER "cat > HDD-image/sda2.tar.bz2"
umount /mnt

Now reboot the box with the new disk. First we need to create a new GPT Table and partitions:

parted /dev/sda
(parted) print
Error: /dev/sda: unrecognised disk label # If a brand new disk is used, you will get this error

(parted) mklabel gpt
(parted) unit mib
(parted) mkpart primary 1 538
(parted) set 1 boot on
(parted) mkpart primary 538 71680
(parted) name 2 root
(parted) mkpart primary 71680 75776
(parted) name 3 swap
(parted) quit


You need to reboot to see all new partitons. Then format them:

mkswap /dev/sda3
mkfs.ext4 /dev/sda2
mkfs.vfat /dev/sda1

Copy the data from the server:

mount /dev/sda1 /mnt/
cd /mnt/
time ssh MYSERVER "cat HDD-image/sda1.tar.bz2" | tar xvfj -
umount /mnt

mount /dev/sda2 /mnt/
cd /mnt/
time ssh MYSERVER "cat HDD-image/sda2.tar.bz2" | tar xvfj

mount the efi boot partition and next copy the new partitions UUIDs to the /etc/fstab file so you can edit this file so that grub will later find its disks:

mount /dev/sda1 /mnt/boot/efi
blkid >> /mnt/etc/fstab
vi /mnt/etc/fstab # to match new UUIDs

Update the following files to represent the new hostname:

  • /mnt/etc/hostname
  • /mnt/etc/exim4/update-exim4.conf.conf
  • /mnt/etc/hosts
  • /mnt/etc/mailname

Update the network settings:

  • /mnt/etc/network/interfaces

Next step is to update EFI boot sector and grub. Mount all necessary partitions and chroot into the clone:

mount --bind /dev /mnt/dev
chroot /mnt
mount -t proc proc /proc
mount -t sysfs sysfs /sys

If you want now is the time to regenerate the SSH keys. This is an optional step:

rm /etc/ssh/ssh_host_*key*
dpkg-reconfigure openssh-server

Install grub-efi and update EFI target:

apt-get install --reinstall grub-efi
grub-install --target=x86_64-efi /dev/sda

The first time the EFI doesn’t know which entry to boot. There is a failback option. We will use this and copy the debian boot loader to the failback location:

mkdir /boot/efi/EFI/BOOT
cp /boot/efi/EFI/debian/grubx64.efi /boot/efi/EFI/BOOT/bootx64.efi

now it’s time to update grub to let it know the new partition UUIDs:


Now everything should be in place. Unmount everything, leave the chroot env and reboot the box. It should boot now from the disk.

umount /proc/
umount /sys/
exit # from chroot
umount /mnt/dev
umount /mnt/boot/efi
umount /mnt

If you call grub-install and update-grub again, you can remove the /boot/efi/EFI/BOOT dir and the box should still boot.

Leave a Reply

Your email address will not be published. Required fields are marked *