一、LVM简介
LVM(Logical Volume Manager)即:逻辑卷管理器,它可以整合多个物理分区在一起,让这些分区看起来像一个磁盘一样,其主要用处之一是实现一个可以弹性调整容量的文件系统;第二就是实现系统快照,这样就可以是备份和还原就变的很简单。下面来分别介绍LVM这两方免得重要功能。
二、逻辑卷(LV)的扩展和缩减
1、将/dev/sda5制作成LV。其步骤如下:
(1)创建分区并将其卷标改为8e。
[root@localhost ~]# fdisk /dev/sda
- The number of cylinders for this disk is set to 15665.
- There is nothing wrong with that, but this is larger than 1024,
- and could in certain setups cause problems with:
- 1) software that runs at boot time (e.g., old versions of LILO)
- 2) booting and partitioning software from other OSs
- (e.g., DOS FDISK, OS/2 FDISK)
- Command (m for help): n
- First cylinder (5367-15665, default 5367):
- Using default value 5367
- Last cylinder or +size or +sizeM or +sizeK (5367-15665, default 15665): +10G
- Command (m for help): t
- Partition number (1-5): 5
- Hex code (type L to list codes): 8e
- Changed system type of partition 5 to 8e (Linux LVM)
- Command (m for help): n
- First cylinder (6584-15665, default 6584):
- Using default value 6584
- Last cylinder or +size or +sizeM or +sizeK (6584-15665, default 15665): +5G
- Command (m for help): t
- Partition number (1-6): 6
- Hex code (type L to list codes): 8e
- Changed system type of partition 6 to 8e (Linux LVM)
- Command (m for help): p
- Disk /dev/sda: 128.8 GB, 128849018880 bytes
- 255 heads, 63 sectors/track, 15665 cylinders
- Units = cylinders of 16065 * 512 = 8225280 bytes
- Device Boot Start End Blocks Id System
- /dev/sda1 * 1 13 104391 83 Linux
- /dev/sda2 14 5235 41945715 8e Linux LVM
- /dev/sda3 5236 5366 1052257+ 82 Linux swap / Solaris
- /dev/sda4 5367 15665 82726717+ 5 Extended
- /dev/sda5 5367 6583 9775521 8e Linux LVM
- /dev/sda6 6584 7192 4891761 8e Linux LVM
- Command (m for help): w
以上创建了两个分区/dev/sda5和/dev/sda6其卷标都为8e
(2)将/dev/sda5创建成pv(物理卷)。
[root@localhost ~]# pvcreate /dev/sda5
- Writing physical volume data to disk "/dev/sda5"
- Physical volume "/dev/sda5" successfully created
- [root@localhost ~]# pvdisplay /dev/sda5
- "/dev/sda5" is a new physical volume of "9.32 GB"
- --- NEW Physical volume ---
- PV Name /dev/sda5
- VG Name
- PV Size 9.32 GB
- Allocatable NO
- PE Size (KByte) 0
- Total PE 0
- Free PE 0
- Allocated PE 0
- PV UUID X1mEFY-Hk3e-LT10-ori0-Dpz9-2ucd-VktkXg
(3)将/dev/sda5创建卷组myvg。
[root@localhost ~]# vgcreate myvg1 /dev/sda5
- Volume group "myvg1" successfully created
- [root@localhost ~]# vgdisplay myvg1
- --- Volume group ---
- VG Name myvg1
- System ID
- Format lvm2
- Metadata Areas 1
- Metadata Sequence No 1
- VG Access read/write
- VG Status resizable
- MAX LV 0
- Cur LV 0
- Open LV 0
- Max PV 0
- Cur PV 1
- Act PV 1
- VG Size 9.32 GB
- PE Size 4.00 MB
- Total PE 2386
- Alloc PE / Size 0 / 0
- Free PE / Size 2386 / 9.32 GB
- VG UUID wHo82H-hk3K-gK8j-vMY3-X83q-Fcof-SqT88x
(4)在myvg1上创建创建2G的LV(逻辑卷)。
[root@localhost ~]# lvcreate -L 2G -n mylv1 myvg1
- Logical volume "mylv1" created
- [root@localhost ~]# lvdisplay /dev/myvg1/mylv1
- --- Logical volume ---
- LV Name /dev/myvg1/mylv1
- VG Name myvg1
- LV UUID HIVsSx-X318-RbZl-fcTs-jy82-I59N-1Z4pre
- LV Write Access read/write
- LV Status available
- # open 0
- LV Size 2.00 GB
- Current LE 512
- Segments 1
- Allocation inherit
- Read ahead sectors auto
- - currently set to 256
- Block device 253:3
这样一个逻辑卷就创建成功了。
2、放大LV的容量。
放大LV要先扩展卷组的容量,再来扩展
[root@localhost ~]# pvcreate /dev/sda7
- Writing physical volume data to disk "/dev/sda7"
- Physical volume "/dev/sda7" successfully created
- [root@localhost ~]# vgextend myvg1 /dev/sda7
- Volume group "myvg1" successfully extended
- [root@localhost ~]# vgs
- VG #PV #LV #SN Attr VSize VFree
- myvg 1 1 0 wz--n- 4.66G 3.66G
- myvg1 2 1 0 wz--n- 14.91G 12.91G
- vol0 1 2 0 wz--n- 40.00G 5.84G
- [root@localhost ~]# lvresize -L +1G /dev/myvg1/mylv1
- Extending logical volume mylv1 to 3.00 GB
- Logical volume mylv1 successfully resized
这样就将LV的容量扩大到3G了。
3、缩小LV的容量。
缩小逻辑卷是比较危险的操作,必须按照一定的步骤进行。首先要先卸载卷,而且要强制进行文件系统检测,然后用resize2fs缩减逻辑边界,再次用lvreduce缩减物理边界,最后就可以挂载使用了,其具体步骤如下:
- root@localhost ~]# umount /backup
- [root@localhost ~]# fsck -f /dev/myvg1/mylv1
- fsck 1.39 (29-May-2006)
- e2fsck 1.39 (29-May-2006)
- Couldn't find ext2 superblock, trying backup blocks...
- ext3 recovery flag is clear, but journal has data.
- Recovery flag not set in backup superblock, so running journal anyway.
- /dev/myvg1/mylv1: recovering journal
- 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
- Free blocks count wrong for group #0 (15717, counted=15716).
- Fix<y>? yes
- Free blocks count wrong (498882, counted=498881).
- Fix<y>? yes
- Free inodes count wrong for group #0 (16373, counted=16372).
- Fix<y>? yes
- Free inodes count wrong (262133, counted=262132).
- Fix<y>? yes
- /dev/myvg1/mylv1: ***** FILE SYSTEM WAS MODIFIED *****
- /dev/myvg1/mylv1: 12/262144 files (8.3% non-contiguous), 25407/524288 blocks
- [root@localhost ~]# resize2fs /dev/myvg1/mylv1 1G
- resize2fs 1.39 (29-May-2006)
- Resizing the filesystem on /dev/myvg1/mylv1 to 262144 (4k) blocks.
- The filesystem on /dev/myvg1/mylv1 is now 262144 blocks long.
- [root@localhost ~]# lvreduce -L 1G /dev/myvg1/mylv1
- WARNING: Reducing active logical volume to 1.00 GB
- THIS MAY DESTROY YOUR DATA (filesystem etc.)
- Do you really want to reduce mylv1? [y/n]: y
- Reducing logical volume mylv1 to 1.00 GB
- Logical volume mylv1 successfully resized
- [root@localhost ~]# lvs
- LV VG Attr LSize Origin Snap% Move Log Copy% Convert
- mylv1 myvg -wi-a- 1.00G
- mylv1 myvg1 -wi-a- 1.00G
- home vol0 -wi-ao 4.88G
- root vol0 -wi-ao 29.28G
三、LVM系统快照的建立
1、创建快照区
- [root@localhost backup]# lvcreate -l 60 -s -n my-lvshot /dev/myvg1/mylv1
- Logical volume "my-lvshot" created
- [root@localhost backup]# lvs
- LV VG Attr LSize Origin Snap% Move Log Copy% Convert
- mylv1 myvg -wi-a- 1.00G
- my-lvshot myvg1 swi-a- 240.00M mylv1 0.00
- mylv1 myvg1 owi-ao 1.00G
- home vol0 -wi-ao 4.88G
- root vol0 -wi-ao 29.28G
2、利用快照进行备份
- [root@localhost backup]# mount /dev/myvg1/my-lvshot /mnt/snapshot/
- [root@localhost backup]# df -lh
- Filesystem Size Used Avail Use% Mounted on
- /dev/mapper/vol0-root
- 29G 4.6G 23G 17% /
- /dev/mapper/vol0-home
- 4.8G 139M 4.4G 4% /home
- /dev/sda1 99M 35M 60M 37% /boot
- tmpfs 252M 0 252M 0% /dev/shm
- /dev/mapper/myvg1-mylv1
- 1008M 67M 891M 7% /backup
- /dev/mapper/myvg1-my--lvshot
- 1008M 67M 891M 7% /mnt/snapshot
- [root@localhost backup]# ls /backup
- fstab lost+found
- [root@localhost backup]# cp /etc/inittab ./
- [root@localhost backup]# ls
- fstab inittab lost+found
- [root@localhost backup]# rm fstab
- rm: remove regular file `fstab'? y
- [root@localhost backup]# ls
- inittab lost+found
- [root@localhost backup]# ls /mnt/snapshot/
- fstab lost+found
- [root@localhost backup]# tar -jcf /root/backup.tar.bz2 /mnt/snapshot/*
- tar: Removing leading `/' from member names
3、备份完成后就可以将快照删除了
- [root@localhost ~]# umount /mnt/snapshot/
- [root@localhost ~]# lvremove -f /dev/myvg1/my-lvshot
- Logical volume "my-lvshot" successfully removed
- [root@localhost ~]# lvs
- LV VG Attr LSize Origin Snap% Move Log Copy% Convert
- mylv1 myvg -wi-a- 1.00G
- mylv1 myvg1 -wi-ao 1.00G
- home vol0 -wi-ao 4.88G
- root vol0 -wi-ao 29.28G
这样快照就被移除了,以上内容只是对逻辑卷的创建、缩减、扩展和快照的创建简单的实现,希望对大家有所帮助。