.img file restore to disk partition

Solution 1:

  • If you made the image of the whole drive for example /dev/sdb then you should restore it to a whole drive.

  • But if you made the image of a partition for example /dev/sdb1, then you should restore it to a partition.

You can check for the content of the image file with kpartx. It can see directory structure, if there is one (and not only a file system sitting at the head end of the image). See this example,

Map:

$ sudo kpartx -av t-3.5g.img
add map loop0p1 (253:0): 0 6293372 linear 7:0 503907
add map loop0p2 (253:1): 0 1954 linear 7:0 1953
add map loop0p3 (253:2): 0 500000 linear 7:0 3907

Delete map:

$ sudo kpartx -d template-3.5GB.img

Please notice that the devices for partitions will probably look like this, when seen by other tools:

/dev/mapper/loop0p1
...

Another example,

$ sudo kpartx -av lubuntu-18.04.5-desktop-amd64.iso
add map loop0p1 (253:0): 0 2345472 linear 7:0 0
add map loop0p2 (253:1): 0 4928 linear 7:0 2314176

$ lsblk -f /dev/loop0
NAME      FSTYPE  LABEL                     UUID                                 MOUNTPOINT
loop0     iso9660 Lubuntu 18.04.5 LTS amd64 2020-08-06-23-08-05-00               
├─loop0p1 iso9660 Lubuntu 18.04.5 LTS amd64 2020-08-06-23-08-05-00               
└─loop0p2 vfat                              EC34-71CF                            

$ lsblk -f /dev/mapper/loop0p1
NAME    FSTYPE  LABEL                     UUID                                 MOUNTPOINT
loop0p1 iso9660 Lubuntu 18.04.5 LTS amd64 2020-08-06-23-08-05-00               

When you know what you have, you will also know how to clone the content. But it is also possible to mount a mapped source partition and the target partition and extract files (copy them with rsync or some other program).