1. 程式人生 > >Linux系統擴容根目錄磁碟空間的操作方法

Linux系統擴容根目錄磁碟空間的操作方法

一、使用背景
Linux根目錄磁碟空間不夠用了,當修改了虛擬機器模版增加磁碟大小或者插入了一塊新硬碟,但是發現系統裡的大小還是沒改變。
產生的原因是沒有給磁碟格式化,沒有增加分割槽。
二、操作方法
1.檢視磁碟空間大小,使用df -h 命令,發現掛載根目錄節點的/dev/mapper/vg_node003-lv_root 只有7.1G容量。

[[email protected] ~]# df -h
Filesystem                                              Size  Used Avail Use% Mounted on
/dev/mapper/vg_node003-lv_root                          18G   9.3G  7.1G  57% /
tmpfs                                                   491M     0  491M   0% /dev/shm
/dev/sda1                                               477M   42M  411M  10% /boot

2.增加磁碟空間,例如下圖使用VM虛擬機器增加的方式。
在這裡插入圖片描述
3.使用fdisk -l命令檢視磁碟資訊。當看到第一行Disk /dev/sda: 53.7 GB與實際df -h顯示內容不符時,說明增加磁碟成功了。

[[email protected] ~]# fdisk -l

Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00092cf6

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        2611    20458496   8e  Linux LVM

Disk /dev/mapper/vg_node003-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/vg_node003-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

4.使用fdisk /dev/sda, 建立新分割槽。
注意:不同作業系統的磁碟命名方式不同,有些是/dev/vda。具體使用方式參考第三步中fdisk -l首行顯示Disk。如筆者在該系統中首行顯示為Disk /dev/sda: 53.7 GB, 53687091200 bytes ,故使用命令fdisk /dev/sda)
按照下面Linux操作進行分割槽。

[[email protected] ~]# fdisk /dev/vda
 
命令(輸入 m 獲取幫助): m
命令操作
  a  toggle a bootable flag
  b  edit bsd disklabel
  c  toggle the dos compatibility flag
  d  delete a partition
  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 獲取幫助): n #new 新分割槽
Partition type:
  p  primary (1 primary, 1 extended, 2 free)
  l  logical (numbered from 5)
Select (default p): p  #選擇主分割槽
分割槽號 (1-4,預設為 3): 3 #分割槽序號
起始 sector (499712-314572799,預設為 499712): #分割槽開始回車預設
將使用預設值 499712
Last sector, +扇區 or +size{K,M,G} (499712-501757,預設為 501757): 
將使用預設值 501757
 命令(輸入 m 獲取幫助): n #new 新分割槽
Partition type:
  p  primary (2 primary, 1 extended, 1 free)
  l  logical (numbered from 5)
Select (default p): p #選擇主分割槽
Selected partition 4  #分割槽序號
起始 sector (62912512-314572799,預設為 62912512): #分割槽開始回車預設
將使用預設值 62912512
Last sector, +扇區 or +size{K,M,G} (62912512-314572799,預設為 314572799): 
將使用預設值 314572799
 
命令(輸入 m 獲取幫助): t  #修改分割槽格式
分割槽號 (1-5): 4  #修改分割槽號
Hex code (type L to list codes): 8e #格式選擇8e  linux LVM
Changed system type of partition 4 to 8e (Linux LVM)
 
命令(輸入 m 獲取幫助): p #顯示分割槽資訊
 
Disk /dev/sda: 161.1 GB, 161061273600 bytes
16 heads, 63 sectors/track, 312076 cylinders, total 314572800 sectors
Units = 扇區 of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0001a023
 
  裝置 啟動   起點     終點   塊數  Id 系統
/dev/sda1  *    2048   499711   248832  83 Linux
/dev/sda2     501758  62912511  31205377  5 擴充套件
/dev/sda3     499712   501757    1023  83 Linux
Partition table entries are not in disk order
命令(輸入 m 獲取幫助): w #儲存資訊
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 裝置或資源忙.
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.

5.重啟Linux作業系統,使用reboot命令。
6.建立物理卷,使用pvcreate /dev/sda3命令。

[[email protected] dev]# pvcreate /dev/sda3 
  Physical volume "/dev/sda3" successfully created

7.檢視新建的物理卷和大小,使用pvdisplay命令。

[[email protected] dev]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               vg_node003
  PV Size               19.51 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4994
  Free PE               0
  Allocated PE          4994
  PV UUID               UC4j5m-rJO4-DroW-E92L-n5Vj-SU0U-PJyfJf
   
  "/dev/sda3" is a new physical volume of "30.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name               
  PV Size               30.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               nng8Gx-51fP-DIBH-YeF4-Sw7B-S0jQ-oGXfpa

8.將新增新的物理卷,載入到vg_node003卷組,使用vgextend vg_node003 /dev/sda3命令。

[[email protected] dev]# vgextend vg_node003 /dev/sda3
  Volume group "vg_node003" successfully extended

9.檢視卷組資訊,使用vgdisplay命令。

[[email protected] dev]# vgdisplay
  --- Volume group ---
  VG Name               vg_node003
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               49.50 GiB
  PE Size               4.00 MiB
  Total PE              12673
  Alloc PE / Size       4994 / 19.51 GiB
  Free  PE / Size       7679 / 30.00 GiB
  VG UUID               v37e5T-4iPX-olm0-Claw-8EfO-axHf-evCAfc

10.增加vg_node003大小,增加30G。使用lvresize -L +30G /dev/mapper/vg_node003-lv_root命令。
注:vg_node003-lv_root在/dev/mapper裡面檢視

[[email protected] mapper]# lvresize -L +29.99G /dev/mapper/vg_node003-lv_root  
  Rounding size to boundary between physical extents: 29.99 GiB
  Size of logical volume vg_node003/lv_root changed from 17.51 GiB (4482 extents) to 47.50 GiB (12160 extents).
  Logical volume lv_root successfully resized

11.重新識別vg_node003大小,使用resize2fs /dev/mapper/vg_node003-lv_root命令。

[[email protected] mapper]# resize2fs /dev/mapper/vg_node003-lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_node003-lv_root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 3
Performing an on-line resize of /dev/mapper/vg_node003-lv_root to 12451840 (4k) blocks.
The filesystem on /dev/mapper/vg_node003-lv_root is now 12451840 blocks long.

12.檢視擴容後的大小 ,使用df -h命令。

[[email protected] mapper]# df -h
Filesystem                         Size  Used Avail Use% Mounted on
/dev/mapper/vg_node003-lv_root      47G  9.3G   36G  21% /
tmpfs                              491M     0  491M   0% /dev/shm
/dev/sda1                          477M   42M  411M  10% /boot

當看到/dev/mapper/vg_node003-lv_root 已經更新到新的大小時,則表示增加成功。
三、總結
以上所述是小編給大家介紹的Linux系統擴容根目錄磁碟空間的操作方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。