1案例1:硬盤分區及格式化
1.1 問題
本例要求熟悉硬盤分區結構,使用fdisk分區工具在磁盤/dev/vdb上按以下要求建立分區:
- 採用默認msdos分區模式
- 第一個分區/dev/vdb1的大小爲200MiB
- 第二個分區/dev/vdb2的大小爲2000MiB
- 第三個分區/dev/vdb3的大小爲1000MiB
完成分區後,能夠配置開機自動掛載/dev/vdb2分區: - 文件系統類型爲EXT4
- 將其掛載到/mnt/part2目錄
1.2 方案
實現此案例需要按照如下步驟進行。
步驟一:新建分區表
1)打開fdisk工具,操作磁盤/dev/vdb
[root@server0 ~]# 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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x9ac1bc10.
Command (m for help): //交互操作提示信息
2)新建第1個分區/dev/vdb1
Command (m for help): n //新建分區
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p //類型爲p(主分區)
Partition number (1-4, default 1): 1 //分區編號1
First sector (2048-20971519, default 2048): //起始位置默認
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +200M
Partition 1 of type Linux and of size 200 MiB is set //結束位置+200MiB大小
Command (m for help): p //確認當前分區表
.. ..
Device Boot Start End Blocks Id System
/dev/vdb1 2048 411647 204800 83 Linux
3)新建第2個分區/dev/vdb2
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p //類型爲p(主分區)
Partition number (2-4, default 2): 2 //分區編號2
First sector (411648-20971519, default 411648): //起始位置默認
Using default value 411648
Last sector, +sectors or +size{K,M,G} (411648-20971519, default 20971519): +2000M
Partition 2 of type Linux and of size 2 GiB is set //結束位置+2000MiB大小
Command (m for help): p //確認當前分區表
.. ..
Device Boot Start End Blocks Id System
/dev/vdb1 2048 411647 204800 83 Linux
/dev/vdb2 411648 4507647 2048000 83 Linux
4)新建第3個分區/dev/vdb3
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (4507648-20971519, default 4507648):
Using default value 4507648
Last sector, +sectors or +size{K,M,G} (4507648-20971519, default 20971519): +1000M
Partition 3 of type Linux and of size 1000 MiB is set
Command (m for help): p //確認當前分區表
.. ..
Device Boot Start End Blocks Id System
/dev/vdb1 2048 411647 204800 83 Linux
/dev/vdb2 411648 4507647 2048000 83 Linux
/dev/vdb3 4507648 6555647 1024000 83 Linux
5)調整分區類型標識(可選)
將/dev/vdb1的類型(默認爲83,表示EXT2/3/4分區)修改爲8e(LVM設備):
Command (m for help): t //修改分區類型標識
Partition number (1-3, default 3): 1 //指定第1個分區
Hex code (type L to list all codes): 8e //類型改爲8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p //確認當前分區表
.. ..
Device Boot Start End Blocks Id System
/dev/vdb1 2048 411647 204800 8e Linux LVM
/dev/vdb2 411648 4507647 2048000 83 Linux
/dev/vdb3 4507648 6555647 1024000 83 Linux
6)保存分區更改,退出fdisk分區工具
Command (m for help): w //保存並退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
7)刷新分區表
[root@server0 ~]# partprobe /dev/vdb //重新檢測磁盤分區
//或者
[root@server0 ~]# reboot //對已使用中磁盤的分區調整,應該重啓一次
.. ..
步驟二:格式化及掛載
1)將分區/dev/vdb2格式化爲EXT4文件系統
[root@server0 ~]# mkfs.ext4 /dev/vdb2
.. ..
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
2)配置開機自動掛載
[root@server0 ~]# vim /etc/fstab
.. ..
/dev/vdb2 /mnt/part2 ext4 defaults 0 0
3)創建掛載點,並驗證掛載配置
[root@server0 ~]# mkdir /mnt/part2 //創建掛載點
[root@server0 ~]# mount -a //掛載fstab中的可用設備
[root@server0 ~]# df -hT /mnt/part2/ //檢查文檔所在的文件系統及設備
Filesystem Type Size Used Avail Use% Mounted on
/dev/vdb2 ext4 1.9G 5.9M 1.8G 1% /mnt/part2