{"id":100353,"date":"2020-10-29T12:00:13","date_gmt":"2020-10-29T09:00:13","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-mount-a-qemu-virtual-disk-image-cloudsavvy-it\/"},"modified":"2020-10-29T12:00:13","modified_gmt":"2020-10-29T09:00:13","slug":"how-to-mount-a-qemu-virtual-disk-image-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-mount-a-qemu-virtual-disk-image-cloudsavvy-it\/","title":{"rendered":"#How to mount a QEMU virtual disk image \u2013 CloudSavvy IT"},"content":{"rendered":"<p><strong>&#8220;#How to mount a QEMU virtual disk image \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-7524\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/34712ab5d64eaea8026ce30494f85fac\/p\/uploads\/2020\/10\/5c8bb131.png\" alt=\"\" width=\"700\" height=\"300\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Let\u2019s say you discover critical business data in a legacy DOS spreadsheet file, and Excel can\u2019t read the file. If the legacy program originally ran on DOS, you might boot a copy of FreeDOS, and install the legacy program there to extract or <em>export<\/em> the data to a common file format, like a comma-separated CSV file.<\/p>\n<p>You can follow our instructions to install and boot FreeDOS using the QEMU virtual machine. But having installed FreeDOS, how do you then install and run the legacy program in QEMU?<\/p>\n<p>You can mount a QEMU disk image using two basic methods: using an offset to mount the image directly, or using the <code>libguestfs-tools<\/code> \u00a0package.<\/p>\n<p>Linux can mount QEMU\u2019s raw disk image format, assuming it knows where to find the start of the C: drive partition on the virtual disk. The virtual disk can contain multiple partitions, but for most legacy operating system installations like DOS, you probably only have one partition. This is the C: drive.<\/p>\n<p>Let\u2019s look at output from the Linux <code>fdisk<\/code> program. Using the <code>-l<\/code> option will list the partitions on the virtual disk:<\/p>\n<pre>$ fdisk -l image.img &#13;\n<strong>Disk image.img: 100 MiB, 104857600 bytes, 204800 sectors<\/strong>&#13;\nUnits: sectors of 1 * 512 = 512 bytes&#13;\nSector size (logical\/physical): 512 bytes \/ 512 bytes&#13;\nI\/O size (minimum\/optimal): 512 bytes \/ 512 bytes&#13;\nDisklabel type: dos&#13;\nDisk identifier: 0x00000000&#13;\n&#13;\n<strong>Device     Boot Start    End Sectors  Size Id Type<\/strong>&#13;\nimage.img1 *       63 204623  204561 99.9M  6 FAT16<\/pre>\n<p>Here, the C: drive is the first and only partition on the virtual drive, listed as <code>image.img1<\/code> .To determine the offset of the C: drive, you need to know the starting sector of that partition. The output from <code>fdisk<\/code> shows this as 63.<\/p>\n<p>To calculate the offset, you also need to know the sector size. This virtual disk image has sectors of 512 bytes. You calculate the offset as the product of the offset and the sector size: 63 \u00d7 512 = 32256.<\/p>\n<p>Use this offset value to mount the virtual C: drive from Linux:<\/p>\n<pre>$ mkdir \/tmp\/dos&#13;\n$ sudo mount -o loop,offset=32256 image.img \/tmp\/dos&#13;\n$ ls \/tmp\/dos&#13;\nAUTOEXEC.BAT\u00a0 COMMAND.COM\u00a0 FDCONFIG.SYS\u00a0 FDOS\u00a0 KERNEL.SYS&#13;\n$ sudo umount \/tmp\/dos<\/pre>\n<p>You can automate the calculation using a Bash <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">script<\/a>. The script first needs to gather the values in an easily-parsable format. On my Linux system, I use the <code>-o<\/code> option with <code>fdisk<\/code> to specify which columns to print. Since I only need to match the partition (Device) and the Start value, I use this <code>fdisk<\/code> command to list the columns I need:<\/p>\n<pre>$ fdisk -l -o Device,Start image.img &#13;\n<strong>Disk image.img: 100 MiB, 104857600 bytes, 204800 sectors<\/strong>&#13;\nUnits: sectors of 1 * 512 = 512 bytes&#13;\nSector size (logical\/physical): 512 bytes \/ 512 bytes&#13;\nI\/O size (minimum\/optimal): 512 bytes \/ 512 bytes&#13;\nDisklabel type: dos&#13;\nDisk identifier: 0x00000000&#13;\n&#13;\n<strong>Device     Start<\/strong>&#13;\nimage.img1    63<\/pre>\n<p>And a Bash script can parse that output using Gawk to calculate the offset on its own:<\/p>\n<pre>#!\/bin\/bash&#13;\n# usage: qemu-mount {imagefile}&#13;\n# 1st argument: QEMU raw image file&#13;\n&#13;\nif [ $# -ne 1 ] ; then&#13;\n  echo 'usage: qemu-mount imagefile'&#13;\n  echo 'Mounts a QEMU raw image file to \/tmp\/dos'&#13;\n  exit 1&#13;\nfi&#13;\n&#13;\nstart=$( fdisk -l -o Device,Start ${1} | grep \"^${1}1\" | gawk '{print $2}' )&#13;\nsectors=$( fdisk -l ${1} | grep '^Units: sectors of' | gawk '{print $(NF-1)}' )&#13;\noffset=$(( $start * $sectors ))&#13;\n&#13;\n[ -d \/tmp\/dos ] || mkdir \/tmp\/dos&#13;\nsudo mount -o loop,offset=$offset ${1} \/tmp\/dos<\/pre>\n<p>After mounting the QEMU virtual disk image to the <code>\/tmp\/dos<\/code> temporary mount point, you can read and write data on the virtual C: drive. This is a handy way to copy a legacy program installer to the virtual machine, or copy data out of the virtual machine. However, you should only mount the virtual disk image when QEMU is not running. If you attempt to mount a QEMU disk image while it is in use, you may corrupt the virtual disk.<\/p>\n<p>When you have finished accessing the virtual disk image from Linux, don\u2019t forget to unmount the image with the\u00a0<code>umount<\/code>\u00a0command.<\/p>\n<p>The <code>libguestfs-tools<\/code> package provides a set of tools to access virtual machine disk images. Included in this toolkit is <code>guestmount<\/code> , which makes it really easy to\u00a0mount a QEMU disk image from Linux. If your distribution does not include this package by default, you may need to install the <a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/libguestfs.org\/\">libguestfs<\/a> software separately. However, many Linux distributions including Fedora, Red Hat Enterprise Linux, Debian, and Ubuntu provide a <code>libguestfs-tools<\/code> package you can install via your package manager.<\/p>\n<p>Two options tell <code>guestmount<\/code> how to access the QEMU\u00a0disk image. The <code>--add\u00a0<em>imagefile<\/em><\/code> option specifies the virtual disk image to work with, and the <code>--mount\u00a0<em>device<\/em><\/code>\u00a0option tells <code>guestmount<\/code> which partition to use from the disk image. You need to indicate the device using a standard Linux path, such as <code>\/dev\/sda1<\/code> \u00a0for the first partition in the virtual disk image.<\/p>\n<p>With these two options, mounting the virtual disk image from Linux is a single-line command:<\/p>\n<pre>guestmount --add image.img --mount \/dev\/sda1 \/tmp\/dos<\/pre>\n<p>After a moment, this mounts the virtual C: drive to the \/tmp\/dos temporary mount point. You can access files on the virtual disk image\u00a0<code>\/tmp\/dos<\/code> using standard Linux tools, such as <code>cp<\/code> and <code>mv<\/code>, to copy data into and out of the virtual disk. Be careful to only mount the disk image when QEMU is not running, or you may corrupt the virtual disk.<\/p>\n<p>When you have finished with the virtual C: drive, you can unmount the filesystem using the <code>guestunmount<\/code> command.\n<\/div>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener noreferrer\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/7517\/how-to-mount-a-qemu-virtual-disk-image\/\" target=\"_blank\" rel=\"noopener noreferrer\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to mount a QEMU virtual disk image \u2013 CloudSavvy IT&#8221; Let\u2019s say you discover critical business data in a legacy DOS spreadsheet file, and Excel can\u2019t read the file. If the legacy program originally ran on DOS, you might boot a copy of FreeDOS, and install the legacy program there to extract or export&#8230;<\/p>\n","protected":false},"author":1,"featured_media":100354,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/10\/5c8bb131.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-100353","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/100353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=100353"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/100353\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/100354"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=100353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=100353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=100353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}