虛擬機擴展Linux根目錄磁盤空間


簡要擴展空間方法http://www.kwx.gd/CentOSApp/Xen-Centos6-Mounted-HardDrive.html

 

 

最近在VMware虛擬機上使用Centos,用着用着,發現虛擬機的磁盤空間不夠了。通過以下步驟成功擴展了根目錄的磁盤空間

 

 

1、Centos 關機,選擇編輯虛擬機設置,硬盤,在實用工具那里選擇“擴展”

填寫擴展的磁盤容量

點擊擴展后,VMware會提示磁盤已成功擴展。您必須從客戶機操作系統內部對磁盤重新進行分區和擴展文件系統。也就是說,這里擴展的磁盤空間,在操作系統里面還不可用,還沒生效,需要在操作系統里面進行設置后才能使用。

2、啟動客戶機操作系統Centos,查看磁盤情況

輸入指令

fdisk -l

顯示結果如下

  • Disk /dev/loop0: 107.4 GB, 107374182400 bytes
    255 heads, 63 sectors/track, 13054 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/loop1: 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


    Disk /dev/sda: 37.6 GB, 37580963840 bytes
    255 heads, 63 sectors/track, 4568 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: 0x000c8278

    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 1306 9972736 8e Linux LVM

    Disk /dev/mapper/vg_test001-lv_root: 9135 MB, 9135194112 bytes
    255 heads, 63 sectors/track, 1110 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_test001-lv_swap: 1073 MB, 1073741824 bytes
    255 heads, 63 sectors/track, 130 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/docker-253:0-264224-pool: 107.4 GB, 107374182400 bytes
    255 heads, 63 sectors/track, 13054 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 65536 bytes
    Disk identifier: 0x00000000

其中,可以看到現在的分區情況如下

  • 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   306   9972736  8e  Linux LVM

3、創建分區 sda3

輸入指令

fdisk /dev/sda

然后根據命令提示創建分區,具體見以下 # 后面的注釋

WARNING: DOS-compatible mode is deprecated. It s strongly recommended to
         switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): n # 輸入 n 表示創建新分區 Command action e extended p primary partition (1-4) p Partition number (1-4): 3 # 輸入 3 表示sda3,因為前面已經有兩個了 First cylinder (1306-4568, default 1306): # 回車,按默認值 Using default value 1306 Last cylinder, +cylinders or +size{K,M,G} (1306-4568, default 4568): # 回車,按默認值 Using default value 4568 Command (m for help): w # 輸入 w 保存退出 The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. 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.

再次查看分區情況

fdisk -l

這時可以看到

  • Disk /dev/loop0: 107.4 GB, 107374182400 bytes
    255 heads, 63 sectors/track, 13054 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/loop1: 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


    Disk /dev/sda: 37.6 GB, 37580963840 bytes
    255 heads, 63 sectors/track, 4568 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: 0x000c8278

    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 1306 9972736 8e Linux LVM
    /dev/sda3 1306 4568 26206700 83 Linux

    Disk /dev/mapper/vg_test001-lv_root: 9135 MB, 9135194112 bytes
    255 heads, 63 sectors/track, 1110 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_test001-lv_swap: 1073 MB, 1073741824 bytes
    255 heads, 63 sectors/track, 130 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/docker-253:0-264224-pool: 107.4 GB, 107374182400 bytes
    255 heads, 63 sectors/track, 13054 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 65536 bytes
    Disk identifier: 0x00000000

其中,分區已變為3個了,但這時還沒掛載,還不能用

  • 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    1306   9972736  8e  Linux LVM
  • /dev/sda3       1306   4568  26206700    83   Linux

4、重啟操作系統

reboot

5、格式化新分區為ext3

輸入指令

mkfs -t ext3 /dev/sda3

結果為

  • mke2fs 1.41.12 (17-May-2010)
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    Stride=0 blocks, Stripe width=0 blocks
    1638400 inodes, 6551675 blocks
    327583 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=4294967296
    200 block groups
    32768 blocks per group, 32768 fragments per group
    8192 inodes per group
    Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
    4096000

    Writing inode tables: done
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done

    This filesystem will be automatically checked every 33 mounts or
    180 days, whichever comes first. Use tune2fs -c or -i to override.

6、將物理硬盤分區初始化為物理卷,以便被LVM使用,輸入指令

lvs
pvcreate /dev/sda3

7、向卷組中添加物理卷來增加卷組的容量

查看lvm組名,輸入指令 df -h

[root@test001 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_test001-lv_root #這個是卷組 8.3G 5.4G 2.5G 69% / tmpfs 1001M 0 1001M 0% /dev/shm /dev/sda1 477M 54M 394M 12% /boot

其中,vg_test001 就是 根 所在的組名

輸入指令,增加卷組的容量

vgextend vg_test001 /dev/sda3  #組名根據實際

8、查看可擴展的空間大小

輸入指令,顯示LNM卷組的元數據信息

vgdisplay

結果為

    • --- Volume group ---
      VG Name vg_test001
      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 34.50 GiB
      PE Size 4.00 MiB
      Total PE 8831
      Alloc PE / Size 2434 / 9.51 GiB
      Free PE / Size 6397 / 24.99 GiB  # 剩余可分配空間,1G=256 
      VG UUID JuPKc8-Ydps-1bsN-0Wld-fbYS-0Hd2-NgvX1q

其中 Free PE / Size 就是可供分配的自由空間,最多有 24.99G,在擴展時輸入小於該值 

9、擴充磁盤空間

lvextend -L+24G /dev/mapper/vg_test001-lv_root /dev/sda3

lvextend -L+6397 /dev/mapper/vg_test001-lv_root /dev/sda3

10、使用e2fsck指令檢查文件系統錯誤

e2fsck -a /dev/mapper/vg_test001-lv_root

11、使用resize2fs指令來增大或者收縮未加載的“ext2/ext3”文件系統的大小

resize2fs /dev/mapper/vg_test001-lv_root

12、查看擴展空間后的結果

[root@test001 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_test001-lv_root 32G 5.4G 26G 18% / tmpfs 1001M 0 1001M 0% /dev/shm /dev/sda1 477M 54M 394M 12% /boot

可以看出,在根目錄下磁盤空間已經擴容了,大功告成!


免責聲明!

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



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