19/04/2008

HowTo mount a partition image

Ok, just did a backup of a whole partition, changed the partition size and right after something went wrong - so how to restore the image?

In my case I extended an Ext3 Partition because of disk space issues. Now there is a problem, if I did dd the image (see the previous post in this blog) back to the partition, it would be exactly the same size as I created it, which I do not want to have. I would actually like to copy the CONTENT of the image to the new partition. Here is how to do this:

Uncompress the image (if it was compressed) via the command
gunzip /pathtoyourfile/file

If you stored the file on an external harddrive, I recommend to use a (k/x)ubuntu live cd. By opening a terminal and commanding sudo -s you will get a root console to do anything you want.

Now create a mountpoint whereever you want and then mount the uncompressed image as root (sudo) in the shell commanding:
mount -t ext3 -o loop,ro /pathtoyourfile/filename.img /mountpointyouwanttouse

The option loop will create a virtual device mimicing a block device for your image, the option ro will mount your image in read-only mode (to avoid accidental deletion of anything!)
As you see you can specify the file system, so this will work for all known file systems, e.g. cd/dvd iso images or ntfs images. Here's the command for mounting a CD/DVD iso image:
mount -o loop -t iso9660 /pathtoisofile/filename.iso /mountpointyouwanttouse

Now you can access the content of your image and e.g. copy it via cp or get just single files.

To restore the whole content of a linux partition you might want to use the command
cp -r -p -P /pathtoyourimage/* /pathtomountpointofyourLinuxInstallation/


For explanation: -r will do it recursively for you, -p will preserve the owner information, mode and so on (otherwise everything would belong to root afterwards!!) and -P will tell cp not to follow symbolic links (due to that recursion you would otherwise write your installation several times until the partition is out of free space!)

No comments: