You need to create a large empty disk image file first; then, you need
to install a Linux OS onto it. There are two methods you can choose.
One is directly installing it using a VMX guest while booting from the
OS installation CD-ROM. The other is copying an installed OS into it.
The boot loader will also need to be installed.
The image size should be big enough to accommodate the entire OS.
This example assumes the size is 1G (which is probably too small for
most OSes).
# dd if=/dev/zero of=hd.img bs=1M count=1 seek=1023
Install Xen and create VMX with the original image file with booting
from CD-ROM. Then it is just like a normal Linux OS installation. The
VMX configuration file should have these two entries before creating:
cdrom='/dev/cdrom'
boot='d'
If this method does not succeed, you can choose the following method of copying an installed Linux OS into an image file.
Directly installing is an easier way to make partitions and
install an OS in a disk image file. But if you want to create a
specific OS in your disk image, then you will most likely want to use
this method.
- Install a normal Linux OS on the host machine
You can choose any way to install Linux, such as using yum to
install Red Hat Linux or YAST to install Novell SuSE Linux. The rest of
this example assumes the Linux OS is installed in /var/guestos/.
- Make the partition table
The image file will be treated as hard disk, so you should make the partition table in the image file. For example:
# losetup /dev/loop0 hd.img
# fdisk -b 512 -C 4096 -H 16 -S 32 /dev/loop0
press 'n' to add new partition
press 'p' to choose primary partition
press '1' to set partition number
press "Enter" keys to choose default value of "First Cylinder" parameter.
press "Enter" keys to choose default value of "Last Cylinder" parameter.
press 'w' to write partition table and exit
# losetup -d /dev/loop0
- Make the file system and install grub
# ln -s /dev/loop0 /dev/loop
# losetup /dev/loop0 hd.img
# losetup -o 16384 /dev/loop1 hd.img
# mkfs.ext3 /dev/loop1
# mount /dev/loop1 /mnt
# mkdir -p /mnt/boot/grub
# cp /boot/grub/stage* /boot/grub/e2fs_stage1_5 /mnt/boot/grub
# umount /mnt
# grub
grub> device (hd0) /dev/loop
grub> root (hd0,0)
grub> setup (hd0)
grub> quit
# rm /dev/loop
# losetup -d /dev/loop0
# losetup -d /dev/loop1
The losetup option -o 16384 skips the partition table in the image file. It is the number of sectors times 512. We need /dev/loop because grub is expecting a disk device name, where name represents the entire disk and name1 represents the first partition.
- Copy the OS files to the image
If you have Xen installed, you can easily use lomount instead of losetup and mount when coping files to some partitions. lomount just needs the partition information.
# lomount -t ext3 -diskimage hd.img -partition 1 /mnt/guest
# cp -ax /var/guestos/{root,dev,var,etc,usr,bin,sbin,lib} /mnt/guest
# mkdir /mnt/guest/{proc,sys,home,tmp}
- Edit the /etc/fstab of the guest image
The fstab should look like this:
# vim /mnt/guest/etc/fstab
/dev/hda1 / ext3 defaults 1 1
none /dev/pts devpts gid=5,mode=620 0 0
none /dev/shm tmpfs defaults 0 0
none /proc proc defaults 0 0
none /sys sysfs efaults 0 0
- umount the image file
# umount /mnt/guest
Now, the guest OS image hd.img is ready. You can also reference https://free.oszoo.org for quickstart images. But make sure to install the boot loader.