天天看點

Linux添加新硬碟分區并格式化(centos7.5)第一種方法(linux标準分區)第二種方法(linux lvm磁盤管理分區)

簡介:伺服器上存儲空間不夠用,需要添加磁盤以增大伺服器的存儲空間。 

我先用虛拟機做的模拟,添加了兩塊新的磁盤,用了兩種分區方法,現在用第二種居多。

在Linux裡有三種分區指令fdisk,gdisk,parted。我這裡主要用的是fdisk這個指令。

  • fdisk隻能用于MBR分區,gdisk,parted可以用于GPT分區。
  • fdisk大多數運維從業人員已經習慣這個互動模式。
  • parted指令在建立删除分區使用指令比較友善,但是功能不是太完善,沒有備份還原指令。
  • gdisk在分區上指令和fdisk風格一樣, 使用友善,學習難度低且功能強大,推薦使用。

備注:gdisk與fdisk指令基本一樣,差别不大

第一種方法(linux标準分區)

1.先看一下伺服器上的磁盤資訊

[[email protected] ~]# fdisk -l

磁盤 /dev/sda:42.9 GB, 42949672960 位元組,83886080 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/實體):512 位元組 / 512 位元組
I/O 大小(最小/最佳):512 位元組 / 512 位元組
磁盤标簽類型:dos
磁盤辨別符:0x0009a8a4

   裝置 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    83886079    40893440   8e  Linux LVM

磁盤 /dev/sdb:10.7 GB, 10737418240 位元組,20971520 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/實體):512 位元組 / 512 位元組
I/O 大小(最小/最佳):512 位元組 / 512 位元組


磁盤 /dev/sdc:10.7 GB, 10737418240 位元組,20971520 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/實體):512 位元組 / 512 位元組
I/O 大小(最小/最佳):512 位元組 / 512 位元組


磁盤 /dev/mapper/centos-root:37.7 GB, 37706792960 位元組,73646080 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/實體):512 位元組 / 512 位元組
I/O 大小(最小/最佳):512 位元組 / 512 位元組


磁盤 /dev/mapper/centos-swap:4160 MB, 4160749568 位元組,8126464 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/實體):512 位元組 / 512 位元組
I/O 大小(最小/最佳):512 位元組 / 512 位元組
           

2.對磁盤sdb做分區

[[email protected] ~]# fdisk /dev/sdb
歡迎使用 fdisk (util-linux 2.23.2)。

更改将停留在記憶體中,直到您決定将更改寫入磁盤。
使用寫入指令前請三思。

Device does not contain a recognized partition table
使用磁盤辨別符 0xe56d8a4f 建立新的 DOS 磁盤标簽。

指令(輸入 m 擷取幫助):m
指令操作
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

指令(輸入 m 擷取幫助):p

磁盤 /dev/sdb:10.7 GB, 10737418240 位元組,20971520 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/實體):512 位元組 / 512 位元組
I/O 大小(最小/最佳):512 位元組 / 512 位元組
磁盤标簽類型:dos
磁盤辨別符:0xe56d8a4f

   裝置 Boot      Start         End      Blocks   Id  System

指令(輸入 m 擷取幫助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分區号 (1-4,預設 1):
起始 扇區 (2048-20971519,預設為 2048):
将使用預設值 2048
Last 扇區, +扇區 or +size{K,M,G} (2048-20971519,預設為 20971519):
将使用預設值 20971519
分區 1 已設定為 Linux 類型,大小設為 10 GiB

指令(輸入 m 擷取幫助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盤。
           
Linux添加新硬碟分區并格式化(centos7.5)第一種方法(linux标準分區)第二種方法(linux lvm磁盤管理分區)

 3.格式化磁盤分區sdb1

[[email protected] ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=655296 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=2621184, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
           

4.挂載磁盤到opt目錄下

[[email protected] ~]# mount /dev/sdb1 /opt
[[email protected] ~]# df -h
檔案系統                 容量  已用  可用 已用% 挂載點
/dev/mapper/centos-root   36G  7.9G   28G   23% /
devtmpfs                 1.9G     0  1.9G    0% /dev
tmpfs                    1.9G     0  1.9G    0% /dev/shm
tmpfs                    1.9G   13M  1.9G    1% /run
tmpfs                    1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1               1014M  170M  845M   17% /boot
tmpfs                    378M  4.0K  378M    1% /run/user/42
tmpfs                    378M   28K  378M    1% /run/user/0
/dev/sdb1                 10G   33M   10G    1% /opt
           

以上就是伺服器添加磁盤,分區,格式化的步驟。

第二種方法(linux lvm磁盤管理分區)

為了防止伺服器的磁盤在發生不夠用的情況,能夠靈活添加磁盤采用lvm磁盤管理。

LVM是邏輯盤卷管理(Logical Volume Manager)的簡稱,它是Linux環境下對磁盤分區進行管理的一種機制,LVM是建立在硬碟和分區之上的一個邏輯層,來提高磁盤分區管理的靈活性。LVM是在磁盤分區和檔案系統之間添加的一個邏輯層,來為檔案系統屏蔽下層磁盤分區布局,提供一個抽象的盤卷,在盤卷上建立檔案系統。實體卷(physical volume)實體卷就是指硬碟分區或從邏輯上與磁盤分區具有同樣功能的裝置(如RAID),是LVM的基本存儲邏輯塊,但和基本的實體存儲媒體(如分區、磁盤等)比較,卻包含有與LVM相關的管理參數。

PV(Physical Volume)- 實體卷 

實體卷在邏輯卷管理中處于最底層,它可以是實際實體硬碟上的分區,也可以是整個實體硬碟,也可以是raid裝置。

VG(Volumne Group)- 卷組 

卷組建立在實體卷之上,一個卷組中至少要包括一個實體卷,在卷組建立之後可動态添加實體卷到卷組中。一個邏輯卷管理系統工程中可以隻有一個卷組,也可以擁有多個卷組。

LV(Logical Volume)- 邏輯卷 

邏輯卷建立在卷組之上,卷組中的未配置設定空間可以用于建立新的邏輯卷,邏輯卷建立後可以動态地擴充和縮小空間。系統中的多個邏輯卷可以屬于同一個卷組,也可以屬于不同的多個卷組

1.對磁盤sdb做分區

[[email protected] ~]# fdisk /dev/sdb
歡迎使用 fdisk (util-linux 2.23.2)。

更改将停留在記憶體中,直到您決定将更改寫入磁盤。
使用寫入指令前請三思。


指令(輸入 m 擷取幫助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分區号 (1-4,預設 1):
起始 扇區 (2048-20971519,預設為 2048):
将使用預設值 2048
Last 扇區, +扇區 or +size{K,M,G} (2048-20971519,預設為 20971519):
将使用預設值 20971519
分區 1 已設定為 Linux 類型,大小設為 10 GiB

指令(輸入 m 擷取幫助):t
已選擇分區 1
Hex 代碼(輸入 L 列出所有代碼):8e
已将分區“Linux”的類型更改為“Linux LVM”

指令(輸入 m 擷取幫助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盤。
           
Linux添加新硬碟分區并格式化(centos7.5)第一種方法(linux标準分區)第二種方法(linux lvm磁盤管理分區)

 2.建立實體卷

[[email protected] ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.
           

檢視實體卷

[[email protected] ~]# pvdisplay 
           
Linux添加新硬碟分區并格式化(centos7.5)第一種方法(linux标準分區)第二種方法(linux lvm磁盤管理分區)

3.建立卷組

[[email protected] ~]# vgcreate datavg /dev/sdb1
  Volume group "datavg" successfully created
           

檢視卷組

[[email protected] ~]# vgdisplay 
           
Linux添加新硬碟分區并格式化(centos7.5)第一種方法(linux标準分區)第二種方法(linux lvm磁盤管理分區)

 4.建立邏輯卷

[[email protected] ~]# lvcreate -L +9.9G -n datalv datavg
  Rounding up size to full physical extent 9.90 GiB
  Logical volume "datalv" created.
           

檢視邏輯卷

[[email protected] ~]# lvdisplay 
           
Linux添加新硬碟分區并格式化(centos7.5)第一種方法(linux标準分區)第二種方法(linux lvm磁盤管理分區)

5.格式化邏輯卷

[[email protected] ~]# mkfs.xfs /dev/datavg/datalv
meta-data=/dev/datavg/datalv     isize=512    agcount=4, agsize=648960 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=2595840, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

           

6.配置開機自動挂載磁盤

[[email protected] ~]# vi /etc/fstab
           
Linux添加新硬碟分區并格式化(centos7.5)第一種方法(linux标準分區)第二種方法(linux lvm磁盤管理分區)

 挂載

[[email protected] ~]# mount -a
[[email protected] ~]# df -h
檔案系統                   容量  已用  可用 已用% 挂載點
/dev/mapper/centos-root     36G  3.7G   32G   11% /
devtmpfs                   1.9G     0  1.9G    0% /dev
tmpfs                      1.9G     0  1.9G    0% /dev/shm
tmpfs                      1.9G   13M  1.9G    1% /run
tmpfs                      1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1                 1014M  170M  845M   17% /boot
tmpfs                      378M   24K  378M    1% /run/user/0
/dev/sr0                   4.2G  4.2G     0  100% /run/media/root/CentOS 7 x86_64
/dev/mapper/datavg-datalv  9.9G   33M  9.9G    1% /opt
           

磁盤分區完成。

在之後工作中如果磁盤空間不足,隻需要擴容卷組和邏輯卷即可。

1.擴容卷組

[[email protected] ~]# vgextend datavg /dev/sdc
  Physical volume "/dev/sdc" successfully created.
  Volume group "datavg" successfully extended
           

檢視卷組資訊

[[email protected] ~]# vgdisplay
  --- Volume group ---
  VG Name               datavg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               19.99 GiB
  PE Size               4.00 MiB
  Total PE              5118
  Alloc PE / Size       5118 / 19.99 GiB
  Free  PE / Size       0 / 0   
  VG UUID               PjpIQe-9Vce-jHqF-pipm-OCoh-qu1D-bHHNOq
           

2.擴容邏輯卷

[[email protected] ~]# lvextend -l +100%FREE /dev/datavg/datalv
  Size of logical volume datavg/datalv changed from <10.00 GiB (2559 extents) to 19.99 GiB (5118 extents).
  Logical volume datavg/datalv successfully resized.
           

檢視邏輯卷資訊

[[email protected] ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/datavg/datalv
  LV Name                datalv
  VG Name                datavg
  LV UUID                pJjBQn-fxxa-2ohn-2bZJ-wYHK-hQl3-ARj1KY
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2021-07-19 18:19:06 +0800
  LV Status              available
  # open                 1
  LV Size                19.99 GiB
  Current LE             5118
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
           

同步一下磁盤資訊

[[email protected] ~]# xfs_growfs /dev/datavg/datalv 
meta-data=/dev/mapper/datavg-datalv isize=512    agcount=5, agsize=648960 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=2620416, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2620416 to 5240832
           

檢視挂載資訊

Linux添加新硬碟分區并格式化(centos7.5)第一種方法(linux标準分區)第二種方法(linux lvm磁盤管理分區)

 3.擴充磁盤空間完成。

繼續閱讀