ubuntu 服務器添加新磁盤

原文

 

Linux系統擴容根目錄磁盤空間的操做方法

 

這篇文章主要介紹了Linux系統擴容根目錄磁盤空間的操做方法,很是不錯,具備必定的參考借鑑價值,須要的朋友能夠參考下

1、使用背景linux

Linux根目錄磁盤空間不夠用了,當修改了虛擬機模版增長磁盤大小或者插入了一塊新硬盤,可是發現系統裏的大小仍是沒改變。ubuntu

產生的緣由是沒有給磁盤格式化,沒有增長分區。bash

2、操做方法app

1. 查看磁盤空間大小,使用df -h 命令,發現掛載根目錄節點的/dev/mapper/ubuntu14--vg-root 只有28G容量。ide

1
2
3
4
5
6
7
8
9
10
root@ubuntu14: /opt # df -h
文件系統            容量 已用 可用 已用% 掛載點
/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. 增長磁盤空間,例以下圖使用VM虛擬機增長的方式。網站

3.使用fdisk -l命令查看磁盤信息。當看到第一行Disk /dev/vda: 161.1 GB與實際df -h顯示內容不符時,說明增長磁盤成功了。ui

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
root@ubuntu14: /opt # fdisk -l
 
Disk /dev/vda : 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/vda1  *    2048   499711   248832  83 Linux
/dev/vda2     501758  62912511  31205377  5 擴展
/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 = 扇區 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 = 扇區 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, 建立新分區。this

注意:不一樣操做系統的磁盤命名方式不一樣,有些是/dev/sda。具體使用方式參考第三步中fdisk -l首行顯示Disk。如筆者在該系統中首行顯示爲Disk /dev/vda: 161.1 GB, 161061273600 bytes ,故使用命令fdisk /dev/vda)spa

按照下面Linux操做進行分區。操作系統

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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): #分區開始回車默認
將使用默認值 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/vda : 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/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 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: 設備或資源忙.
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/vda4命令。

1
2
root@ubuntu14:~ # pvcreate /dev/vda4
  Physical volume "/dev/vda4" successfully created

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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命令。

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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命令。

1
2
3
root@ubuntu14:~ # lvresize -L +120G /dev/mapper/ubuntu14--vg-root
  Extending logical volume root to 147.76 GiB
  Logical volume root successfully resized

11. 從新識別ubuntu14-vg大小,使用resize2fs /dev/mapper/ubuntu14--vg-root命令。

1
2
3
4
5
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.查看擴容後的大小 ,使用df -h命令。

1
2
3
4
5
6
7
8
9
10
root@ubuntu14:~ # df -h
文件系統            容量 已用 可用 已用% 掛載點
/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

當看到/dev/mapper/ubuntu14--vg-root 已經更新到新的大小時,則表示增長成功。

總結

以上所述是小編給你們介紹的Linux系統擴容根目錄磁盤空間的操做方法,但願對你們有所幫助,若是你們有任何疑問請給我留言,小編會及時回覆你們的。在此也很是感謝你們對腳本之家網站的支持!

相關文章
相關標籤/搜索