The best way to mount a QEMU digital disk picture

The best way to mount a QEMU digital disk picture


Let’s say you uncover important enterprise knowledge in a legacy DOS spreadsheet file, and Excel can’t learn the file. If the legacy program initially ran on DOS, you would possibly boot a replica of FreeDOS, and set up the legacy program there to extract or export the info to a standard file format, like a comma-separated CSV file.

You’ll be able to comply with our directions to put in and boot FreeDOS using the QEMU virtual machine. However having put in FreeDOS, how do you then set up and run the legacy program in QEMU?

You’ll be able to mount a QEMU disk picture utilizing two fundamental strategies: utilizing an offset to mount the picture instantly, or utilizing the libguestfs-tools  package deal.

Utilizing an Offset

Linux can mount QEMU’s uncooked disk picture format, assuming it is aware of the place to seek out the beginning of the C: drive partition on the digital disk. The digital disk can include a number of partitions, however for many legacy working system installations like DOS, you in all probability solely have one partition. That is the C: drive.

Let’s have a look at output from the Linux fdisk program. Utilizing the -l choice will record the partitions on the digital disk:

$ fdisk -l picture.img 
Disk picture.img: 100 MiB, 104857600 bytes, 204800 sectors
Models: sectors of 1 * 512 = 512 bytes
Sector measurement (logical/bodily): 512 bytes / 512 bytes
I/O measurement (minimal/optimum): 512 bytes / 512 bytes
Disklabel sort: dos
Disk identifier: 0x00000000

System     Boot Begin    Finish Sectors  Dimension Id Kind
picture.img1 *       63 204623  204561 99.9M  6 FAT16

Right here, the C: drive is the primary and solely partition on the digital drive, listed as picture.img1 .To find out the offset of the C: drive, you could know the beginning sector of that partition. The output from fdisk reveals this as 63.

To calculate the offset, you additionally must know the sector measurement. This digital disk picture has sectors of 512 bytes. You calculate the offset because the product of the offset and the sector measurement: 63 × 512 = 32256.

Use this offset worth to mount the digital C: drive from Linux:

$ mkdir /tmp/dos
$ sudo mount -o loop,offset=32256 picture.img /tmp/dos
$ ls /tmp/dos
AUTOEXEC.BAT  COMMAND.COM  FDCONFIG.SYS  FDOS  KERNEL.SYS
$ sudo umount /tmp/dos

You’ll be able to automate the calculation utilizing a Bash script. The script first wants to assemble the values in an easily-parsable format. On my Linux system, I take advantage of the -o choice with fdisk to specify which columns to print. Since I solely must match the partition (System) and the Begin worth, I take advantage of this fdisk command to record the columns I would like:

$ fdisk -l -o System,Begin picture.img 
Disk picture.img: 100 MiB, 104857600 bytes, 204800 sectors
Models: sectors of 1 * 512 = 512 bytes
Sector measurement (logical/bodily): 512 bytes / 512 bytes
I/O measurement (minimal/optimum): 512 bytes / 512 bytes
Disklabel sort: dos
Disk identifier: 0x00000000

System     Begin
picture.img1    63

And a Bash script can parse that output utilizing Gawk to calculate the offset by itself:

#!/bin/bash
# utilization: qemu-mount {imagefile}
# 1st argument: QEMU uncooked picture file

if [ $# -ne 1 ] ; then
  echo 'utilization: qemu-mount imagefile'
  echo 'Mounts a QEMU uncooked picture file to /tmp/dos'
  exit 1
fi

begin=$( fdisk -l -o System,Begin ${1} | grep "^${1}1" | gawk '{print $2}' )
sectors=$( fdisk -l ${1} | grep '^Models: sectors of' | gawk '{print $(NF-1)}' )
offset=$(( $begin * $sectors ))

[ -d /tmp/dos ] || mkdir /tmp/dos
sudo mount -o loop,offset=$offset ${1} /tmp/dos

After mounting the QEMU digital disk picture to the /tmp/dos non permanent mount level, you’ll be able to learn and write knowledge on the digital C: drive. It is a useful approach to copy a legacy program installer to the digital machine, or copy knowledge out of the digital machine. Nevertheless, you need to solely mount the digital disk picture when QEMU isn’t working. When you try and mount a QEMU disk picture whereas it’s in use, chances are you’ll corrupt the digital disk.

When you have got completed accessing the digital disk picture from Linux, don’t overlook to unmount the picture with the umount command.

Utilizing libguestfs-tools

The libguestfs-tools package deal offers a set of instruments to entry digital machine disk photos. Included on this toolkit is guestmount , which makes it very easy to mount a QEMU disk picture from Linux. In case your distribution doesn’t embrace this package deal by default, chances are you’ll want to put in the libguestfs software program individually. Nevertheless, many Linux distributions together with Fedora, Purple Hat Enterprise Linux, Debian, and Ubuntu present a libguestfs-tools package deal you’ll be able to set up by way of your package deal supervisor.

Two choices inform guestmount the best way to entry the QEMU disk picture. The --add imagefile choice specifies the digital disk picture to work with, and the --mount system choice tells guestmount which partition to make use of from the disk picture. It is advisable point out the system utilizing a normal Linux path, akin to /dev/sda1  for the primary partition within the digital disk picture.

With these two choices, mounting the digital disk picture from Linux is a single-line command:

guestmount --add picture.img --mount /dev/sda1 /tmp/dos

After a second, this mounts the digital C: drive to the /tmp/dos non permanent mount level. You’ll be able to entry recordsdata on the digital disk picture /tmp/dos utilizing normal Linux instruments, akin to cp and mv, to repeat knowledge into and out of the digital disk. Watch out to solely mount the disk picture when QEMU isn’t working, or chances are you’ll corrupt the digital disk.

When you have got completed with the digital C: drive, you’ll be able to unmount the filesystem utilizing the guestunmount command.



Source link

Uncategorized