Saturday, May 09, 2020

Linux dd multiple partitions into single image file


For example, USB 1 drive with 3 partitions to be written into a single image file.
List block devices

[root@localhost ddtest]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   1  244M  0 disk
├─sdb1   8:17   1   30M  0 part /run/media/root/datapartition2
├─sdb2   8:18   1   40M  0 part /run/media/root/datapartition
└─sdb3   8:19   1   50M  0 part /run/media/root/datapartition1

/dev/sdb1,2,3 = 30M,40M,50M in size
Umount them

[root@localhost ddtest]# umount /dev/sdb*

Confirm again

[root@localhost ddtest]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   1  244M  0 disk
├─sdb1   8:17   1   30M  0 part
├─sdb2   8:18   1   40M  0 part
└─sdb3   8:19   1   50M  0 part

Fdisk the drive to find its last partition end sector

root@localhost ddtest]# fdisk -l /dev/sdb

Disk /dev/sdb: 255 MB, 255852544 bytes, 499712 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
Disk label type: dos
Disk identifier: 0xf4f4f4f4

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048       63487       30720   83  Linux
/dev/sdb2           63488      145407       40960   83  Linux
/dev/sdb3          145408      247807       51200   83  Linux

Calculate dd count over 1MB with formula below
(<last partition end sector> + 1) / 2048
For this USB we have (247807 + 1) / 2048 = 121

Now dd the whole /dev/sdb to test.iso with count of 121 times of 1MBytes

[root@localhost ddtest]# dd if=/dev/sdb of=test.iso bs=1M count=121 status="progress" conv=sync,noerror
121+0 records in
121+0 records out
126877696 bytes (127 MB) copied, 37.6761 s, 3.4 MB/s

Check the test.iso file size

[root@localhost ddtest]# ls -alh
-rw-r--r--. 1 root root 121M May  8 23:37 test.iso

Test the ISO file by restoring the ISO to another USB drive.
Plug out USB 1 and plug in USB 2 and check lsblk

[root@localhost ddtest]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   1  3.7G  0 disk

Dd the test.iso file to /dev/sdb

[root@localhost ddtest]# dd if=test.iso of=/dev/sdb bs=1M conv=sync,noerror
121+0 records in
121+0 records out
126877696 bytes (127 MB) copied, 25.2433 s, 5.0 MB/s

Check lsblk again

[root@localhost ddtest]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   1  3.7G  0 disk
├─sdb1   8:17   1   30M  0 part
├─sdb2   8:18   1   40M  0 part
└─sdb3   8:19   1   50M  0 part

Now we have 3 partitions restored identical to USB 1
Try to mount and check partition content

[root@localhost ddtest]# mount /dev/sdb1 1
[root@localhost ddtest]# mount /dev/sdb2 2
[root@localhost ddtest]# mount /dev/sdb3 3
[root@localhost ddtest]# ls 1
WinSCP-5.17.5-Setup.exe
[root@localhost ddtest]# ls 2
WinSCP-5.17.5-Setup.exe
[root@localhost ddtest]# ls 3
WinSCP-5.17.5-Setup.exe

Check  lsblk

[root@localhost ddtest]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   1  3.7G  0 disk
├─sdb1   8:17   1   30M  0 part /root/Desktop/temp/ddtest/1
├─sdb2   8:18   1   40M  0 part /root/Desktop/temp/ddtest/2
└─sdb3   8:19   1   50M  0 part /root/Desktop/temp/ddtest/3

It also works with Windows tools such as Rufus...


Or Win32 Disk Imager, but only if the device exists under the drop button circled below


How the partitions look like under Windows Disk Management


Just to add, if you are having missing partition from Gparted you can try to zeroing the 1st sector of 512 bytes (MBR)

[root@localhost ddtest]# dd if=/dev/zero of=/dev/sdc bs=512 count=1
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.00308483 s, 166 kB/s




No comments:

Post a Comment