一、使用背景
Linux根目錄磁盤空間不夠用了,當修改了虛擬機模版增加磁盤大小或者插入了一塊新硬盤,但是發現系統里的大小還是沒改變。
產生的原因是沒有給磁盤格式化,沒有增加分區。
二、操作方法
1. 查看磁盤空間大小,使用df -h 命令,發現掛載根目錄節點的/dev/mapper/ubuntu14--vg-root 只有28G容量。
root@ubuntu14:/opt# df -h文件系統 容量 已用 可用 已用% 掛載點/dev/mapper/ubuntu14--vg-root 28G 23G 3.3G 88% /none 4.0K 0 4.0K 0% /sys/fs/cgroupudev 3.9G 4.0K 3.9G 1% /devtmpfs 799M 384K 799M 1% /runnone 5.0M 0 5.0M 0% /run/locknone 3.9G 0 3.9G 0% /run/shmnone 100M 0 100M 0% /run/user/dev/vda1 236M 37M 188M 17% /boot
2. 增加磁盤空間,例如下圖使用VM虛擬機增加的方式。

3.使用fdisk -l命令查看磁盤信息。當看到第一行Disk /dev/vda: 161.1 GB與實際df -h顯示內容不符時,說明增加磁盤成功了。
root@ubuntu14:/opt# fdisk -lDisk /dev/vda: 161.1 GB, 161061273600 bytes16 heads, 63 sectors/track, 312076 cylinders, total 314572800 sectorsUnits = 扇區 of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x0001a023 設備 啟動 起點 終點 塊數 Id 系統/dev/vda1 * 2048 499711 248832 83 Linux/dev/vda2 501758 62912511 31205377 5 擴展/dev/vda5 501760 62912511 31205376 8e Linux LVMDisk /dev/mapper/ubuntu14--vg-root: 29.8 GB, 29804724224 bytes255 heads, 63 sectors/track, 3623 cylinders, total 58212352 sectorsUnits = 扇區 of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00000000Disk /dev/mapper/ubuntu14--vg-root doesn't contain a valid partition tableDisk /dev/mapper/ubuntu14--vg-swap_1: 2147 MB, 2147483648 bytes255 heads, 63 sectors/track, 261 cylinders, total 4194304 sectorsUnits = 扇區 of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00000000Disk /dev/mapper/ubuntu14--vg-swap_1 doesn't contain a valid partition table
4.使用fdisk /dev/vda, 創建新分區。
注意:不同操作系統的磁盤命名方式不同,有些是/dev/sda。具體使用方式參考第三步中fdisk -l首行顯示Disk。如筆者在該系統中首行顯示為Disk /dev/vda: 161.1 GB, 161061273600 bytes ,故使用命令fdisk /dev/vda)
按照下面Linux操作進行分區。
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 新分區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): #分區開始回車默認將使用默認值 499712Last 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): #分區開始回車默認將使用默認值 62912512Last 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 LVMChanged system type of partition 4 to 8e (Linux LVM)命令(輸入 m 獲取幫助): p #顯示分區信息Disk /dev/vda: 161.1 GB, 161061273600 bytes16 heads, 63 sectors/track, 312076 cylinders, total 314572800 sectorsUnits = 扇區 of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x0001a023  設備 啟動   起點     終點   塊數  Id 系統/dev/vda1  *    2048   499711   248832  83 Linux/dev/vda2     501758  62912511  31205377  5 擴展/dev/vda3     499712   501757    1023  83 Linux/dev/vda4    62912512  314572799  125830144  8e Linux LVM/dev/vda5     501760  62912511  31205376  8e Linux LVMPartition 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 atthe next reboot or after you run partprobe(8) or kpartx(8)Syncing disks.
新聞熱點
疑難解答