IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    在线扩展 LVM root 分区

    Phoenix Nemo发表于 2020-09-14 04:20:20
    love 0

    才不是没东西写了呢

    遇到一个奇葩的原因导致 root 分区被占满的。而且还是奇葩的 CentOS,root 分区是 LVM,Hypervisor 里扩展磁盘后无法直接用 resize2fs。

    既然如此就只能暴力重建分区咯。

    重建分区

    操作前确保操作的分区和之后新建时 Start 保持一致,修改分区表后不至于分区崩坏。

    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
    ~> fidks /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

    Disk /dev/sda: 103.1 GB, 103079215104 bytes, 201326592 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: 0x000a8e23

    Device Boot Start End Blocks Id System
    /dev/sda1 * 2048 2099199 1048576 83 Linux
    /dev/sda2 2099200 50331647 24116224 8e Linux LVM

    Command (m for help): d
    Partition number (1,2, default 2): 2
    Partition 2 is deleted

    Command (m for help): n
    Partition type:
    p primary (1 primary, 0 extended, 3 free)
    e extended
    Select (default p):
    Using default response p
    Partition number (2-4, default 2):
    First sector (2099200-201326591, default 2099200):
    Using default value 2099200
    Last sector, +sectors or +size{K,M,G} (2099200-201326591, default 201326591):
    Using default value 201326591
    Partition 2 of type Linux and of size 95 GiB is set

    Command (m for help): 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.

    ~> partprobe

    现在就可以看到 /dev/sda2 的大小已经变化了:

    1
    2
    3
    4
    5
    6
    7
    8
    ~> lsblk
    NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    sda 8:0 0 96G 0 disk
    ├─sda1 8:1 0 1G 0 part /boot
    └─sda2 8:2 0 95G 0 part
    ├─centos-root 253:0 0 20.6G 0 lvm /
    └─centos-swap 253:1 0 2.4G 0 lvm [SWAP]
    sr0 11:0 1 906M 0 rom

    扩展 Volume Group

    VG 的好处也就是能够灵活扩展分区大小…

    1
    2
    3
    ~> pvresize /dev/sda2
    Physical volume "/dev/sda2" changed
    1 physical volume(s) resized / 0 physical volume(s) not resized
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    ~> vgdisplay
    --- Volume group ---
    VG Name centos
    System ID
    Format lvm2
    Metadata Areas 1
    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 1
    Act PV 1
    VG Size <95.00 GiB
    PE Size 4.00 MiB
    Total PE 24319
    Alloc PE / Size 5887 / <23.00 GiB
    Free PE / Size 18432 / 72.00 GiB
    VG UUID TpbtuH-AjTZ-PU3v-UN31-FvfX-kSLv-xLiJG7

    至此已经可以看到 Free PE 的部分有多出的 72GB 空间。

    扩展 Logic Volume

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    ~> lvextend -r -l +100%FREE /dev/centos/root
    Size of logical volume centos/root changed from 20.59 GiB (5272 extents) to 92.59 GiB (23704 extents).
    Logical volume centos/root successfully resized.
    meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=1349632 blks
    = sectsz=512 attr=2, projid32bit=1
    = crc=1 finobt=0 spinodes=0
    data = bsize=4096 blocks=5398528, imaxpct=25
    = sunit=0 swidth=0 blks
    naming =version 2 bsize=4096 ascii-ci=0 ftype=1
    log =internal bsize=4096 blocks=2636, version=2
    = sectsz=512 sunit=0 blks, lazy-count=1
    realtime =none extsz=4096 blocks=0, rtextents=0
    data blocks changed from 5398528 to 24272896

    确认效果:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    ~> lsblk
    NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    sda 8:0 0 96G 0 disk
    ├─sda1 8:1 0 1G 0 part /boot
    └─sda2 8:2 0 95G 0 part
    ├─centos-root 253:0 0 92.6G 0 lvm /
    └─centos-swap 253:1 0 2.4G 0 lvm [SWAP]
    sr0 11:0 1 906M 0 rom

    ~> df -h
    Filesystem Size Used Avail Use% Mounted on
    /dev/mapper/centos-root 93G 21G 73G 23% /
    devtmpfs 3.9G 0 3.9G 0% /dev
    tmpfs 3.9G 8.0K 3.9G 1% /dev/shm
    tmpfs 3.9G 8.6M 3.9G 1% /run
    tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
    /dev/sda1 1014M 185M 830M 19% /boot
    tmpfs 783M 0 783M 0% /run/user/0

    搞定收工(‘・ω・’)

    后话

    其实虚拟机还用 LVM 的话,直接新增一块虚拟硬盘是最方便的方案。直接 vgextend 一路搞定…



沪ICP备19023445号-2号
友情链接