Outils pour utilisateurs

Outils du site


issue171:tutoriel2

Ceci est une ancienne révision du document !


When I first started using Ubuntu over 10 years ago, it was because of its ability to boot from a Live CD, thinking that this facility would be useful if I had a problem with viruses that prevented normal booting of my Windows system. I quickly realized the power and potential of Ubuntu and, although I occasionally use Windows for certain specific purposes (Income Tax, for example), Ubuntu is my working system. Many years ago, I tried, with some success, to install a full Ubuntu system on a USB stick but it was very slow, and using a Live USB was actually preferable.

I was still intrigued by the idea of having a totally portable version of my home system that I could plug into any computer. There is a substantial amount of information on this topic on the Internet. but it is somewhat fragmented and often very specific to one situation or another. Typically, it is aimed at the expert user rather than the amateur (like myself). After much research and experimentation, I have distilled what I have learned into what I believe to be a straightforward technique for creating a portable hybrid clone. I decided to try it out on a 128GB high speed USB stick I purchased on sale from Amazon (£12 or $20 CAN). With claimed read/write speeds of 300+ MB/sec, I thought that it would make a great way to run Ubuntu on my new, but entry level, Win 10 laptop which has only 64GB eMMC storage and consequently is not suitable for a dual-boot setup. Although a bit skeptical of the advertised speeds, I was pleasantly surprised to find that the system booted from the USB that I created in under 40 seconds, including login!

This article provides a step-by-step discussion of how I accomplished my goal of creating a clone of my home system (Ubuntu Mate 20.04 LTS) that will boot on either a UEFI based computer or an older machine that requires Legacy/BIOS boot. I have tried to include just enough background information to allow for an understanding of the process without getting bogged down in technical detail. I should also say that, in Linux Land, there are always many ways to do things and I make no claim that this method is the best; however, it does make use of very common Ubuntu tools and minimizes the use of the terminal. Hopefully this will help make it accessible to a wide range of readers.

It is not possible to anticipate the particulars of every possible system, so I have made a few assumptions: • The system to be cloned is a typical installation with everything on a single partition and no swap partition (if you do have a separate /home partition, and/or /swap partition, use the technique described below to clone them also. Boot is not needed as it is re-created later). • The removable media that you are using can be an external HDD, SSD, or flash drive, but the sector size must be 512 bytes and your internal hard drive must also have a logical sector size of 512 bytes (copying partitions across different sector sizes causes problems). • Your home system can use either UEFI or Legacy/BIOS boot, the clone will boot in either mode. • 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 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.

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 ‘/’ symbol. On my machine, this is opposite /dev/sda2, and this tells me that the system presently running is on partition #2 (bottom left). Make a note of this number from your system, the device name might change after rebooting but the partition number won’t, so it is important to remember which partition you are going to copy.

Cloning Your System

Now reboot your computer from a Live USB, you can use the install media for your system or any other Ubuntu flavour. Booting from a live USB is important as you are going to be working on the system partitions and it is much safer if nothing is mounted. This way we can be sure that your home system in particular is unmounted, and not damaged by anything that you do. • 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 (bottom right). I have arranged my system with 2 partitions for storage of personal files (/dev/sda3 and /dev/sda4), which means I can have my Ubuntu system partition (/dev/sda2, mounted at /) at just 30GB. The first partition is unused and is a leftover from a previous installation. 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 USB (mine is 128GB) that you will use to clone your system.

If your USB 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 USB drive. The USB may show a partition but it doesn’t matter, just select Device → Create Partition and make sure you choose “gpt” as the partitioning system so that it is compatible with UEFI booting. Then your drive will look like the image with unallocated space for the entire drive (top right).

Now select Partition > New to create the first of 3 partitions.

The “Free space following” will vary depending on the size of your drive, but the other values should be as shown here (below). Click “Add” and then click on the green checkmark at the top to apply this change.

Next select Partition > New to create the second partition and adjust the values as shown: Once again, select Add and then click on the green checkmark to apply this change.

Finally, select Partition > New to create the last partition, this one for the Ubuntu system itself: Click Add and select the green checkmark to apply this change.

The last thing you need to do is to set the appropriate flags, so right-click on partition #1, select Manage flags, and choose bios_grub as the flag, click Close, and this change will be applied automatically. Repeat for partition #2, but this time select boot, esp as the flags. Your USB drive should now be partitioned and ready to receive its own operating system.

Each of these partitions serves an important function: • Partition #1 provides a protected place for GRUB files used in Legacy/BIOS booting so there is no conflict with the space used by the GPT partition table. • Partition #2 provides a place for the UEFI GRUB bootloader files to go. • Partition #3 contains your Ubuntu filesystem.

Now go back to select /dev/sda and right-click on your Ubuntu partition, /dev/sda2 in my case, and select Unmount (if it is not mounted this choice will be greyed out.) Once it is unmounted, right-click and select Copy. Switch to the USB drive, make sure Partition #3 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 (Hybrid in this case) and the UUID (Universal Unique ID) (bottom left). Once this copying step is complete, right-click on partition #3, and give the partition a different label, for example, Ubuntu_USB, just to make it easier to identify; and, click the green arrow to apply the change. If you open a terminal and type sudo blkid, you will note that both /dev/sda2 (the hard drive Ubuntu partition) and /dev/sdb3 (the partition on the USB – the different letter is due to a reboot between taking screenshots for this article) have exactly the same UUID (shown bottom right). 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 the USB version of Ubuntu. Run gparted again, select the USB drive, right-click on the Ubuntu partition (/dev/sdb3), and select New UUID. Click on the green checkmark to apply this change.

Open a terminal and type sudo blkid again and you will see that the UUID for partition /dev/sda2 is unchanged BUT /dev/sdb3 now has a different UUID (bottom left).

This change needs to be added to the fstab file. Open the file manager and double-click on Ubuntu_USB, then Edit as Administrator the file /etc/fstab. Copy and paste the UUID for /dev/sdb3 into this file and delete the previous UUID entry.

You will also need to add an entry (line 14) so that the UEFI partition gets mounted (top right).

(Note: All of these UUIDs will be different on your system so adjust as needed.) Save the file and exit.

This change in UUID also needs to be reflected in the /boot/grub/grub.cfg file, or you will be unable to boot into the USB-based system. Edit this file as an administrator and find the old UUID and highlight it (bottom right). Then select Search, and this highlighted value will be in the “Search for” box, paste in the new UUID from the terminal into the “Replace with” box, and select replace all. Save this file and exit. Finally, use the file manager to examine the contents of the /boot folder on Ubuntu_USB. If there is a folder called efi already, make no changes, otherwise right-click to create a new folder, and then rename it as efi. This ensures that there is a mount point of /boot/efi for partition #2 as required.

Installing Bootloaders

We now have a complete Ubuntu installation on the USB but installing GRUB on removable media has known problems – difficulties that can be avoided if we install the GRUB bootloader files from a running system. And, there is a simple way to boot into the USB system so that we can install GRUB.

Since we are now finished with the live USB, shut down the system, remove the live USB and reboot to your base system. Open a terminal and run sudo update-grub. When you do this, GRUB should find the Ubuntu installation on /dev/sdb3 (bottom left), and add it to the boot menu. It will be bootable from the GRUB menu (because we adjusted the grub.cfg file to point to the correct UUID) despite having no bootloader of its own. If update-grub does not find Ubuntu on /dev/sdb3, you need to edit the etc/default/grub file as administrator and add or edit the following line: GRUB_DISABLE_OS_PROBER=“false” and try updating GRUB again. Assuming it works this time, reboot your system and check that the new GRUB menu includes an entry for the Ubuntu system on /dev/sdb3. Use the down-arrow to select it and the system should boot into the Ubuntu filesystem on the USB drive. You should open a terminal and type df just to check that /dev/sdb3 is mounted as root to ensure that you really have booted from the USB rather than your hard drive. At this point, I suggest that you right-click on the Desktop and change the background. Later on, this is an easy way for you to know you booted from the USB rather than the system on the hard drive. Now we are ready to install the appropriate bootloader files and check that the new portable drive works as it should.

The first thing to check before proceeding is that all of the appropriate grub packages are installed on your system. To do this open Synaptic, select Status → Installed, and scroll down to grub entries. There are 6 things to look for (top right). Depending on how your system was 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 (that are missing) may get installed automatically. If Synaptic is not on your system, 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.

For legacy/BIOS systems, open a terminal and type: sudo grub-install –target=i386-pc /dev/sdb, since the device name given in our df command was /dev/sdb3 and this tells us the drive name is /dev/sdb (next page, top right).

The USB can now be booted in Legacy/BIOS mode.

For UEFI systems, open a terminal and type sudo apt-get install shim-signed. (If you are told this is already installed, then use sudo apt-get install –reinstall shim-signed to repopulate the efi folder). You will be asked to confirm where you want to install the bootloader files, and this is /dev/sdb2 for this system (the UEFI partition we created at the start). On your system, the drive letter may be different so use what you found from the df command. 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.

The advantage to this particular method is it actually installs a series of programs that permit booting with secure boot enabled. If you Open as Administrator the folder /boot/efi, and then double-click on EFI, you will see 2 folders, BOOT and Ubuntu. These folders contain the files needed to boot Ubuntu, and also a “fallback” option in case something doesn’t work properly. The final step is to type sudo update-grub which will ensure that all the pointers are correct in the grub.cfg file. You will notice that the update “finds” a Ubuntu system on the hard drive (/dev/sda2 in my case, yours will be different), and this will be reflected in the grub menu with a boot entry. You can just ignore it, but, if you are offended by it, edit the file /etc/default/grub (as administrator), and add (or edit) the line which invokes the os-prober to search for other operating systems to read: GRUB_DISABLE_OS_PROBER=“true”. This will prevent other operating systems being detected and added to the grub menu. You will need to run sudo update-grub after you do this so that it takes effect – this might result in the boot menu not being displayed when you boot since there is no other system to choose; but it will still wait the specified timeout behind the scenes.

Your hybrid clone should now work. Reboot your system and press the F12 key (or whatever key your computer uses) to bring up the boot menu. Select the USB device and you should boot into Ubuntu with the new background screen (this way you know you aren’t booting from the hard drive).

I have successfully booted this USB flash drive on 5 laptops/netbooks of varying age and type (Acer, Asus and Dell) as well as both Legacy/BIOS and UEFI boot – with and without Secure Boot enabled. It is impossible to guarantee that it will perform correctly on every system; but, hopefully it works for you!

issue171/tutoriel2.1627838357.txt.gz · Dernière modification : 2021/08/01 19:19 de auntiee