1. 程式人生 > >硬盤分區及格式化

硬盤分區及格式化

swap 新分區 welcom phy 之前 tab 交換分區 ide 模式

案例1:硬盤分區及格式化 案例2:新建一個邏輯卷 案例3:調整現有磁盤的分區 案例4:擴展邏輯卷的大小

1 案例1:硬盤分區及格式化
1.1 問題

本例要求熟悉硬盤分區結構,使用fdisk分區工具在磁盤 /dev/vdb 上按以下要求建立分區:

采用默認的 msdos 分區模式
第1個分區 /dev/vdb1 的大小為 200MiB
第2個分區 /dev/vdb2 的大小為 2000MiB
第3個分區 /dev/vdb3 的大小為 1000MiB

完成分區後,能夠配置開機自動掛載 /dev/vdb2 分區:

文件系統類型為 EXT4
將其掛載到 /mnt/part2 目錄

1.2 方案

fdisk分區工具用來建立msdos分區方案,其交互模式中的主要指令如下:

m:列出指令幫助
p:查看當前的分區表信息
n:新建分區
d:刪除分區
t:更改分區標識
q:放棄分區更改並退出
w:保存對分區表所做的更改

1.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:新建分區表

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.

6)刷新分區表

[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

2 案例2:新建一個邏輯卷
2.1 問題

本例要求沿用前一天案例,使用分區 /dev/vdb1 構建 LVM 存儲,相關要求如下:

新建一個名為 systemvg 的卷組
在此卷組中創建一個名為 vo 的邏輯卷,大小為180MiB
將邏輯卷 vo 格式化為 EXT4 文件系統
將邏輯卷 vo 掛載到 /vo 目錄,並在此目錄下建立一個測試文件 votest.txt,內容為“I AM KING.”

2.2 方案

LVM創建工具的基本用法:

vgcreate  卷組名  物理設備.. ..
lvcreate  -L  大小  -n  邏輯卷名  卷組名

2.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:創建卷組

1)新建名為systemvg的卷組

[root@server0 ~]# vgcreate  systemvg  /dev/vdb1
  Physical volume "/dev/vdb1" successfully created
  Volume group "systemvg" successfully created

2)確認結果

[root@server0 ~]# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "systemvg" using metadata type lvm2

步驟二:創建邏輯卷

1)新建名為vo的邏輯卷

[root@server0 ~]# lvcreate  -L  180MiB  -n  vo  systemvg 
  Logical volume "vo" created

2)確認結果

[root@server0 ~]# lvscan
  ACTIVE            ‘/dev/systemvg/vo‘ [180.00 MiB] inherit

步驟三:格式化及掛載使用

1)格式化邏輯卷/dev/systemvg/vo

[root@server0 ~]# mkfs.ext4  /dev/systemvg/vo
.. ..
Allocating group tables: done 
Writing inode tables: done 
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

2)掛載邏輯卷/dev/systemvg/vo

[root@server0 ~]# mkdir  /vo                              //創建掛載點
[root@server0 ~]# mount  /dev/systemvg/vo  /vo             //掛載
[root@server0 ~]# df  -hT  /vo/                         //檢查結果
Filesystem              Type  Size  Used Avail Use% Mounted on
/dev/mapper/systemvg-vo ext4  171M  1.6M  157M   1% /vo

3)訪問邏輯卷/dev/systemvg/vo

[root@server0 ~]# cat  /vo/votest.txt
I AM KING.

3 案例3:調整現有磁盤的分區
3.1 問題

本例要求沿用前一天案例,對磁盤/dev/vdb的分區表進行調整,要求如下:不更改原有分區,利用剩余空間新增三個分區,大小依次為:500MiB、2000MiB、512MiB

然後再基於剛建立的 2000MiB 分區構建新的 LVM 存儲:

新的邏輯卷命名為 database,大小為50個物理擴展單元(Physical Extent),屬於 datastore 卷組
在 datastore 卷組中的所有邏輯卷,其物理擴展單元(Physical Extent)的大小為16MiB
使用 EXT3 文件系統對邏輯卷 database 格式化,此邏輯卷應該在開機時自動掛載到 /mnt/database 目錄

3.2 方案

創建卷組時,可以通過-s選項指定PE的大小。

在給新建的邏輯卷分配空間時,空間大小只能是PE大小的倍數。
3.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:調整現有磁盤分區

1)新建擴展分區(使用剩余可用空間)

[root@server0 ~]# fdisk  /dev/vdb
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
Command (m for help): n                                 //新建分區
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): e                                 //類型指定為e(擴展分區)
Selected partition 4                                     //只一個可用編號,自動選取
First sector (6555648-20971519, default 6555648):          //起始位置默認
Using default value 6555648
Last sector, +sectors or +size{K,M,G} (6555648-20971519, default 20971519): 
Using default value 20971519                             //結束位置默認
Partition 4 of type Extended and of size 6.9 GiB is set
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
/dev/vdb4         6555648    20971519     7207936    5  Extended

2)在擴展分區中新建3個邏輯分區

創建第1個邏輯卷(由於主分區編號已用完,分區類型自動選l邏輯分區):

Command (m for help): n 
All primary partitions are in use
Adding logical partition 5                              //分區編號5
First sector (6557696-20971519, default 6557696):          //起始位置默認
Using default value 6557696
Last sector, +sectors or +size{K,M,G} (6557696-20971519, default 20971519): +500M
                                                      //結束位置默認
Partition 5 of type Linux and of size 500 MiB is set

創建第2個邏輯卷:

Command (m for help): n
All primary partitions are in use
Adding logical partition 6                              //分區編號6
First sector (7583744-20971519, default 7583744):          //起始位置默認
Using default value 7583744
Last sector, +sectors or +size{K,M,G} (7583744-20971519, default 20971519): +2000M
                                                      //結束位置默認
Partition 6 of type Linux and of size 2 GiB is set

創建第3個邏輯卷:

Command (m for help): n
All primary partitions are in use
Adding logical partition 7                              //分區編號7
First sector (11681792-20971519, default 11681792):      //起始位置默認
Using default value 11681792
Last sector, +sectors or +size{K,M,G} (11681792-20971519, default 20971519): +512M
                                                      //結束位置默認
Partition 7 of type Linux and of size 512 MiB is set

根據預計的用途調整分區類型(可選):

Command (m for help): t                                 //修改
Partition number (1-7, default 7): 5                     //第5個分區
Hex code (type L to list all codes): 8e                 //類型為8e(LVM)
Changed type of partition ‘Linux‘ to ‘Linux LVM‘
Command (m for help): t                                 //修改
Partition number (1-7, default 7): 6                     //第6個分區
Hex code (type L to list all codes): 8e                 //類型為8e(LVM)
Changed type of partition ‘Linux‘ to ‘Linux LVM‘
Command (m for help): t                                 //修改
Partition number (1-7, default 7): 7                     //第7個分區
Hex code (type L to list all codes): 82                 //類型為82(交換分區)
Changed type of partition ‘Linux‘ to ‘Linux swap / Solaris‘

確認分區結果並保存:

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
/dev/vdb4         6555648    20971519     7207936    5  Extended
/dev/vdb5         6557696     7581695      512000   8e  Linux LVM
/dev/vdb6         7583744    11679743     2048000   8e  Linux LVM
/dev/vdb7        11681792    12730367      524288   82  Linux swap / Solaris
Command (m for help): w                                 //保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.                                         //提示重啟

3)刷新分區表

[root@server0 ~]# partprobe  /dev/vdb
[root@server0 ~]# reboot

步驟二:新建卷組、邏輯卷

1)新建卷組datastore,指定PE大小為16MiB

[root@server0 ~]# vgcreate  -s 16MiB  datastore  /dev/vdb6
  Volume group "datastore" successfully created
[root@server0 ~]# vgscan                                  //確認新建的卷組
  Reading all physical volumes.  This may take a while...
  Found volume group "systemvg" using metadata type lvm2
  Found volume group "datastore" using metadata type lvm2

2)新建邏輯卷database,大小設置為50個PE

[root@server0 ~]# lvcreate  -l 50  -n  database  datastore
  Logical volume "database" created
[root@server0 ~]# lvscan                                  //確認新建的邏輯卷
  ACTIVE            ‘/dev/systemvg/vo‘ [180.00 MiB] inherit
  ACTIVE            ‘/dev/datastore/database‘ [800.00 MiB] inherit

步驟三:格式化及使用邏輯卷

1)格式化邏輯卷/dev/datastore/database

[root@server0 ~]# mkfs.ext3  /dev/datastore/database
.. ..
Allocating group tables: done 
Writing inode tables: done 
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

2)配置開機掛載

[root@server0 ~]# mkdir  /mnt/database                     //創建掛載點
[root@server0 ~]# vim  /etc/fstab
.. ..
/dev/datastore/database         /mnt/database   ext3    defaults    0 0

3)驗證掛載配置

[root@server0 ~]# mount  -a
[root@server0 ~]# df  -hT  /mnt/database/                 //確認掛載點設備
Filesystem                     Type  Size  Used Avail Use% Mounted on
/dev/mapper/datastore-database ext3  772M  828K  715M   1% /mnt/database

4 案例4:擴展邏輯卷的大小
4.1 問題

本例要求沿用練習一,將邏輯卷 vo 的大小調整為 300MiB,要求如下:

原文件系統中的內容必須保持完整
必要時可使用之前準備的分區 /dev/vdb5 來補充空間
註意:分區大小很少能完全符合要求的大小,所以大小在270MiB和300MiB之間都是可以接受的

4.2 方案

對於已經格式化好的邏輯卷,在擴展大小以後,必須通知內核新大小。

如果此邏輯卷上的文件系統是EXT3/EXT4類型,需要使用resize2fs工具;

如果此邏輯卷上的文件系統是XFS類型,需要使用xfs_growfs。
4.3 步驟

實現此案例需要按照如下步驟進行。

步驟一:確認邏輯卷vo的信息

1)找出邏輯卷所在卷組

[root@server0 ~]# lvscan
  ACTIVE            ‘/dev/systemvg/vo‘ [180.00 MiB] inherit
  ACTIVE            ‘/dev/datastore/database‘ [800.00 MiB] inherit

2)查看該卷組的剩余空間是否可滿足擴展需要

[root@server0 ~]# vgdisplay  systemvg
  --- Volume group ---
  VG Name               systemvg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               196.00 MiB                          //卷組總大小
  PE Size               4.00 MiB
  Total PE              49
  Alloc PE / Size       45 / 180.00 MiB
  Free  PE / Size       4 / 16.00 MiB                      //剩余空間大小
  VG UUID               czp8IJ-jihS-Ddoh-ny38-j521-5X8J-gqQfUN

此例中卷組systemvg的總大小都不夠300MiB、剩余空間才16MiB,因此必須先擴展卷組。只有剩余空間足夠,才可以直接擴展邏輯卷大小。

步驟二:擴展卷組

1)將提前準備的分區/dev/vdb5添加到卷組systemvg

[root@server0 ~]# vgextend  systemvg  /dev/vdb5
  Physical volume "/dev/vdb5" successfully created
  Volume group "systemvg" successfully extended

2)確認卷組新的大小

[root@server0 ~]# vgdisplay  systemvg
  --- Volume group ---
  VG Name               systemvg
  .. ..
  VG Size               692.00 MiB                          //總大小已變大
  PE Size               4.00 MiB
  Total PE              173
  Alloc PE / Size       45 / 180.00 MiB
  Free  PE / Size       128 / 512.00 MiB                  //剩余空間已達512MiB
  VG UUID               czp8IJ-jihS-Ddoh-ny38-j521-5X8J-gqQfUN

步驟三:擴展邏輯卷大小

1)將邏輯卷/dev/systemvg/vo的大小調整為300MiB

[root@server0 ~]# lvextend  -L 300MiB  /dev/systemvg/vo 
  Extending logical volume vo to 300.00 MiB
  Logical volume vo successfully resized

2)確認調整結果

[root@server0 ~]# lvscan
  ACTIVE            ‘/dev/systemvg/vo‘ [300.00 MiB] inherit
  ACTIVE            ‘/dev/datastore/database‘ [800.00 MiB] inherit

3)刷新文件系統大小

確認邏輯卷vo上的文件系統類型:

[root@server0 ~]# blkid  /dev/systemvg/vo
/dev/systemvg/vo: UUID="d4038749-74c3-4963-a267-94675082a48a" TYPE="ext4"

選擇合適的工具刷新大小:

[root@server0 ~]# resize2fs  /dev/systemvg/vo 
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/systemvg/vo to 307200 (1k) blocks.
The filesystem on /dev/systemvg/vo is now 307200 blocks long.

確認新大小(約等於300MiB):

[root@server0 ~]# mount  /dev/systemvg/vo  /vo/
[root@server0 ~]# df  -hT  /vo
Filesystem              Type  Size  Used Avail Use% Mounted on
/dev/mapper/systemvg-vo ext4  287M  2.1M  266M   1% /vo

硬盤分區及格式化