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
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.