Month: April 2009

Remastering Ubuntu CD : Exploring initrd.gz

( This document was written in connection with  Ubuntu Re mastering workshop held at College of Engineering Attingal on 25th of April 2009 . See this post )

On an Ubuntu CD kernel and the associated initrd.gz are found under casper directory.  initrd.gz is an  initial ram image  which helps the kernel to boot the system and mount the squashfs image.
You can  tweak  initrd.gz   with  the   following steps.

Mount the ubuntu cdrom/ iso image

# mount -o loop ubuntu.iso /cdrom

Create a temporary  directory and change to it.

# mkdir  initrd_remaster

# cd initrd_remaster

2) Copy initrd.gz    and unzip it

# cp /cdrom/casper/initrd.gz   .

Unzip it

# gunzip initrd.gz
The resulting file is a cpio archive
Uncompressed it  with the following command
# cpio -i  < initrd

You will get several folders  and a file named init  after this.

bin  conf  etc  init  lib  sbin  scripts  usr  var

If you watch system start up carefully,  you can  see   initrd.gz getting uncompressed.  The system starts executing the script named init immediately after that.  This script will in turn  find out  the actual root   file system (/) a, mount it and then chroot to  the actual file system. You can open up init in an editor and tweak it.     There are several  other scripts which are  called from init.

Finally, you can use cpio and gzip to recreate initrd.gz

# find ./ | cpio -H newc -o > ../initrd

# cd ..

# gunzip intrd.gz

You can replace the original initrd.gz in Casper directory with the modified initrd image

Building an Ubuntu Live CD from Scratch

( This document was written in connection with the one day Live Cd workshop held at college of Engineering Attingal. )

1. Introduction

This document describes how to build a minimal live CD based on ubuntu 8.10.. The most recent version of this document can be found at http://brainstorms.in.

2. System Requirements

  1. A Host System with ubuntu 8.10 installed. ( Other versions will also work)
  2. Plenty of Disk space.
  3. A steady Internet connection or access to ubuntu repositories.
  4. Root access on the host system.

3 Exploring an existing ubuntu CD.

In order to understand the working of a live CD, let us look at an existing ubuntu CD

If you have the CD with you , put it into your CD drive. If you have an image , mount it on the loop device.

# mount -o loop ubuntu-8.10-desktop-i386.iso /cdrom

The directory structure of a typical ubuntu CD look as below.

autorun.inf dists isolinux pics preseed ubuntu wubi.exe

casper install md5sum.txt pool README.diskdefines umenu.exe

The exe files and autorun.inf are for installing under MS Windows. Some of the other files are self explanatory. Our main focus will be on isolinux and casper directories..

The isolinux directory contains files needed to boot from the live CD, Infact, isolinux belongs to a set of bootloaders written by Peter. H Anwin. The home page is here. As I mentioned isolinux , is the bootloader.. If you look at isolinux subdirectory, you will find several files. Some of them will be as shown below.

isolinux.bin isolinuc binary

isolinux.cfg Configuration File

boot.cat boot catlogue

bootlogo Logo

There are several other files. They are mainly for providing language support. ( Please feel free to look at some of them). The isolinux.cfg includes several other configuration files corresponding to differnt boot options. We will write a simple isolinux.cfg later.

The casper directory contains the following.

filesystem.manifest filesystem.squashfs vmlinuz

filesystem.manifest-desktop initrd.gz

vmlinuz and initrd.gz are the kernel and ram image from which isolinux will boot the system.

The filesystem.squashfs contains a compressed image of the filesystem. When we boot the live CD, this image will be uncompressed and moounted as the root filesystem.. All remastering programs and custom distros try to rebuild this image.. Filesystem,manifest and filesystem.manifest-desktop are text files which contain a list of packages inside the squashfs image.

The squashfs image can be loop mounted like an iso image.

# mount -o loop filesystem.squashfs /mnt /tmp

4 Preparing the host system.

Ensure that the Ubuntu 8.10 is installed on the host system. Log to the host system and update the system using apt. (I am assuming that you have direct access to Internet..)

a) Install necessary packages

apt-get install syslinux squashfs-tools mkisofs sbm

b) Prepare a working directory

Open a terminal on the host system, Become root.

$sudo su

#

Create a temporary work directory.

# mkdir work

Inside work create the following.

# mkdir work/filesys

#mkdir work/image

#mkdir work/image/casper

#mkdir work/image/isolinux

#mkdir work/image/install

We will be building the iso image from the image directory. We will populate the image directory one by one and finally build a CD image. The filesys directory is a temporary area where we will build the root filesystem of the Live CD.

5 Building a Squashfs image

For building the compressed root image,

we will build a bare minimum filesystem under filesys directory and then convert it into squashfs.

Step 1.

Install debootstrap on the host system

# apt-get install debootstrap.

Debootstrap is a program to bootstrap a basic debian system.

# cd work

# debootstrap –arch i386 intrepid filesys

This will create a bare minimum filesystem inside the filesys directory. If you look inside the filesys directory, you can see that the structure resembles root directory as shown below.

bin dev home lib mnt proc sbin sys usr vmlinuz
boot etc initrd.img media opt root srv tmp var

Step 2

Copy the /etc/resolv.conf and /etc/apt/sources.list to the corresponding locations inside the filesys directrory.

# cp /etc/resolv.conf filesys/etc/resolv.conf

# cp /etc/apt/sources.list filesys/etc/apt/sources.list
Step 3

Now, we will use chroot command to change the root directory to filesys.

# chroot filesys

You will get back the # prompt. However inside this terminal you will be referring to filesys directory as / .
Now run the following ( You are inside chroot )
# mount /proc

# mount /sys

# mount -t devpts none /dev/pts
The above commands mount the respective directories inside chroot.
Step 4

Update the system. (Note that we are inside the chroot jail not on your host system.)

# apt-get update

To avoid locale issues and in order to import GPG keys set the following shell variables.

#export HOME=/root

#export LC_ALL=C
Step 5

Install packages needed for Live System:

#apt-get install –yes ubuntu-standard casper

#apt-get install –yes discover  laptop-detect os-prober

#apt-get install –yes linux-generic

The package ubuntu-standard will install most of the packages needed for a working installation.

The other packages are mainly for hardware detection. Linux generic will install the kernel..

Step 6

Install additional  packages.

You can get the list of currently installed packages with

# dpkg -l

You can remove some of the packages and try to save space.

Step 7

Cleanup the unwanted files.

#apt-get clean
#rm -rf /tmp/*
#rm /etc/resolv.conf
#umount -l -f /proc

#umount -l -f /sys

#umount /dev/pts

#exit

Now you are out of chroot and your filesys directoy is ready to be compressed into a squashfs image.

Step 8

# mksquashfs filesys image/casper/filesystem.squashfs -e filesys/boot

Note: If the system you are building will only be used as a live system and you will never use it to install, you can exclude the /boot folder. The live system boots from outside the chroot and so the /boot folder is not used. If you will be using ubiquity to install the custom-built system, you will need to keep the /boot folder. Use this instead of the previous command to preserve the /boot folder.

# mksquashfs filesys image/casper/filesystem.squashfs.

6 Populate rest of the casper directory

Step 1

Copy kernel and initrd image.

#cp filesys/boot/vmlinuz-2.6.**-**-generic image/casper/vmlinuz

# cp filesys/boot/initrd.img-2.6.**-**-generic image/casper/initrd.gz

Step 2

Create manifest:

Run the following commands.

# chroot filesys  dpkg-query -W --showformat='${Package} ${Version}\n' |  tee image/casper/filesystem.manifest

#cp -v image/casper/filesystem.manifest{,-desktop}
# REMOVE=’ubiquity casper live-initramfs user-setup discover  xresprobe os-prober libdebian-installer4′
# for i in $REMOVE

do

sed -i “/${i}/d” image/casper/filesystem.manifest-desktop

done

Now all the files in the Casper directory are ready.

7 Building the isolinux directory

The isolinux directory contains files needed for booting the live CD.

Step 1

Copy isolinux and memtest to isolinux directory
#cp /usr/lib/syslinux/isolinux.bin image/isolinux/

#cp /boot/memtest86+.bin image/install/memtest
#cp /boot/sbm.img image/install/
Step 2

Create an isolinux.txt file in image/isolinux to display at boot time.

Eg.

This is a Live CD created at College of Engineering Attingal.For the default live system, enter “live”.
To verify the CD for errors, enter “check”.
To run memtest86+, enter “memtest”

Step 2a (optional)

If you put the name of a speacilly encoded image file file in the beginning of isolinux.txt ,it will be displayed when we boot up.

^Xsplash.rle

The filename should be preceded by ^X .

( To enter ^X in vi editor, do ctrl-V and follow with ctrl-X).

To create the splash.rle file, create an image 480 pixels wide, and convert it to 15 colors, indexed, using the gimp. Save it as .bmp.

Install the netpbm package.

#apt-get install netpbm

#bmptoppm splash.bmp > splash.ppm #ppmtolss16 '#ffffff=7' < splash.ppm > splash.rle

Step 3

Create an isolinux.cfg file in image/isolinux/  /usr/share/doc/syslinux/syslinux.doc is an idea place to start  looking for information on  various  configuration options. Here is an example  that you can use as a starting point.

DEFAULT live
LABEL live
menu label ^Start or install Ubuntu
kernel /casper/vmlinuz
append file=/cdrom/preseed/ubuntu.seed boot=casper initrd=/casper/initrd.gz quiet splash —
LABEL check
menu label ^Check CD for defects
kernel /casper/vmlinuz
append boot=casper integrity-check initrd=/casper/initrd.gz quiet splash —
LABEL memtest
menu label ^Memory test
kernel /install/memtest
append –
LABEL hd
menu label ^Boot from first hard disk
localboot 0x80
append –
DISPLAY isolinux.txt
TIMEOUT 300
PROMPT 1

8 Create miscellaneous files

Step 1

Create a diskdefines:

vi image/README.diskdefines

example:

#define DISKNAME Ubuntu 8.10 “Intrepid” – Release i386 **Remix**

#define TYPE binary

#define TYPEbinary 1

#define ARCH i386

#define ARCHi386 1

#define DISKNUM 1

#define DISKNUM1 1

#define TOTALNUM 0

#define TOTALNUM0 1

Step 2

Calculate MD5

# cd image && find . -type f -print0 | xargs -0 md5sum > md5sum.txt

9 Create and Test the CD image

Create iso image using the following command


#mkisofs -r -V "MyUbuntu" -cache-inodes -J -l -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o ../ubuntu-remix.iso .

# cd ..

The boot.cat file will be automatically created.

Now, the image is ready for testing. You can install virtual box and mount the image as a cdrom and try booting from it.

10 To Do

Installing the live CD into a hard disk

Ubiquity.

How do we vote in India with Electronic Voting machine

India is going to polls during the months of April and May 2009.  Elections in India employ Electronic Voting machine(EVM). These machines  are manufactured by  two companies  owned by the defense department.  I had the opportunity to get hands on training in operating the machine as a part of pre election training for polling officials.( I am going to work as a presiding officer in  a  polling booth in the first phase of elections.)

The electronic voting machines that we use, have two parts :- a control unit and a balloting unit. They are interconnected by a flat ribbon cable of 5m length.

evm1

The Indian Electronic Voting machine

The balloting unit enables the voters to cast their votes. Each balloting unit can accommodate 16 candidates. 4 such units can be cascaded and connected to a single control unit.

033020092951 033020092960

Balloting unit                                                             Control Unit

The connecting  cable is permanently attached to the balloting unit. It is connected to the control unit via a connector on the rear. Also, the main on off switch is on the rear side of control unit.

033020092953 033020092971

The various parts of balloting unit is shown below.

evm_ballot

033020092969 033020092959

A voter can vote when the ready lamp glows. He can press  the candidate button shown against  his candidate. As soon as the   button press is received the unit produces a long beep sound and the candidate lamp glows.

The  control unit is organized   as shown below.

evm_control

The control unit  has several functions. These are performed at different points of election process. It is powered by  a  6V rugged battery.  The battery and  candidate set switch are sealed before delivery to the polling station. The polling officials are not supposed to open this section. The candidate  set button can be used to set the number of candidates on the balloting unit. Also the same control unit can be used for 2 simultaneous elections ( ie Parliament elections and  state elections.)

evm_control_candidate_set

033020092993 033020092995

The result section is used for displaying the results, closing the poll and resetting the machine. This section is sealed at the start of the election in the presence of   polling agents

evm_control_result

033020092990033020092984

At the end of the polling the close bottom is pressed and the machine is sealed again. The ballot section on the bottom of the control unit is used  along with the balloting unit. As soon as a voter is identified, the officer in charge of the control unit presses the ballot button. The busy lamp on the top of the control unit glows  along with the ready lamp on the balloting unit. When a voter casts his vote, the busy light goes off.

The total button can be used to find out the number of votes cast at any time.

033020092970 033020092975

The polls start at 7 am in the morning and end at  5pm. Before the start of actual  poll, a mock poll will be conducted.  This is to ensure that EVM is not having any preset votes and to make sure that   the machine works as advertised.  The result section will be sealed and signatures of  polling agents and presiding officer  will be affixed on the seal.at this time  At the end of the polls the close button is pressed. The machine is sealed again and  put into a box and returned to a counting center.

Update April 17 : I had my election duty at Keezhpaloor , near Meenagal in Aryanadu. The  polling was smooth and about 65% of the electorate cast votes in my booth.

One day workshop on Re-mastering Live Cds

I am planning to conduct a  one day workshop on re-mastering Ubuntu CDs on 25th of April 2009. The  agenda for the workshop will be

1)  Exploring the ubuntu Live CD
i) isolinux – How it works  Config files.
Customizing  boot splash and  boot messages.
ii) Casper Directory –
What is squashfs How to  mount it
iii) Other files

2) Building an Ubuntu CD from scratch
i) Requirements
ii)  Build  CD
iii) Test it with virtual box
iv) Unpacking an existing Live CD
v) Adding additional applications .

3)   Automating Re mastering
i) Remaster sys
ii) Reconstructor
iii) Miscellaneous topics. Usplash
4) Building a USB boot disk and carrying your distro

If you are interested   in attending,  email me.