Using gparted
remove the existing partitions from your usb, and fix the msdos
partition table (by going to the device menu and selecting "create partition Table"). Then, create a new partition fat32
by right clicking on the unallocated
space and selecting new, making a primary FAT32 partition.
Next step create your bootable usb:
dd if=/path_to_iso_without_space.iso of=/dev/sdx
sync
You can add the bs=4M
option to make it faster:
dd bs=4M if=/path_to_iso.iso of=/dev/sdx
Example: if your device is sdb1
you should type sdb
dd if=/path_to_iso_without_space.iso of=/dev/sdb
Bootable usb thumb drive with 2 (or more) partitions, and windows to access the data partition.
Edit: This example uses an ISO instead of an installed OS. It should be simple to adjust for an installed OS.
Windows and others may only see the first partition on a usb device even when there are multiple partitions. Therefore make your first primary partition the fat32 or NTFS partition so windows can see and use it.
- partition 1 - ntfs or vfat
- partition 2 - ext4
- partition 3 - linux swap
The second partition is where you will store the bootable iso, or the installed OS. Use grub to load and select what OS you want to use.
Steps:
1: Zero out partition table on the USB device.
dd if=/dev/zero of=/dev/sdx bs=512 count=4
2: Create partitions (use cli "fdisk" or gui "gparted")
Create partition table "msdos" type.
Create Partitions.
Format partitions.
3: Install grub bootloader to usb device.
Mount ext partition on USB to PC. mount /dev/sdx2 /mnt/usbp2
sudo grub-install --boot-directory /mnt/usbp2/boot /dev/sdx
Verify: If these exist all is well so far...
/mnt/usbp2/boot/grub/fonts
-- minimum unicode.pf2
/mnt/usbp2/boot/grub/i386-pc
-- *.mod modules to load in you grub.cfg
/mnt/usbp2/boot/grub/local
-- languages
/mnt/usbp2/boot/grub/grubenv
-- environment variable storage
4: Create a grub.cfg
file for the OS's on the host pc.
sudo grub-mkconfig --output=/mnt/usbp2/boot/grub/grub.cfg
Test by booting to usb.
When you boot the USB drive you should get a list of the OS that was on the host PC not the empty USB, but this will test the grub and grub config that is on the USB.
5: Copy support files to the USB.
iso files
installed OS image from clonezilla or the like.
memdisk binary -- get from syslinux
6: Edit the grub.cfg
on the USB for your ISOs and installed OS.
Each live ISO may require different grub information.
If you only get a grub command line, your grub.cfg
probably contains errors. Go minimal to start like a single ISO.
Example grub.cfg for free live distro PartedMagic and others.
Best Answer
Introduction
For a drive with PC partitions (which is what you'll find on most USB sticks), the bootloader consist in a tiny part at the very beginning of the drive (the stage 1 bootloader, in the boot sector of the drive) and a larger part elsewhere (the stage 2 bootloader, in a file). The stage 1 data contains the physical location of stage 2. If you copy the whole device (what you tried with
dd
), the stage 1 part is in the same physical location and the stick works, but the new stick has to be at least as large as the original. If you recreate the partitions and copy the files, you're missing the stage 1 since it's not a file.I think (but I haven't checked) that the stage 2 is in the first (FAT32) partition. If so, you can copy that partition wholesale, and copy the boot sector as well, then edit the partition table so that the second partition occupies exactly the space that's left, and copy the files on the second partition. You might as well keep a raw image of the boot sector and of the first partition in a file on a hard disk (but you can clone them from a USB stick too).
Making a clone of a different size
Below I'll call the device containing the original stick
/dev/sdb
and the device containing the clone/dev/sdc
; substitute names as needed. Create mount points if needed. I assume that the FAT32 partition (at the beginning of the disk) is numbered 1 and the ext3 partition is numbered 2; substitute different numbers if needed.First part, if cloning from a stick:
First part, if cloning from image files:
The call to
partprobe
is needed so that the kernel reloads the partition table from the disk (it's in the boot sector, and the first command in either scenario above modifies the boot sector). If you omit it, either you'll get errors or the kernel will silently write garbage to the stick.To make the image files:
Now after you've got the boot sector and the first partition, you need to edit the partition table again, to resize the second partition. You can do this automatically by feeding the right input to
fdisk
. Warning, this is extremely error-prone, and I haven't tested it. If you don't feel completely comfortable with this part, do it by hand.Finally reload the partition table again, create an ext3 filesystem, and copy the files there.
Simplified method to make a larger clone
If you prefer, you can store an image of the whole stick. You'll be able to use that image directly on any stick that's at least as large: just copy that image onto the new stick with
cat <whole.img >/dev/sdc
, then runpartprobe /dev/sdc
(or unplug the stick and put it back in) to re-read the partition table.Then, if you like, you can enlarge the clone. In this order:
parted
can do it with themove
command (notresize
, which resizes the filesystem), but you need to figure out the right arguments — switch to sectors (unit s
) to avoid rounding errors.resize2fs /dev/sdc2
to make the filesystem use the whole partition size.If you're starting from a larger stick than necessary, you can shrink it by following the steps above in the reverse order (first shrink the filesystem with
resize2fs
, then shrink the partition withparted
).Alternatively, you can first copy the stick to an image file, then work on that image file. To access the partitions, use a loop device, specifically associating a portion of the file to a disk. Here's a sketch of the steps if you want to shrink the image (warning, untested, proceed carefully and double-check the math).