Linux系統(tǒng)怎么擴(kuò)容根目錄磁盤(pán)空間

小億
208
2023-07-18 17:14:58
欄目: 云計(jì)算

一、使用背景

linux根目錄磁盤(pán)空間不夠用了,當(dāng)修改了虛擬機(jī)模版增加磁盤(pán)大小或者插入了一塊新硬盤(pán),但是發(fā)現(xiàn)系統(tǒng)里的大小還是沒(méi)改變。產(chǎn)生的原因是沒(méi)有給磁盤(pán)格式化,沒(méi)有增加分區(qū)。

二、操作方法

1. 查看磁盤(pán)空間大小,使用df -h 命令,發(fā)現(xiàn)掛載根目錄節(jié)點(diǎn)的/dev/mapper/ubuntu14--vg-root 只有28g容量。

root@ubuntu14:/opt# df -h
文件系統(tǒng)            容量 已用 可用 已用% 掛載點(diǎn)
/dev/mapper/ubuntu14--vg-root  28g  23g 3.3g  88% /
none              4.0k   0 4.0k  0% /sys/fs/cgroup
udev              3.9g 4.0k 3.9g  1% /dev
tmpfs             799m 384k 799m  1% /run
none              5.0m   0 5.0m  0% /run/lock
none              3.9g   0 3.9g  0% /run/shm
none              100m   0 100m  0% /run/user
/dev/vda1           236m  37m 188m  17% /boot

2. 增加磁盤(pán)空間,例如下圖使用vm虛擬機(jī)增加的方式。

3.使用fdisk -l命令查看磁盤(pán)信息。當(dāng)看到第一行disk /dev/vda: 161.1 gb與實(shí)際df -h顯示內(nèi)容不符時(shí),說(shuō)明增加磁盤(pán)成功了。

root@ubuntu14:/opt# fdisk -l

disk /dev/vda: 161.1 gb, 161061273600 bytes
16 heads, 63 sectors/track, 312076 cylinders, total 314572800 sectors
units = 扇區(qū) 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

  設(shè)備 啟動(dòng)   起點(diǎn)     終點(diǎn)   塊數(shù)  id 系統(tǒng)
/dev/vda1  *    2048   499711   248832  83 linux
/dev/vda2     501758  62912511  31205377  5 擴(kuò)展
/dev/vda5     501760  62912511  31205376  8e linux lvm

disk /dev/mapper/ubuntu14--vg-root: 29.8 gb, 29804724224 bytes
255 heads, 63 sectors/track, 3623 cylinders, total 58212352 sectors
units = 扇區(qū) of 1 * 512 = 512 bytes
sector size (logical/physical): 512 bytes / 512 bytes
i/o size (minimum/optimal): 512 bytes / 512 bytes
disk identifier: 0x00000000

disk /dev/mapper/ubuntu14--vg-root doesn't contain a valid partition table

disk /dev/mapper/ubuntu14--vg-swap_1: 2147 mb, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders, total 4194304 sectors
units = 扇區(qū) of 1 * 512 = 512 bytes
sector size (logical/physical): 512 bytes / 512 bytes
i/o size (minimum/optimal): 512 bytes / 512 bytes
disk identifier: 0x00000000

disk /dev/mapper/ubuntu14--vg-swap_1 doesn't contain a valid partition table

4.使用fdisk /dev/vda, 創(chuàng)建新分區(qū)。

注意:不同操作系統(tǒng)的磁盤(pán)命名方式不同,有些是/dev/sda。具體使用方式參考第三步中fdisk -l首行顯示disk。如筆者在該系統(tǒng)中首行顯示為disk /dev/vda: 161.1 gb, 161061273600 bytes ,故使用命令fdisk /dev/vda)

按照下面linux操作進(jìn)行分區(qū)。

root@ubuntu14:/opt# 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 新分區(qū)
partition type:
  p  primary (1 primary, 1 extended, 2 free)
  l  logical (numbered from 5)
select (default p): p  #選擇主分區(qū)
分區(qū)號(hào) (1-4,默認(rèn)為 3): 3 #分區(qū)序號(hào)
起始 sector (499712-314572799,默認(rèn)為 499712): #分區(qū)開(kāi)始回車(chē)默認(rèn)
將使用默認(rèn)值 499712
last sector, +扇區(qū) or +size{k,m,g} (499712-501757,默認(rèn)為 501757): 
將使用默認(rèn)值 501757

命令(輸入 m 獲取幫助): n #new 新分區(qū)
partition type:
  p  primary (2 primary, 1 extended, 1 free)
  l  logical (numbered from 5)
select (default p): p #選擇主分區(qū)
selected partition 4  #分區(qū)序號(hào)
起始 sector (62912512-314572799,默認(rèn)為 62912512): #分區(qū)開(kāi)始回車(chē)默認(rèn)
將使用默認(rèn)值 62912512
last sector, +扇區(qū) or +size{k,m,g} (62912512-314572799,默認(rèn)為 314572799): 
將使用默認(rèn)值 314572799

命令(輸入 m 獲取幫助): t  #修改分區(qū)格式
分區(qū)號(hào) (1-5): 4  #修改分區(qū)號(hào)
hex code (type l to list codes): 8e #格式選擇8e  linux lvm
changed system type of partition 4 to 8e (linux lvm)

命令(輸入 m 獲取幫助): p #顯示分區(qū)信息

disk /dev/vda: 161.1 gb, 161061273600 bytes
16 heads, 63 sectors/track, 312076 cylinders, total 314572800 sectors
units = 扇區(qū) 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

  設(shè)備 啟動(dòng)   起點(diǎn)     終點(diǎn)   塊數(shù)  id 系統(tǒng)
/dev/vda1  *    2048   499711   248832  83 linux
/dev/vda2     501758  62912511  31205377  5 擴(kuò)展
/dev/vda3     499712   501757    1023  83 linux
/dev/vda4    62912512  314572799  125830144  8e linux lvm
/dev/vda5     501760  62912511  31205376  8e linux lvm
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: 設(shè)備或資源忙.
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操作系統(tǒng),使用reboot命令。

6. 創(chuàng)建物理卷,使用pvcreate /dev/vda4命令。

root@ubuntu14:~# pvcreate /dev/vda4
 physical volume "/dev/vda4" successfully created

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

root@ubuntu14:~# pvdisplay
 --- physical volume ---
 pv name        ubuntu14-vg
 pv size        29.76 gib / not usable 2.00 mib
 allocatable      yes (but full)
 pe size        4.00 mib
 total pe       7618
 free pe        0
 allocated pe     7618
 pv uuid        xr32ty-arqc-iqc6-ocx7-aa9x-kdje-bhmabd
 "/dev/vda4"        /dev/vda5
 vg name is a new physical volume of "120.00 gib"
 --- new physical volume ---
 pv name        /dev/vda4
 vg name        
 pv size        120.00 gib
 allocatable      no
 pe size        0  
 total pe       0
 free pe        0
 allocated pe     0
 pv uuid        mvk0it-ldgp-ewwz-fzqm-tdyy-krq-nbk122

8. 將添加新的物理卷,加載到ubuntu14-vg卷組,使用vgextend ubuntu14-vg /dev/vda4命令。

root@ubuntu14:~# vgextend ubuntu14-vg /dev/vda4
volume group "ubuntu14-vg" successfully extended

9. 查看卷組信息,使用vgdisplay命令。

root@ubuntu14:~# vgdisplay         
 --- volume group ---
 vg name        ubuntu14-vg
 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        149.76 gib
 pe size        4.00 mib
 total pe       38338
 alloc pe / size    7618 / 29.76 gib
 free pe / size    30720 / 120.00 gib
 vg uuid        cq2z6g-p6j1-s2gm-mibz-d63v-njks-dhye7d

10.增加ubuntu14-vg大小,增加120g。使用lvresize -l +120g /dev/mapper/ubuntu14--vg-root命令。

root@ubuntu14:~# lvresize -l +120g /dev/mapper/ubuntu14--vg-root
 extending logical volume root to 147.76 gib
 logical volume root successfully resized

11. 重新識(shí)別ubuntu14-vg大小,使用resize2fs /dev/mapper/ubuntu14--vg-root命令。

root@ubuntu14:~# resize2fs /dev/mapper/ubuntu14--vg-root
resize2fs 1.42.9 (4-feb-2014)
filesystem at /dev/mapper/ubuntu14--vg-root is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 10
the filesystem on /dev/mapper/ubuntu14--vg-root is now 38733824 blocks long.

12.查看擴(kuò)容后的大小 ,使用df -h命令。

root@ubuntu14:~# df -h
文件系統(tǒng)            容量 已用 可用 已用% 掛載點(diǎn)
/dev/mapper/ubuntu14--vg-root 146g  23g 117g  17% /
none              4.0k   0 4.0k  0% /sys/fs/cgroup
udev              3.9g 4.0k 3.9g  1% /dev
tmpfs             799m 388k 799m  1% /run
none              5.0m   0 5.0m  0% /run/lock
none              3.9g   0 3.9g  0% /run/shm
none              100m   0 100m  0% /run/user
/dev/vda1           236m  37m 188m  17% /boot

當(dāng)看到/dev/mapper/ubuntu14--vg-root 已經(jīng)更新到新的大小時(shí),則表示增加成功。 

0