Linux系統系統盤擴容

  • 2020 年 3 月 14 日
  • 筆記

  在Linux學習過程中,可能會遇到根目錄存儲空間不足的問題,這時候如果只是新增一塊硬盤並掛載到某個目錄上,還需要將數據轉移至新的硬盤中才能緩解存儲壓力。這種操作未免有些繁瑣,那可不可以直接對跟目錄進行擴容呢?當然是可以的,接下來就給大家介紹操作步驟:

  實驗環境:RHEL7系統(默認已部署LVM)、VMware Workstation 12虛擬軟件

  第1步:給虛擬機新增一塊20GB的硬盤

   第2步:查看系統盤的分區類型,最後記得輸入q不保存退出

[root@linuxprobe ~]# fdisk /dev/sda  Welcome to fdisk (util-linux 2.23.2).    Changes will remain in memory only, until you decide to write them.  Be careful before using the write command.      Command (m for help): p      //輸入p查看分區信息    Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors  Units = sectors of 1 * 512 = 512 bytes  Sector size (logical/physical): 512 bytes / 512 bytes  I/O size (minimum/optimal): 512 bytes / 512 bytes  Disk label type: dos  Disk identifier: 0x00091636       Device Boot      Start         End      Blocks   Id  System  /dev/sda1   *        2048     1026047      512000   83  Linux  /dev/sda2         1026048    41943039    20458496   8e  Linux LVM    //分區類型為Linux LVM

 Command (m for help): q     //輸入q不保存退出

  第3步:對新硬盤進行分區,並設置分區類型

[root@linuxprobe ~]# fdisk /dev/sdb  Welcome to fdisk (util-linux 2.23.2).    Changes will remain in memory only, until you decide to write them.  Be careful before using the write command.      Command (m for help): n    //添加一個新分區  Partition type:     p   primary (0 primary, 0 extended, 4 free)     e   extended  Select (default p): p    //主分區  Partition number (1-4, default 1): 1    //分區編號為1  First sector (2048-41943039, default 2048): <此處按下回車鍵>  Using default value 2048  Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G    //分區大小為10GB  Partition 1 of type Linux and of size 10 GiB is set    Command (m for help): p    //再次查看分區信息    Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors  Units = sectors of 1 * 512 = 512 bytes  Sector size (logical/physical): 512 bytes / 512 bytes  I/O size (minimum/optimal): 512 bytes / 512 bytes  Disk label type: dos  Disk identifier: 0x1894a517       Device Boot      Start         End      Blocks   Id  System  /dev/sdb1            2048    20973567    10485760   83  Linux     //新的分區添加成功,不過分區類型不對    Command (m for help): t   //變更分區的類型  Selected partition 1  Hex code (type L to list all codes): 8e    //輸入8e即"Linux LVM"  Changed type of partition 'Linux' to 'Linux LVM'    Command (m for help): p     //再次查看分區信息    Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors  Units = sectors of 1 * 512 = 512 bytes  Sector size (logical/physical): 512 bytes / 512 bytes  I/O size (minimum/optimal): 512 bytes / 512 bytes  Disk label type: dos  Disk identifier: 0x1894a517       Device Boot      Start         End      Blocks   Id  System  /dev/sdb1            2048    20973567    10485760   8e  Linux LVM   //分區類型變更成功,與系統分區一致    Command (m for help): w      //最後記得要保存退出  The partition table has been altered!    Calling ioctl() to re-read partition table.  Syncing disks.

  第4步:讓新添加的硬盤分區支持LVM技術

[root@linuxprobe ~]# pvcreate /dev/sdb1    Physical volume "/dev/sdb1" successfully created

  第5步:查看當前系統的卷組,並將/dev/sdb1硬盤分區加入到卷組中

[root@linuxprobe ~]# vgdisplay    --- Volume group ---    VG Name               rhel    //卷組名為"rhel"    System ID    Format                lvm2    Metadata Areas        1    Metadata Sequence No  3    VG Access             read/write    VG Status             resizable    MAX LV                0    Cur LV                2    Open LV               2    Max PV                0    Cur PV                1    Act PV                1    VG Size               19.51 GiB     //卷組的總容量大小    PE Size               4.00 MiB    Total PE              4994    Alloc PE / Size       4994 / 19.51 GiB    Free  PE / Size       0 / 0    VG UUID               mGomiV-U0sF-wKii-YxOh-V7Gw-VEQ4-yIkcRD  [root@linuxprobe ~]# vgextend rhel /dev/sdb1         //把/dev/sdb1硬盤分區加入到rhel卷組中    Volume group "rhel" successfully extended  [root@linuxprobe ~]# vgdisplay    --- Volume group ---    VG Name               rhel    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               29.50 GiB      //卷組的容量增大了10GB    PE Size               4.00 MiB    Total PE              7553    Alloc PE / Size       4994 / 19.51 GiB    Free  PE / Size       2559 / 10.00 GiB    VG UUID               mGomiV-U0sF-wKii-YxOh-V7Gw-VEQ4-yIkcRD

  第6步:查看當前邏輯卷信息

[root@linuxprobe ~]# lvdisplay    --- Logical volume ---    LV Path                /dev/rhel/swap    //用來充當SWAP分區,我們這裡不管它    LV Name                swap    VG Name                rhel    LV UUID                d2gNWI-6Oin-9Q3r-OGLp-0nf5-0Dun-Z8EvgS    LV Write Access        read/write    LV Creation host, time localhost, 2020-02-15 20:19:35 +0800    LV Status              available    # open                 2    LV Size                2.00 GiB    Current LE             512    Segments               1    Allocation             inherit    Read ahead sectors     auto    - currently set to     256    Block device           253:1      --- Logical volume ---    LV Path                /dev/rhel/root   //該邏輯卷實際就是Linux系統盤,我們將對它進行擴容    LV Name                root     //邏輯卷名為root    VG Name                rhel    LV UUID                rI6Xvu-eCJx-0WFO-TuUj-LQWM-WuUc-3uE6zc    LV Write Access        read/write    LV Creation host, time localhost, 2020-02-15 20:19:35 +0800    LV Status              available    # open                 1    LV Size                17.51 GiB   //邏輯卷的大小,即系統盤大小    Current LE             4482    Segments               1    Allocation             inherit    Read ahead sectors     auto    - currently set to     256    Block device           253:0

  第7步:對root邏輯卷進行擴容操作

[root@linuxprobe ~]# lvextend -L +10G /dev/rhel/root    Extending logical volume root to 27.51 GiB    Insufficient free space: 2560 extents needed, but only 2559 available   //顯示實際空閑容量不足10GB,比10GB稍微小一點點  [root@linuxprobe ~]# lvextend -L +5G /dev/rhel/root    //這裡我們先擴容5GB    Extending logical volume root to 22.51 GiB    Logical volume root successfully resized  [root@linuxprobe ~]# lvdisplay    ----------------省略部分輸出內容------------------------      --- Logical volume ---    LV Path                /dev/rhel/root    LV Name                root    VG Name                rhel    LV UUID                rI6Xvu-eCJx-0WFO-TuUj-LQWM-WuUc-3uE6zc    LV Write Access        read/write    LV Creation host, time localhost, 2020-02-15 20:19:35 +0800    LV Status              available    # open                 1    LV Size                22.51 GiB   //容量已從17.51GB提升至22.51GB    Current LE             5762    Segments               2    Allocation             inherit    Read ahead sectors     auto    - currently set to     8192    Block device           253:0

  第8步:重置root邏輯卷的大小。xfs系統不需要先umount操作

[root@linuxprobe ~]# df -h  Filesystem             Size  Used Avail Use% Mounted on  /dev/mapper/rhel-root   18G  3.1G   15G  18% /  devtmpfs               985M     0  985M   0% /dev  tmpfs                  994M   80K  994M   1% /dev/shm  tmpfs                  994M  8.9M  986M   1% /run  tmpfs                  994M     0  994M   0% /sys/fs/cgroup  /dev/sr0               3.5G  3.5G     0 100% /media/cdrom  /dev/sda1              497M  119M  379M  24% /boot  [root@linuxprobe ~]# xfs_growfs /dev/rhel/root        //重置root邏輯卷大小  meta-data=/dev/mapper/rhel-root  isize=256    agcount=4, agsize=1147392 blks           =                       sectsz=512   attr=2, projid32bit=1           =                       crc=0  data     =                       bsize=4096   blocks=4589568, imaxpct=25           =                       sunit=0      swidth=0 blks  naming   =version 2              bsize=4096   ascii-ci=0 ftype=0  log      =internal               bsize=4096   blocks=2560, version=2           =                       sectsz=512   sunit=0 blks, lazy-count=1  realtime =none                   extsz=4096   blocks=0, rtextents=0  data blocks changed from 4589568 to 5900288  [root@linuxprobe ~]# df -h  Filesystem             Size  Used Avail Use% Mounted on  /dev/mapper/rhel-root   23G  3.1G   20G  14% /       //系統盤成功擴容至23G,且重啟依然生效  devtmpfs               985M     0  985M   0% /dev  tmpfs                  994M   80K  994M   1% /dev/shm  tmpfs                  994M  8.9M  986M   1% /run  tmpfs                  994M     0  994M   0% /sys/fs/cgroup  /dev/sr0               3.5G  3.5G     0 100% /media/cdrom  /dev/sda1              497M  119M  379M  24% /boot

 至此,Linux系統盤擴容操作完畢。

 知識補充:

  1、重置邏輯卷命令 “xfs_growfs /dev/rhel/root” 和 “xfs_growfs /dev/mapper/rhel-root” 效果一樣;

  2、如果是ext4文件系統,重置命令為”resize2fs /dev/rhel/root”;

  3、xfs文件系統只能擴容,不允許縮容。

 

[root@linuxprobe ~]# pvs    PV         VG   Fmt  Attr PSize  PFree    /dev/sda2  rhel lvm2 a--  19.51g    0    /dev/sdb1  rhel lvm2 a--  10.00g 5.00g      //還省5GB空閑容量