linux dd copy all partitions,dd 命令详解

dd是Linux/UNIX下的一个非常有用的命令,作用是用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换。

dd的主要选项:

指定数字的地方若以下列字符结尾乘以相应的数字:

b=512, c=1, k=1024, w=2, xm=number m

if=file

输入文件名,缺省为标准输入。

of=file

输出文件名,缺省为标准输出。

ibs=bytes

一次读入bytes个字节(即一个块大小为bytes个字节)。

obs=bytes

一次写bytes个字节(即一个块大小为bytes个字节)。

bs=bytes

同时设置读写块的大小为bytes,可代替ibs和obs。

cbs=bytes

一次转换bytes个字节,即转换缓冲区大小。

skip=blocks

从输入文件开头跳过blocks个块后再开始复制。

seek=blocks

从输出文件开头跳过blocks个块后再开始复制。(通常只有当输出文件是磁盘或磁带时才有效)

count=blocks

仅拷贝blocks个块,块大小等于ibs指定的字节数。

conv=conversion[,conversion...]

用指定的参数转换文件。

转换参数:

ascii转换EBCDIC为ASCII。

ebcdic转换ASCII为EBCDIC。

ibm转换ASCII为alternate EBCDIC.

block把每一行转换为长度为cbs的记录,不足部分用空格填充。

Unblock

使每一行的长度都为cbs,不足部分用空格填充。

lcase把大写字符转换为小写字符。

ucase把小写字符转换为大写字符。

swab交换输入的每对字节。Unlike theUnix dd, this works when an odd number of bytes are read. If

the input file contains an odd number of bytes, the last byte is simply

copied (since there is nothing to swap it with).

Noerror

出错时不停止。

Notrunc

不截短输出文件。

sync把每个输入块填充到ibs个字节,不足部分用空(NUL)字符补齐。

由于dd命令允许二进制方式读写,所以特别适合在原始物理设备上进行输入/输出。例如可以用下面的命令为软盘建立镜像文件:

dd if=/dev/fd0 of=disk.img bs=1440k

有趣的是,这个镜像文件能被HD-Copy,Winimage等工具软件读出。再如把第一个硬盘的前512个字节存为一个文件:

dd if=/dev/hda of=disk.mbr bs=512 count=1

The basic command is structured as follows:

dd

if=of=bs=(some power of

2, not less than 512 bytes(ie, 512, 1024, 2048, 4096, 8192, 16384)

conv=.

Source is the data being read. Target is

where the data gets written. If you mess up, and accidently reverse the

source and target, you can wipe out a lot of data.

Examples::

Copy one hard disk partition to another hard disk:

dd if=/dev/sda2 of=/dev/sdb2 bs=4096 conv=notrunc,noerror

sda2,

sdb2 are partitions. You want to copy sda2 to sdb2. If sdb2 doesn't

exist, dd will start at the beginning of the disk, and create it. Be

careful with order of if and of. You can write a blank disk to a good

disk if you get confused.

Make an iso image of a CD:

dd if=/dev/hdc of=/home/sam/mycd.iso bs=2048 conv=notrunc

CD

sectors are 2048 bytes, so this copies sector for sector. The result

will be a hard disk image file of the CD. You can use "chmod a+rwx

mycd.iso" to make the image writable. You can mount the image with

"mkdir /mnt/mycd", this line in fstab: "/home/sam/mycd.iso /mnt/mycd

iso9660 rw,user,noauto 0 0", save fstab, "mount -o loop /mnt/mycd". Then

the file system will be viewable as files and directories in the

directory /mnt/mycd. You can edit the image as you wish, and the new

file will be "/home/sam/mycd.iso" dd does not write to CD's. You need to

use a burning utility, or the cdrdao command

Copy a floppy disk:

dd if=/dev/fd0 of=/home/sam/floppy.image bs=2x80x18b conv=notrunc

The

18b specifies 18 sectors of 512 bytes, the 2x multiplies the sector

size by the number of heads, and the 80x is for the cylinders--a total

of 1474560 bytes. This issues a single 1474560-byte read request to

/dev/fd0 and a single 1474560 write request to /tmp/floppy.image. This

makes a hard drive image of the floppy, with bootable info intact.

Copy a hard drive image of a floppy to a floppy:

dd if=/home/sam/floppy.image of=fd0 bs=2x80x18b conv=notrunc

Copy just the MBR and boot sector of a floppy to hard drive image:

dd if=/dev/fd0 of=/home/sam/MBRboot.image bs=512 count=2

This copies the first 2 sectors of the floppy

Cloning an entire hard disk:

dd if=/dev/sda of=/dev/sdb conv=notrunc,noerror

in

this example, sda is the source. sdb is the target. Do not reverse the

intended source and target. Surprisingly many people do. notrunc means

to not truncate. noerror means to keep going if there is an error.

Normally dd stops at any error. if you have a question about a hard

drive on whether or not it works, you can try to use it as the source

drive for the dd command. You should get an error if it is not working.

target drives need to be really messed up to give an error in dd.

Copy MBR only of a hard drive:

dd if=/dev/sda of=/home/sam/MBR.image bs=446 count=1

this

will copy the first 446 bytes of the hard drive to a file. If you

haven't already guessed, reversing the objects of if and of, in the dd

command line reverses the direction of the write.

Wipe a hard drive of all data (you would want to boot from a cd to do this)

is a good boot cd

the helix boot environment contains the DoD version of dd called dcfldd. It works the same way, but is has a progress bar.

dd if=/dev/zero of=/dev/sda conv=notrunc

This is useful for getting rid of viruses, DRM trojans and the like.

It would be useful, at this time to interject a tip:

I

have several machines, but on the one I use a lot I have two SATA hard

drives. They are exactly the same. Before I do anything dangerous, I

boot from the helix CD, run

dcfldd if=/dev/sda of=/dev/sdb bs=4096 conv=notrunc,noerror

and

copy my present working sda drive system to the sdb drive. If I wreck

the installation on sda, I just boot with the helix cd and

dcfldd if=/dev/sdb of=/dev/sda bs=4096 conv=notrunc,noerror

Please

note: bs=4096 works fast for machines with at least 128 MB of ram. dd

uses a lot of buffers. At bs=4096, on modern machines, the optimal

transfer rate is reached for hard drives.

To make a file of 100 random bytes

dd if=/dev/urandom of=/home/sam/myrandom bs=1 count=100

Here,

urandom is the linux random byte device. myrandom is a file. Byte size

equals 1 and there are 100 of them. Gpg requires a random seed to

generate keys. Generating a file of say 4096 random bytes, which can be

passed to Gpg, will allow a truly random seed.

Write random data over a file before deleting it:

first do an ls -l to find filesize. In this case it is 3769

ls -l afile

-rw------- ... 3769 Nov 2 13:41 dd if=/dev/urandom of=\ bs=3769 count=1 conv=notrunc

This will write random characters over the entire file.

Copy a disk partition to a file on a different partition. Do not copy a partition to the same partition.

dd if=/dev/sdb2 of=/home/sam/partition.image bs=4096 conv=notrunc,noerror

This

will make a file that is an exact duplicate of the sdb2 partition. You

can substitue hdb, sda, hda, or whatever the disk is called.

Restore a disk partition from an image file.

dd if=/home/sam/partition.image of=/dev/sdb2 bs=4096 conv=notrunc,noerror

This

way you can get a bazonga hard drive and partition it so you can back

up your root partition. If you mess up your root partition, you just

boot from the helix cd and restore the image.

To covert a file to all uppercase:

dd if=filename of=filename conv=ucase

Copy ram memory to a file:

dd if=/dev/mem of=/home/sam/mem.bin bs=1024

The

device /dev/mem is your system memory. You can actually copy any blobk

or character device to a file with dd. Memory capture on a fast system,

with bs=1024 takes about 60 seconds. Copying a 120 GB HDD takes about an

hour. Copying a CD to hard drive takes about 10 minutes. Copying a

floppy to a hard drive takes about 2 minutes. With dd, your floppy drive

images will not change at all. If you have a bootable DOS diskette, and

you save it to your HDD as an image file, when you restore that image

to another floppy it will be bootable. dd is an excellent way to make an

image of MS Windows XP install CD's. When you make a copy of such a cd,

there is one sector that is of nonstandard length. It is the last

sector. dd doesn't pad this sector, making the copy indistinguishable

from the original. If you burn the CD using cdrdao, the resulting disk

will be an absolutely exact copy of the original.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DDCOPY 1.5 版使用说明 功能特点: 程序容量小,速度快;不到40KB的程序可以快速拷贝几十GB的硬盘; 支持8GB以上容量的大硬盘间拷贝; 提供BADCOPY(坏盘拷贝)功能,可以跳过损坏的扇区继续拷贝; 对同一PC机内的多个硬盘进行全盘复制,支持IDE,EIDE,SCSI; 对硬盘的文件系统无限制(FAT32,NTFS,UNIX……); 快速清除硬盘的主引导扇区(MBR); 自动检测所接硬盘参数(柱面数、磁头数、扇区数、容量); 硬盘连接方法: 将硬盘接到计算机,并在CMOS中正确设置硬盘参数; 记下硬盘的排放顺序,以免拷贝时出现错误; 如果条件许可,以下连接方法可以提高20%拷贝速度: 对于 IDE硬盘,将源盘与目标盘分别用两根数据(排)线连接于主板上; 对于SCSI硬盘,将源盘与目标盘分别连接在不同的SCSI控制器上; 通常的连接方法(在CMOS中设置以"C"启动时): IDE硬盘: Primary Master -> 80h Primary Slave -> 81h Secondary Master-> 82h Secondary Slave -> 83h SCSI硬盘: ID=0 -> 80h ID=1 -> 81h ... 程序的运行: DDCOPY [-d|]|[-c|[-s]] [-y] DDCOPY [选项] 源盘标识 目标盘1# 目标盘2# ... Options[选项]: -s: 显示全部或指定硬盘信息 -d: 默认拷贝方式(拷贝PC机内所有硬盘,顺序为80h -> 81h 82h ...) -c: 清除指定硬盘的主引导扇区(MBR) -y: 对所有确认信息以回答“Yes” Samples[示例]: ddcopy -d -y 以默认顺序拷贝所有硬盘 ddcopy -c 81 80 清除指定硬盘的主引导扇区 ddcopy -s 80 81 显示指定硬盘的信息 ddcopy 81 80 82 按照指定顺序拷贝指定硬盘(此处81h为源盘) ==================================================== RouterOS爱好者|ROS爱好者 http://www.126cm.com 收集整理 天偶发现一个十分好用的软件,与大家分享(希望能进精华):      一般我们对系统进行备份,克隆都是用了ghost软件,但在UNIX系统方面,使用GHOST克隆会出现不能正常启动的问题,或者操作方法十分的麻烦。   推荐大家使用DDCOPY软件对UNIX系统进行备份或者双硬盘对拷。DDCOPY采用的是完全物理扇区拷贝方法,而GHOST是识别磁盘上的文件系统,所以只拷贝有效数据,在速度方面ghost是比ddcopy快得多,但ddcopy可以完完全全的把unix系统备份好。我用ddcopy对双硬盘linux7.3对拷,拷贝时间大概花了2个半小时,硬盘大小36G,数据完整是最重要的,时间花得多些也值。   ddcopy特点: (1)彻底的硬盘拷贝 (2)坏伞区拷贝 (3)支持单硬盘容量最大2TB (4)支持多系统 下载地址: http://www.100free.com/ddcopy/html/page2.html (最好使用1.5版本的,我双硬盘对拷时在1.6版本下不成功,在1.5版本下成功了) ddcopy的使用方法:   程序的运行 ddcopy [-d s=m d=d1,dn]|[-p s=m:i d=d1:j,dn] [-v n] [-s] [-c] [-f] [-y] [-h] 命令行参数 -d 设定拷贝模式为硬盘整盘拷贝 s 表示数据源,m可以为硬盘号或映像文件. d 表示目标,dn可以为硬盘号或映像文件,多个目标间用“,” 分隔. -p 设定拷贝模式为分区拷贝 s 表示数据源,m可以为硬盘号或映像文件,i为分区号. d 表示目标,dn可以为硬盘号或映像文件,k为分区号,多个 目标间用“,”分隔. -v 表示如目标为映像文件,则对其分卷存储,n为每卷容量(MB) -c 采用数据压缩方式保存映像文件 -s 显示计算机内所有硬盘的信息 -f 缺省拷贝模式(拷贝计算机内所有的硬盘),源盘为第1硬盘, 目标盘为其余的硬盘。此参数等同于-d s=1 d=2,3, ... -y 对于在运行过程的提示信息均回答Yes -h 显示帮助信息和示例 使用示例 ddcopy -d s=1 d=2,3,4 将第1硬盘上的数据复制到第2、3、4硬盘上。 ddcopy -d s=1 d=2,x:\d1.img -c 将第1硬盘复制到第2硬盘,并采用压缩方式保存到映像文件“d1.img”上。 ddcopy -d s=x:\d1.img d=1,3 将映像文件“d1.img”上的数据复制到第1、3硬盘上。 ddcopy -p s=1:1 d=2:1,3:2 将第1硬盘第1分区上的数据复制到第2硬盘的第1分区和第3硬盘的第2分区上。 ddcopy -p s=2:1 d=1:2,x:\p21.img -v 634 将第2硬盘第1分区上的数据复制到第1硬盘的第2分区上,并保存到映像文件“p21.img”中, 并设定分卷容量为634MB (649216KB)。 ddcopy -p s=x:\p21.img d=2:1,1:2 将映像文件“p21.img”中的数据复制到第2硬盘的第1分区和第1硬盘的第2分区上。 使用说明 DDCOPY是DOS模式下运行的程序,可以运行在PC-DOS、MS-DOS和Win95/98/Me的DOS环境下。不能在NT、Win2000、XP环境下运行。 硬盘号:“1”表示80h硬盘,即BIOS中所认的第一块硬盘,“2”表示81h硬盘,即BIOS中所认的第二块硬盘,依此类推。 分区号:“1”表示硬盘上的第一个分区,“2”表示硬盘上的第二个分区。每个硬盘上取多有四个分区。 映像文件所在的磁盘区域不要位于将要对其进行操作的“源盘”或“目标盘”上。 硬盘连接方法 前提:在计算机BIOS中设置以"C"或"IDE0"启动时 IDE硬盘: Primary Master ->; 第1硬盘 (80h) Primary Slave ->; 第2硬盘 (81h) Secondary Master->; 第3硬盘 (82h) Secondary Slave ->; 第4硬盘 (83h) SCSI硬盘: ID=0 ->; 第1硬盘 (80h) ID=1 ->; 第2硬盘 (81h) …… 说明: 将硬盘接到计算机,并在BIOS中正确设置硬盘参数; 记下硬盘的排放顺序,以免拷贝时出现错误; 提高拷贝速度 如果条件允许,用以下连接方法可以提高20%拷贝速度: 对于IDE硬盘,将源盘与目标盘分别用两根数据(排)线与主板连接 对于SCSI硬盘,将源盘与目标盘分别连接到不同的SCSI控制器上
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值