Install to USB From within Linux

From DSL Wiki

Back to parent page. This page in other languages: EspaƱol


Contents

Introduction

The basic process for installing DSL from within another Linux distro is:

  1. Get DSL and your boot loader (GRUB or Syslinux)
  2. Reformat/repartition your stick (or just clear it off)
  3. Put the current DSL ISO contents onto the stick
  4. Set the boot flag, put in a boot loader
  5. Set up the boot loader (make sure it knows where the boot device is)

Read the Requirements for sure, then Formatting. If one of the tutorials doesn't work for you, try another one before making drastic changes or blasting the original author. Some hardware just doesn't like some setups.

Requirements

  • If you want to start with a clean slate, you should repartition and format your USB drive (next topic on this page). For some installation types, this isn't required, but it's a good idea. In any case, BE CAREFUL. Partitioning and formatting tools are a good way to wipe your main OS drive, too. No prizes for reformatting your hard drive.
  • You'll need to know your flash drive location (device node). It will be something like /dev/sdb (sda is typically your system drive). One way to find out is to open a terminal and run:
       fdisk -l

You may have to use sudo ... but be careful. fdisk is a great tool for mass destruction of your data.

Alternatively you can pull out your stick, then plug it back in and use:

       dmesg | tail

dmesg will probably let you know what device node the drive showed up in ... (sdb1, sda2, something like that).

or ... If you're using a distro that auto-mounts your USB drive, it should show up in the mount list:

       mount

Just browse the list for your drive. Again, don't mess up and pick your hard drive!

Formatting Your Flash Drive (most installations)

This applies to almost all of the tutorials below. Pick a tutorial first - you might not have to repartition/reformat your drive.

If you do want/have to, then ...

Right now (late 2008), there are two major tools for repartitioning and formatting your drive.

  • One is GParted, the GNU Partitioning Tool.
  • The other is fdisk, a command-line tool.

You should probably start with GParted. If that doesn't work out, try fdisk.

Either way, repartitioning should only take a couple of minutes (tops!).

With GParted

GParted is a GUI/GTK partitioning tool. You can download it via apt-get or Synaptic. You can also visit [the GParted website] and download a Live CD that boots up to GParted. Whatever version you use, the process is simple:

  1. BACKUP YOUR USB DATA. You're about to delete everything ... forever!
  2. Open GParted (the program)
  3. Select your drive (drop down list in the top right). DO NOT SELECT THE WRONG DISK.
  4. Clear all your existing drive partitions (right click on each partition bar, then select delete)
  5. Create one large partition for the whole drive (right click, make new partition, set it to the size of the drive)
  6. Set the drive format to FAT16 (most compatible possible file system; right click, set using 'format to')
  7. CHECK THAT YOU PICKED THE RIGHT DRIVE ... and click 'apply' (the green check mark). Within a few moments, GParted will let you know whether it succeeded or failed. If you have trouble, try the whole process again.
  8. After repartitioning, set the boot flag (right click, manage flags).

Done! Move on to installing an ISO and bootloader.

If you got through all that and accidentally repartitioned your main drive ... (1) you're really dense (2) you may be able to save your data if you DO NOT REBOOT and (3) start digging online for somebody else's advice (4) don't tell anybody you did that. Ever.

Note: you may be able to use Test Disk (at least as long as you don't write any more files to you hard drive!)

With fdisk

fdisk is a command-line (terminal) alternative to GParted. To run fdisk, use:

       fdisk /dev/[USBDRIVE]

Where [USBDRIVE] is your drive. It may be sda, sdb ... It will depend upon your system. Just be sure you DO NOT PICK YOUR HARD DRIVE.

Use 'd' to delete existing partitions until none remain. Use 'n', 'p', '1' to create a new primary partition. Use 'a' to make it bootable. Use 't', 'b' to make it W95 FAT32. Don't forget to hit 'a' to make it bootable! Use 'p' to view your changes.

After you set up your partition on /dev/sdX, it should look something like this:

       Device     Boot      Start         End      Blocks   Id  System
       /dev/sdX1   *           1         1021      253177    b  W95 FAT32

Finally, write the new partition table by pressing 'w' to save the changes and exit.

It is possible that fdisk will generate an error (Kernel still uses old partition table, please reboot, etc.) You may have to reboot, but first try to load the new partition table into the kernel using:

       partprobe /dev/[USBDRIVE]

Then open fdisk again and read the partition table to see if it stuck (fdisk -l /dev/[USBDRIVE]). If it didn't stick, you'll have to reboot.

After exiting fdisk, you'll need to use mkfs to create a file a FAT16 or FAT32 file system. For example:

        mkfs.vfat -F 32 /dev/sdX1

"-F 32" will create a FAT32 partition; "-F 16" will make a FAT16 one.

Modifying the MBR (some cases)

If you play with your USB stick a lot, it is slightly possible you'll need to rewrite its Master Boot Record (MBR). This shouldn't be necessary unless there's another funky bootloader in the MBR (like, if you were experimenting with another bootable USB linux distro).

One rule when working with the MBR: BE *VERY* CAREFUL NOT TO WIPE YOUR HARD DRIVE'S MBR!!!

1. From the command line, use the command dd. Replace sdX with the path to your USB drive:

       dd if=/dev/zero of=/dev/sdX bs=446 count=1

2. Now there are 2 ways to replace your MBR:

Either copy in the bootloader from Syslinux:
       locate mbr.bin #locate the file mbr.bin; the 'locate' command doesn't exist on all systems - also try 'find' (RTM)
       cat /somepath/share/syslinux/mbr.bin > /dev/sdX #replace sdX with your flash drive path
OR install ms-sys and use it to replace your old MBR:
       ms-sys -s /dev/sdX

Method I: Typical Embedded Install (Syslinux + QEMU)

This is probably the fastest/easiest manual installation. Five easy steps!

The typical embedded installation includes the QEMU processor emulator and Syslinux. By installing both, you can boot from within Linux or Windows (w/ QEMU), OR natively from the USB drive (w/ Syslinux).

Unfortunately, this method does NOT give a 50MB installation because QEMU is (comparatively) big. You will need about 110 MB for the total install.

Steps for installation:

  1. Download the dsl-embedded zipfile
  2. Mount the USB drive
  3. unzip dsl-embedded.zip file onto your USB drive
  4. Unmount the USB drive
  5. use syslinux to make it bootable:
       syslinux -s /dev/sdX1

Done!

If this installation doesn't work for you, consider a method that uses GRUB as a boot loader (see below). GRUB is very useful if you're using DSL as part of a data/system rescue toolkit, as you can include multiple preset boot configurations and other utilities.

Notes

  • Read the notes on Syslinux with DSL
  • If your system mounts or boots the stick with argument iocharset=utf8, you may get an error where 'KNOPPIX/KNOPPIX' cannot be found upon booting.

Method II: Current ISO + Syslinux

In this method, you don't have to repartition or reformat your USB drive. It is possible to just install the ISO contents non-destructively onto a USB partition - this guide gives an example. Leave out all the partitioning and formating stuff that prepares your USB drive for DSL. Then follow these steps:


1. Mount USB drive with something like:

       mount /dev/sdx1 /flash #can be either FAT16 or FAT32!

2. Mount ISO image:

       mkdir /tmp/iso
       mount /tmp/dsl-3.2.iso /tmp/iso -o loop

3. Copy all contents from ISO to USB drive:

       cp -vr /tmp/iso/* /flash/

4. Rename and move Syslinux files to root directory of the stick:

       mv /flash/boot/isolinux/* /flash/

[Edit: The /boot subdirectory does not seem to be there in the file I downloaded --- where is it? Help!]

5. Rename isolinux.cfg to syslinux.cfg:

       mv /flash/isolinux.cfg /flash/syslinux.cfg

6. Unmount USB drive

       umount /flash

7. Install Syslinux:

       syslinux /dev/sdx1

8. And set the MBR boot flag for this partition (with fdisk).

       fdisk /dev/sdx #may need to use sudo ... be careful! Set the boot flag using 'a', then write with 'w'


Done! Reboot and play.

Notes

  • Be careful using sudo fdisk. This is a tool for both good and evil.
  • Make sure you use the right path names (/dev/sda, /dev/sdb, etc.) or you may corrupt existing partitions or files! You can read about this in the Requirements section, above.

Method III: Using GRUB as boot loader

Note: This method has been reported not to work under certain conditions

  • Download the current ".iso" dsl-cd image.
  • Create an ext2 partition (=> 51 MB) on pendrive. Mount it.
   mke2fs -v -L "DSL" /dev/<device>
   mkdir -p /tmp/pendrive
   mount /dev/<device> /tmp/pendrive/

where <device> is the corresponding file which represents your USB storage device/partition.

Note: ext3 is ok too. You can even use this method on a fat16 formatted partition.

  • Change to the directory where you mounted the ext2 partition, install GRUB boot loader:
   cd /tmp/pendrive/
   grub-install  --no-floppy  --root-directory=.   /dev/<rootdevice>  

Note: Replace <rootdevice> with the corresponding file which represents your USB storage device WITHOUT the partition number, so that the mbr is updated. For example /dev/sdc

Note 2: This method did not work for me, I used the manual method of running grub and entered "root (hd1,1)" and "setup (hd1)" (your paths will probably vary).

  • Copy the contents of the cd image to the usb drive.
   mkdir /tmp/dsl-cd
   mount [/path_to/]current.iso /tmp/dsl-cd/ -o loop
   cp -vR /tmp/dsl-cd/* /tmp/pendrive/
  • While in the same directory - where you mounted the ext2 filesystem - create a menu.lst file for grub in the directory ./boot/grub/menu.lst
  cat > /tmp/pendrive/boot/grub/menu.lst << EOF
  title           Damn Small Linux
  root            (hd0,0)
  kernel          /boot/isolinux/linux24 root=/dev/sda1 ro lang=us toram noeject frugal
  initrd          /boot/isolinux/minirt24.gz
  boot
  EOF

Note: You might have to change (hd0,0) to the correct partition for your USB drive. However, the default should work on most systems.

  • Unmount the filesystem.
  • You can now boot from your USB storage device.

Method IV: w/ GRUB as boot loader

Someone found the above process doesn't work and has documented for others this working process

The Situation:

  • Running SLED 10 (Suse)
  • The USB key is a 1GB and appears as /dev/sda
  • Logged in as root
  • The DSL ISO is downloaded to /root/Desktop/dsl-3.3.iso
  • Two partitions, the first to use as general USB storage, the second for DSL
  • The DSL partition wanted to be ext3 so that Windows doesn't mess it up
  • The first partition needs to be the big storage one as Windows doesn't like addressing the second partition on flash drives

The procedure:

  • fdisk /dev/sda
    • d - Delete all partitions on the key
    • n - Make a partition
      • p - Primary partition for general storage
      • 1 - First partition
      • 1 - From the first block
      • 948 - Most of the space, about 933MB
    • n - Make a partition
      • p - Primary partition for DSL
      • 2 - Second partition
      • 949 - Start from the next available sector
      • 1012 - To the end of the disk. I give it 64 MB
    • a - Make the partition bootable
      • 2 - Mark the DSL partition bootable
    • t - Change the partition type
      • 1 - Change partition 1
      • b - Change it to Win95 FAT32
    • w - Write the changes
  • fdisk -l - Gives the following output:
        Device    Boot      Start         End      Blocks   Id  System
	/dev/sda1               1         948      954131+   b  W95 FAT32
	/dev/sda2   *         949        1012       64416   83  Linux

Your flash drive should now look like this

  • mkdir /mnt/iso
  • mkdir /mnt/usb
  • mount -o loop /root/Desktop/dsl-3.3.iso /mnt/iso
  • mkfs.vfat -F 32 /dev/sda1 # use -F 16 for FAT16
  • mke2fs /dev/sda2

Note: If you want to have an ext3 filesystem, add the -j flag: mke2fs -j /dev/sda2

  • mount /dev/sda2 /mnt/usb/
  • cd /mnt/iso
  • tar cvp . | (cd /mnt/usb; tar xp )
  • mkdir -p /mnt/usb/boot/grub
  • cp /boot/grub/*stage* /mnt/usb/boot/grub/

(If your system doesn't boot with grub, try: cp /usr/lib/grub/i386-pc/*stage* /mnt/usb/boot/grub/)

  • cat > /mnt/usb/boot/grub/menu.lst << EOF
   title Damn Small Linux
   root (hd0,1)
   kernel /boot/isolinux/linux24 root=/dev/sda2 ro lang=us toram noeject frugal
   initrd /boot/isolinux/minirt24.gz
   boot
   EOF
  • cd /mnt/usb/
  • grub-install --no-floppy --root-directory=. /dev/sda

(In the line above, please note the space character between the dot in the device name)

  • cd /root
  • umount /mnt/usb
  • umount /mnt/iso
  • rm -r /mnt/iso
  • rm -r /mnt/usb