1简介

在工作中,有一种常见的场景是要安装一个系统,这个系统可能安装好了开发环境、环境变量配置、网卡驱动等设置,如果要重新安装一个一模一样的系统,会非常复杂,在这个场景下我们就可以借助工具,即dd命令来完整把这个系统拷贝到另外一个硬盘上,dd命令会完整的拷贝文件系统,分区,网卡,系统配置等信息到另外的硬盘上,这样把这个携带一模一样的硬盘安装到另外的服务器上,只要在开机时,指定启动项,即可进入一个安装好的一模一样的环境,这时只要修改一下ip即可正常使用,省时省力。

2操作步骤

2.1命令集合

2.1.1 fdisk

分区是将一个硬盘驱动器分成若干个逻辑驱动器,分区是把硬盘连续的区块当做一个独立的磁硬使用。分区表是一个硬盘分区的索引,分区的信息都会写进分区表。
fdisk主要作用是进行分区表控制的。可以通过fdisk查看系统当前所有硬盘,并查看分区状况。

fdisk  is  a dialog-driven program for creation and manipulation of partition tables.  It understands
       GPT, MBR, Sun, SGI and BSD partition tables.

       Block devices can be divided into one or more logical disks  called  partitions.   This  division  is
       recorded  in the partition table, usually found in sector 0 of the disk.  (In the BSD world one talks
       about `disk slices' and a `disklabel'.)

       All partitioning is driven by device I/O limits (the topology) by default.  fdisk is able to optimize
       the  disk  layout for a 4K-sector size and use an alignment offset on modern devices for MBR and GPT.
       It is always a good idea to follow fdisk's defaults as the default values (e.g. first and last parti‐
       tion  sectors)  and  partition  sizes  specified  by the +<size>{M,G,...} notation are always aligned
       according to the device properties.

       Note that partx(8) provides a rich interface for scripts to  print  disk  layouts,  fdisk  is  mostly
       designed  for  humans.   Backward  compatibility in the output of fdisk is not guaranteed.  The input
       (the commands) should always be backward compatible.

笔者在Ubuntu系统中建立的硬盘为50G,使用fdisk查看所有分区情况如下:

root@sqh-virtual-machine:/home/sqh# fdisk -l
Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 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: dos
Disk identifier: 0x924a1a09

Device     Boot   Start       End   Sectors  Size Id Type
/dev/sda1  *       2048    391167    389120  190M 83 Linux
/dev/sda2        393214 104855551 104462338 49.8G  5 Extended
/dev/sda5        393216   4390911   3997696  1.9G 82 Linux swap / Solaris
/dev/sda6       4392960 104855551 100462592 47.9G 83 Linux

2.1.2 df -h

linux中df命令的功能是用来检查linux服务器的文件系统的磁盘空间占用情况。可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息。

df命令显示指定磁盘文件的可用空间。如果没有文件名被指定,则所有当前被挂载的文件系统的可用空间将被显示。默认情况下,磁盘空间将以 1KB 为单位进行显示,除非环境变量 POSIXLY_CORRECT 被指定,那样将以512字节为单位进行显示。

root@sqh-virtual-machine:/home/sqh# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            1.9G     0  1.9G   0% /dev
tmpfs           393M  6.2M  387M   2% /run
/dev/sda6        48G   16G   30G  34% /
tmpfs           2.0G  188K  2.0G   1% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda1       180M  137M   31M  82% /boot
tmpfs           393M   28K  393M   1% /run/user/108
tmpfs           393M     0  393M   0% /run/user/1000

因此,从上面的命令结果可以看到,df可以记录的内容为文件系统的磁盘使用情况

2.1.3 dd

Linux dd命令用于读取、转换并输出数据。

dd可从标准输入或文件中读取数据,根据指定的格式来转换数据,再输出到文件、设备或标准输出。
在完整克隆硬盘上的文件系统时,可以不进行分区而直接进行dd命令进行文件系统的写入。

3操作步骤

3.1 前提

准备两块硬盘,最好大小一致。其中/dev/sda为系统运行的盘,而/dev/sdb为系统插入的新的裸盘。

3.2 使用fdisk删除/dev/sdb中旧有分区

由于新拿来的硬盘可能没有格式化,因此请在确保硬盘可以格式化的前提下进行该操作。
使用root执行如下命令

fdisk /dev/sdb

根据提示删除所有分区

root@sqh-virtual-machine:/home/sqh# fdisk /dev/sda

Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table


Command (m for help): d
Partition number (1,2,5,6, default 6): 6

Partition 6 has been deleted.

注意,虽然提示分区6已经被删除,但此时这种变化并未落到磁盘上,因此需要在Command界面再次选择w,把更改落到磁盘上,这种变化才会生效。

w write table to disk and exit

对所有分区进行删除操作,最后可以得到一个裸盘/dev/sdb

3.3 dd克隆

使用root用户进行dd命令的执行

dd if=/dev/sda of=/dev/sdb bs=4M

执行该命令,则终端会悬停,等待dd命令执行结束。

3.4 显示进度

在dd命令执行时,看不到dd命令执行的进度,此时可以另起一个shell终端,发送如下信号即可在dd所在终端打印命令执行进度
在这里插入图片描述
注意:上述命令仅仅用于演示效果,实际执行dd命令以3.2为准。

3.5 设置开机启动项

虽然已经把系统安装到了硬盘/dev/sdb,但如果直接把该硬盘插入另外一个服务器节点,若在该节点存在系统盘的情形下,服务器会默认加载原来的系统盘中的内容,与U盘装系统类似,需要在服务器启动时更改启动项,进入Boot界面设置系统从新硬盘启动。
一般在开机时点击F2或者F11,然后根据提示操作即可。
选择了启动项之后,会发现系统成功的进入了之前复制的系统,此时更改IP即可。
配置静态ip可参见使用VMWare安装Ubentu指南

3.6 设置从硬盘启动选项

虽然上述的设置可以进入Ubuntu系统中,但如果下次开启服务器时,不设置启动项,服务器会再次默认加载原来的系统镜像,因此,需要修改默认启动项。
修改BIOS从指定硬盘启动才行。

4 总结

文章简要介绍使用dd命令从一个包含系统的硬盘完整克隆出一个新的一模一样的系统时的具体的过程,希望能有所帮助。

										2019-04-17 23:54于江宁汉庭酒店
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐