Random Tech Thoughts

The title above is not random

Qemu

Mount raw disk image

Summary from this reply on Arch Linux’s forum for more information.

A raw image is not an image of a partition but of a whole drive. That is, there may be a master boot record at the beginning of the image and probably some padding after the MBR.

  1. First read/parse the MBR of the image and find the offset of the partition

    $ fdisk -ul disk.img
    ...
    Units = sectors of 1 * 512 = 512 bytes
    ...
    Device Boot         Start         End      Blocks   Id  System
    disk.img1              63       16064        8001    1  FAT12
    disk.img2           16065       48194       16065    5  Extended
    disk.img5           16128       32129        8001   83  Linux
    disk.img6           32193       48194        8001    1  FAT12
    

    The offset is calculated as <start block> * 512

  2. Mount the partition by specifying the offset of the image

    # mount -o loop,offset=$((16128 * 512)) /tmp/disk.img /mnt
    

    The above example mounts the disk.img5 partition.

Telnet monitor access

From Two ways to access your virtual machine monitor across the network

When starting qemu, add the following option:

-monitor telnet:localhost:4444,server,nowait
  • server means listening in server mode
  • nowait qemu will wait for a client socket application to connect to the port before continuing unless this option is used.

Start Linux system using a kernel outside disk image

Use the -kernel, -append option:

qemu-system-x86_64 -nographic -serial mon:/dev/tty -hda <img> -kernel <kern> -append "console=ttyS0,57600n8 root=/dev/hda"

Setting up Linux to use serial line

2 things need to be done:

  • Tell the kernel use the serial port as console
  • Allow login from serial line

The first need to change grup configuration to pass the command line option to the kernel. Suppose you are using Grub 2 on Debian 6, edit /etc/default/grub:

GRUB_CMDLINE_LINUX_DEFAULT="console=ttyS0,57600n8"

After that, run update-grub in chroot environment:

mount -t proc none chroot/proc
mount -o bind /dev chroot/dev
mount -o bind /sys chroot/sys
chroot chroot /bin/bash
update-grub

(If you are doing this for qemu image file, update-grub may report it can’t stat disk image. In that case, you have to manually edit /boot/grub/grub.cfg, change the root setting to the correct value.)

To allow login from serial port, modify /etc/inittab, make the original gettty from tty1 to ttyS0 (which means serial port):

1:2345:respawn:/sbin/getty 57600 ttyS0
#1:2345:respawn:/sbin/getty 38400 tty1

Create initrd and use it as root disk

From Debian Lenny 5.0.1 PXE initrd update

cd armroot
find . -print0 | cpio -0 -H newc -ov | gzip -c > ../initrd.gz

To let linux use the initrd as root disk and do not mount normal disk, just create a /init executable file (script is OK). For more information about initial ramdisk and the linux boot process, read Linux initial RAM disk (initrd) overview

Fsck disk image

losetup -o <offset> /dev/loop0 debian.img
fsck.ext4 /dev/loop0
losetup -d /dev/loop0

Specify NIC card and NAT network

Use the following option to specify realtek NIC

-net nic,model=rtl8139 -net user

Port forwarding

Use the --redir option.

qemu -hda disk.img -redir tcp:<host port>::<guest port>

After this, connecting to <host port> will connect to the <guest port> in the guest system.

Enable KVM

Must first load host kernel module

modprobe kvm
modprobe kvm_intel