Saturday, August 17, 2013

Create a KNOPPIX USB (boot-)stick from a running Linux system

I was recently examining, how to boot a (live) KNOPPIX from an USB stick. It turns out I did not find a manual about how to create a bootable USB stick containing KNOPPIX without burning KNOPPIX to a CD/DVD first?! So this is a short howto without burning anything. I'm showing how to put a KNOPPIX ISO image to an USB stick. I need an ISO image of KNOPPIX, an USB stick and syslinux installed. I've chosen to put the 4GB DVD image KNOPPIX_V7.2.0DVD-2013-06-16-EN.iso onto a 16GB USB stick. The place left on the USB device should be available to the user as NTFS partition. I'll use the term /dev/sdX to refer to the stick (where X is a lowercase character, e.g. /dev/sdb) and completely empty it first (THIS WILL DELETE ALL DATA INCLUDING THE PARTITION TABLE!):

shred -z /dev/sdX

Now after the ISO image has been downloaded, we need to alter it a bit. For this some information about the USB stick is required. The relevant bits heads and sectors/track are highlighted below:

fdisk -l /dev/sdX

Disk /dev/sdX: 16.0 GB, 16008609792 bytes
64 heads, 32 sectors/track, 15267 cylinders, total 31266816 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0000

   Device Boot      Start         End      Blocks   Id  System

Now we change the ISO image for hybrid mode:

isohybrid -o 1 -h 64 -s 32 -e 1 KNOPPIX_V7.2.0DVD-2013-06-16-EN.iso

and copy the ISO image to the USB stick:

cat KNOPPIX_V7.2.0DVD-2013-06-16-EN.iso > /dev/sdX

After the command has succeeded, the partition table will show a bootable "Hidden HPFS/NTFS" partition. Note that you can adjust the partition type by using the -t, --type switch of isohybrid.

fdisk -l /dev/sdX

Disk /dev/sdX: 16.0 GB, 16008609792 bytes
64 heads, 32 sectors/track, 15267 cylinders, total 31266816 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdX   *           1     8032255     4016127+  17  Hidden HPFS/NTFS

Now we can create one or more partitions on the stick for shipping data or $whatever by using fdisk, e.g. create a new primary partition and set the type to HPFS/NTFS/exFAT:

fdisk /dev/sdX

Command (m for help): p

Disk /dev/sdX: 16.0 GB, 16008609792 bytes
64 heads, 32 sectors/track, 15267 cylinders, total 31266816 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x12345678

   Device Boot      Start         End      Blocks   Id  System
/dev/sdX   *           1     8032255     4016127+  17  Hidden HPFS/NTFS

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (1-4, default 2): 2
First sector (8032256-31266815, default 8032256): 
Using default value 8032256
Last sector, +sectors or +size{K,M,G} (8032256-31266815, default 31266815): 
Using default value 31266815

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 7
Changed system type of partition 2 to 7 (HPFS/NTFS/exFAT)

Command (m for help): p

Disk /dev/sdX: 16.0 GB, 16008609792 bytes
64 heads, 32 sectors/track, 15267 cylinders, total 31266816 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x12345678

   Device Boot      Start         End      Blocks   Id  System
/dev/sdX1   *           1     8032255     4016127+  17  Hidden HPFS/NTFS
/dev/sdX2         8032256    31266815    11617280    7  HPFS/NTFS/exFAT

Command (m for help): w

[..]

Run mkfs.ntfs on /dev/sdX2 to create the NTFS file system there. The USB stick is ready now. Connected to the system where one wants to start a live KNOPPIX and chosen to boot this system from USB will result in a running KNOPPIX system.

The same should also work for creating a Debian live system USB stick. Thanks to the author of this article.

15 comments:

  1. Excellent article, danke schön!

    ReplyDelete
  2. Thank you very much!

    ReplyDelete
  3. I tried your method, it works. But, after booting, the NTFS partition can not be accessed. Assume it is /dev/sdb1 and /dev/sdb2, where sdb1 is the bootable partition and sdb2 is the ntfs partition. After booting the partitions are visible in fdisk. But I cannot mount /dev/sdb2 as it says /dev/sdb is mounted as iso9660 etc. Any work arounds? ( My email is bgupta@bluestsoft.com I did not understand how to fill the entry below so selected Anonym. My name is Basudeb) Thanks.

    ReplyDelete
    Replies
    1. Please take a look at the article I mentioned in the last sentence. It is a limitation you observe, but there seems to be a trick involving the command losetup. Please report back, if that worked for you.

      Delete
  4. Thanks for your comment. However I did not understand which last sentence. What I did was:
    cat KNOPPIX_V7.2.0DVD-2013-06-16-EN.iso > /dev/sdX
    Then fdisk and mkfs.ntfs
    Now it creates a bootable partition and a NTFS partition. The NTFS partition is visible from Windows well, and can be accessed properly.However if I boot from this usb, It boots up to linux but shows in mount list, the /dev/sdx mounted. So, I cannot mound /dev/sdx1 or /dev/sdx2. Which means I cannot access /dev/sdx2 from linux. Any suggestions? Basudeb

    ReplyDelete
    Replies
    1. See this article: http://ccc.zerties.org/index.php/Debian_Tips. The trick seems to be something like this:

      mkdir /mnt/mypath

      losetup /dev/loop1 /dev/sdb2

      mount /dev/loop1 /mnt/mypath


      and afterwards

      umount /mnt/mypath

      losetup -d /dev/loop1


      Delete
    2. Thanks, this worked like a charm. I need to understand though why it worked.

      Delete
  5. fdisk -l /dev/sdX
    where X = b1 in my case
    says Cannot open /dev/sdb1

    I might add that this occurs regardless of whether one runs the shred command first or has formatted with another utility (the shred command just goes on indefinitely, by the way)

    ReplyDelete
    Replies
    1. a) In /dev/sdb1 the number represents the partition 1 of the device /dev/sdb. You want to run `fdisk -l /dev/sdb'.
      b) Are you sure, the device is /dev/sdb? The command `dmesg' should show you, which device is added when you put on the stick.
      c) shred usually doesn't go on "indefinitely". The default is to run three iterations of randomly overwriting the stick. By using the -z switch another iteration is added to overwrite the stick with zeros. This needs hours, not just minutes. You can view the progress by using the -v switch.

      Delete
  6. Hi!

    Thanks for this cool and to-the-point howto. - There's just one thing that I don't understand: Why is it necessary to specify an offset of one (isohybrid -o 1 [...])?

    And just another thought: It might be better to use dd instead of cat for writing the image to disk. - cat is meant for text streams and sometimes does strange things to binary date. - Broke my neck once, because I used it like you do here, and I lost a lot of valuable data. :-(

    Bye, Loona.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Hi Daniel,
    can it be that in end-207 fdisk -l doensn't list the heads no more? I tried to use 'fdisk -H' but this one couldnt deliver the information either. Can you help me?

    thanks!
    Max

    ReplyDelete
    Replies
    1. You can try running isohybrid without specifying heads (-h) and sectors (-s). I read some comments, that this kind of information is not accurate these days anymore. Please report, if you have success.

      Delete