Linux添加硬盤創建新的邏輯卷方式


有同仁看了上文《Linux添加硬盤擴充已有分區存儲空間方式》一文后,提出疑問,現在很多雲服務器本來沒有邏輯卷,添加數據盤后需要自行添加,如何處理?

此文將以某雲服務器為例,詳細進行解說。

總體步驟

  • 磁盤初始化分區

  • 創建物理卷

  • 創建卷組

  • 創建邏輯卷

  • 通知文件系統生效

磁盤初始化分區

  1 [root@VM_15_115_centos ~]# df -h
  2 文件系統        容量  已用  可用 已用% 掛載點
  3 /dev/vda1        50G  1.6G   45G    4% /
  4 devtmpfs        7.8G     0  7.8G    0% /dev
  5 tmpfs           7.8G   24K  7.8G    1% /dev/shm
  6 tmpfs           7.8G  464K  7.8G    1% /run
  7 tmpfs           7.8G     0  7.8G    0% /sys/fs/cgroup
  8 tmpfs           1.6G     0  1.6G    0% /run/user/0
  9 [root@VM_15_115_centos ~]# fdisk -l
 10 
 11 磁盤 /dev/vda:53.7 GB, 53687091200 字節,104857600 個扇區
 12 Units = 扇區 of 1 * 512 = 512 bytes
 13 扇區大小(邏輯/物理):512 字節 / 512 字節
 14 I/O 大小(最小/最佳):512 字節 / 512 字節
 15 磁盤標簽類型:dos
 16 磁盤標識符:0x0009ac89
 17 
 18    設備 Boot      Start         End      Blocks   Id  System
 19 /dev/vda1   *        2048   104857599    52427776   83  Linux
 20 
 21 磁盤 /dev/vdb:536.9 GB, 536870912000 字節,1048576000 個扇區
 22 Units = 扇區 of 1 * 512 = 512 bytes
 23 扇區大小(邏輯/物理):512 字節 / 512 字節
 24 I/O 大小(最小/最佳):512 字節 / 512 字節
 25 
 26 [root@VM_15_115_centos ~]# fdisk /dev/vdb
 27 歡迎使用 fdisk (util-linux 2.23.2)。
 28 
 29 更改將停留在內存中,直到您決定將更改寫入磁盤。
 30 使用寫入命令前請三思。
 31 
 32 Device does not contain a recognized partition table
 33 使用磁盤標識符 0xae10029f 創建新的 DOS 磁盤標簽。
 34 
 35 命令(輸入 m 獲取幫助):m
 36 命令操作
 37    a   toggle a bootable flag
 38    b   edit bsd disklabel
 39    c   toggle the dos compatibility flag
 40    d   delete a partition
 41    g   create a new empty GPT partition table
 42    G   create an IRIX (SGI) partition table
 43    l   list known partition types
 44    m   print this menu
 45    n   add a new partition
 46    o   create a new empty DOS partition table
 47    p   print the partition table
 48    q   quit without saving changes
 49    s   create a new empty Sun disklabel
 50    t   change a partition's system id
 51    u   change display/entry units
 52    v   verify the partition table
 53    w   write table to disk and exit
 54    x   extra functionality (experts only)
 55 
 56 命令(輸入 m 獲取幫助):g
 57 Building a new GPT disklabel (GUID: AB878B71-5712-455A-9C62-3EBA92ECA888)
 58 
 59 
 60 命令(輸入 m 獲取幫助):p
 61 
 62 磁盤 /dev/vdb:536.9 GB, 536870912000 字節,1048576000 個扇區
 63 Units = 扇區 of 1 * 512 = 512 bytes
 64 扇區大小(邏輯/物理):512 字節 / 512 字節
 65 I/O 大小(最小/最佳):512 字節 / 512 字節
 66 磁盤標簽類型:gpt
 67 Disk identifier: AB878B71-5712-455A-9C62-3EBA92ECA888
 68 
 69 
 70 #         Start          End    Size  Type            Name
 71 
 72 命令(輸入 m 獲取幫助):m
 73 命令操作
 74    d   delete a partition
 75    g   create a new empty GPT partition table
 76    G   create an IRIX (SGI) partition table
 77    l   list known partition types
 78    m   print this menu
 79    n   add a new partition
 80    o   create a new empty DOS partition table
 81    p   print the partition table
 82    q   quit without saving changes
 83    s   create a new empty Sun disklabel
 84    t   change a partition's system id
 85    v   verify the partition table
 86    w   write table to disk and exit
 87    x   extra functionality (experts only)
 88 
 89 命令(輸入 m 獲取幫助):n
 90 分區號 (1-128,默認 1):
 91 第一個扇區 (2048-1048575966,默認 2048):
 92 Last sector, +sectors or +size{K,M,G,T,P} (2048-1048575966,默認 1048575966):
 93 已創建分區 1
 94 
 95 
 96 命令(輸入 m 獲取幫助):p
 97 
 98 磁盤 /dev/vdb:536.9 GB, 536870912000 字節,1048576000 個扇區
 99 Units = 扇區 of 1 * 512 = 512 bytes
100 扇區大小(邏輯/物理):512 字節 / 512 字節
101 I/O 大小(最小/最佳):512 字節 / 512 字節
102 磁盤標簽類型:gpt
103 Disk identifier: AB878B71-5712-455A-9C62-3EBA92ECA888
104 
105 
106 #         Start          End    Size  Type            Name
107  1         2048   1048575966    500G  Linux filesyste
108 
109 命令(輸入 m 獲取幫助):w
110 The partition table has been altered!
111 
112 Calling ioctl() to re-read partition table.
113 正在同步磁盤。

 

執行結果

 1 [root@VM_15_115_centos ~]# fdisk -l
 2 
 3 磁盤 /dev/vda:53.7 GB, 53687091200 字節,104857600 個扇區
 4 Units = 扇區 of 1 * 512 = 512 bytes
 5 扇區大小(邏輯/物理):512 字節 / 512 字節
 6 I/O 大小(最小/最佳):512 字節 / 512 字節
 7 磁盤標簽類型:dos
 8 磁盤標識符:0x0009ac89
 9 
10    設備 Boot      Start         End      Blocks   Id  System
11 /dev/vda1   *        2048   104857599    52427776   83  Linux
12 WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
13 
14 磁盤 /dev/vdb:536.9 GB, 536870912000 字節,1048576000 個扇區
15 Units = 扇區 of 1 * 512 = 512 bytes
16 扇區大小(邏輯/物理):512 字節 / 512 字節
17 I/O 大小(最小/最佳):512 字節 / 512 字節
18 磁盤標簽類型:gpt
19 Disk identifier: AB878B71-5712-455A-9C62-3EBA92ECA888
20 
21 
22 #         Start          End    Size  Type            Name
23  1         2048   1048575966    500G  Linux filesyste

第二塊磁盤已經初始化完畢,空間500G

 

創建物理卷

 1 [root@VM_15_115_centos ~]# pvdisplay
 2 [root@VM_15_115_centos ~]# pvcreate /dev/sdb1
 3   Device /dev/sdb1 not found.
 4 [root@VM_15_115_centos ~]# pvcreate /dev/vdb1
 5   Physical volume "/dev/vdb1" successfully created.
 6 [root@VM_15_115_centos ~]# pvdisplay
 7   "/dev/vdb1" is a new physical volume of "<500.00 GiB"
 8   --- NEW Physical volume ---
 9   PV Name               /dev/vdb1
10   VG Name
11   PV Size               <500.00 GiB
12   Allocatable           NO
13   PE Size               0
14   Total PE              0
15   Free PE               0
16   Allocated PE          0
17   PV UUID               gV2Rlx-sFHV-9nJT-biZn-iXYf-rl1H-442MZF

注意:設備路徑,有些設備是 /dev/sdb 有些是 /dev/vdb ,需要具體根據 fdisk -l 的結果確定。

創建卷組

1 [root@VM_15_115_centos ~]# vgdisplay
2 [root@VM_15_115_centos ~]#
3 [root@VM_15_115_centos ~]# vgcreate centos /dev/vdb1
4   Volume group "centos" successfully created

創建結果

 1 [root@VM_15_115_centos ~]# vgdisplay
 2   --- Volume group ---
 3   VG Name               centos
 4   System ID
 5   Format                lvm2
 6   Metadata Areas        1
 7   Metadata Sequence No  1
 8   VG Access             read/write
 9   VG Status             resizable
10   MAX LV                0
11   Cur LV                0
12   Open LV               0
13   Max PV                0
14   Cur PV                1
15   Act PV                1
16   VG Size               <500.00 GiB
17   PE Size               4.00 MiB
18   Total PE              127999
19   Alloc PE / Size       0 / 0
20   Free  PE / Size       127999 / <500.00 GiB
21   VG UUID               ndS3xn-MIrv-cn2Y-TTxb-pWYi-EBhB-f9wCp2

創建邏輯卷

1 [root@VM_15_115_centos ~]# lvdisplay
2 [root@VM_15_115_centos ~]#
3 [root@VM_15_115_centos ~]# lvcreate -l +100%FREE centos
4   Logical volume "lvol0" created.

注意:centos為上文創建的卷組的名字

此處將該卷組全部剩余空間划入本邏輯卷

創建結果:

 1 [root@VM_15_115_centos ~]# lvdisplay
 2   --- Logical volume ---
 3   LV Path                /dev/centos/lvol0
 4   LV Name                lvol0
 5   VG Name                centos
 6   LV UUID                ulWla4-C4SA-WE2e-HnjW-kHbB-TVSp-hstVYQ
 7   LV Write Access        read/write
 8   LV Creation host, time VM_15_115_centos, 2019-07-06 08:52:24 +0800
 9   LV Status              available
10   # open                 0
11   LV Size                <500.00 GiB
12   Current LE             127999
13   Segments               1
14   Allocation             inherit
15   Read ahead sectors     auto
16   - currently set to     8192
17   Block device           252:0
18 
19 [root@VM_15_115_centos ~]#

 

通知文件系統生效

新卷則建議采用 xfs 類型,當然,采用 ext4 亦可。

但需要注意的是,xfs文件系統使用 xfs_growfs,ext文件系統使用 resize2fs;xfs文件系統只支持增大不支持縮小。

 

先格式化邏輯卷磁盤分區,卷的名字從 lvdisplay 中查找

 1 [root@VM_15_115_centos ~]# mkfs.xfs /dev/centos/lvol0
 2 meta-data=/dev/centos/lvol0      isize=512    agcount=4, agsize=32767744 blks
 3          =                       sectsz=512   attr=2, projid32bit=1
 4          =                       crc=1        finobt=0, sparse=0
 5 data     =                       bsize=4096   blocks=131070976, imaxpct=25
 6          =                       sunit=0      swidth=0 blks
 7 naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
 8 log      =internal log           bsize=4096   blocks=63999, version=2
 9          =                       sectsz=512   sunit=0 blks, lazy-count=1
10 realtime =none                   extsz=4096   blocks=0, rtextents=0
11 [root@VM_15_115_centos ~]#

然后修改文件系統掛載表文件/etc/fstab

 1 [root@VM_15_115_centos ~]# cat /etc/fstab
 2 UUID=4b499d76-769a-40a0-93dc-4a31a59add28            /                    ext4       noatime,acl,user_xattr 1 1
 3 proc                 /proc                proc       defaults              0 0
 4 sysfs                /sys                 sysfs      noauto                0 0
 5 debugfs              /sys/kernel/debug    debugfs    noauto                0 0
 6 devpts               /dev/pts             devpts     mode=0620,gid=5       0 0
 7 [root@VM_15_115_centos ~]#
 8 [root@VM_15_115_centos ~]#
 9 [root@VM_15_115_centos ~]#
10 [root@VM_15_115_centos ~]#
11 [root@VM_15_115_centos ~]#
12 [root@VM_15_115_centos ~]# vi /etc/fstab
13 [root@VM_15_115_centos ~]#
14 [root@VM_15_115_centos ~]#
15 [root@VM_15_115_centos ~]#
16 [root@VM_15_115_centos ~]# cat /etc/fstab
17 UUID=4b499d76-769a-40a0-93dc-4a31a59add28            /                    ext4       noatime,acl,user_xattr 1 1
18 proc                 /proc                proc       defaults              0 0
19 sysfs                /sys                 sysfs      noauto                0 0
20 debugfs              /sys/kernel/debug    debugfs    noauto                0 0
21 devpts               /dev/pts             devpts     mode=0620,gid=5       0 0
22 /dev/centos/lvol0    /data                xfs        defaults              0 0
23 [root@VM_15_115_centos ~]#

再掛載,然后查看掛載結果

 1 [root@VM_15_115_centos ~]# mount -a
 2 [root@VM_15_115_centos ~]#
 3 [root@VM_15_115_centos ~]# df -h
 4 文件系統                  容量  已用  可用 已用% 掛載點
 5 /dev/vda1                  50G  1.6G   45G    4% /
 6 devtmpfs                  7.8G     0  7.8G    0% /dev
 7 tmpfs                     7.8G   24K  7.8G    1% /dev/shm
 8 tmpfs                     7.8G  496K  7.8G    1% /run
 9 tmpfs                     7.8G     0  7.8G    0% /sys/fs/cgroup
10 tmpfs                     1.6G     0  1.6G    0% /run/user/0
11 /dev/mapper/centos-lvol0  500G   33M  500G    1% /data
12 [root@VM_15_115_centos ~]#

至此,操作成功。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM