代码改变世界

LVM quick start

2014-06-05 17:12  梁小白  阅读(339)  评论(0编辑  收藏  举报

这里记录一些任务用到的快速命令,详细LVM管理可参考:

http://wenku.baidu.com/view/c29b8bc4bb4cf7ec4afed0ad.html

1.把home分区的磁盘空间移到/根目录

[root@localhost ~]# lvs
  LV      VG       Attr       LSize   Pool Origin Data%  Move Log Cpy%Sync Convert
  lv_home VolGroup -wi-a----- 353.23g
  lv_root VolGroup -wi-ao----  50.00g
  lv_swap VolGroup -wi-ao----   4.00g
[root@localhost ~]# pvs
 PV         VG       Fmt  Attr PSize   PFree
  /dev/sda3  VolGroup lvm2 a--  407.23g    0
[root@localhost ~]# lvremove /dev/VolGroup/lv_home
Do you really want to remove active logical volume lv_home? [y/n]: y
  Logical volume "lv_home" successfully removed

[root@localhost ~]# lvextend  /dev/VolGroup/lv_root /dev/sda3
  Extending logical volume lv_root to 403.23 GiB
  Logical volume lv_root successfully resized
[root@localhost ~]# resize2fs /dev/VolGroup/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required
old desc_blocks = 4, new_desc_blocks = 26
Performing an on-line resize of /dev/VolGroup/lv_root to 105704448 (4k) blocks.
The filesystem on /dev/VolGroup/lv_root is now 105704448 blocks long.

[root@localhost ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root  397G  2.9G  374G   1% /
tmpfs                         127G  224K  127G   1% /dev/shm
/dev/sda2                     485M   39M  421M   9% /boot
/dev/sda1                     200M  260K  200M   1% /boot/efi
[root@localhost ~]#
最后别忘了vim /etc/fstab
把里面关于lv_home的那行删除,否则重启会出错,万一出错,使用
mount -w -o remount /
重新挂载文件系统删除相应行即可。

 

2. 给根目录增加空间

add a disk size 200G /dev/sdb
pvcreate /dev/sdb
vgextend VolGroup /dev/sdb
lvextend /dev/VolGroup/lv_root /dev/sdb
resize2fs /dev/VolGroup/lv_root #rhel 6 and below
fdisk -l

对于RHEL7以上,因为使用XFS做为文件系统,使用resize2fs会报错,请用xfs_growfs代替:

[root@localhost ~]# xfs_growfs /dev/rhel/root

meta-data=/dev/mapper/rhel-root  isize=256    agcount=4, agsize=1026816 blks

         =                       sectsz=512   attr=2, projid32bit=1

         =                       crc=0        finobt=0

data     =                       bsize=4096   blocks=4107264, 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 4107264 to 17223680

[root@localhost ~]# df

 

3. Reduce

Sometimes when we are running out of disk space in our linux box and if partition created on LVM , then we can make some free space in the volume group by reducing the LVM using lvreduce command.In this article we will discuss the required steps to reduce the size of LVM safely .

Scenario : Suppose we want to reduce /home by 2GB which is LVM and formated as ext4.

[root@cloud ~]# df -h /home/ 
Filesystem            Size  Used Avail Use% Mounted on 
/dev/mapper/vg_cloud-LogVol00 
                             12G  9.2G  1.9G  84% /home

Step:1 Umount the filesystem

[root@cloud ~]# umount /home/

Step:2 check the filesystem for Errors using e2fsck command .

[ root@cloud ~]# e2fsck -f /dev/mapper/vg_cloud-LogVol00 
e2fsck 1.41.12 (17-May-2010) 
Pass 1: Checking inodes, blocks, and sizes 
Pass 2: Checking directory structure 
Pass 3: Checking directory connectivity 
Pass 4: Checking reference counts 
Pass 5: Checking group summary information 
/dev/mapper/vg_cloud-LogVol00: 12/770640 files (0.0% non-contiguous), 2446686/3084288 blocks

Note: In the above command e2fsck , we use the option '-f' to forcefully check the filesystem , even if the filesystem is clean.

Step:3 Shrink the size of /home to desire size.

As shown in the above scenario , size of /home is 12 GB , so by reducing it by 2GB , then the size will become 10GB.

[root@cloud ~]# resize2fs /dev/mapper/vg_cloud-LogVol00 10G 
resize2fs 1.41.12 (17-May-2010) 
Resizing the filesystem on /dev/mapper/vg_cloud-LogVol00 to 2621440 (4k) blocks.
The filesystem on /dev/mapper/vg_cloud-LogVol00 is now 2621440 blocks long.

Step:4 Now reduce the size using lvreduce command.

[root@cloud ~]# lvreduce -L 10G /dev/mapper/vg_cloud-LogVol00 
  WARNING: Reducing active logical volume to 10.00 GiB 
  THIS MAY DESTROY YOUR DATA (filesystem etc.) 
Do you really want to reduce LogVol00? [y/n]: y 
  Reducing logical volume LogVol00 to 10.00 GiB 
  Logical volume LogVol00 successfully resized

Step:5 ( Optional) For the safer side , now check the reduced filesystem for errors

[root@cloud ~]# e2fsck -f /dev/mapper/vg_cloud-LogVol00 
e2fsck 1.41.12 (17-May-2010) 
Pass 1: Checking inodes, blocks, and sizes 
Pass 2: Checking directory structure 
Pass 3: Checking directory connectivity 
Pass 4: Checking reference counts 
Pass 5: Checking group summary information 
/dev/mapper/vg_cloud-LogVol00: 12/648960 files (0.0% non-contiguous), 2438425/2621440 blocks

Step:6 Mount the file system and verify the size.

[root@cloud ~]# mount /home/ 
[root@cloud ~]# df -h /home/ 
Filesystem            Size  Used Avail Use% Mounted on 
/dev/mapper/vg_cloud-LogVol00 
                               9.9G  9.2G  208M  98% /home

 从LVM 添加和移除硬盘

add disk to vm

pvcreate /dev/sdb
vgextend vg00 /dev/sdb
lvcreate --size 1g -name sas /dev/vg00/sas
mkdir /mnt/sas
mount /dev/vg00/sas /mnt/sas



remove disk from lvm

umount /dev/vg00/sas
lvremove /dev/vvg00/sas
vgreduce vg00 /dev/sdb
pvremove /dev/sdb
remove disk from hardware