{"id":2913,"date":"2023-10-30T03:39:32","date_gmt":"2023-10-30T03:39:32","guid":{"rendered":"https:\/\/voodoo.business\/?p=2913"},"modified":"2024-05-13T14:44:48","modified_gmt":"2024-05-13T14:44:48","slug":"mounting-qcow2-kvm-qemu-directly","status":"publish","type":"post","link":"https:\/\/www.voodoo.business\/blog\/2023\/10\/30\/mounting-qcow2-kvm-qemu-directly\/","title":{"rendered":"Mounting QCOW2 (KVM\/QEMU) directly"},"content":{"rendered":"\n<p>First, the tools you need<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">apt-get install qemu-utils<\/pre>\n\n\n\n<p>Now, enable NBD<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">modprobe nbd max_part=8<\/pre>\n\n\n\n<p>Once that is enabled, <strong>connect <\/strong>the file as a <strong>block device<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">qemu-nbd --connect=\/dev\/nbd0 \/hds\/usb\/virts\/Windows\/main.qcow2<\/pre>\n\n\n\n<p>Now, the block device should appear like any other, alongside the partitions inside !<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">fdisk -l<\/pre>\n\n\n\n<p>On my machine, this resulted in <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Disk \/dev\/nbd0: 95 GiB, 102005473280 bytes, 199229440 sectors\nUnits: sectors of 1 * 512 = 512 bytes\nSector size (logical\/physical): 512 bytes \/ 512 bytes\nI\/O size (minimum\/optimal): 512 bytes \/ 512 bytes\nDisklabel type: dos\nDisk identifier: 0xc5324c42\n\nDevice      Boot     Start       End   Sectors  Size Id Type\n\/dev\/nbd0p1 *         2048    104447    102400   50M  7 HPFS\/NTFS\/exFAT\n\/dev\/nbd0p2         104448 198138958 198034511 94.4G  7 HPFS\/NTFS\/exFAT\n\/dev\/nbd0p3      198139904 199225343   1085440  530M 27 Hidden NTFS WinRE\n<\/pre>\n\n\n\n<p>This disk was around 40GB, but fdisk will see the number corresponding to the largest allowed size, 100GB in this case ! let us <strong>mount <\/strong>the drive<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mount \/dev\/nbd0p2 \/hds\/loop<\/pre>\n\n\n\n<p>Now, in this case in particular, like any other block device that held the windows operating system, more often than not, you will get the message saying<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">The disk contains an unclean file system (0, 0).\nMetadata kept in Windows cache, refused to mount.\nFalling back to read-only mount because the NTFS partition is in an\nunsafe state. Please resume and shutdown Windows fully (no hibernation\nor fast restarting.)\nCould not mount read-write, trying read-only<\/pre>\n\n\n\n<p>The solution to that is simple, follow the following two steps to remedy the issue and then force mount the file by using remove_hiberfile<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ntfsfix \/dev\/nbd0p2\nmount -t ntfs-3g -o remove_hiberfile \/dev\/nbd0p2 \/hds\/loop<\/pre>\n\n\n\n<p>The result of NTFSFIX was <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Mounting volume... The disk contains an unclean file system (0, 0).\nMetadata kept in Windows cache, refused to mount.\nFAILED\nAttempting to correct errors...\nProcessing $MFT and $MFTMirr...\nReading $MFT... OK\nReading $MFTMirr... OK\nComparing $MFTMirr to $MFT... OK\nProcessing of $MFT and $MFTMirr completed successfully.\nSetting required flags on partition... OK\nGoing to empty the journal ($LogFile)... OK\nChecking the alternate boot sector... OK\nNTFS volume version is 3.1.\nNTFS partition \/dev\/nbd0p2 was processed successfully.<\/pre>\n\n\n\n<p>And the following mount command worked as you would expect, silently<\/p>\n\n\n\n<p>Now, if you want to disconnect the NBD image, you need to unmount (Like you normally would) THEN<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#Disconnect the image from the NBD device<br>qemu-nbd --disconnect \/dev\/nbd0;<br>#Unload the NBD module<br>rmmod nbd;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>First, the tools you need apt-get install qemu-utils Now, enable NBD modprobe nbd max_part=8 Once that is enabled, connect the file as a block device qemu-nbd &#8211;connect=\/dev\/nbd0 \/hds\/usb\/virts\/Windows\/main.qcow2 Now, the block device should appear like any other, alongside the partitions inside ! fdisk -l On my machine, this resulted in Disk \/dev\/nbd0: 95 GiB, 102005473280 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[140,32,189,139],"tags":[],"class_list":["post-2913","post","type-post","status-publish","format-standard","hentry","category-kvm","category-ntfs","category-qcow2","category-virtualization-linux"],"_links":{"self":[{"href":"https:\/\/www.voodoo.business\/blog\/wp-json\/wp\/v2\/posts\/2913","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.voodoo.business\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.voodoo.business\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.voodoo.business\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.voodoo.business\/blog\/wp-json\/wp\/v2\/comments?post=2913"}],"version-history":[{"count":8,"href":"https:\/\/www.voodoo.business\/blog\/wp-json\/wp\/v2\/posts\/2913\/revisions"}],"predecessor-version":[{"id":3551,"href":"https:\/\/www.voodoo.business\/blog\/wp-json\/wp\/v2\/posts\/2913\/revisions\/3551"}],"wp:attachment":[{"href":"https:\/\/www.voodoo.business\/blog\/wp-json\/wp\/v2\/media?parent=2913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.voodoo.business\/blog\/wp-json\/wp\/v2\/categories?post=2913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.voodoo.business\/blog\/wp-json\/wp\/v2\/tags?post=2913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}