Saturday, November 27, 2010

Make A Tiny Linux Distribution

Who hasn't wanted their own tiny Linux Distro that can run from a livecd?  Here is how to make one....



First things first, you will need a Linux system to build from.

Root Filesystem:
  1.  Create a new folder that will become the root filesystem for your new CD; I named mine rootfs.
  2. In rootfs add these folders: bin, dev, etc, proc, etc/init.d
  3. Download and build Busybox.  You want it to be statically linked, when you eventually compile it there should be an executable in the busybox build folder called busybox: run a file command on it, it should say something like this: 

    busybox: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, for GNU/Linux 2.6.15, stripped
     

    Statically linked meaning that it has no dependencies, and stripped meaning that  all of the extra debugging code has been removed; this is what you want.
  4. Copy the Busybox executable to the bin folder of your new root.  In a terminal started in the bin folder run ./busybox  a list will be shown of all the functions available, we need to link all of these options to the busybox executable so the system can find them; Python to the rescue!  Just copy and paste all of the commands in to the appropriate location in the following script, then run it in a python interpriter (in the bin directory):

    import os

    names = '''[, [[, acpid, addgroup, ...and all the rest...'''

    names = names.replace(" ", "")
    names = names.replace("\n", "")
    names = names.replace("\t", "")

    names = names.split(",")

    for name in names:
    os.system("ln busybox "+name) #Link all of the programs to busybox
    </pre>


  5. Now we need to build the most basic of devices, we will need a console, a null device, a ramdisk and a tty. cd to the dev folder and run:
    sudo mknod console c 5 1
    sudo mknod null c 1 3
    sudo mknod ram0 b 1 0
    sudo mknod tty c 5 0
    This will create all of the devices.
  6. Three more files and the root filesystem will be complete: fstab, inittab, and rcS.
  7. fstab holds general information for the filesystems all it needs to do here is to mount the special filesystem proc:

    # /etc/fstab: static file system information.
    #
    #
    proc /proc proc defaults 0 0


  8. inittab is run by the init program, this should at minimum provide a way to shutdown the computer and the progrm to be run on startup (a login would be nice!)

    #/etc/inittab for linux (SysV)
    #Man page http://www.netadmintools.com/html/5inittab.man.html
    #Busybox inittab help: http://linuxembedded.blogspot.com/2006/11/understanding-busybox-inittab.html

    ::sysinit:/etc/init.d/rcS

    ::askfirst:/bin/sh

    # Stuff to do before rebooting or shutting down
    ::ctrlaltdel:/bin/reboot
    ::shutdown:/bin/umount -a -r
    ::shutdown:/bin/swapoff -a
     
  9. rcS is a script that starts all of your other stuff after inittab is run, start your daemons here and mount your filesystems.  You can see it is called in the /etc/inittab file right on startup.  It should be placed in the /etc/init.d folder.

    #!/bin/sh
    mount -a #Mount all of the filesystems in /etc/fstab

  10. Your root file-system is done, congratulations.
LiveCD:
Now we need to build the system in to a real LiveCD so it can be run from your favorite computer natively.
  1. Make a new folder outside of the root and name it livecd, inside this folder you need the folder ISOLINUX, and inside that put another folder named LINUX.
  2. ISOLINUX is a bootloader that runs from livecds to bootstrap your filesystem and the Kernel download the latest version.  I used version 4.03.
  3. Extract the file core/isolinux.bin to your ISOLINUX directory.
  4. Create the configuration file for ISOLINUX in the ISOLINUX directory and name it ISOLINUX.CFG.  Its contents should look like this:

    TIMEOUT 20
    DEFAULT linux
    LABEL linux
    KERNEL linux/bzimage
    APPEND root=/dev/ram0 initrd=fs

    This tells ISOLINUX to load the Linux kernel in the LINUX directory with the initial filesystem fs. You have created neither.
     
  5. Create a bzipped Linux kernel place it in the linux directory and name it bzimage (beyond the scope of this article).
  6. Make the initial file-system from the rootfs you created earlier by changing to the directory just outside of the romfs and run the command: genromfs -d rootfs -f fs  this will package up your root filesystem in to an image the Kernel can read.  Copy the created file in to the ISOLINUX folder.
  7. Now package everything together by changing to the directory just outside the iso folder by running:
    mkisofs -o output.iso    -b ISOLINUX/isolinux.bin -c ISOLINUX/boot.cat    -no-emul-boot -boot-load-size 4 -boot-info-table    cd
Done!  Start it up in Virtualbox.

No comments:

Post a Comment