减小lvm根分区容量, 分配到swap

在安装Linux系统时LVM划分/分区过.

在安装Linux系统时LVM划分/分区过大,使用一段时间后觉得没必要把/分区(/dev/vgsrv/root)分的这么大(100G呢),此时可以从Linux安装光盘启动进入rescue模式,选择相关的语言,键盘模式,当系统提示启用网络设备时,选择“NO”,然后在提示允许rescue模式挂载本地Linux系统到/mnt/sysimage下时选择“Skip”,文件系统必须不被挂载才可以对/分区减小容量操作。最后系统会提示选择进入shell终端还是reboot机器,选择进入shell终端。

依次输入pvscan、vgscan、lvscan三个命令扫描pv、vg、lv相关信息,

然后输入 lvchange -ay /dev/vgsrv/root(上文提到的/分区名称)此命令是激活/分区所在的逻辑卷。

下一步是缩小文件系统大小和逻辑卷/dev/vgsrv/root,

在缩小文件系统前先检查下硬盘,e2fsck -f /dev/vgsrv/root

缩小文件系统为指定大小: resize2fs/dev/vgsrv/root 50G

设置逻辑卷大小为50G:lvreduce -L 50g /dev/vgsrv/root

系统会进入是否缩小逻辑卷,输入 y 确定。

再检查下硬盘,e2fsck -f /dev/vgsrv/root

缩小逻辑卷成功后可输入vgdisplay,lvdisplay查看。输入exit可退出rescue模式。

 

正常进入系统后, 下面针对系统Swap分区扩容与缩减做说明: 

[root@server10 ~]# free -g
            total       used       free     shared    buffers     cached
Mem:            62         62          0          0          0         60
-/+ buffers/cache:          2         60
Swap:          127          0        127
[root@server10 ~]# df -h
[root@server10 ~]# cat /etc/fstab
/dev/VolGroup00/LogVol01 swap                    swap    defaults        0 0
[root@server10 ~]# lvdisplay
 — Logical volume —
  LV Name                /dev/VolGroup00/LogVol00
 VG Name                VolGroup00
 LV UUID                oZ7rEm-hphT-MsGk-fNaD-RC5X-INgZ-oCJdml
 LV Write Access        read/write
 LV Status              available
 # open                 1
  LV Size                150.84 GB
 Current LE             4827
 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                pCpqVB-cwXV-MunF-OtBQ-usYA-hFxt-l07IJc
 LV Write Access        read/write
 LV Status              available
 # open                 1
  LV Size                127.91 GB
 Current LE             4093
 Segments               1
 Allocation             inherit
 Read ahead sectors     auto
 – currently set to     256
 Block device           253:1
 
[root@server10 ~]# vgdisplay
 — 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               278.75 GB
 PE Size               32.00 MB
 Total PE              8920
  Alloc PE / Size       8920 / 278.75 GB
 Free  PE / Size       0 / 0  

 VG UUID               9hHRZZ-VUd4-bMwc-rnhK-wiyq-sPZw-dtJREo  
[root@server10 ~]# free -m
            total       used       free     shared    buffers     cached
Mem:         64449      64129        320          0        327      61494
-/+ buffers/cache:       2307      62142
Swap:       130975          0     130975
[root@server10 ~]# swapoff -a
[root@server10 ~]# free -m
            total       used       free     shared    buffers     cached
Mem:         64449      64065        384          0        327      61494
-/+ buffers/cache:       2243      62206
Swap:            0          0          0
[root@server10 ~]# lvreduce /dev/VolGroup00/LogVol01 -L -64G
 WARNING: Reducing active logical volume to 63.91 GB
 THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce LogVol01? [y/n]: y
 Reducing logical volume LogVol01 to 63.91 GB
 Logical volume LogVol01 successfully resized
[root@server10 ~]# mkswap /dev/VolGroup00/LogVol01
Setting up swapspace version 1, size = 68618809 kB
[root@server10 ~]# swapon /dev/VolGroup00/LogVol01
[root@server10 ~]# free -m
            total       used       free     shared    buffers     cached
Mem:         64449      64150        299          0        327      61549
-/+ buffers/cache:       2273      62175
Swap:        65439          0      65439
[root@server10 ~]# free -g
            total       used       free     shared    buffers     cached
Mem:            62         62          0          0          0         60
-/+ buffers/cache:          2         60
Swap:           63          0         63
[root@server10 ~]# vgdisplay
 — 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               278.75 GB
 PE Size               32.00 MB
 Total PE              8920
  Alloc PE / Size       6872 / 214.75 GB
 Free  PE / Size       2048 / 64.00 GB

 VG UUID               9hHRZZ-VUd4-bMwc-rnhK-wiyq-sPZw-dtJREo
 
[root@server10 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                     147G   74G   65G  54% /

/dev/sda1              99M   13M   81M  14% /boot
tmpfs                  32G     0   32G   0% /dev/shm
[root@server10 ~]# mount
/dev/mapper/VolGroup00-LogVol00 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
[root@server10 ~]# lvextend /dev/VolGroup00/LogVol00 -l +100%FREE
  Extending logical volume LogVol00 to 214.84 GB
 Logical volume LogVol00 successfully resized

[root@server10 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                     147G   74G   65G  54% /

/dev/sda1              99M   13M   81M  14% /boot
tmpfs                  32G     0   32G   0% /dev/shm
[root@server10 ~]# resize2fs /dev/VolGroup00/LogVol00
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing required
Performing an on-line resize of /dev/VolGroup00/LogVol00 to 56320000 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now 56320000 blocks long.

[root@server10 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                     209G   74G  124G  38% /

/dev/sda1              99M   13M   81M  14% /boot
tmpfs                  32G     0   32G   0% /dev/shm
[root@server10 ~]# 

——————————————————————

以上是减容量,下面针对增加容量做一下命令解析:

swapoff -a   //关闭swap分区
lvextend -l +100%free /dev/VolGroup00/LogVol00 //增加swap卷所有可用空间
mkswap /dev/VolGroup00/LogVol00 //建立swap分区
swapon /dev/VolGroup00/LogVol00//启用swap分区