Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


How to restore .img file with partition on it?
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

How to restore .img file with partition on it?

Hello, I need this as fast way to reinstall/restore OS I create/test on virtual environments.
I convert this image .qcow2 to raw .img file using command

qemu-img convert -f qcow2 -O raw my-os.qcow2 my-os.img
When I checked img file using fdisk I got this

fdisk -lu  my-os.img
Disk my-os.img: 8 GiB, 8589934592 bytes, 16777216 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x2c54c34d

Device     Boot  Start      End  Sectors  Size Id Type
my-os.img1  *      2048   206847   204800  100M  7 HPFS/NTFS/exFAT
my-os.img2       206848 16775167 16568320  7.9G  7 HPFS/NTFS/exFAT

I was planning to restore it on the other virtual machine (using Live Linux CD) using dd command.

dd if=my-os.img of=/dev/vda1

But obviously this won't work, since the img file had two partitions.
So should I restore each both .img1 .img2 file to separated partition (e.g. sda1 and sda2) ?

Comments

  • alt_alt_ Member
    dd if=my-os.img1 of=/dev/vda1
    dd if=my-os.img2 of=/dev/vda2
    

    I am not sure, but this probably works.

    Thanked by 1JustPfff
  • stonedstoned Member
    edited March 2023
    $  losetup --partscan --find --show my-os.img
    /dev/loop20
    
    # Inside loop20 would be your partitions by p1, p2, etc.
    $  ls /dev/loop20*
    /dev/loop20p3  /dev/loop20p2  /dev/loop20p1
    
    # Mount them like this
    $ sudo mount /dev/loop20p3 /mnt
    
    # Or copy them using dd to somewhere else
    $ dd if=/dev/loop20p3 of=/dev/vda3
    
    

    But perhaps instead of all that, just write the my-os.img to /dev/vda (not a partition number)
    dd if=my-os.img of=/dev/vda

    Entire disk structure would be copied to the block device, including any partitions it may have inside.

    Thanked by 1JustPfff
  • @stoned said: $ losetup --partscan --find --show my-os.img

    Thanks for reply, I reach similar idea to use losetup or kpartx, unfortunately my other VPS runs on OpenVZ ( which I kept the backup of .img file ) ,
    I'll try to run it on my laptop (Arch Linux) and do the test again.

Sign In or Register to comment.