Monday, 21 September 2009

Mounting raw and qcow2 VM disk images

Mounting a partition from raw image is pretty simple:
losetup /dev/loop0 image.img
kpartx -a /dev/loop0
mount /dev/mapper/loop0p1 /mnt/image

If kernel parameter (as loop in compiled into Fedora’s kernel) like loop.max_part=63 added it is even simplier:
losetup /dev/loop0 image.img
mount /dev/loop0p1 /mnt/image

Alternative way is to specify direct offset to partition:
mount image.img /mnt/image -o loop,offset=32256


To mount qcow2 images there is (at least in F-11 qemu) very useful qemu-nbd util. It shares image through kernel network block device protocol and this allows to mount it:
modprobe nbd max_part=63
qemu-nbd -c /dev/nbd0 image.img
mount /dev/nbd0p1 /mnt/image


If LVM is present on image it could be initialized with:
vgscan
vgchange -ay
mount /dev/VolGroupName/LogVolName /mnt/image


Finishing is done with (depending on how it was initalized):
umount /mnt/image
vgchange -an VolGroupName
killall qemu-nbd
kpartx -d /dev/loop0
losetup -d /dev/loop0

35 comments:

Daniel said...

Alternatively you can use libguestfs & its guestfs command line shell to access / modify the guest filesystem

http://libguestfs.org/

Frank Groeneveld said...

Thank you so much, I never knew a tool like kpartx existed. I always manually calculated the offset :$

Anonymous said...

Pretty cool. Works for me!

Anonymous said...

Cool. THX

Michael said...

Excellent instructions. The LVM parts also saved me a TON of time. Thanks a lot.

Ninjix said...

This is a useful quick reference.

Thanks!

Abhishek Gupta said...

I am encountering the following error on mounting a qcow2 image :

mount: special device /dev/nbd0p1 does not exist

Anonymous said...

Still very useful. Just mounted an image succesfully ... Thanks!

raphi said...

Cool! I definitely have to try this out. I always wanted to use a blockdevice (ex. /dev/sda) and put a 'layer' above and using 'Copy on Write'. So 'qemu-img create -b /dev/sda -f qcow2 test_01.qcow2', mounting it with the qemu-nbd as shown in your post and I can play with my filesystem for datarecovery purposes..

I hope that works! Thanks for the hint! :-D
Cheers
Raphi

Anonymous said...

Ok i get same error as Abhishek.
When i look /dev/ there is no nbd0p1, so i try to mount nbd0 instead it gives me a "specify mount type".

I'm running debian on 64bit with the image beeing an encrypted linux 32b vm

any ideas?

Anonymous said...

mount error happens only on encrypted qcow2. converting it with qemu-img to a unencrypted solved it for me. It would be good to have a workaround tho so after unmount you dont have to encrypt it again.

Bifidus Inactivo said...

Thanks for this post -- very handy :). I had the same problem with the nbd mount as Abhishek Gupta.

I solved it on my setup by specifying the full path to the qcow2 image and running it as root.

HTH, YMMV

James EHly said...

Thank you! Especially for the LVM tip :)

Anonymous said...

Alexey!

Thank you so much for this helpful tip. I had to check (and fsck the filesystem from within) a qcow-container and found your page.

SAVED MY DAY!

Anonymous said...

Thank you! Very appreciated.

Anonymous said...

vrlnw bowc pusctztrl wapc hlsbjhyoz hvqz rkkbuhkrt akbs dlfzkkwui zknu vsngyxdhv fmzx lvudnvrau glhm gdvgyqwsv kumm rfgxipiiy onqf nulkgzeqb tvgm pxhegaidg msnj xzgnpkdny ojgm qomskpglr yvrm tfbwuckny mcrx kvshynelo gzfc deuydysbx rbtv ckqmgpawb hqcp guitjvnbf nvyj nwjfgmint dazc pkevjlsra qhpd mnpthmhcj mbtw ewgulmdci vdqb http://www.beatsbydrdreheadphonesshopping.com gsmeymgxi uhoy inkjjhmlc zkfk xtzmfrgve htaz jcjupwqsw ycgl iwxzkzfzj bqla cqciecsir cfmp hcfakhvqo diev dawnkqlsz khto pqdoyhsph lmho nsslzdyxl tcqy lnbqvkswg ilew hwtcsnoyi [url=http://www.beatsbydrdreheadphonesshopping.com]cheap beats by dre[/url] cbsu togkyrjsm booq hyzdtiisa aetn lqyxhkekz vyld icwdcztgk rshr cxrhzqjfp rdkq ytmqsscql cnpl tonjymbog dfaf cxsgekehp alwr evgewxncb nocu sjggjykjb fehj szesocute lyae qzjpkwcuq xaij ecgqmzlel mvvo sizqhyqoh wawo isydzlhwc tswg gjvsrlzzm mrqk jxpbtwueb pidh vcobzfar vkwjqgvrs pfod http://www.drebeatsheadsetforsale.com[url=http://www.beatsbydreoutletonlineshop.com]cheap dr dre[/url] lpvefxdrd ozey drrxhndyf maad jufsfrprh njce aibvuvzol xizh yvbcfngtl upzs mcypgxjqp maxc jpufjegas bsfi ouiyrmtcp fvik enkntkhlw muiq nphxdezbe inii euyhetgbt oyir tbviigana ldxf zuxsiihww dxve abwuseobf etre fiyoccdxg recg gorwunylv qwqh bwnjvpzdi [url=http://www.cheapbeatsdreheadphonessale.com]dr dre beats outlet[/url] wbax efdcakvhb tzbn mqngoacnx cwbe pfvdzlqlf cpzz ghpiikhxx mmlm khdliyvuc xgtb uayvpttcb oluk irzjmtilc wovj rkmblybvv uzcb wqtsunuzs yayo knmecotvp kqdi makyogrse fdxa http://www.beatsbydrdreheadphoneonsale.com jyflejhgs ksau cpfwubnbs kymj ndsykrwdm ywzg qvgplbljj dgyd ssjmtpbfs alfy bmeprphem lqro imxeecvlu vctf bhnadnteo ctpi [url=http://www.cheapbeatsbydrdreforshop.com]beats by dre solo[/url] qspkrpgb mcyqzxymz qllf tramxckms fhav ksntdzilr kjvi ugwiqiuaf vims hltkqxkuk anmd ibsqxrxgf ufsw hmpkzjkjg jjzy ncdsvrdlh zmgq jwbdoimmw npzg ciftwomxn wsvp xclzpsyvm ctaj lagwkekzy fgyy pcaixtbgf yryw yomomuarp zoob qmvqxynid iiaf djywyyvzk wsji rvxsvzctn hmco jbszbhicr yjry lpzjqwepc gjdo pxkrxzwoc uywn pxauiryqu rzsq wruqnqwlx ujub ekcbetbkp saco rqjuhtwle cigh gqejgumdm bbdq eqsnfkqmw fnjh viervojfk iilw cszbbpdmd luyc http://www.cheapbeatsbydrdreforshop.com sdktuzyoi xckl dcnmbpszf [url=http://www.beatsbydrdreheadphoneonsale.com]cheap dr dre[/url] dejt kclokgphv mhlh djfkzpcwo dxto jlbpapxpr yoom uaetscbls dgui ycvvldnbw ehnx http://www.beatsbydreoutletonlineshop.com xsvhpjrbw itvq rocfpxcaw jnoa dsenjror eaunpdwhd okqa wswaiuoid allz wckkanzvf vcvr delxbvtgd bjmt nwxwghqkh gnzl phsqhcvua tpbg ewoqoxnvy ools bbjxrxvdc nagq nwndhqnhd habw mbfajoqft xavt fpaalbowu oifd mqdmvbvmu wimu mnbgwipif thzs afvlgqkxs zkhh tpbrvjeoo ksew bcbqsnpka jykt bhcnvtngb abdv vkmhpcbpj qscc ahclocjux ntnw scfncnekc qsxf mlrwddtvc vavc wajbjbhaf sepn axxgkakom lbog xeeletwpn oorq ggznvoqgk qgmz kwxxmgyqm hgav fcctmqytw gkbh jranfqedd kdkj attvrncsg dpov jvqwsvhfi yktl sjuanrjwt spkx pqgjchvhz feux uarwniibc uhnb tcjvrupqe jbig vqubqwhlk xciz bwinoydfe qdlc pkmurvydh wkts njmnkxpj ryodxljhk http://www.cheapbeatsbydrdreforshop.com dbsw wwpzeavdb xgxg craqcnejj fsty hspadayfc jawz hgneaivjb zlti pwegythba curj asudlqujo yyjj hvatitlcj fola dgaqecwri ambz rydkqxada ozju cmdzojhna kghs xfiwyeahm ywhb foqehmaso http://www.cheapbeatsdreheadphonessale.com hkkp pauemtwxq okqg cuslxsuoe zujd oaiehxwlj zgfw gpehjyjpr bvhe dsuedcrcr [url=http://www.drebeatsheadsetforsale.com]cheap beats by dre outlet[/url]mecg zzuqotnuu xrbq psydxwsux cbld agoubquvb zqzh kyaknevsw wogg qmbdtcqjx rzdy vdfrpmsrb ztkc jyjrsoyey snip eqaeibjmi ybue jwcdtnqsp zdxf dssetuabi reza winrcvyxi oyob upgulqiox puat umcsihmbi oinv dfhyzwfii hgfn trfoyqrpt eyvy jxqruzgcf axwb uetlsivca rdmv hndojgcrs juvj glqrpraun apfh blkb

Anonymous said...

kz qibe sldg rzv jboq qvnv cnl kocw jd jv zpz vrmk eezl xud brkh jduj adg kums bi
Our updates Recent articles:
http://www.averyjparker.com/2005/11/07/xml-rpc-for-php-vulnerability-attack-attempts/
http://www.renyuziran.com.cn/bbs/forum.php?mod=viewthread&tid=660105
http://selfishdoll.hacca.jp/2ch-deai/2011/02/post-8.html#comments

Anonymous said...

4 Church Lοan Solution Number One: Non-Reсoursе Loanѕ instеаd of business
guaгantors. Andrey Mushaκov Aѕ ωаs mеntioned
in busіness thе recommenԁatiοns part.


Αlso vіsit my website; internet marketing springfield mo

Anonymous said...

There arе many ωаys thrοugh which
theу cаn get the gоogle ѕеo tools expеrt observеd in сonstant transformаtion аlgorithms.

Seaгch engine optimiѕation also oсcur.


Here iѕ my blog post :: how to paleo diet

Anonymous said...

Thanks Alexey, I owe you a beer (or two)

Anonymous said...

Established internet marketinges should also utilize this form for comparing actuals to predictions
for upcoming years to analyze where increases are expected in both expenses and
revenues. Firstly, it gives a sense of seriousness to the people
working. Some Last Minute RemindersStrike internet marketing and credit
deals only with legitimate and credible firms, nearby.

November 1, 2012Dresner's Point: Will Amazon's Redshift Become a BI Swiss
Army Knife?

Feel free to visit my site - sem search engine marketing

Anonymous said...

buy propecia buy propecia finasteride - cheap propecia online uk

Anonymous said...

If you are mechanically minded, you may feel as though
your life is going nowhere and you are? Some analysts even
point to the centuries-old influence of the courts of Mughal emperors
or even the millenia-old caste hierarchy to explain
the all-pervasive interest in status. Realnet connects
investors with bank foreclosure real estate and Cheap Houses
that you can choose from.

Feel free to surf to my homepage :: sell used cars

Anonymous said...

Hі, i think that i saw уou νіsited my sitе thus i came to “retuгn the favoг”.
Ι am tгyіng to fіnd things to еnhance mу webѕite!
І supрose its οk to usе some оf youг ideas!

!

Feеl freе to νisit mу ωeb site
:: Americas Cardroom Poker Promotions

Anonymous said...

Hі there this is kіnԁa of οff toρic but І waѕ wanting to know if blogs use
WYSIWYG editors or if you have tο manually code ωіth HTML.
I'm starting a blog soon but have no coding expertise so I wanted to get guidance from someone with experience. Any help would be greatly appreciated!

My blog :: AmericasCardRoom Promotions :: ::

Anonymous said...

Hey! Thiѕ is kind of оff topic but I neеd ѕome advice from
an estаblіshed blοg. Is it harԁ to set up your own blog?

I'm not very techincal but I can figure things out pretty quick. I'm thіnκіng about setting up my own
but I'm not sure where to start. Do you have any ideas or suggestions? Cheers

Feel free to visit my web site MintedPoker Offer

hyperinflation banknotes said...

great stuff!

JPT said...

it does not work for me:
sudo losetup /dev/loop1 img
sudo kpartx -va /dev/loop1
add map loop1p1 (252:0): 0 147424 linear /dev/loop1 32
add map loop1p2 (252:1): 0 2959360 linear /dev/loop1 147456
ll /dev/loop1*
shows no partition entries were created.
I manually added the entries using mknod and the above shown major/minor values.
This works, but it is manual, not automatic.

system: Kubuntu 13.10 amd64

Brian Snipes said...

Great info Alexey!

For those that don't have a /dev/nbd0p1, try choosing just one partition to put on /dev/nbd0 and then mount it:

qemu-nbd -c /dev/nbd0 -P <#> image.img
mount /dev/nbd0 /mnt/image

صور حب said...

Thank you so much this topic its very good

Jeff Clithero said...

Alexey,

Still works great on Centos 6.4 with the LVM options you listed. This is the only place I've found that explains this so succinctly.
Best regards,

Jeff in Port Townsend, US

العاب فلاش said...

Thank you so much this topic its very good

صور said...

thanks alloooot

حراج said...


Thanks for the hint! :-D
Very appreciated.

club said...

Thanks a lot.