Difference between pages "Dd" and "Cp -pr"

From Linuxintro
(Difference between pages)
 
imported>ThorstenStaerk
(New page: The command cp -pr copies files and preserves it attributes. This is useful if you want to copy special files like devices: tweedleburg:~ # cp -pr /dev/sda . tweedleburg:~ # ll sda...)
 
Line 1: Line 1:
[[dd]] is a utility to create a '''d'''isk '''d'''ump by reading every single block on a disk, e.g. your hard drive. However, its architecture is laid out so it can do much more than creating a dump. Here is what dd can do for you:
+
The command
* manage a disk backup
+
[[cp]] -pr
** create a backup from a disk to a file
+
copies files and preserves it attributes. This is useful if you want to copy special files like devices:
** restore a backup from a file to a disk
+
  tweedleburg:~ # cp -pr /dev/sda .
** clone a harddisk
+
  tweedleburg:~ # ll sda
** create a disk image and transfer it over the network
+
  brw-r----- 1 root disk 8, 0 Dec 20 08:39 sda
* create an iso image of a CD
 
* rescue a file that contains bad blocks
 
* analyze your disk by displaying selected [[blocks]]
 
* create your own operating system by dumping your bootloader to the boot sector
 
* benchmark the throughput of your disks
 
 
 
= Disk Backup =
 
 
 
== Create a backup ==
 
Say we have a harddisk /dev/sda that we want to backup entirely (sector-by-sector) to a USB volume /dev/sdb1, mounted on /mnt/sdb1. We call this a ''dump'' or an ''image'' of /dev/sda. The dump shall be named ''backup.img''. Here is the dd command:
 
dd if=/dev/sda of=/mnt/sdb1/backup.img
 
In this command, '''if''' stands for '''i'''nput '''f'''ile and '''of''' for '''o'''utput '''f'''ile.
 
 
 
== Restore a backup ==
 
To restore this backup, we boot from a live CD and do the command vice versa.  
 
'''This can overwrite all content on your harddisk, this is the intention.'''
 
dd if=/mnt/sdb1/backup.img of=/dev/sda
 
 
 
== Clone a harddisk ==
 
To clone a disk A to B, both disks need to have the same capacity. It is very convenient for USB disks. Say our USB disk source is called /dev/sdb and the target is called /dev/sdc. Do it like this:
 
  dd if=/dev/sdb of=/dev/sdc
 
Now if sdc has a bigger capacity, this capacity will be lost because the file system is not aware of it.
 
 
 
== Transfer a disk image ==
 
To transfer a disk image over the network to a computer named ''target'', use
 
dd if=/dev/sdb | ssh root@''target'' "(cat >backup.img)"
 
 
 
= create an iso image of a CD =
 
To create an iso image of a CD, read it block-by-block and save the blocks to a file. In case of an error, dd is not supposed to abort:
 
dd if=/dev/cdrom of=cdimage.iso conv=noerror
 
 
 
= create a disk image =
 
To create an empty image of a harddisk, e.g. for [[qemu]] [[virtualization]]:
 
dd if=/dev/zero of=harddisk.img bs=516096 seek=6241 count=0
 
creates a 3GB-Image. For a 6GB-Image, use seek=12482.
 
 
 
= rescue a file that contains bad blocks =
 
If your favorite movie or song cannot be played any longer because the file is corrupt, you can use dd to ignore the corrupt part:
 
dd if=movie.avi of=rescued_movie.avi conv=noerror
 
 
 
= analyze your disk =
 
With dd you can read any byte from your disk. For example to read the first 40 bytes from your first disk, issue
 
dd if=/dev/sda bs=40 count=1
 
The output will be displayed on the console. It will look about like this:
 
  tweedleburg:~ # dd if=/dev/sda bs=40 count=1
 
3���؎м|��W���������RR�A��U1�1+0 records in
 
1+0 records out
 
40 bytes (40 B) copied, 4.6373e-05 s, 863 kB/s
 
 
 
 
 
To analyze your disk by displaying selected [[blocks]], in this case block 1001 use:
 
dd if=/dev/sdc1 count=1 skip=1000
 
 
 
= Create your own bootloader =
 
To create your own operating system by dumping your bootloader to the boot sector of a bootable disk image use
 
  dd conv=notrunc if=bootloader of=[[qemu]].img
 
 
 
= benchmark the throughput of your disks =
 
To benchmark the throughput of your disk ''/dev/sda1'', e.g. for different [[block size]]s, proceed like this:
 
# dd if=/dev/sda1 of=/dev/null bs=512 count=1000000
 
1000000+0 records in
 
1000000+0 records out
 
512000000 bytes (512 MB) copied, 5.16588 s, 99.1 MB/s
 
# dd if=/dev/sda1 of=/dev/null bs=4096 count=1000000
 
1000000+0 records in
 
1000000+0 records out
 
4096000000 bytes (4.1 GB) copied, 36.0667 s, 114 MB/s
 
However, make sure you have read [[Background:How caching works]] first otherwise you will be surprised by a mysterious accelleration like this:
 
# dd if=/dev/sda1 of=/dev/null bs=512 count=1000000
 
1000000+0 records in
 
1000000+0 records out
 
512000000 bytes (512 MB) copied, 5.32254 s, 96.2 MB/s
 
# dd if=/dev/sda1 of=/dev/null bs=512 count=1000000
 
1000000+0 records in
 
1000000+0 records out
 
512000000 bytes (512 MB) copied, 1.09851 s, 466 MB/s
 
 
 
= get the progress =
 
During a long dd run you may want to know the progress. dd will output it for you if it receives a special signal, USR1. So,
 
* find out the number of SIGUSR
 
  # kill -l
 
  1) SIGHUP      2) SIGINT      3) SIGQUIT      4) SIGILL      5) SIGTRAP
 
  6) SIGABRT      7) SIGBUS      8) SIGFPE      9) SIGKILL    '''10''') SIGUSR1
 
11) SIGSEGV    12) SIGUSR2    13) SIGPIPE    14) SIGALRM    15) SIGTERM
 
16) SIGSTKFLT  17) SIGCHLD    18) SIGCONT    19) SIGSTOP    20) SIGTSTP
 
21) SIGTTIN    22) SIGTTOU    23) SIGURG      24) SIGXCPU    25) SIGXFSZ
 
26) SIGVTALRM  27) SIGPROF    28) SIGWINCH    29) SIGIO      30) SIGPWR
 
31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
 
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
 
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
 
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
 
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8 57) SIGRTMAX-7
 
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
 
* send SIGUSR to all dd [[process]]es:
 
killall -10 [[dd]]
 
The output will read like this:
 
2989696+0 records in
 
2989696+0 records out
 
1530724352 bytes (1.5 GB) copied, 583.336 s, 2.6 MB/s
 
 
 
[[Category:low-level]]
 
[[Category:command]]
 

Revision as of 11:32, 20 December 2008

The command

cp -pr

copies files and preserves it attributes. This is useful if you want to copy special files like devices:

tweedleburg:~ # cp -pr /dev/sda .
tweedleburg:~ # ll sda
brw-r----- 1 root disk 8, 0 Dec 20 08:39 sda