內容
本文主要介紹了CentOS系統的阿里雲ECS服務器中數據盤的格式化和掛載多分區ext4格式的文件系統。
版本
操作系統: CentOS 7.2 64位
適合人群
linux運維人員
說明
轉載請說明出處:Linux入門實踐筆記(三)——數據盤格式化和和多分區掛載
參考
Linux 格式化數據盤:https://help.aliyun.com/document_detail/25426.html
ext2、ext3與ext4的區別:https://blog.csdn.net/macrossdzh/article/details/5973639
步驟
查看文件系統的使用情況
通過"df -h "命令查看當前文件系統的使用情況,可以觀察到文件系統的名稱、總容量、已用、可用、使用比例、掛載點等信息。
[user1@iz8vb62snc6e5cage5yvz9z /]$ df -h #文件系統的名稱 總容量 已用 可用 使用比例 掛載點
Filesystem Size Used Avail Use% Mounted on /dev/vda1 20G 1.9G 17G 11% / devtmpfs 3.9G 0 3.9G 0% /dev tmpfs 3.9G 0 3.9G 0% /dev/shm tmpfs 3.9G 320K 3.9G 1% /run tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup tmpfs 783M 0 783M 0% /run/user/0
由於筆者所購服務器還含有50G的數據盤,但是通過"df -h"命令沒有查找到。經過查閱阿里雲的文檔,發現數據盤需要格式化並掛載后才能正常使用。
下面就演示如何將數據盤分成兩個25G的區,格式化分區后分別掛載到/home/user1/redis/data和/home/user1/program。這里需要先創建這兩個目錄,此處就不再演示。
查看磁盤的信息
通過" fdisk -l"命令可以查看當前磁盤的信息,本服務器有個20GB的系統盤/dev/vda,和50GB的數據盤/dev/vdb。可以看到磁盤的扇區數,每個扇區的大小(512字節)。
[user1@iz8vb62snc6e5cage5yvz9z /]$ sudo fdisk -l #20G系統盤/dev/vda
Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000d2717 Device Boot Start End Blocks Id System
/dev/vda1 * 2048 41943039 20970496 83 Linux #50GB的數據盤/dev/vdb
Disk /dev/vdb: 53.7 GB, 53687091200 bytes, 104857600 sectors Units = sectors of 1 * 512 = 512 bytes #每個扇區的大小(512字節)
Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes
執行分區操作
運行 fdisk /dev/vdb對數據盤進行分區,此處創建兩個分區,大小分別為25GB。
步驟1:輸入 n 並按回車鍵:創建一個新分區;如果有舊的分區,可以輸入d進行刪除。
步驟2:輸入 p 並按回車鍵:選擇主分區類型。創建4個以內的分區,都可以使用主分區類型。
步驟3:輸入分區編號並按回車鍵,第一個分區的分區編號設置為1,第二個分區的分區編號設置為2。
步驟4:分別輸入第一個扇區的編號和最后一個扇區的編號,此處第一個分區的扇區從2048到52400000,第二個分區的扇區從52400128到104857599。
如果創建多個分區,重復執行上述4個步驟。
步驟5:輸入 wq 並按回車鍵,開始分區。
[user1@iz8vb62snc6e5cage5yvz9z ~]$ sudo fdisk /dev/vdb 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. #如果有舊的分區,可以輸入d進行刪除。
Command (m for help): d Selected partition 3 Partition 3 is deleted #首先輸入 n 並按回車鍵:創建一個新分區。
Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) #主分區類型
e extended #擴展分區類型 #輸入 p 並按回車鍵:選擇主分區類型
Select (default p): p #輸入分區編號並按回車鍵
Partition number (1-4, default 1): 1
#輸入第一個扇區的編號
First sector (2048-104857599, default 2048): Using default value 2048
#輸入最后一個扇區的編號
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599): 52400000 Partition 1 of type Linux and of size 25 GiB is set #重復執行上述4個步驟,創建第二個分區
Command (m for help): n Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): p Partition number (2-4, default 2): 2 First sector (52400001-104857599, default 52400128): Using default value 52400128 Last sector, +sectors or +size{K,M,G} (52400128-104857599, default 104857599): Using default value 104857599 Partition 2 of type Linux and of size 25 GiB is set #最后輸入 wq 並按回車鍵,開始分區。
Command (m for help): wq The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
確認分區信息
再次執行" fdisk -l"命令可以查看當前磁盤的信息,可以看到上述操作創建的分區/dev/vdb1與/dev/vdb2。
[user1@iz8vb62snc6e5cage5yvz9z ~]$ sudo fdisk -l Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000d2717 Device Boot Start End Blocks Id System
/dev/vda1 * 2048 41943039 20970496 83 Linux Disk /dev/vdb: 53.7 GB, 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 Disk label type: dos Disk identifier: 0xc95b5e42 #上述操作創建的分區/dev/vdb1與/dev/vdb2
Device Boot Start End Blocks Id System
/dev/vdb1 2048 52400000 26198976+ 83 Linux /dev/vdb2 52400128 104857599 26228736 83 Linux
創建文件系統
執行mkfs.ext4 命令,分別在新分區/dev/vdb1與/dev/vdb2上創建ext4類型的文件系統。
[user1@iz8vb62snc6e5cage5yvz9z ~]$ sudo mkfs.ext4 /dev/vdb1 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 1638400 inodes, 6549744 blocks 327487 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2155872256
200 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, 2654208,
4096000 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done
這里創建的文件系統類型為ext4,阿里雲官方文檔創建的是ext3,下面從參考的文章中摘錄一些內容說明ext3和ext4的關系:
Linux kernel 自 2.6.28 開始正式支持新的文件系統 Ext4。 Ext4 是 Ext3 的改進版,修改了 Ext3 中部分重要的數據結構,而不僅僅像 Ext3 對 Ext2 那樣,只是增加了一個日志功能而已。Ext4 可以提供更佳的性能和可靠性,還有更為豐富的功能:與 Ext3 兼容;更大的文件系統和更大的文件;多塊分配;延遲分配;日志校驗;在線碎片整理等。
備份並寫入分區信息
首先備份分區信息文件/etc/fstab,然后在該文件下追加/dev/vdb1與/dev/vdb2的分區信息,包括掛載點和文件系統的格式。
#備份分區信息文件
[user1@iz8vb62snc6e5cage5yvz9z /]$ sudo cp /etc/fstab /etc/fstab.bak #user1用戶使用sudo執行命令扔提示權限不足,切換到root賬戶下執行命令
[user1@iz8vb62snc6e5cage5yvz9z data]$ sudo echo /dev/vdb1 /home/user1/redis/data ext4 defaults 0 0 >> /etc/fstab bash: /etc/fstab: Permission denied #追加/dev/vdb1的分區信息,該分區掛載的路徑為/home/user1/redis/data,格式為ext4
[root@iz8vb62snc6e5cage5yvz9z data]# echo /dev/vdb1 /home/user1/redis/data ext4 defaults 0 0 >> /etc/fstab #追加/dev/vdb2的分區信息,該分區掛載的路徑為/home/user1/program,格式為ext4
[root@iz8vb62snc6e5cage5yvz9z home]# echo /dev/vdb2 /home/user1/program ext4 defaults 0 0 >> /etc/fstab
#查看分區信息
[root@iz8vb62snc6e5cage5yvz9z home]# cat /etc/fstab # # /etc/fstab # Created by anaconda on Thu Aug 17 07:38:21 2017 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=976105f5-f402-456c-aadd-50de49ff88f9 / ext4 defaults 1 1
/dev/vdb1 /home/user1/redis/data ext4 defaults 0 0
/dev/vdb2 /home/user1/program/ ext4 defaults 0 0
掛載分區
使用mount將分區掛載到對應的路徑下,並使用df -h觀察掛載后的文件系統信息。
到此為止,完成了數據盤的分區、格式化、掛載等工作;不需要重啟實例即可開始使用新的文件系統。
#使用mount將分區掛載到對應的路徑下
[root@iz8vb62snc6e5cage5yvz9z data]# mount /dev/vdb1 /home/user1/redis/data
[root@iz8vb62snc6e5cage5yvz9z data]# mount /dev/vdb2 /home/user1/program
#使用df -h觀察掛載后的文件系統
[root@iz8vb62snc6e5cage5yvz9z home]# df -h
Filesystem Size Used Avail Use% Mounted on /dev/vda1 20G 1.9G 17G 11% / devtmpfs 3.9G 0 3.9G 0% /dev tmpfs 3.9G 0 3.9G 0% /dev/shm tmpfs 3.9G 328K 3.9G 1% /run tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup tmpfs 783M 0 783M 0% /run/user/0
/dev/vdb1 25G 45M 24G 1% /home/user1/redis/data /dev/vdb2 25G 45M 24G 1% /home/user1/program
