Monday, March 12, 2007

How to Create a Bootable Linux Image on a USB stick

Create a Server Image on a usb stick ¶

Plug in the usb stick and use dmesg or 'tail -f /var/log/messages' to find the device name assigned.

Wipe the Device ¶

Zero the entire disk (or usb pen) including the partition table and boot sectors as follows:-
dd if=/dev/zero of=/dev/sda
(Use /dev/random or /dev/urandom to write random data to the disk)

Create the Partitions ¶

To view disk contents and create partitions type:-
fdisk /dev/sda
(cfdisk - graphical version)
In fdisk do the following:-
Type 'n' (to create a new partition)
Type 'p' (to make the new partition a primary partition)
Type '1' (to set the partition number to one)
Type Return (to select the default start block)
Type Return (to select the default end block)
Type 'a' (to toggle on the boot flag which makes the partition bootable)
Type 't' (to change the type of the partition)
Type 'b' (selects W95 FAT32 as the partition type)
Type 'w' (to write your changes to the usb stick and exit fdisk)
fdisk -l /dev/sda - lists the partitions on the device

Create the Boot Sector ¶

dd if=/usr/lib/syslinux/mbr.bin of=/dev/sda

Create the File System ¶

mkfs vfat /dev/sda1

Mount the file system ¶

mount /dev/sda1 /mnt/usbstick

Make the usb pen a bootable linux drive ¶

Create an empty file called 'livecd' to enable Gentoo to find the bootable disk.
touch /mnt/usbstick/livecd
umount /dev/sda1
Run syslinux on the bootable partition to place ldlinux.sys where the master boot record points:-
syslinux /dev/sda1
mount /dev/sda1 /mnt/usbstick
ls -l /mnt/usbstick
-r-xr--r-- 1 root root 9436 Mar 1 16:57 ldlinux.sys
-rwxr--r-- 1 root root 0 Mar 1 13:17 livecd
umount /dev/sda1

No comments: