Cloning from a .img file to a hard drive

How to clone from a .IMG file to a physical hard drive using the Ubuntu preboot installer

Setup

Ensure that you have your .img file ready, this guide does not cover how to turn a physical drive into a .img file. The file should be stored on external media using the ext4 partition scheme. We will use the term golden image to refer to this .img file and golden key to refer to the physical medium that it is stored on.

Your target system (the machine that you are cloning TO) must have at least two open USB ports:

  • The port you will plug the Ubuntu installation media into
  • The port you will plug the golden key into

Preflight checklist

  • [ ] I have at least two USB ports on the target machine
  • [ ] I have an Ubuntu installation USB (or some Linux UEFI install media) prepared and have verified that it works
  • [ ] I have a my golden key ready and have verified that it contains a valid version of the golden image on it
  • [ ] I have a backup of the golden image stored securely somewhere else, ideally using the 3-2-1 backup methodology (at least three copies, using at least two different types of media (USB flash drive vs SSD vs Amazon AWS S3 blob storage), and at least one of these copies is off-site. This protects against media incompatibility, physical device failure/corruption, and physical damage (fires, floods).
  • [ ] The target machine does not contain any data that we are not comfortable losing (this process will completely delete the data on the target drive, and it will not be recoverable), or the data is safely backed up.
  • [ ] The target machine has a drive that is greater than or equal to the size of the golden image

Getting started

  • Plug the Ubuntu installation USB into the machine and boot from it (this varies by manufacturer, sometimes you need to press F12 during startup, check your owners manual)
  • Select Try/Install Ubuntu
  • When Ubuntu boots, close the installer
  • Open the disks application by pressing the SUPER key (Windows key on most keyboards), searching disks, and pressing enter.
  • Identify the disk that is your target machine's internal hard drive. It will probably be bigger than your USB drive. It will generally be named something like /dev/sdX# where X is some letter (often but not always A) and the number will be the partition number (if there are more than one partitions, click each and verify that the name is still /dev/SDX and only the number changes)
  • Plug in your golden image and note what the drive path for it is (it will follow the same /dev/sdX format, but the X will be different)
  • Close the Disks application and open the files application
  • In files, navigate to your golden key drive, then right-click and select Open in Terminal.

Terminal commands

  • Caution: improperly entering the dd command can permenately corrupt any of the drives plugged into the machine (your golden key, the target hard drive, even your ubuntu installation USB). While closely following these instructions reduces your risk of corruption, it does not eliminate it. Use your best discretion, and seek qualified assistance if you are unsure.

Lets get started.

  • Type the command ls and press enter, identify the file name of your golden image. It will likely have the .IMG extension
  • Run the following commands in order. Replace /dev/sdX with the path to your target machine drive that you identified earlier and GOLDEN_IMAGE.img with the actual name of your golden image file.
sudo dd if=GOLDEN_IMAGE.img of=/dev/sdX bs=64K status=progress

This command will take a substantial amount of time. Do not stop it until it finishes.

  • When the command finishes you will be able to enter more commands in the terminal. If you cannot type in the terminal, then the command is not finished.
  • Upon completion, close the terminal, shut down (not reboot) the machine.
  • When the power is off, you may disconnect both your golden key and your Ubuntu installation USB
  • Power on the computer and verify that the system boots normally

Troubleshooting post-clone

If the system does not boot, the most likely issue is with the GRUB2 bootloader. Resolving this will involve booting back into the Ubuntu installation media, mounting the physical drive, binding the device paths to the mounted paths, then running the grub-install /dev/sdX command followed by the update-grub command.

This can be challenging for beginners and may warrant its own instrucitonal post. Please seek qualified help if this occurs.