dd命令使用总结

dd常用参数

if:就是input file,也可以是某设备文件
of:就是output file,也支持是设备文件
bs:规划的一个block大小,若未指定则默认为512byte
count:指定bs的数量
skip=n,指 if 后面的原文件跳过 n bytes 再开始读取;
seek=n,指 of 后面的目标文件跳过 n bytes 再开始写入;
oflag=direct,真实磁盘io

测试硬盘读写速度

  • 测试磁盘的IO写速度
~# time dd if=/dev/zero of=1.db bs=8k count=30000  
30000+0 records in
30000+0 records out
245760000 bytes (246 MB) copied, 0.731931 s, 336 MB/s

real    0m0.735s
user    0m0.001s
sys     0m0.151s
  • 测试磁盘的IO读速度
~# dd if=test.dbf bs=8k count=300000 of=/dev/null
30000+0 records in
30000+0 records out
245760000 bytes (246 MB) copied, 0.0342157 s, 7.2 GB/s

dd备份磁盘

  • 查看磁盘信息
root@aikeru:~# fdisk -u -l /dev/sdb
Disk /dev/sdb: 50 MiB, 52428800 bytes, 102400 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
Disklabel type: gpt
Disk identifier: C94E55EA-A4D2-4E78-9D73-46CBAE7A03EF

Device     Start    End Sectors Size Type
/dev/sdb1   2048  32767   30720  15M EFI System
/dev/sdb2  32768  94207   61440  30M Linux filesystem
/dev/sdb3  94208 102366    8159   4M BIOS boot
  • 备份为文件

    • count=fdisk命令中最大的end数+1
dd bs=512 count=102367 if=/dev/sdb of=./nas.img
  • 备份到远程主机
dd bs=512 count=102367 if=/dev/sdb | ssh 192.168.2.2 "of=./nas.img"  
  • 添加硬盘不重启
partprobe
for i in `seq 0 32`; do echo "- - -" > /sys/class/scsi_host/host$i/scan;done
  • 还原
dd bs=512 count=102367 if=./nas.img of=/dev/sdc

dd克隆硬盘

  • 本地克隆
dd bs=512 count=102367 if=/dev/sdb of=/dev/sdc
  • 远程克隆
dd bs=512 count=102367 if=/dev/sdb | ssh 192.168.2.2 "of=./dev/sdc"  

dd添加swap分区

    dd if=/dev/zero of=/swapfile bs=1M count=4000
    mkswap /swapfile
    chmod 600 /bigfile
    swapon /bigfile 

猜你喜欢

转载自blog.51cto.com/m51cto/2372674