Prebuilt distributions part 1

Previously I took a look at unpacking Fedora and Ubuntu live CDs to find out what’s inside them and to ask the question can we use the prebuilt filesystem image that these live CDs contain to quickly create a Fedora or Ubuntu “all-defaults” virtual machine?

This is my first attempt, and it’s not successful, but it does demonstrate a large and interesting guestfish script doing a non-trivial amount of work.

This script:

  1. mounts the prebuilt filesystem from either a Fedora or Ubuntu live CD
  2. creates a disk image with a 200 MB /boot partition and a single / (root) logical volume covering the remainder of the disk
  3. uses the cp -a command to recursively copy the prebuilt filesystem to the disk

Where it fails is that “cp” isn’t very fast. On my local machine it took 18 minutes to copy all the files across, which means this isn’t a practical “instant install” method. (I didn’t in the end try to boot the final disk image).

In part 2 this week, I’ll look at the approach that anaconda takes: It dd’s the disk image and then runs resize2fs on it to expand it into the available space.

In part 3 I’ll compare this approach to others: virt-install, manual installation, kickstart, cobbler, debootstrap and ubuntu-vm-builder.

The script itself follows after the cut:

#!/bin/bash -

function usage ()
{
    echo "virt-prebuilt.sh live.iso disk.img size"
    echo "  where 'live.iso' is a Fedora or Ubuntu Live ISO"
    echo "        'disk.img' is the target disk image"
    echo "        'size' is the target size in Gigabytes"
    exit 1
}

if [ $# -lt 3 ]; then
    usage
fi

# Make the commands to unpack the ISO.
# See:
# https://rwmj.wordpress.com/2009/11/21/looking-closer-at-fedora-ubuntu-live-cds
case $(basename $1) in
    ubuntu-*.iso)
	mount_cmds="
mkmountpoint /t1
mount-ro /dev/sda /t1
mkmountpoint /fs
mount-loop /t1/casper/filesystem.squashfs /fs
"
	;;
    Fedora-*Live.iso)
	mount_cmds="
mkmountpoint /t1
mount-ro /dev/sda /t1
mkmountpoint /t2
mount-loop /t1/LiveOS/squashfs.img /t2
mkmountpoint /fs
mount-loop /t2/LiveOS/ext3fs.img /fs
"
	;;
    --help|-help)
	usage
	;;
    *)
	echo "$1: unknown ISO image type (must be Ubuntu or Fedora ISO)"
	usage
esac

# Work out size of target LV.
target_boot_mb=200
target_lv_mb=$(($3 * 1024 - $target_boot_mb - 128))

# Run guestfish.
guestfish -x -a "$1" <<EOF
# The source ISO will be /dev/sda.  The target drive will be /dev/sdb.
alloc "$2" "$3G"
run

# This mounts up the ISO filesystem on /fs
$mount_cmds

echo fstab on target:
cat /fs/etc/fstab

# Make a separate /boot partition on the target disk.
sfdiskM /dev/sdb ",$target_boot_mb ,"
mkmountpoint /target
mkfs ext3 /dev/sdb1

# Make the root LV.
pvcreate /dev/sdb2
vgcreate vg_live /dev/sdb2
lvcreate lv_live vg_live $target_lv_mb
mkfs ext3 /dev/vg_live/lv_live

# Mount up the target disks.
mount /dev/vg_live/lv_live /target
mkdir /target/boot
mount /dev/sdb1 /target/boot

# Copy everything across.
time cp-a /fs /target

unmount /target
-unmount /fs
-unmount /t2
-unmount /t1

echo Done.
EOF
Advertisement

2 Comments

Filed under Uncategorized

2 responses to “Prebuilt distributions part 1

  1. Pingback: Prebuilt distributions part 2 « Richard WM Jones

  2. Pingback: Prebuilt distributions part 3 « Richard WM Jones

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.