HostOnNet Blog

Mounting KVM qcow2 image

Looking for Linux Server Admin or WordPress Expert? We can help.

To mount qcow2 virtual machine image, you need to install qemu-nbd.

On Ubuntu, run

apt-get install qemu-utils

Load the kernel module with command

modprobe nbd

For testing, we download Fedora 25 gcow2 image

wget https://download.fedoraproject.org/pub/fedora/linux/releases/26/CloudImages/x86_64/images/Fedora-Cloud-Base-26-1.5.x86_64.qcow2

Lets check the file details

root@server2:~# file Fedora-Cloud-Base-26-1.5.x86_64.qcow2 
Fedora-Cloud-Base-26-1.5.x86_64.qcow2: QEMU QCOW Image (v2), 4294967296 bytes
root@server2:~# 

Lets mount it on device /dev/nbd0

qemu-nbd -c /dev/nbd0 Fedora-Cloud-Base-26-1.5.x86_64.qcow2 

You need to find the device name, that will be something like /dev/nbd0xx, try all until you get it mounted.

root@backup:~# ls -l /dev/ | grep nb
brw-rw---- 1 root disk     43,   0 Aug 23 03:25 nbd0
brw-rw---- 1 root disk     43,   1 Aug 23 03:25 nbd0p1
brw-rw---- 1 root disk     43,  16 Aug 23 03:24 nbd1
brw-rw---- 1 root disk     43, 160 Aug 23 03:24 nbd10
brw-rw---- 1 root disk     43, 176 Aug 23 03:24 nbd11
brw-rw---- 1 root disk     43, 192 Aug 23 03:24 nbd12
brw-rw---- 1 root disk     43, 208 Aug 23 03:24 nbd13
brw-rw---- 1 root disk     43, 224 Aug 23 03:24 nbd14
brw-rw---- 1 root disk     43, 240 Aug 23 03:24 nbd15
brw-rw---- 1 root disk     43,  32 Aug 23 03:24 nbd2
brw-rw---- 1 root disk     43,  48 Aug 23 03:24 nbd3
brw-rw---- 1 root disk     43,  64 Aug 23 03:24 nbd4
brw-rw---- 1 root disk     43,  80 Aug 23 03:24 nbd5
brw-rw---- 1 root disk     43,  96 Aug 23 03:24 nbd6
brw-rw---- 1 root disk     43, 112 Aug 23 03:24 nbd7
brw-rw---- 1 root disk     43, 128 Aug 23 03:24 nbd8
brw-rw---- 1 root disk     43, 144 Aug 23 03:24 nbd9
root@backup:~# 

In our chase, it is nbd0p1

Let us mount it with

mount /dev/nbd0p1 /mnt

Now you will be able to see files in folder /mnt

If you are done with with image, you can unmount it with

umount /mnt

Lets remove the device mapping with

qemu-nbd -d /dev/nbd0

Example

root@server2:~# modprobe nbd
root@server2:~# qemu-nbd -c /dev/nbd0 Fedora-Cloud-Base-26-1.5.x86_64.qcow2 
root@server2:~# mount /dev/nbd0p1 /mnt
root@server2:~# cd /mnt
root@server2:/mnt# ls -la
total 84
dr-xr-xr-x. 18 root root  4096 Jul  5 17:47 .
drwxr-xr-x  22 root root  4096 Aug 23 03:57 ..
lrwxrwxrwx.  1 root root     7 Feb 10  2017 bin -> usr/bin
dr-xr-xr-x.  6 root root  4096 Jul  5 17:49 boot
drwxr-xr-x.  2 root root  4096 Jul  5 17:45 dev
drwxr-xr-x. 71 root root  4096 Jul  5 17:50 etc
drwxr-xr-x.  2 root root  4096 Feb 10  2017 home
lrwxrwxrwx.  1 root root     7 Feb 10  2017 lib -> usr/lib
lrwxrwxrwx.  1 root root     9 Feb 10  2017 lib64 -> usr/lib64
drwx------.  2 root root 16384 Jul  5 17:45 lost+found
drwxr-xr-x.  2 root root  4096 Feb 10  2017 media
drwxr-xr-x.  2 root root  4096 Feb 10  2017 mnt
drwxr-xr-x.  2 root root  4096 Feb 10  2017 opt
drwxr-xr-x.  2 root root  4096 Jul  5 17:45 proc
dr-xr-x---.  2 root root  4096 Jul  5 17:50 root
drwxr-xr-x.  2 root root  4096 Jul  5 17:45 run
lrwxrwxrwx.  1 root root     8 Feb 10  2017 sbin -> usr/sbin
drwxr-xr-x.  2 root root  4096 Feb 10  2017 srv
drwxr-xr-x.  2 root root  4096 Jul  5 17:45 sys
drwxrwxrwt.  7 root root  4096 Jul  5 17:50 tmp
drwxr-xr-x. 12 root root  4096 Jul  5 17:47 usr
drwxr-xr-x. 19 root root  4096 Jul  5 17:47 var
root@server2:/mnt# cd
root@server2:~# umount /mnt
root@server2:~# qemu-nbd -d /dev/nbd0
/dev/nbd0 disconnected
root@server2:~# 

Posted in Virtualization

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.