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於江寧漢庭酒店