Linux dd命令

备份文件

[[email protected] ~]# dd if=/etc/passwd of=/tmp/passwd.bak
1+1 records in
1+1 records out
798 bytes (798 B) copied, 0.00270593 s, 295 kB/s
[[email protected] ~]# ll /etc/passwd /tmp/passwd.bak
-rw-r--r--. 1 root root 798 Jun  9 02:28 /etc/passwd
-rw-r--r--. 1 root root 798 Aug  8 10:18 /tmp/passwd.bak
if=FILE         read from FILE instead of stdin
of=FILE         write to FILE instead of stdout
1+1 records in
1+1 records out
1个完整的block(默认512Bytes)+ 1个不完整的block,上面的例子是798 = 512 + 286

备份分区或整个硬盘

在对物理服务器的系统做虚拟化时,使用dd命令也是方法之一

[[email protected] ~]# df -h /boot
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1      1014M  159M  856M  16% /boot
[[email protected] ~]# dd if=/dev/sda1 of=/tmp/sda1.img
2097152+0 records in
2097152+0 records out
1073741824 bytes (1.1 GB) copied, 15.2213 s, 70.5 MB/s
[[email protected] ~]# ll -h /tmp/sda1.img
-rw-r--r--. 1 root root 1.0G Aug  8 10:45 /tmp/sda1.img
dd生成的文件大小,是/dev/sda1这个分区的容量(1G),而不是实际使用的磁盘大小(159M)
如果使用dd对整个系统(或整个硬盘)做备份,生成的备份文件大小就是硬盘的容量。
如果硬盘是1TB,则生成的文件就是1TB,因此dd的of=必须是一个系统外部的目录(你无法将整个硬盘再装入硬盘本身)
[[email protected] ~]# dd if=/dev/sda of=/tmp/sda.img
dd: writing to ‘/tmp/sda.img’: No space left on device
54080841+0 records in
54080840+0 records out
27689390080 bytes (28 GB) copied, 471.135 s, 58.8 MB/s
当磁盘没有空间时,dd命令退出
dd命令在执行的过程中没有任何提示,直到命令完成或出现错误才退出
但之前写入到/tmp/sda.img的内容并没有因为dd命令退出而删除,需要手动删除/tmp/sda.img
[[email protected] ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   28G   28G   20K 100% /
devtmpfs                 233M     0  233M   0% /dev
tmpfs                    244M     0  244M   0% /dev/shm
tmpfs                    244M  4.5M  240M   2% /run
tmpfs                    244M     0  244M   0% /sys/fs/cgroup
/dev/sda1               1014M  159M  856M  16% /boot
tmpfs                     49M     0   49M   0% /run/user/0

将镜像写入磁盘

可以将一个安装光盘或启动镜像文件(如.iso,.img)写入一个磁盘或U盘,这样就可以从新磁盘或U盘启动系统。 或者,之前已经将某个系统完整复制到一个文件,现在将这个文件使用dd命令写入一个新的磁盘,达到复制一个系统的目的
下面的例子是将openwrt镜像直接复制到一块虚拟机硬盘,然后就可以从虚拟机启动openwrt
本例是在VMware Workstation中操作的,原来已经有一台CentOS虚拟机,在VMware中对这台虚拟机添加一个1G的硬盘。将镜像写入新的硬盘后,将新硬盘移出CentOS虚拟机,以新硬盘重新创建一台虚拟机。

[[email protected] ~]# fdisk -l
Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 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
Disk label type: dos
Disk identifier: 0x000df1ec
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   125829119    61864960   8e  Linux LVM
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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

查看新硬盘的分区,是空的

[[email protected] ~]# fdisk /dev/sdb
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x88fd0532.
Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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
Disk label type: dos
Disk identifier: 0x88fd0532
   Device Boot      Start         End      Blocks   Id  System
Command (m for help):

将镜像文件写入新硬盘

[[email protected] ~]# ll
-rw-r--r--. 1 root root 248844387 Aug  8 13:24 openwrt-koolshare-mod-v2.19-r8118-26796a2-x86-64-combined-squashfs.img
[[email protected] ~]# dd if=/root/openwrt-koolshare-mod-v2.19-r8118-26796a2-x86-64-combined-squashfs.img of=/dev/sdb
486024+1 records in
486024+1 records out
248844387 bytes (249 MB) copied, 10.8128 s, 23.0 MB/s

查看新硬盘的分区,已经看到有两个分区,这就是镜像文件中的分区

[[email protected] ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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
Disk label type: dos
Disk identifier: 0x9cfbe460
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *         512      410111      204800   83  Linux
/dev/sdb2          410624     1434623      512000   83  Linux

新的虚拟机可以正常启动

系统启动完成后的界面
系统启动完成后的界面

0 Comments

发表评论

电子邮件地址不会被公开。 必填项已用*标注