2013-11-28

Magic ISO Image Booting with GNU GRUB 2

Recently I needed to prepare a USB thumb drive with several Ubuntu installations. A little research quickly yielded many setup instructions, for example like the one from Pendrivesystem.com.  I was really surprised at how well this works and wanted to understand it better.

In essence all recipes rely on GNU GRUB in version 2 and the loopback feature that it contains and on the OS's ability to work off an ISO image. The loopback command mounts a CD or HDD image that contains the kernel and initrd from an ISO image. As a result one can put several ISO images on the boot media without the need to extract them. The OS then also mounts the ISO image and uses that instead of a CD/DVD drive.

So here is my version of the recipe, the USB thumb drive is in /dev/sdc in my examples:

1. partition & format device

I prefer to partition the device with parted because it aligns the partition at 1MB so that it leaves enough space for GRUB to embed itself into the first sectors of the disk:

$ export $DEV=/dev/sdc
$ sudo parted -s $DEV mklabel msdos mkpart primary ext2 0% 100% print
Model: Generic Flash Disk (scsi)
Disk /dev/sdc: 1035MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  1035MB  1034MB  primary  ext4

$ sudo mkfs.ext4 -qL INSTALL ${DEV}1

Note: It seems that many BIOS boot from this only if the partition is not marked active!

2. mount & install grub

$ sudo mount -vL INSTALL /mnt
mount: you didn't specify a filesystem type for /dev/sdc1
       I will try type ext4
/dev/sdc1 on /mnt type ext4 (rw)

$ sudo grub-install --boot-directory /mnt /dev/sdc
Installation finished. No error reported.

3. Add some ISOs that support loopback booting

$ sudo wget -P /mnt \
http://cdimage.ubuntu.com/xubuntu/releases/12.04.3/release/xubuntu-12.04.3-desktop-i386.iso
...
Length: 726663168 (693M) [application/x-iso9660-image]
Saving to: ‘/mnt/xubuntu-12.04.3-desktop-i386.iso’

100%[==============>] 726,663,168 2.71MB/s   in 6m 12s 


4. Create GRUB menu entry


$ sudo tee /mnt/grub/grub.cfg >/dev/null <<EOF
menuentry "XUbuntu 12.04.3 i386" {
    set iso=/xubuntu-12.04.3-desktop-i386.iso
    loopback loop $iso
    linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=$iso noeject noprompt splash locale=de_DE bootkbd=de console-setup/layoutcode=de --
    initrd (loop)/casper/initrd.lz
}
EOF

Here the trick is to set a variable (iso) with the ISO file, loopback mount the iso image and load kernel/initrd from the ISO image. The same ISO image is also passed as a parameter to the booting OS.

5. Umount & try it out


Thermionix published a nice example of a fancy grub.cfg, it contains many examples for loopback booting with various distros and tools.

2013-11-22

Setting hostname from DHCP in Debian

For our team monitors that use the Kiosk Browser on a Raspberry Pi I am building a Raspbian wheezy image. One requirement is that the system will configure the hostname from DHCP so that we can use the same image for all devices.

Sadly, setting the hostname from DHCP is not trivial in Debian, here is the result of my research into the topic. I found 2 things to be essential and learned both of them by analysing the dhclient script.

1. Set the hostname to localhost

The first thing to do is to set the system hostname to localhost:

    # echo localhost >/etc/hostname

2. Workaround for broken dhclient-script

The dhclient-script has (IMHO) a bug: If there is an old DHCP lease with a hostname in the lease database (e.g. in /var/lib/dhcp/dhclient.eth0.leases), then the script will not set the hostname from DHCP even if the system name is still localhost.

To circumvent this bug simply create a dhclient enter hook to unset the old host name variable:

    # echo unset old_host_name \
      >/etc/dhcp/dhclient-enter-hooks.d/unset_old_hostname

The result is that now the hostname will be always set from DHCP, even if something went wrong or if there are old lease infos.

2013-11-04

Idea: Electric Family Van

Today I was walking a bit on the way home. After the third silent taxi (Toyota Prius Hybrid) passed by I started to think about what would be the ideal electric car for my use case. We are a large household and won't do with the standard 4 or 5 seat cars that are now beeing offered as electric or hybrid cars. Since the car makers don't offer what I need I am posting my idea here.

This is an approximation of our current family car:

It is nice, fairly large, seats 8 people comfortably and even has some extra luggage space in the back. We use it for short trips in the city to go shopping, drive the kids around and do the occasional family sunday trip to the surrounding country side.

When we go travelling, we usually need a larger trunk so we have this add-on trunk:





This is a trailer which is about half as long as the car and of the same height and width. It fits everything we need even for a long vacation. Or all our bikes for a shorter one. Or the insanely large family shopping at IKEA. Or ...

So, in our case it almost never happens that we travel far without our trailer attached. So why not use the trailer for the extra engine to boost an electric van? It would reduce the trailers capacity by a little and increase the range of the electric family van by a lot.


For us the combination of an electric car with an add-on range extender would be the best. In the city we charge the car from a wall socket and drive not too much per day. For trips we hook up the trailer which extends both the luggage carrying capacity and the driving range. With this combination we could still pass a good distance on the batteries, e.g. in a electric-only zone.

At the destination of our trip we could again take off the trailer and do short trips purely electrical.

Dear car makers: Start to produce useful and affordable electric cars and I'll be most happy to buy one. Think about modularization and reusablity to go beyond electrifying standard cars.

Update: I just found www.eptender.com who offer a tiny trailer (for small cars) with this idea, which proves that the idea is working :-)