3

How do I use dd to copy the first 2 partitiions of /dev/sda (windows reserved, window7) to a file on /Dev/sdb?

I think I can cd to /media/sam/1TB-NTFS/{ a folder I create } and run dd from there:

dd if=/dev/sda of={the folder created}/filename` 

but I'm having trouble figuring out how many bytes to copy from sda so when I restore it I don't step on sda3.

................. results of fdisk ...............

am@Homebuilt:~$ sudo fdisk -l
[sudo] password for sam:      
Disk /dev/sda: 465.76 GiB, 500107862016 bytes, 976773168 sectors
Disk model: WDC WD5000AZLX-0
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xc1f6d562

Device     Boot     Start       End   Sectors   Size Id Type
/dev/sda1            2048    206847    204800   100M  7 HPFS/NTFS/exFAT
/dev/sda2          206848 585312209 585105362   279G  7 HPFS/NTFS/exFAT
/dev/sda3  *    585312256 586362879   1050624   513M ef EFI (FAT-12/16/32)
/dev/sda4       586364926 976771071 390406146 186.2G  5 Extended
/dev/sda5       586364928 976771071 390406144 186.2G 83 Linux

Partition 4 does not start on physical sector boundary.

Disk /dev/sdb: 931.51 GiB, 1000204886016 bytes, 244190646 sectors
Disk model: EZEX-00BN5A0    
Units: sectors of 1 * 4096 = 4096 bytes
Sector size (logical/physical): 4096 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x000e72ed

Device     Boot Start       End   Sectors   Size Id Type
/dev/sdb1         256 244189951 244189696 931.5G  7 HPFS/NTFS/exFAT

.........................................................................

204800 + 585105362 = 585310162 total sectors for sda1 + sda2 @ 512 byts/sector = 
299678802944 (bytes)/ 1024 = 292655081 1k blocks
299678802944 (bytes)/ 2048 = 146327540.5 2k blocks

something isn't right here, the # of bytes should be evenly divisible by 2048,4096,8192 ....etc should it not?

roaima
  • 107,089
  • 14
  • 139
  • 261
user256282
  • 31
  • 1
  • sorry, copy/paste of fdisk -l not very readable and formatting of my post did not survive making it also hard to read. My first Q here so I don't know how that happened. – user256282 Apr 24 '23 at 05:14
  • 2
    I reformatted this posting, so you can see how to do this part. See https://unix.stackexchange.com/editing-help for further help on formatting. – Philippos Apr 24 '23 at 05:30
  • I don't really understand the problem: You start `dd /dev/sda1 of=sda1.img` without a `count` attribute, so the whole partition gets copied to the file, then the same thing with sda2. When copying back, `dd` stops at the end of the file, so nothing will go wrong, no matter how the disk is formatted. – Philippos Apr 24 '23 at 05:36
  • 1
    So, you want to `dd` the whole disk to a file and restore only the first 2 partitions to the disk? Either `dd` the partitions separate (easy approach) or calculate manually. The sector size of `sdb` has nothing to do with that. Sector count of `sda` has to be divideable by 512 as it's the logical sector size. – stoney Apr 24 '23 at 05:51
  • Use the partition as input, /dev/sda1 instead of /dev/sda and you don't have to worry about the start and the size – user253751 Apr 24 '23 at 14:04

1 Answers1

6

You don't need to use dd at all. Ensure you're root:

sudo -s

And then just copy the partitions

cat /dev/sda1 >/media/sam/1TB-NTFS/sda1.img
cat /dev/sda2 >/media/sam/1TB-NTFS/sda2.img

Here there's no need to calculate any offsets for /dev/sda as the kernel has already done that by exposing the individual partitions.

I've used cat instead of dd because for a simple cases like this it's better to use a simpler tool. (There are many options for dd but so many people misunderstand and misuse them that in general it's safer, more reliable, and often more efficient to steer away from it completely.)

If you want an ongoing status report of progress then use pv instead of cat.

roaima
  • 107,089
  • 14
  • 139
  • 261
  • 2
    What do you suppose to be the advantage over using `dd` on each partition? – Philippos Apr 24 '23 at 06:27
  • 5
    dd vs cat -- is dd still relevant these days? https://unix.stackexchange.com/questions/12532/dd-vs-cat-is-dd-still-relevant-these-days – Z0OM Apr 24 '23 at 06:38
  • 5
    What's the difference between dd and cat for writing image files? https://superuser.com/questions/944603/whats-the-difference-between-dd-and-cat-for-writing-image-files – Z0OM Apr 24 '23 at 06:39
  • @Philippos answer updated to answer your question – roaima Apr 24 '23 at 07:07
  • @BlockchainOffice answer updated to answer your question – roaima Apr 24 '23 at 07:07
  • 5
    @roaima | I didn't have a question just wanted to add information to your answer ^^ – Z0OM Apr 24 '23 at 07:27
  • I thought I needed (or it would be better) to copy sda1,2 as 1 unit. I think I see why nothing above 1024 divides evenly (the size of the partitions). dd with block size 1024 would take a long time anyway perhaps defeating the purpose of only copying sda1,2. I'm still curious about how many bytes I would have had to copy, 299678802944 ? So 204800 includes the 4 sectors before it? Just so I understand, the output of cat is copied by > to /path/filename and compressed because it was called an .img file, right? So how to restore to original location? – user256282 Apr 25 '23 at 14:08
  • @sam1951, "_output of cat is copied by `>` to `/path/filename` and compressed because it was called an .img file_" - why would it be compressed? `cat` doesn't compress, it con_cat_enates (copies) one or more files into an output. The shell understands `> file` as meaning "_send the output of the command to `file`_". The `cat` command doesn't see any of that. You can put any suffix you like on the filename, or none at all, but I chose `.img`. – roaima Apr 25 '23 at 14:32
  • So cat /path/filename >/dev/sda1 and cat /path/filename >/dev/sda2 would restore them to the correct place on sda? – user256282 Apr 25 '23 at 14:45
  • @sam1951 yes, exactly. You'll need to be root (`sudo -s`) to write to a device. Double double check (i.e. check really carefully) that you will be writing to the correct device. Getting it wrong will **completely destroy** any data already on the target, with absolutely no hope of recovery – roaima Apr 25 '23 at 14:49
  • Also dd if=/path/filename of=/dev/sda1(sda2) would restore to correct place, not step on sda3 so disk will again boot? – user256282 Apr 25 '23 at 14:54
  • Don't use `dd`, @sam1951 – roaima Apr 25 '23 at 15:02