How to make a Bochs disk image

  1. First, use a real machine to setup a partition to your liking. If you need to tar up the disk, use something like:
    tar czlp --same-owner -f c.img.tgz --exclude /tmp /
    I exclude certain directories to save space.
  2. Use the bximage tool that comes with Bochs to create a disk image:
    bximage: hd, flat, 1500, c.img
      sectors per track=63
      total sectors=3071376
      total size=1499.70 megabytes
  3. Use fdisk to create a partition table on the image file:
    fdisk c.img
    1. Setup geometry:
      • x - extra functionality
      • h 16
      • s 63
      • c 3047
      • r - return to main menu
    2. n, p, 1, enter, enter - create new partition
    3. a 1 - make bootable (optional?)
    4. w - write the table to disk
  4. Setup the loopback device -- note that you need to use an offset corresponding to the location of the first partition (explanation).
    /sbin/losetup -o 32256 /dev/loop0 c.img
  5. Format the disk:
    /sbin/mke2fs -j /dev/loop0
  6. Mount the disk to put files on it
    mount /dev/loop0 /mnt
    cd /mnt
    tar xzvpf /home/bits7/NOBACKUP/kbarr/hpldisk/c.img.tgz
  7. Now you need a bootloader. The /boot/grub/menu.lst on the image needs to be set up (presumably you did this in step 1). You'll need at least grub 0.95 as explained in this thread
    % ./grub-0.95/grub/grub --no-floppy
    grub> device (hd0) c.img
    grub> geometry (hd0) 3047 16 63
    drive 0x80: C/H/S = 3047/16/63, The number of sectors = 3071376, c.img
       Partition num: 0,  Filesystem type is ext2fs, partition type 0x83
    grub> root (hd0,0)
     Filesystem type is ext2fs, partition type 0x83
    grub> setup (hd0)
     Checking if "/boot/grub/stage1" exists... yes
     Checking if "/boot/grub/stage2" exists... yes
     Checking if "/boot/grub/e2fs_stage1_5" exists... yes
     Running "embed /boot/grub/e2fs_stage1_5 (hd0)"...  22 sectors are embedded.
     Running "install /boot/grub/stage1 (hd0) (hd0)1+22 p (hd0,0)/boot/grub/stage2 /boot/grub/menu.lst"..
    . succeeded
    Make sure grub on the image is also 0.95. Also there may be problems if you try to use the graphical bootloader, so make sure you have the text-based one selected on the image's /boot/grub/menu.lst.

    Offsets and loopback mounting

    To determine the correct offset for mounting your disk image, do:
    /sbin/sfdisk debian-3.0r0.img
    You'll get a chart like:
    Units = cylinders of 516096 bytes, blocks of 1024 bytes, counting from 0
       Device         Boot Start     End   #cyls   #blocks   Id  System
    debian-3.0r0.img1          0+    260     261-   131512+  82  Linux swap
    debian-3.0r0.img2        261    1023     763    384552   83  Linux
    Take the starting cylinder that you care about (261). sfdisk is using cylinders of 516096 bytes, so the offset is 261*516096 = 134701056. Or, use fdisk (with -u) instead:
    /sbin/fdisk -l -u c.img
    In this case
    Disk c.img: 0 heads, 0 sectors, 0 cylinders
    Units = sectors of 1 * 512 bytes
    Device Boot    Start       End    Blocks   Id  System
    c.img1            63   1596671    798304+  83  Linux
    And we take 63 * 512 = 32256.