Tag Archives: cd

libguestfs: Live CD inspection

New in libguestfs 1.9.4, you can now run virt-inspector on some types of installer CD and live CD. The output tells you what type of operating system is on the CD:

$ virt-inspector cd.iso
<?xml version="1.0"?>
    <product_name>Windows Server 2003 Enterprise x64 Edition</product_name>
      <mountpoint dev="/dev/sda">/</mountpoint>
      <filesystem dev="/dev/sda">


On IRC someone asked me how long this takes. Here is the time to run the above command on my laptop:

real	0m4.736s
user	0m1.134s
sys	0m2.897s

The use case is in tools like virt-manager where we would like to detect what sort of ISO a user is installing from so that we can set the OS hints correctly. 5 seconds is just about acceptable as long as this happens in the background and doesn’t disturb the UI.

Leave a comment

Filed under Uncategorized

Customizing a Windows 7 install ISO

I’m told that if you want to pass additional files to Windows when it is installing, you have to put them on the actual install CD/ISO. However modifying ISOs is a palaver: they are write-once filesystems. The recommended way to modify them — using multisession support — doesn’t work for virtualization because the virtual CD drives don’t support it (nor do quite a few real CD drives).

You can unpack and repack an ISO file like this (and because we’re using libguestfs, root is not required):

$ mkdir /tmp/cd
$ cd /tmp/cd
$ guestfish --ro -a ../win.iso -m /dev/sda tar-out / - | tar xf -
$ mkisofs -o ../new-win.iso [...options...] .

If you add your own files into the /tmp/cd directory between the third and fourth steps then they will appear on the new ISO.

That’s the easy bit. The problem is that the new ISO will not be bootable, so you won’t be able to use it to install Windows from. Making it bootable involves an exploration of the command line options to “mkisofs” and a quick excursion into the ISO format.

It’s a good idea first to find out what format the existing Windows 7 ISO is in. Firstly I mount it up in guestfish to take a look around:

$ guestfish --ro -a ../en_windows_7_enterprise_x64_dvd_x15-70749.iso 

Welcome to guestfish, the libguestfs filesystem interactive shell for
editing virtual machine filesystems.

Type: 'help' for a list of commands
      'man' to read the manual
      'quit' to quit the shell

><fs> run
><fs> list-filesystems
/dev/vda: udf
><fs> mount-ro /dev/vda /
><fs> ll /
total 1164
dr-xr-xr-x  7 4294967295 4294967295    548 Jul 14  2009 .
drwxr-xr-x 20        500        500   4096 Nov  4 14:16 ..
-r-xr-xr-x  1 4294967295 4294967295    122 Jul 14  2009 autorun.inf
dr-xr-xr-x  4 4294967295 4294967295    568 Jul 14  2009 boot
-r-xr-xr-x  1 4294967295 4294967295 383562 Jul 14  2009 bootmgr
-r-xr-xr-x  1 4294967295 4294967295 667712 Jul 14  2009 bootmgr.efi
dr-xr-xr-x  3 4294967295 4294967295    100 Jul 14  2009 efi
-r-xr-xr-x  1 4294967295 4294967295 106760 Jul 14  2009 setup.exe
dr-xr-xr-x 10 4294967295 4294967295  10940 Jul 14  2009 sources
dr-xr-xr-x  5 4294967295 4294967295    200 Jul 14  2009 support
dr-xr-xr-x  3 4294967295 4294967295     92 Jul 14  2009 upgrade
><fs> exit

The format is UDF, the DVD replacement for ISO 9660. There is no obvious “cdboot.img” file which is what we will need in order to boot this thing.

Now look at the same disk with isoinfo:

$ isoinfo -d -i ../en_windows_7_enterprise_x64_dvd_x15-70749.iso
[lots of stuff]
Eltorito validation header:
    Hid 1
    Arch 0 (x86)
    ID 'Microsoft Corporation'
    Key 55 AA
    Eltorito defaultboot header:
        Bootid 88 (bootable)
        Boot media 0 (No Emulation Boot)
        Load segment 0
        Sys type 0
        Nsect 8
        Bootoff 2DE 734

What is interesting is that it’s an El Torito no-emulation bootable disk. The boot image required to boot it is 8 sectors long (“Nsect 8”) starting at sector number 734 (“Bootoff” in decimal).

CD sectors are 2048 bytes, so we can grab the boot image directly:

$ dd if=../en_windows_7_enterprise_x64_dvd_x15-70749.iso \
    of=boot.img bs=2048 count=8 skip=734

Now with all the information collected above, we can (with a great deal of experimentation) come up with a mkisofs command line that makes a bootable image:

$ mkisofs -o ../new-win.iso -b boot.img -no-emul-boot -c BOOT.CAT \
    -iso-level 2 -udf \
    -J -l -D -N -joliet-long -relaxed-filenames .


Filed under Uncategorized

Looking closer at Fedora, Ubuntu live CDs

Previously I’ve shown you can use guestfish to unpack a Fedora live CD.

I’m interested in whether we can use the contents of these live CDs to mass-install operating systems using libguestfs.

If you imagine that you go through an “all defaults” install of say Fedora or Ubuntu to a new virtual machine, then when you end up with is an identical disk image containing 1-2 GB of default packages and a lot of empty space. Two people asked to go through the same all-defaults install of the same distro would end up with roughly the same content. The details on the disk would be slightly different because some parts of the disk partitioning and ext3 superblocks change slightly depending on the overall disk size. But really those things can be fixed up afterwards using a little repartitioning, lvresize and resize2fs.

Let’s look inside an Ubuntu live CD:

$ guestfish --ro -a ubuntu-9.10-desktop-amd64.iso

Welcome to guestfish, the libguestfs filesystem interactive shell for
editing virtual machine filesystems.

Type: 'help' for help with commands
      'quit' to quit the shell

><fs> run
><fs> list-devices
><fs> file /dev/sda
ISO 9660 CD-ROM filesystem data 'Ubuntu 9.10 amd64
><fs> mkmountpoint /t1
><fs> mount /dev/sda /t1
><fs> ll /t1/casper
total 691049
dr-xr-xr-x  2 root root      2048 Oct 27 14:31 .
dr-xr-xr-x 10 root root      2048 Oct 27 14:31 ..
-r--r--r--  2 root root     37288 Oct 27 14:19 filesystem.manifest
-r--r--r--  2 root root     35354 Oct 27 14:16 filesystem.manifest-desktop
-r--r--r--  2 root root 697778176 Oct 27 14:25 filesystem.squashfs
-r--r--r--  2 root root   5836401 Oct 27 14:20 initrd.lz
-r--r--r--  2 root root   3941696 Oct 16 12:12 vmlinuz
><fs> mkmountpoint /t2
><fs> mount-loop /t1/casper/filesystem.squashfs /t2
><fs> cat /t2/etc/debian_version

The file /casper/filesystem.squashfs seems to be a complete Ubuntu installation, and if I’m understanding this correctly the Ubuntu installer will copy this to the newly created filesystem directly. That will be the new Ubuntu installation, plus or minus some config file changes and some extra packages downloaded afterwards from the net.

Here’s the same examination of the Fedora 12 Live CD:

$ guestfish --ro -a Fedora-12-x86_64-Live.iso

Welcome to guestfish, the libguestfs filesystem interactive shell for
editing virtual machine filesystems.

Type: 'help' for help with commands
      'quit' to quit the shell

><fs> run
><fs> list-devices
><fs> file /dev/sda
ISO 9660 CD-ROM filesystem data 'Fedora-12-x86_64-Live
><fs> mkmountpoint /t1
><fs> mount /dev/sda /t1
><fs> ls /t1
><fs> ll /t1/LiveOS
total 655291
dr-xr-xr-x 2 root root      2048 Nov  9 14:45 .
dr-xr-xr-x 5 root root      2048 Nov  9 14:44 ..
-r-xr-xr-x 1 root root     23040 Nov  9 14:44 livecd-iso-to-disk
-r-xr-xr-x 1 root root      8192 Nov  9 14:45 osmin.img
-r-xr-xr-x 1 root root 670982144 Nov  9 14:48 squashfs.img
><fs> mkmountpoint /t2
><fs> mount-loop /t1/LiveOS/squashfs.img /t2
><fs> ls /t2
><fs> ls /t2/LiveOS/
><fs> mkmountpoint /t3
><fs> mount-loop /t2/LiveOS/ext3fs.img /t3
><fs> cat /t3/etc/redhat-release
Fedora release 12 (Constantine)

Again I hope I assume correctly that the installer copies ext3fs.img to the hard disk when installing Fedora 12.

So my vague plan, assuming anything I’ve written above is correct, is to take these pre-made filesystem images and allow people to quickly install specific operating system images from a simple tool:

$ virt-press Fedora-12 F12

which would stamp out a Fedora 12 VM in a few seconds and register it with libvirt as “F12”.

I’m not sure this is possible yet …


Filed under Uncategorized

Unpack the “Russian doll” of a F11 live CD

Fedora 11 live CDs are like Russian Matryoshka dolls – a filesystem within a filesystem within a filesystem.

On the outside they have to be in the standard CD ISO format because that’s what CDs require. However the CD ISO format is pretty useless, slow, limited filename length, and no support for SELinux labelling. So …

Inside the ISO is another filesystem, a Linux squashfs. (Question: why do they use the squashfs?) That’s not the end though, because …

Inside the squashfs is the final ext3 filesystem as a blob.

In libguestfs / guestfish 1.0.62+ I’ve added some commands to make it easy to unpack this, so you can read out the contents of the inner ext3 filesystem.

All without needing to be root, of course.

Here’s how:

$ guestfish -a Fedora-11-i686-Live.iso

Welcome to guestfish, the libguestfs filesystem interactive shell for
editing virtual machine filesystems.

Type: 'help' for help with commands
      'quit' to quit the shell

><fs> mkmountpoint /cd
><fs> mkmountpoint /squash
><fs> mount /dev/sda /cd
><fs> mount-loop /cd/LiveOS/squashfs.img /squash
><fs> ls /squash/
><fs> ls /squash/LiveOS/
><fs> mkmountpoint /ext3
><fs> mount-loop /squash/LiveOS/ext3fs.img /ext3
><fs> ls /ext3/

After this, the inner ext3 filesystem is mounted inside guestfish as /ext3 and you can go ahead and read out files from it. (But not, unfortunately, edit it because ISO images are read-only. See my earlier posting about that).


Filed under Uncategorized

Use libguestfs to edit a CD ISO

If you want to install a virtual machine from a CD, but you need to pass extra parameters to the Linux kernel, how can you do it?

Virt-install has an option to pass extra parameters to the kernel, but it doesn’t work with CD images because Linux CDs contain their own boot configuration system called ISOLINUX. Of course you can manually type in the extra parameters at the boot prompt, but that cannot be automated.

I was asked today can we use libguestfs to edit the ISOLINUX configuration inside the ISO directly? My first answer was yes, just do:

guestfish -a boot.iso -m /dev/sda edit /isolinux/isolinux.cfg

Unfortunately this doesn’t work for CD ISOs formatted using the standard ISO9660 filesystem. The kernel driver that guestfish uses is read-only, and you’ll get an error when the edit command tries to save the file. (As an aside, it should just work on UDF-formatted ISOs such as DVD images, but I didn’t try it).

Still, all is not lost and we can use guestfish to make the edit and still not require root. We just have to unpack the ISO, make the edit, and pack it up again, like this:

mkdir /tmp/iso
cd /tmp/iso
guestfish -a ../boot.iso -m /dev/sda tar-out / - | tar xvf -
vi isolinux/isolinux.cfg
mkisofs [flags - see comments] -o ../newboot.iso .


Filed under Uncategorized