PDA

View Full Version : mount a disk image (made with dd) + LVM



nt4cats
19th January 2009, 09:50 PM
I like to do fresh installs when I 'upgrade' from one Fedora release to the next. That way any hacks or workarounds that I made in the prior release don't get in my way. I'm also enough of a geek that I pretty much upgrade on release day every release (since Fedora Core 3).

Prior to doing a fresh install I take a drive image -- so if the new install doesn't go well I have an easy roll-back path. When F10 came out, I booted to a Live CD image, attached a big external hard drive and did some variation of 'dd if=/dev/sda of=/mnt/usb-disk/f9-disk.img bs=8M'.

In the past I also did a file-level copy (using rsync) for easy access. This time I didn't have enough disk space on the external drive for both the drive image AND a file-level copy, so I decided I'd just do the image -- figuring I could just mount it if I needed to get files.

This gives me a nice 200G image file of every bit that was on my hard drive before I wiped it.

I want to grab a few files off of the old drive image, but I can't figure out how to mount this image. If this were an external USB device with LVM on it, I know how to 'discover' the PV/LV stuff and mount it. I know how to use the mount command to mount a file system -- but this isn't a file system.

I guess I need a way to make this .img file appear to be a block device so that the pvchange and lvscan commands can see it.

Additional info: I always use the "create default layout" option for my disks (small boot part'n, LVM for data and swap) when doing an install -- EXCEPT that I go in and edit the volume names to include the Fedora Release number. That way there will not be a conflict with my current volume names if I try and access the disk image.

I think I was a bit too clever for my own good here ...

nt4cats
19th January 2009, 09:54 PM
... or maybe this is a better way to put my question:

If this was an external USB disk, I'd attach it, and the magic gnomes and fairies in my computer would work together to make this USB disk available as /dev/sdc.

How do I tell the magic gnomes and fairies to present the data at /mnt/usb-disk/f9-disk.img as /dev/sd[X]?

sideways
19th January 2009, 11:02 PM
I haven't got the image file to test this atm, but I recall that you need to run kpartx on the image file before you run vgscan


kpartx -av /mnt/usb-disk/f9-disk.img
vgscan

nightmarcus
20th January 2009, 12:05 AM
Try mounting the file like you would mount a normal file system. I made a .img of my Windows hard drive for backups at one point and I just mounted it with the normal mount -t ntfs blah blah blah and it worked for me. Just in the place of where you would put the source device you're trying to mount, put the path to the img file. Example that I used in my situation was I believe this:


modprobe loop
mount -o loop -t ntfs ./<filename>.img /mnt/<some folder>

nt4cats
20th January 2009, 12:15 AM
Nightmarcus:

If this was a dd of a partition with a filesystem on it, then your command would work perfectly.

There are two problems:

1) This is a dd of a disk (lower-level than the partition) -- it is a dd of /dev/sda, NOT a dd of /dev/sda1
2) The partition with the data on it is managed with LVM, so even if I had direct access to the partition (which I will shortly) I have to make the LVM infrastructure aware of the physical and logical volumes stored in the image -- and THEN I can get to the ext3 filesystem using mount.

nt4cats
20th January 2009, 01:18 AM
Okay, I have it. Thanks to sideways who gave me the last "missing link" in the chain.

Here is what my machine looks like normally:

[root@cylon ~]# df -mlh
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroupAlpha-LogVolCylonRoot
178G 91G 79G 54% /
/dev/sda1 190M 30M 151M 17% /boot
tmpfs 2.0G 76K 2.0G 1% /dev/shm
[root@cylon ~]# ls -l /dev/mapper/
total 0
crw-rw---- 1 root root 10, 63 2009-01-19 18:43 control
brw-rw---- 1 root disk 253, 0 2009-01-19 18:43 VolGroupAlpha-LogVolCylonRoot
brw-rw---- 1 root disk 253, 1 2009-01-19 18:43 VolGroupAlpha-LogVolCylonSwap

Okay, so plug in my external USB drive ...

[root@cylon ~]# df -mlh
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroupAlpha-LogVolCylonRoot
178G 91G 79G 54% /
/dev/sda1 190M 30M 151M 17% /boot
tmpfs 2.0G 76K 2.0G 1% /dev/shm
/dev/sdb1 459G 360G 76G 83% /media/disk

... and here is the disk image I created with dd:

[root@cylon ~]# ls -l /media/disk/fc9-cylon/fc9-disk.dd
-rw-r--r-- 1 root root 200049647616 2008-11-25 23:03 /media/disk/fc9-cylon/fc9-disk.dd

First: create a loop device. This maps the bits in the file to a block device under /dev/.

[root@cylon ~]# losetup -f /media/disk/fc9-cylon/fc9-disk.dd
[root@cylon ~]# losetup -a
/dev/loop0: [0811]:1007622 (/media/disk/fc9-cylon/fc9-disk.dd)

Second (thanks Sideways!): read the partition table from the new block device and create device mappings for the partitions:

[root@cylon ~]# kpartx -a -v /dev/loop0
add map loop0p1 (253:2): 0 401562 linear /dev/loop0 63
add map loop0p2 (253:3): 0 390315240 linear /dev/loop0 401625
[root@cylon ~]# ls -l /dev/mapper
total 0
crw-rw---- 1 root root 10, 63 2009-01-19 18:43 control
brw-rw---- 1 root disk 253, 2 2009-01-19 18:51 loop0p1
brw-rw---- 1 root disk 253, 3 2009-01-19 18:51 loop0p2
brw-rw---- 1 root disk 253, 0 2009-01-19 18:43 VolGroupAlpha-LogVolCylonRoot
brw-rw---- 1 root disk 253, 1 2009-01-19 18:43 VolGroupAlpha-LogVolCylonSwap

LVM will kick in magically and recognize a new physical volume on /dev/mapper/loop0p2 (the 2nd partition on the loop0 device). Apparently "/dev/dm-3" is an alias for the 3rd device mapping in /dev/mapper. If the magic doesn't happen you can trigger it by typing (no quotes) "pvscan".

[root@cylon ~]# pvdisplay
--- Physical volume ---
PV Name /dev/dm-3
VG Name VolGroupPrimary
PV Size 186.12 GB / not usable 23.61 MB
Allocatable yes
PE Size (KByte) 32768
Total PE 5955
Free PE 1
Allocated PE 5954
PV UUID vBDvjw-qfvA-tgnp-Xajg-KHb2-fPPz-3iGcix

--- Physical volume ---
PV Name /dev/sda2
VG Name VolGroupAlpha
PV Size 186.12 GB / not usable 23.61 MB
Allocatable yes
PE Size (KByte) 32768
Total PE 5955
Free PE 1
Allocated PE 5954
PV UUID G02Ic7-BfPl-71fP-6WZl-PEDB-RmcK-0gxHLe
Now we need to activate the LVM volume group:

[root@cylon ~]# vgchange -a y
2 logical volume(s) in volume group "VolGroupPrimary" now active
2 logical volume(s) in volume group "VolGroupAlpha" now active
[root@cylon ~]# ls -l /dev/mapper
total 0
crw-rw---- 1 root root 10, 63 2009-01-19 18:43 control
brw-rw---- 1 root disk 253, 2 2009-01-19 18:51 loop0p1
brw-rw---- 1 root disk 253, 3 2009-01-19 18:51 loop0p2
brw-rw---- 1 root disk 253, 0 2009-01-19 18:43 VolGroupAlpha-LogVolCylonRoot
brw-rw---- 1 root disk 253, 1 2009-01-19 18:43 VolGroupAlpha-LogVolCylonSwap
brw-rw---- 1 root disk 253, 4 2009-01-19 18:54 VolGroupPrimary-LogVolRootPartn
brw-rw---- 1 root disk 253, 5 2009-01-19 18:54 VolGroupPrimary-LogVolSwapPartn
Now we are finally ready to mount it!

[root@cylon ~]# mkdir /mnt/old-disk
[root@cylon ~]# mount /dev/mapper/VolGroupPrimary-LogVolRootPartn /mnt/old-disk/

When you're done, here is how to shut everything down cleanly ...

[root@cylon ~]# umount /mnt/old-disk
[root@cylon ~]# vgchange -a n VolGroupPrimary
0 logical volume(s) in volume group "VolGroupPrimary" now active
[root@cylon ~]# kpartx -d /dev/loop0
[root@cylon ~]# losetup -d /dev/loop0