Ceci est une ancienne révision du document !
In a previous article, I described a step-by-step procedure to create a hybrid (boots on BIOS or UEFI system) portable clone of your system on a 512 byte sector USB drive. Although I had tested the technique successfully on a number of devices I had laying around, when I tried it on a recently purchased SSD, it did not work! It turned out that the new SSD had physical/logical sectors of 4096 bytes and this caused two problems with the method employed that made the drive non-bootable. I was able to track down and resolve the difficulty, although it does require a different set of steps to those in the earlier article. As many newer drives are now in 4K format, I thought it would be useful to share the results of my findings. (The process described here does work on 512-byte sector drives with only one minor modification – see Technical Note #2). One important thing to note before we begin is that BIOS booting is not generally supported from drives with 4096-byte sectors; consequently, we cannot create a hybrid clone on such a drive. The resulting cloned system is still portable, however, and should boot on whatever UEFI machine it is plugged into.
Dans un précédent article, j’ai décrit une procédure étape par étape de création d’un clone hybride (qui démarre à la fois sur un système BIOS ou UEFI) portable de votre système sur un lecteur USB de secteurs de 512 bytes. Bien que j’aie testé la technique sur de nombreux dispositifs que j’avais à portée de la main, sans aucun problème, quand je l’ai essayé sur un SSD que je venais d’acheter, cela n’a pas fonctionné ! Il s’est avéré que le nouveau SSD avait des secteurs physiques/logiques de 4096 bytes et cela a causé deux problèmes avec la méthode utilisée et ces problèmes ont rendu la clé non amorçable. J’ai réussi à trouver et à résoudre la difficulté, bien qu’il nécessite un jeu d’étapes différent de celui dans l’autre article. Puisque beaucoup des disques sont maintenant en format 4K, je pensais que ce serait utile de partager les résultats de mes recherches. (Le processus décrit ici fonctionne sur des disques de secteurs de 512 bytes avec une seule modification mineure – CF la Note technique n° 2). Avant de commencer, il est important de noter que le démarrage sur BIOS n’est pas généralement pris en charge par des disques avec des secteurs de 4096 bytes ; par conséquence, nous ne pouvons pas créer un clone hybride sur un tel disque. Toutefois, le système cloné résultant reste portable et devrait démarrer sur n’importe quelle machine UEFI sur laquelle il est branché.
It is not possible to anticipate the particulars of every conceivable system configuration so I have made a few assumptions: • The system to be cloned is a typical installation with the Ubuntu system on a single partition, and no swap partition. • The source computer that you are cloning boots in UEFI mode and has a separate EFI partition. • The destination drive must be big enough to accommodate the Ubuntu partition that we are going to clone – the whole partition, not just the used space! (Resizing your Ubuntu partition to make it smaller with gparted is quite safe – the Ubuntu installer does this when you set up a dual boot installation – BUT there is always the potential for data loss so don’t do it unless you have backed up anything you cannot afford to lose.) • Many of these steps require Administrator privileges so make sure you know how to use this facility on your system as it varies a bit across Ubuntu flavours.
I have written this article for the non-expert (like myself) and so I have included a lot of cross-checks to avoid errors. More experienced readers can probably skip some of these steps if desired.
Getting Information About Your System
Boot your computer normally and open a terminal. Type:
df
and look under the “Mounted On” column to find the root symbol (/). On my machine, this is opposite /dev/sda2, and this tells me that the system presently running is on Partition #2 (below). Make a note of this number from your own system, the drive name might change after rebooting but the partition number won’t, so it is important to remember which partition you are going to copy.
Now type:
sudo mkdir /00_CLONE
to provide an easily recognizable marker for the cloned system in the root folder.
Cloning Your System
Now reboot your computer from a Live USB stick; you can use the install media for your system or any other Ubuntu flavour – I am using a clone of my base system on a USB. Booting from a live USB is important as you will be working on the system partitions, and it is much safer if nothing is mounted. This way, we can be sure that your base system in particular is unmounted, and not damaged by anything that you do. So, begin by opening a terminal and typing
sudo umount -a
to unmount everything.
Plug in your installation media to a vacant USB port.
Open the program gparted (included as part of the live USB)
This is my “testing” laptop and you can see that I have a 250GB drive designated as /dev/sda with 4 partitions (below). I have arranged my system with 2 partitions for storage of personal files (/dev/sda3 and /dev/sda4), so my Ubuntu system partition (/dev/sda2, mounted at /) is just 30GB. If you click the pulldown menu at the top-right, you will get the partition layout for the other drives on the system, in this case the portable SSD that you will use to clone your system, as well as the Live USB that you booted from.
If your drive contains any data, it will be completely destroyed by the following process so make sure you don’t need anything that is left on the SSD. Select the SSD from the drop-down menu, click on Device > Create Partition Table, and make sure you choose “gpt” as the partitioning system so that it is compatible with UEFI booting. Click Apply to make the change, your device now shows unallocated space for the entire drive (top right).
to reboot. It is unclear why this message sometimes appears when other times it does not; however, it seems that clicking Ignore and proceeding does not appear to cause any problems. If this makes you nervous, close gparted, reboot, and then proceed.
Now select Partition > New to create the first of 2 partitions.
The “Free space following (MiB)” will vary depending on the size of your SSD, but the other values should be as shown here – note the use of “cleared” in the File System box, this ensures there are no filesystem remnants still present (bottom right). Click “Add” and then click on the green checkmark at the top to apply this change. Do NOT format this partition to FAT 32, it will not work for devices with 4096-byte sectors. (See Technical Note #1 at the end of the article for a full explanation of why this is the case).
Next select Partition > New to create the second partition for the Ubuntu system itself and adjust the values as appropriate (above).
Once again, select Add and then apply this change.
Each of these partitions serves an important function: Partition #1 provides a place for the UEFI GRUB bootloader files, and Partition #2 contains your Ubuntu filesystem.
Now go back to select the device name of your hard drive, right-click on your Ubuntu partition, and select Copy. Switch to the SSD, double check that Partition #2 is unmounted, right-click on it, and select Paste. Depending on the size of the partition you are copying, this could take an hour or more; for this size of partition it was about 15 minutes. This procedure makes an exact and complete copy of your Ubuntu partition, including the label if there is one, and the UUID (Universal Unique ID). Furthermore, gparted will automatically “grow” the filesystem to use all of the partition (bottom left).
Once this copying step is complete, right-click on Partition #2, and give the partition the label Ubuntu_SSD, just to make it easier to identify; and apply the change. Right-click on Partition #1 and select Manage Flags > boot,esp so that this partition is recognized as an EFI partition. Click Close to apply the change but don’t close gparted yet as it is still needed in the next step.
If you open a terminal and type:
sudo blkid
you will note that both the hard drive Ubuntu partition (/dev/sda2 in my case) and the partition on the SSD (/dev/sdb2 in my case) have exactly the same UUID . This is a problem for GRUB as it typically will boot the first partition it finds with the UUID in its configuration file, generally the hard drive, resulting in a failure to boot from the SSD. Using gparted, select the SSD and right-click on the Ubuntu_SSD partition, select New UUID, and apply this change; now gparted can be closed. Open a terminal and type:
sudo blkid
again and you will see that the UUID for the Ubuntu system partition is unchanged BUT the Ubuntu_SSD partition now has a different UUID.
Now we need to format the EFI partition (/dev/sdb1 in my case) with a FAT32 filesystem. Open a terminal and type:
sudo mkfs.fat -v -F 32 -S 4096 -s 1 /dev/sdX1
(change X to an appropriate value for your system where X = a, b, c …), and after this completes successfully, you can close the Live USB, remove it when prompted to do so, and reboot your system normally.
At this point, I suggest that you right-click on the Desktop and at least temporarily change the background. Later on, this is an easy way for you to know you booted from the SSD (which will have the old background) rather than the system on the hard drive which has the new one. Also open a terminal and type:
sudo rmdir /00_CLONE
which puts your base system back the way it was before we started; but, leaves a 00_CLONE marker in the root directory of the SSD. This marker provides a good way to ensure that we don’t mix up our locations between the hard drive and the SSD clone and edit the wrong system files.
Adjusting UUIDs to Correct Values
The next thing to do is to change the UUIDs in two system files on the SSD to match their new values so that GRUB finds the SSD system rather than the system on the hard drive that we cloned. (Note: Remember, all of these UUIDs will be different on your system so use the ones displayed in your terminal window rather than the ones displayed here). The two files that require adjustment are: /etc/fstab and /boot/grub/grub.cfg. Open a terminal and type:
sudo blkid
to bring up the UUIDs of all the partitions on your system once again, then open the File Manager and click on Ubuntu_SSD. (bottom left)
You should see the folder 00_CLONE and the address bar showing Ubuntu_SSD mounted at /media since it is an external USB device, otherwise you are in the wrong location. Remedy this immediately, you do NOT want to edit the fstab file of your base system! Navigate to /etc so you can Edit as Administrator the file /etc/fstab. Copy and paste the UUID for Ubuntu_SSD into this file and delete the previous UUID entry (line 9). You will also need to edit the entry like the one at line 14, with the UUID for the EFI partition so that it gets mounted when Ubuntu_SSD boots (above). Save the file and exit after making these changes.
Next, use the up-arrow of File Manager to move back up the directory structure to navigate to /boot/grub, and Edit as Administrator the file /boot/grub/grub.cfg. Find the old UUID (the one for your hard drive) somewhere in the file, and highlight it. Select Search, and this highlighted value will be in the “Search for” box. Paste the UUID for Ubuntu_SSD from the terminal into the “Replace with” box, and select “Replace All (next page, top right). Select Close, then Save this file and exit.
Installing Bootloaders
Although there is a complete Ubuntu installation on the SSD, it is not yet directly bootable; however, it is safest to install boot loaders from a working system. As this step is crucial to the process, I am providing two different methods of booting into your cloned system in the unlikely event that one or the other does not work. This way, the appropriate bootloaders can be installed from the SSD in a working state.
Method #1 (Preferred):
There is an easy “trick” you can use to boot your newly created Ubuntu_SSD system without a bootloader installed. Simply open a terminal and type:
sudo update-grub
When you do this, GRUB should find the Ubuntu_SSD installation on /dev/sdX2 (where /dev/sdX2 is the device name for your Ubuntu_SSD partition), and add it to the GRUB boot menu. If update-grub does not find Ubuntu_SSD on /dev/sdX2, then use Method #2. Otherwise reboot your system and use the down-arrow to select the new entry corresponding to your Ubuntu_SSD installation and boot into the cloned system – you will know you were successful as long as the old background appears.
Method #2 (Fallback):
Reboot your system normally and when the GRUB menu appears, press the “c” key immediately to get a grub > prompt. If you don’t normally get a GRUB menu when you boot, then press the ESC key repeatedly as soon as you reboot and the GRUB menu should appear, then press “c” immediately. Type:
ls
and you will get a list of all the devices that GRUB recognizes, for example: (hd0), (hd0,gpt1), (hd0, gpt2), (hd1), (hd1,gpt1), and so on. You need to find which one of these is the location of Ubuntu_SSD. Because it was installed on the second partition, just check each drive’s second partition until you find the right one. Type:
ls (hd0,2)/
If you see the folder 00_CLONE, you’re done; if not, type ls (hd1,2)/ then ls (hd2,2)/ … until you find it. Now type each of the following commands, one at a time, and press ENTER after each command. If you don’t get an error message, then they have executed correctly. Replace X with the value you found for the Ubuntu_SSD partition in the previous step.
set prefix=(hdX,2)/boot/grub
set root=(hdX,2)
insmod normal
normal
The computer should now boot and display a rather unusual GRUB menu which consists of two typical GRUB menus stacked one after the other. These are the menus from the base system (the top one) and the SSD system (the second one). Simply use the down-arrow to select the “Ubuntu” entry in the second GRUB menu and the computer should boot into the cloned system – once again the old background tells you that you have successfully booted from the SSD.
Open a terminal and type:
df
and check the device name (/dev/sdX2) mounted at the root (/) as drive names sometimes change after a reboot. There should also be a device (/dev/sdX1) mounted at /boot/efi (if there isn't, that will get addressed in a moment). Open the File Manager and check that 00_CLONE is present in the root filesystem, then Open as Administrator the /boot folder of your Ubuntu_SSD system. There should be a folder called efi; if there isn’t, right-click to create a new folder named efi. This ensures that there is a mount point of /boot/efi for the EFI partition as required. The EFI partition ought to have been mounted here by fstab at boot time, but just to err on the side of caution, let’s unmount and remount it to be sure. Type:
sudo umount /boot/efi
and then mount the EFI partition from the SSD with:
sudo mount /dev/sdX1 /boot/efi
(where /dev/sdX1 is the device name for your EFI partition) so that bootloader files can be written to that partition.
The first thing to check before doing anything else is that all of the appropriate grub packages are installed on your SSD. To do this, open Synaptic, select Status > Installed, and scroll down to grub entries. There are 6 things to look for (below). Depending on how your system is configured, these may or may not be installed but they all need to be there to proceed.
If any are missing, select All and scroll down to the grub packages and install it/them. If you do one at a time, others may get installed automatically in the event several are missing. If Synaptic is not on your system (it is no longer installed on newer Ubuntu distros by default), install it with:
sudo apt-get install synaptic
However, if you have another preferred method for checking installed packages and installing any missing ones, you can use that instead. Once the 6 grub packages are all installed, open a terminal and type:
sudo apt-get install –reinstall shim-signed
You may be asked to confirm where you want to install the bootloader files and this is /dev/sdX1 (the EFI partition we created at the start). If you are presented with a graphical screen with several choices, use the space-bar to toggle the asterisk indicating your choice, the arrow keys to navigate; and the ENTER key when you are ready to proceed.
This particular method actually installs a series of bootloader files, including those required for booting with secure boot enabled. If you Open as Administrator the folder /boot/efi and then double-click on EFI, you should see 2 folders, BOOT and ubuntu. These folders contain the files needed to boot Ubuntu (ubuntu) and also a “fallback” option (BOOT) in case something doesn’t work properly.
Your clone Ubuntu_SSD is now a complete, portable system. Reboot your computer and press the F12 key (or whatever key you normally use) to bring up the boot menu. Select the USB SSD device and you should boot into Ubuntu with the old background screen. Open a terminal and type:
df
and you should see both the Ubuntu_SSD partition mounted as root (/) and the EFI partition mounted on /boot/efi; if you don’t, go back and double-check the UUIDs in /etc/fstab and edit as required. As your final step type:
sudo update-grub
to ensure that the GRUB configuration file is correctly updated. If you plan to use this SSD clone in a semi-permanent way, I suggest you modify the boot order settings in the UEFI firmware to boot from USB first. This way, the computer will always boot from the USB SSD if it’s plugged in, and you can always choose to boot to the hard drive by selecting the appropriate option from the GRUB menu that will be displayed at boot time.
I have successfully booted this SSD drive on both of my laptops that support UEFI booting (Acer and Dell). It is impossible to guarantee that it will perform correctly on every system; but, hopefully it works for you!
Technical Note #1
I originally used gparted to format the EFI partition to a FAT32 filesystem and although I did not receive any error messages, the resulting system would not boot! Interestingly, using gparted to check the EFI partition does produce an error message saying that “Filesystem is bigger than its volume!”, but there is really no reason to do that when there is no indication anything is wrong. After seeing this cryptic message, I used the terminal command:
sudo fsck.fat -v -n /dev/sdb1
to check the filesystem on the EFI partition to try to get more detailed information. The resulting details (Figure 11), along with some digging into the FAT32 specification, explain the problem.
A valid FAT32 partition must contain a minimum of 65,525 clusters, where a cluster is a whole number (1, 2, 4, 8, … 128) multiple of the sector size. The sector size of this SSD is 4096 and the program that gparted calls to do the formatting (mkfs.fat) chooses to use a multiple of 8 for a cluster size of 8 × 4096 = 32,768 bytes. The minimum size for a valid FAT32 partition with this cluster size would be 65,525 × 32,768 = 2,147,123,200 bytes (~ 2.1 GB), far bigger than our 500MB partition. I suspect that mkfs.fat uses this multiple because with a sector size of 512, a multiple of 8 produces a 4096-byte cluster, which is the recommended size for partitions between 256MB and 2 GB; however, it seems that gparted doesn’t check sector size before calling mkfs.fat with its default values. Unfortunately, the UEFI firmware checks the number of clusters to decide if there is a valid EFI partition or not, and because there are not 65,525 clusters, it won’t recognize the partition or boot from it. The workaround is to use the terminal command:
sudo mkfs.fat -v -F 32 -S 4096 -s 1 /dev/sdX1
where the -s 1 option specifies a multiple of 1. A multiple of 1 produces a cluster size of 1 × 4096 = 4096, which requires only 4096 × 65,525 = 268,390,400 bytes (~ 268MB), well within the 500 MB size of the EFI partition. Problem solved.
Technical Note #2
If you are using a device with 512-byte sectors, just use gparted to format the EFI partition to FAT32 rather than using the terminal command. Everything else stays the same.