=============================
HOWTO: Create Linux usb-stick
=============================

Instructions tells howto make bootable linux usb-stick. This method can be
compared to the normal filesystem not to live-cds or live-dvds.

---------------
Pre-requrements
---------------
* usb-stick 2GB ->
* sudo
* syslinux
* tools to partition
* qemu/kvm or debootstrap
* DVD/CD of some linux distribution
  - Ubuntu koala karmic is used

-----
NOTES
-----
* sdb can be something like sde in your system


-----------------------
Partition the usb-stick
-----------------------

After inserting stick use dmesg command to get your drive letter

Use fdisk
---------
* sudo fdisk /dev/sdb
  - 32MB fat32 (id: b)
    + make this partition bootable
    + ubuntu kernel, initrd and config files takes about 10MB
  - rest of the space from the stick to your distribution

Make sure kernel knows what is done
-----------------------------------
* sudo partprobe /dev/sdb


----------------------
Make some fs' to stick
----------------------
* sudo mkfs.vfat -F 32 /dev/sdb1
* sudo mkfs.ext4 -L root-rw /dev/sdb2
  - label is needed to find rootfs
  - choose what ever filesystem your distro supports and is ok. This is only 
    example


------------------
Method1 - qemu/kvm
-------------------
* why: to make and test rootfs
* kvm is quite fast
* works amost every distribution


Example: Ubuntu server (karmic)
-------------------------------
If your machine does not support virtualisation use qemu instead of kvm.
(replace kvm with qemu) Installation takes several hours when using qemu.

* wget http://se.releases.ubuntu.com/9.10/ubuntu-9.10-beta-server-i386.iso
* qemu-img create -f qcow2 hdimage.qcow2 3G
  - Choose side what fits for sure to your second partition of your usb-stick
* kvm -m 256 -sdl -hda hdimage.qcow2 -cdrom ubuntu-9.10-beta-server-i386.iso
  - install distribution to one partition
  - use swapfile if you need swap.

* After or while installation
  - might need to boot qemu
    + kvm -hda hdimage.qcow2 -m 256
  - remove everything you do not need
  - install updates and wanted software

* optional: add swapfile
  - in booted qemu system. 256Mb swap
  - change count to suit your requirements.
          512Mb:count=524288, 1Gb:count=1048576
    + sudo dd if=/dev/zero of=/swp.file bs=1024 count=262144
    + sudo mkswap /swp.file
    + add line "/swp.file none swap sw 0 0" to fstab

* mount qcow2 diskimage
  - mkdir /tmp/image
  - sudo modprobe nbd max_part=8
  - sudo qemu-nbd --connect=/dev/nbd0 hdimage.qcow2
  - sudo mount /dev/nbd0p1 /tmp/image/

* umount qcow
  - sudo umount /tmp/image
  - sudo qemu-nbd -d /dev/nbd0

---------------------
Method2 - debootstrap
---------------------
* faster than qemu method. At least when kvm is not possible to use.
* ?needs debian based distribution?

TODO


----------------------------------
Make usb-stick bootable - syslinux
----------------------------------

syslinux method should work with almost any x86 machine.

MBR
---
* sudo dd if=/dev/zero of=/dev/sdb bs=446 count=1
* sudo su -c "cat /usr/lib/syslinux/mbr.bin > /dev/sdb"
  - syslinux path can be different
    + use locate or find command to find mbr.bin 

fat32 partition
---------------
Copy kernel and everything is needed to boot the system to fat partition

* sudo syslinux /dev/sdb1
  - adds linux.sys to your fat partition

* mkdir /tmp/boot
  - mountpoint to fat partition of the stick 
* sudo mount /dev/sdb1 /tmp/boot
* mount image if used qemu
  - see: mount qcow2 image part from qemu section

* cp /tmp/image/initrd.img /tmp/boot/initrd.gz
* cp /tmp/image/vmlinux /tmp/boot/

* modify /tmp/boot/syslinux.cfg
  - if LABEL does not work you should make use UID or other method used by
    initrd. Unpack initrd.gz with gz and cpio and check file called init.
  - or just try this out with grub


EXAMPLES:

syslinux.cfg to framebuffer version
-----------------------------------
"
default linux

prompt 1
timeout 10

label linux
  menu label ^1. Linux
  kernel vmlinuz
  append initrd=/initrd.gz root=LABEL=root-rw quiet -- vga=ask

label hd
  menu label ^2. Boot from first hard disk
  localboot 0x80
"


syslinux.cfg to X11/textconsole version
---------------------------------------
"
default linux

prompt 1
timeout 10

label linux
  menu label ^1. Linux
  kernel vmlinuz
  append initrd=/initrd.gz root=LABEL=root-rw quiet --

label hd
  menu label ^2. Boot from first hard disk
  localboot 0x80
"

--------------------------
Make stick bootable - grub
--------------------------
TODO


------------------------
Copy rootfs to the stick
------------------------

image
-----
* see: mount qcow2 image part from qemu section
* mkdir /tmp/rootfs
* sudo mount /dev/sdb2 /tmp/rootfs
* cd /tmp/image/
* sudo tar cf - * | sudo tar xf - -C /tmp/rootfs/
* sync

debootstrap
-----------
TODO


Both methods (que/kvm and dbootstrap)
----
* modify /tmp/rootfs/etc/fstab
  - just in case comment out the root (/) entry


Bootable usb-stick with "small" rootfs should be ready. With Ubuntu small means
no X11, no Gnome etc. In Ubuntu karmic kernel modules takes about 160M of your
space. You can recompile kernel your self if you want to decrease size.

----
ALSA
----

Install alsa-utils
* sudo aptitude install alsa-utils

Add user to audio group 
* sudo addgroup audio username

---------------
dhcp networking
---------------

I had to add "exec dhclient" to end of the /etc/init/networking.config. Another solution is every time run "sudo dhclient" from commandline.
