2012年11月28日星期三

Construct Linux virtual Image in minutes by BoxGrinder and Guestfish

BoxGrinder and Guestfish are my favorite command line tools when I need to construct virtual images (Linux only,of course).

Note: all my bellow CLI samples verified on Fedora Linux only.

Installation

It's deadly simple to install the above 2 tools:

$ sudo yum install rubygem-boxgrinder-build
$ sudo yum install libguestfs-tools

That's all.

Scenario

Creating a virtual image with RHEL6.3 OS, and tweak several configuration files within it before deploying it into a virtual machine on KVM or other hypervisors.

Procedure

1. prepare yum repository URL. Either remote repository via HTTP or local mounted iso file. Assume it is file:///mnt/rhel63

2. prepare the appliance description file in yaml format. Here's a sample:

$ cat /data/jeos-rhel63.appl 
name: rhel63
summary: RHEL6.3
os:
  name: rhel
  version: 6
  password: ********
repos:
  - name: "base"
    baseurl: "file:///mnt/rhel63"
    ephemeral: true
hardware:
  cpus: 1
  memory: 512
  partitions:
    "/":
      size: 30
      type: ext4

Generally you will find I intend to install nothing except the OS itself, which is called "Just Enough OS". I like this way because the actual image file size can be controlled less than 800MB. Alternatively, if you want to add additional packages during the virtual image creation, use stanza "packages". Like:

packages:
  - mysql-server

You can find more samples and detailed reference here.

3. create the virtual image file by running the following command:
$ sudo boxgrinder-build jeos-rhel63.appl -p vmware -d local


There's many options that you can customize the image construction like disk format, container format, just simply perform this command to find the list of options:
$ sudo boxgrinder-build --help

Notice that it doesn't perform the actual installation procedure over any virtualizationt technology, so the construction goes very fast. Usually it takes 3~5 minutes to get the final virtual image file. The actual time cost depends on your disk write rate and/or the network bandwidth if using remote yum repository over HTTP/FTP.

4. After it completes, you will find the image file under directory ./build/appliances/x86_64/rhel/6/rhel63/1.0/

5. The final step is customize the file system inside this virtual image. Now you need to start using guestfish:
$ sudo guestfish --rw -i rhel63-sda.raw
> 

Then you can perform vi, rm, man and other commands to change the files. For example, I usually dumping the public key under the /root/.ssh/authorized_keys so that no need to use account password.

At the end, you can type quit to exit the guestfish shell environment and transfer the image file to OpenStack or your local KVM hypervisor directly. Notice that you must keep the sparse file or the transfer rate is pretty slow.

Further explore

guestfish-browser is a fancy GUI tool to browse the virtual image file system. you can install it via:
$ sudo yum install guestfish-browser