Linux入門實踐筆記(三)——資料盤格式化和和多分割槽掛載
內容
本文主要介紹了CentOS系統的阿里雲ECS伺服器中資料盤的格式化和掛載多分割槽ext4格式的檔案系統。
版本
作業系統: CentOS 7.2 64位
適合人群
linux運維人員
說明
轉載請說明出處:ofollow,noindex" target="_blank">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 #檔案系統的名稱總容量 已用可用使用比例掛載點 FilesystemSizeUsed Avail Use% Mounted on /dev/vda120G1.9G17G11% / devtmpfs3.9G03.9G0% /dev tmpfs3.9G03.9G0% /dev/shm tmpfs3.9G320K3.9G1% /run tmpfs3.9G03.9G0% /sys/fs/cgroup tmpfs783M0783M0% /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 BootStartEndBlocksIdSystem /dev/vda1*2048419430392097049683Linux #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: pprimary (0 primary, 0 extended, 4 free)#主分割槽型別 eextended#擴充套件分割槽型別 #輸入 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: pprimary (1 primary, 0 extended, 3 free) eextended 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 BootStartEndBlocksIdSystem /dev/vda1*2048419430392097049683Linux 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 BootStartEndBlocksIdSystem /dev/vdb120485240000026198976+83Linux /dev/vdb2524001281048575992622873683Linux
建立檔案系統
執行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 /ext4defaults1 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 FilesystemSizeUsed Avail Use% Mounted on /dev/vda120G1.9G17G11% / devtmpfs3.9G03.9G0% /dev tmpfs3.9G03.9G0% /dev/shm tmpfs3.9G328K3.9G1% /run tmpfs3.9G03.9G0% /sys/fs/cgroup tmpfs783M0783M0% /run/user/0 /dev/vdb125G45M24G1% /home/user1/redis/data /dev/vdb225G45M24G1% /home/user1/program