Klaus Knopper answers your Linux questions
Ask Klaus
Klaus Knopper answers your Linux questions.
Cloning Live System USB Flash Drives
Dear Klaus,
First, I want to thank you once again – this time for solving my problem with 3G connectivity in issue 178 [1]. Knoppix 7.5.0 now runs like a charm on my old Eee PX 901Go, from which I could finally drop the way-too-old Xandros I still used because of the 3G issue.
I have another question: As I have made already a few USB sticks for friends with Knoppix 7.5.0, I found myself always doing the same postinstall jobs (e.g., adding a few programs, changing the keyboard default layout, adding plugins to the web browser, …). It would be nice if I could just make a new clone of such a modified Knoppix 7.5.0 that includes all those changes.
I tried to copy one stick to another on a Linux machine using
dd if=/dev/<modified_stick> of=/dev/<stick_for_friend>
but this almost always failed to work, even when both sticks were the same type and make. I also tried to recreate the ReiserFS you use on the target stick, but that also got me in trouble.
Could you describe how to accomplish this the right way? Maybe it is possible to add this procedure as a new option in the next version of Knoppix – something like Install personalized KNOPPIX to flash disk?
Kind regards, Hugo Coolens
Hello again, Hugo,
Your question touches on the general problem of properly cloning an installed operating system to another medium of different size, which gives me an occasion to discuss the topic here. :-)
Some Knoppix downloaders might wonder why I still distribute Knoppix as a DVD or CD ISO image, even though those drives are getting rare nowadays. The reason is simply because these images are still easier to handle on the side of the computer creating the installed medium than are those for hard disks or flash drives. CD- and DVD-ROMs, at least by standard, do not have a partition table that could cause trouble at bootup, and CD/DVD burning programs can always handle images that are smaller than the theoretical maximum size of the medium.
The script flash-knoppix
included in Knoppix can create USB flash drives from a Knoppix ISO image, without having to burn a DVD first, just by appending the name of the ISO image to the command.
Flash drive images are more difficult. Some BIOSes refuse to boot from pen drives with "incorrect" partition tables. It is mostly OK to have a partition table that does not cover all the available space, but partition tables that claim that the device capacity is larger than it really is will cause trouble – ranging from write errors, by attempting to access blocks outside of the physical limit, to non-bootability. Even with the same brand and given size of a flash disk, the real capacity might be less by just a few kilobytes because of a varying reserve for defect block management; therefore, if you buy an "8GB" drive, it could be anything between 6.5 and 7.5GB of usable capacity. The reserved bytes are not visible, nor accessible, without reprogramming the drive's internal controller.
Listing 1 shows two so-called 8GB flash drives of the same brand from the same manufacturer. Note the difference in the usable capacity and, thus, the different perfect partitioning. You actually have a 50/50 chance that the target drive matches or exceeds the capacity of the source drive. Only if both drives are exactly equal do you get a perfect copy.
Listing 1
Output of "fdisk -l" for Two Flash Pen Drives
If you do a 1:1 copy with dd
from an 8GB to a 16GB drive, the 16GB drive will usually work fine, and you could even extend the last partition to cover the rest of the drive. For Knoppix, this would be an option to provide a "minimum size" flash drive image of around 4.5GB that can, after being copied to an 8GB drive, expand the almost-zero-sized last partition to reach the full capacity of the drive. I'll have to think about this variant again when programs for flashing pen drives with an image are included and become a standard in other operating systems someday.
Unfortunately, it's difficult to resize an installed OS "on the fly" by copying just the data stream, because the stored filesystem information needs to be smaller or equal to the partition size. Otherwise, you will get an invalid or damaged filesystem on the target when its last partition is smaller than that of the source drive – even if you have enough "free space" available at the end of the filesystem – and a truncation is possible.
The flash drive installation script flash-knoppix
solves that problem by first partitioning the target drive perfectly (utilizing the full drive capacity), creating a new FAT32 filesystem on the first partition and a ReiserFS filesystem on the second partition, then copying over the source drive content file by file. Then, a master boot record is put on the first sector of the target device; a Syslinux partition boot record will transfer control to the Syslinux bootloader when booting from the device.
Transferring system and personal configurations to the target device is not an option, yet. Only the bootloader configuration in boot/syslinux/*.cfg
is copied to the target by default. If you also want to transfer the content of the overlay partition after flashing the drive, you could mount the empty filesystem (e.g., target drive /dev/sdc
here),
mount /media/sdc2
and then copy all the changed files from the running system to the target with:
sudo cp -av /KNOPPIX-DATA/* /media/sdc2/
Be aware that this includes all your personal settings and WLAN and website passwords stored in your home directory in the current session! For this reason, I have not yet included the option in the flash-knoppix
script; it would be just to easy to give away personal secrets and passwords when your only intention is to copy a working system for friends.
You could exclude /KNOPPIX-DATA/home
from your copy command with
rsync -Hav --exclude=/home/ /KNOPPIX-DATA/* /media/sdc2/
instead. Still, you might have private information in /etc
, like system-wide password and network settings, that you've forgotten.
Infos
- "Ask Klaus!" by Klaus Knopper, Linux Pro Magazine, issue 178, September 2015, pg. 48, http://www.linuxpromagazine.com/Issues/2015/178/Ask-Klaus
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.