How to Copy Files from MINIX in a Virtual Machine to the Host Linux System

October 3, 2016

Background

Reading MINIX source code directly inside the virtual machine is very inconvenient, and modifying, debugging, or porting it there is even harder. If you copy part of the MINIX source to the host Linux machine, editing and debugging become much easier.

However, MINIX is a fairly bare-bones system. You can't simply install VMware Tools as you would for a Windows guest in VMware (it lacks X Window; in theory you could install X on MINIX, but that's a lot of hassle and overkill just for a few source files). VMware's disk files (.vmdk) are not raw disk images, and VMware doesn't recognize the file systems of niche systems like MINIX very well. As a result, you can't mount the MINIX disk on the host with VMware, so you still can't get the files out of the MINIX guest.

So I chose QEMU instead. QEMU disks are raw data files that can be mounted directly into the Linux file tree (as long as Linux recognizes the file system).

Software and Systems

  • MINIX: minix_R3.2.0-116fcea.iso
  • QEMU: qemu-system-i386

Steps

Download the MINIX ISO, start QEMU on Linux, and install MINIX with:

bash
qemu-system-i386 -enable-kvm -sdl -cdrom minix_R3.2.0-116fcea.iso -hda minix.img

If you don't have the minix.img disk image yet, create it with qemu-img create minix.img 2G or touch minix.img && truncate minix.img --size=2G.

After MINIX is installed, prepare a spare virtual disk image, say a.img. This disk will hold the source files you need. Once the image is created, don't rush to partition or format it on Linux; that should be done inside the MINIX guest.

With both minix.img and a.img ready, boot the VM again to copy files:

bash
qemu-system-i386 -enable-kvm -sdl -hda minix.img -hdb a.img

Here minix.img already has MINIX installed from the previous step.

Once the QEMU VM is up, log in to MINIX as root. MINIX organizes devices differently from Linux: disks are usually named like c0d0p0, where the first c stands for "Controller", the digit is an index, d stands for "Disk", p stands for "Partition", and there is also s for "Subpartition". Run man dev for a detailed explanation.

Use df to figure out which disk minix.img occupies. At this point, a.img is attached to MINIX, but we don't yet know which device it is. First rule out the disk used by minix.img, say c0d0; mounting an already-mounted disk with mount will just report it as busy. The CD-ROM is usually c0d2. Try mounting c0d1:

bash
mount /dev/c0d1 /mnt

Pay attention to the error. If it says the device or address does not exist, try the next one, such as c0d3, until you find a disk that no longer reports that error. Under the assumptions above, exclude c0d0 and c0d2; if c0d1 doesn't report a "no such device or address" error, then a.img is that device.

In MINIX, partition it with fdisk:

bash
fdisk /dev/c0d1

In fdisk, use the c command to change partition information, then use w to write it to the a.img image.

After partitioning a.img, format it with mkfs. Be sure to use the ext2 file system here, because ext2 is well supported by both MINIX and Linux. The command is:

bash
/sbin/mkfs.ext2 /dev/c0d1

Now c0d1 is a mountable device, and you can mount it:

bash
mount /dev/c0d1 /mnt

After mounting, go to /mnt and copy all the files you need there. When done, unmount it and shut down the VM.

Back on Linux, a.img now contains the copied files. Just mount a.img as a device:

bash
sudo mount -t ext2 a.img /mnt

Regular users need sudo to elevate privileges; root doesn't.

Go into /mnt and check that all the files are there.

This is a general way to copy files out of QEMU. The prerequisite is that the guest and host both support a common file system, which then serves as the transfer medium.