dd command

Non release banter
User avatar
Ed_P
Contributor
Contributor
Posts: 8341
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

dd command

Post#1 by Ed_P » 15 May 2019, 05:28

Ok, my hard drive is failing on my netbook and I am trying to copy it to a another, used, hard drive. Do I need to fdisk the used hard drive before running the dd copy to it?

Code: Select all

dd if=/dev/sda of=/dev/sdb conv=noerror status=progress
Ed

User avatar
ncmprhnsbl
DEV Team
DEV Team
Posts: 3924
Joined: 20 Mar 2012, 03:42
Distribution: v5.0-64bit
Location: australia
Contact:

dd command

Post#2 by ncmprhnsbl » 15 May 2019, 06:43

Ed_P wrote:
15 May 2019, 05:28
Do I need to fdisk the used hard drive before running the dd copy to it?
as in format it? i don't think so..
this https://serverfault.com/questions/4906/ ... oning#4912 page appears to have a lot of useful info
Forum Rules : https://forum.porteus.org/viewtopic.php?f=35&t=44

nanZor
Shogun
Shogun
Posts: 381
Joined: 09 Apr 2019, 03:27
Distribution: Porteus 5.01 x86-64 LXQT

dd command

Post#3 by nanZor » 15 May 2019, 09:41

The funny thing about dd is that most examples reflect the IBM OS/360 job control language from whence it came.

How about we treat it like a unix command: ...everything is file right? Try this next time you dd a drive

dd < inputfile > outputfile

Ok, I'll stop being a smart-a** now. :) Impress your hacker friends!
That's a UNIX book - cool. -Garth

User avatar
Ed_P
Contributor
Contributor
Posts: 8341
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

dd command

Post#4 by Ed_P » 15 May 2019, 14:58

Well, after running all night the output drive looks exactly like it did went I went to bed. :o But the dd command says it wrote 80GB.

Code: Select all

root@porteus:/home/guest# ./dd.sh

brw-rw---- 1 disk 8, 0 May 14 15:09 /dev/sda
brw-rw---- 1 disk 8, 16 May 15 00:33 /dev/sdb
Copy sda to sdb?

80025954816 bytes (80 GB, 75 GiB) copied, 20533 s, 3.9 MB/s    
dd: writing to '/dev/sdb': No space left on device
156301489+0 records in
156301488+0 records out
80026361856 bytes (80 GB, 75 GiB) copied, 20533 s, 3.9 MB/s
root@porteus:/home/guest# 
dd.sh

Code: Select all

#!/bin/bash

# Color definitions               # ups.sh
txtbld=$(tput bold)               # Bold
txtred=${txtbld}$(tput setaf 1)   # Bold Red
txtgreen=${txtbld}$(tput setaf 2) # Bold Green 
txtcyan=${txtbld}$(tput setaf 6)  # Bold Cyan
rst=$(tput sgr0)                  # Reset

function redpswd() {
  echo -E "$1" $txtred  
}
function cyan() {
  echo -E "$1" $txtcyan
}

if [ `whoami` != "root" ]; then
   cyan "Enter root's password"
   su -c "sh $0 $1"
   exit
fi
echo $rst

ls -g /dev/sda
ls -g /dev/sdb

echo Copy sda to sdb?
read

dd if=/dev/sda of=/dev/sdb conv=noerror status=progress
-update-

Ok, things just got stranger. I unplugged the USB drive and replugged it in. The sda3 drive appears to have gotten copied and the sda2 but the sdb2 can't be accessed. :%)

The problem is the input drive has 5 partitions. The 1st 2 are system, Acer and Windows recovery, the 3rd is my main Windows c: drive, the 4th is my data d: drive and the 5th is my backup e: drive. I was hoping dd would copy everything, including mbr, but now it seems I may have to copy each partition individually?


-update-

Revised script. Copy a hdd to a new ssd drive (Post by Ed_P #80738)
Last edited by Ed_P on 31 Dec 2020, 22:02, edited 2 times in total.
Reason: Added an update.
Ed

User avatar
ncmprhnsbl
DEV Team
DEV Team
Posts: 3924
Joined: 20 Mar 2012, 03:42
Distribution: v5.0-64bit
Location: australia
Contact:

dd command

Post#5 by ncmprhnsbl » 15 May 2019, 23:17

Ed_P wrote:
15 May 2019, 14:58
but now it seems I may have to copy each partition individually?
yes, it would seem so..
check here: https://www.techrepublic.com/blog/linux ... s-with-dd/
If you wanted to duplicate an existing drive to another, you would obtain a drive of the same (or larger) size. Assuming the drive to copy is /dev/sda and the destination drive is /dev/sdb, first use fdisk to recreate the appropriately-sized partitions, then use dd to do the actual cloning:

# sfdisk -d /dev/sda | sfdisk /dev/sdb

# fdisk -l /dev/sda; fdisk -l /dev/sdb

Compare the output of the two fdisk commands and make sure the partitions on /dev/sdb match those on /dev/sda. Once this is done, you can copy each partition using:

# dd if=/dev/sda of=/dev/sdb bs=446 count=1

# dd if=/dev/sda1 of=/dev/sdb1

# dd if=/dev/sda2 of=/dev/sdb2

...

The first dd call copies the MBR from the first disk to the second. This will allow the second disk to be booted, when it replaces the first. The first 446 bytes are copied with this command; that is the boot code we need.
although here: https://wiki.archlinux.org/index.php/Dd ... nd_restore seems to state that dd can clone multiple partitions in one go..
a key point is that the destination drive must be the same or larger size..
Forum Rules : https://forum.porteus.org/viewtopic.php?f=35&t=44

User avatar
Ed_P
Contributor
Contributor
Posts: 8341
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

dd command

Post#6 by Ed_P » 16 May 2019, 03:39

Thank you ncmp. :beer:
ncmprhnsbl wrote:
15 May 2019, 23:17
a key point is that the destination drive must be the same or larger size..
Yes, that appears to be the case. :(
Ed

Post Reply