2011-12-04

Shrinking the root filesystem

The default install of RedHat 5 fills the entire disk with the root filesystem: this may backfire later during machine lifetime.
Here's a way to resize the root filesystem.
Note that while the size increase can be done online, as of RedHat 5, size reduction must be performed in rescue mode.

Here's a tipical installation:

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                      7.7G  3.1G  4.3G  42% /
/dev/sda1              99M   19M   75M  21% /boot
tmpfs                 502M     0  502M   0% /dev/shm

# vgdisplay -v
    Finding all volume groups
    Finding volume group "VolGroup00"
  --- Volume group ---
  VG Name               VolGroup00
  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               9.88 GB
  PE Size               32.00 MB
  Total PE              316
  Alloc PE / Size       316 / 9.88 GB
  Free  PE / Size       0 / 0
  VG UUID               CJqD4r-kRQM-Cj9e-XwOr-6dn8-TkPH-MfTTyZ

  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol00
  VG Name                VolGroup00
  LV UUID                d4iToX-8m0c-jf9s-sMMN-VDpO-brE8-4kWhf2
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                7.91 GB
  Current LE             253
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol01
  VG Name                VolGroup00
  LV UUID                kEzo7d-AHuv-xiPD-3Qbo-v8yY-6w1D-DFMIwN
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                1.97 GB
  Current LE             63
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

  --- Physical volumes ---
  PV Name               /dev/sda2
  PV UUID               7bPvQd-H8yE-v00N-asPT-WQ92-6oCS-bBh6nj
  PV Status             allocatable
  Total PE / Free PE    316 / 0

The physical volume is full, with 0 free extents.

We are going to reduce / size to 6GB.
Boot in rescue mode

boot: linux rescue

Skip existing installation detection.
Activate existing volume groups:

# lvm vgscan
# lvm vgchange -ay /dev/VolGroup00

Run a filesystem check before resizing:

# e2fsck -f /dev/VolGroup00/LogVol00

Set the filesystem size a litte less than required, so the resizing of the underlying logical volume won't cut short the filesystem itself.

# resize2fs /dev/VolGroup00/LogVol00 5G

Resize the logical volume to the target size.

# lvm lvresize -L 6G /dev/VolGroup00/LogVol00

Now resize again the filesystem without giving any size, so it will fill up the logical volume.

# resize2fs /dev/VolGroup00/LogVol00

Reboot back into the system.
You can see the new / size:

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                      5.9G  3.1G  2.7G  54% /
/dev/sda1              99M   19M   75M  21% /boot
tmpfs                 502M     0  502M   0% /dev/shm

# vgdisplay -v
    Finding all volume groups
    Finding volume group "VolGroup00"
  --- Volume group ---
  VG Name               VolGroup00
  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               9.88 GB
  PE Size               32.00 MB
  Total PE              316
  Alloc PE / Size       255 / 7.97 GB
  Free  PE / Size       61 / 1.91 GB
  VG UUID               CJqD4r-kRQM-Cj9e-XwOr-6dn8-TkPH-MfTTyZ

  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol00
  VG Name                VolGroup00
  LV UUID                d4iToX-8m0c-jf9s-sMMN-VDpO-brE8-4kWhf2
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                6.00 GB
  Current LE             192
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol01
  VG Name                VolGroup00
  LV UUID                kEzo7d-AHuv-xiPD-3Qbo-v8yY-6w1D-DFMIwN
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                1.97 GB
  Current LE             63
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

  --- Physical volumes ---
  PV Name               /dev/sda2
  PV UUID               7bPvQd-H8yE-v00N-asPT-WQ92-6oCS-bBh6nj
  PV Status             allocatable
  Total PE / Free PE    316 / 61

And the physical volume now has some spare space left.

No comments:

Post a Comment