劃分分區並掛載磁盤
本操作以該場景爲例,當雲主機掛載了一塊新的數據盤時,使用fdisk分區工具將該數據盤設爲主分區,分區方式默認設置爲MBR,文件系統設爲ext4格式,掛載在“/mnt/sdc”下,並設置開機啓動自動掛載。
查看系統檢測的硬盤 命令:lsblk
1、執行以下命令,查看新增磁盤
fdisk -l
屏幕回顯如下:
[root@ecs-b656 test]# fdisk -l
Disk /dev/xvda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000cc4ad
Device Boot Start End Blocks Id System
/dev/xvda1 * 2048 2050047 1024000 83 Linux
/dev/xvda2 2050048 22530047 10240000 83 Linux
/dev/xvda3 22530048 24578047 1024000 83 Linux
/dev/xvda4 24578048 83886079 29654016 5 Extended
/dev/xvda5 24580096 26628095 1024000 82 Linux swap / Solaris
Disk /dev/xvdb: 10.7 GB, 10737418240 bytes, 20971520 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
表示當前的雲主機有兩塊磁盤,“/dev/xvda”是系統盤,“/dev/xvdb”是新增數據盤。
2、執行以下命令,進入fdisk模式,開始對新增數據盤執行分區操作。
fdisk 新增數據盤
以新掛載的數“/dev/xvdb”爲例:
fdisk /dev/xvdb
屏幕回顯如下:
[root@ecs-b656 test]# fdisk /dev/xvdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xb00005bd.
Command (m for help):
3、輸入【n】,按【Enter】,開始新建分區。
屏幕回顯如下:
Command action e
extended p
primary partition (1-4)
4、輸入【p】,按【Enter】,開始創建一個主分區。
屏幕回顯如下:
Select (default p): p
Partition number (1-4, default 1):
Partition number表示主分區編號,可以選擇1-4。
5、輸入主分區編號,按【Enter】。
本步驟中以【1】爲例。屏幕回顯如下:
Partition number (1-4, default 1): 1
First sector (2048-20971519, default 2048):
6、以選擇默認初始磁柱編號2048爲例,按【Enter】。
屏幕回顯如下:
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519):
“Last sector”表示截止磁柱區域,可以選擇2048-20971519,默認爲20971519。
7、以選擇默認截止磁柱編號20971519爲例,按【Enter】。
屏幕回顯如下:
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519):
Using default value 20971519
Partition 1 of type Linux and of size 10 GiB is set
Command (m for help):
表示分區完成,即爲10GB的數據盤新建了1個分區。
8、輸入【p】,按【Enter】,查看新建分區。
屏幕回顯如下:
Command (m for help): p
Disk /dev/xvdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0xb00005bd
Device Boot Start End Blocks Id System
/dev/xvdb1 2048 20971519 10484736 83 Linux
Command (m for help):
9、輸入【w】,按【Enter】。 將分區結果寫入分區表中,分區創建完畢。
屏幕回顯如下:
...
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
表示分區創建完成。如果之前分區操作有誤,請輸入【q】,則會退出fdisk分區工具,之前的分區結果將不會被保留。
10、執行以下命令,將新建分區文件系統設爲系統所需格式。
mkfs -t 文件系統格式 /dev/xvdb1
以設置文件系統爲“ext4”爲例:
mkfs -t ext4 /dev/xvdb1
屏幕回顯如下:
[root@ecs-b656 test]# mkfs -t ext4 /dev/xvdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2621184 blocks
131059 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
格式化需要等待一段時間,請觀察系統運行狀態,不要退出。
注意:不同文件系統支持的分區個數和分區大小不同,請根據您的業務需求選擇合適的文件系統。
11、執行如下命令,新建掛載點。
mkdir 掛載點
以新建掛載點“/mnt/sdc”爲例:
mkdir /mnt/sdc
12、執行以下命令,將新建分區掛載到步驟11中新建的掛載點下。
mount /dev/xvdb1 掛載點
以掛載新建分區至“/mnt/sdc”爲例:
mount /dev/xvdb1 /mnt/sdc
13、執行以下命令,查看掛載結果。
df -TH
回顯類似如下信息:
[root@ecs-b656 test]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/xvda2 xfs 11G 7.4G 3.2G 71% /
devtmpfs devtmpfs 4.1G 0 4.1G 0% /dev
tmpfs tmpfs 4.1G 82k 4.1G 1% /dev/shm
tmpfs tmpfs 4.1G 9.2M 4.1G 1% /run
tmpfs tmpfs 4.1G 0 4.1G 0% /sys/fs/cgroup
/dev/xvda3 xfs 1.1G 39M 1.1G 4% /home
/dev/xvda1 xfs 1.1G 131M 915M 13% /boot
/dev/xvdb1 ext4 11G 38M 9.9G 1% /mnt/sdc
表示新建分區“/dev/xvdb1”已掛載至“/mnt/sdc”。
設置開機自動掛載磁盤
如果您需要在雲主機系統啓動時自動掛載磁盤,不能採用在 /etc/fstab直接指定 /dev/xvdb1的方法,因爲雲中設備的順序編碼在關閉或者開啓雲主機過程中可能發生改變,例如/dev/xvdb1可能會變成/dev/xvdb2。推薦使用UUID來配置自動掛載數據盤。
說明:磁盤的UUID(universally unique identifier)是Linux系統爲磁盤分區提供的唯一的標識字符串。
1、執行如下命令,查詢磁盤UUID。
blkid 磁盤分區
以查詢磁盤分區“/dev/xvdb1”的UUID爲例:
blkid /dev/xvde1
屏幕回顯如下:
[root@ecs-b656 test]# blkid /dev/xvdb1
/dev/xvdb1: UUID="1851e23f-1c57-40ab-86bb-5fc5fc606ffa" TYPE="ext4"
表示“/dev/xvdb1”的UUID。
2、執行以下命令,使用VI編輯器打開【fstab】文件。
vi /etc/fstab
3、按【i】,進入編輯模式。
4、將光標移至文件末尾,按【Enter】,添加如下內容。
UUID=1851e23f-1c57-40ab-86bb-5fc5fc606ffa /mnt/sdc ext4 defaults 0 2
5、按【ESC】後,輸入【:wq】,按【Enter】。
6、保存設置並退出編輯器。