设备
centos6
2.0创建逻辑卷lvm
sda中新建一个分区sda7,和一块干净的磁盘sdg创建一个逻辑卷组
pv管理工具
显示pv信息(pvs:简要pv信息显示)
pvdisplay
创建pv
pvcreate /dev/DEVICE
删除pv
pvremove /dev/DEVICE
vg管理工具
显示卷组
vgs
vgdisplay
创建卷组
vgcreate [-s #[kKmMgGtTpPeE]] VolumeGroupName PhysicalDevicePath...
管理卷组
vgextend VolumeGroupName PhysicalDevicePath...
vgreduce VolumeGroupName PhysicalDevicePath...
删除卷组
先做pvmove,再做vgremove
lv管理工具
显示逻辑卷
lvs
Lvdisplay
创建逻辑卷
lvcreate -L #[mMgGtT] -n NAME VolumeGroup
lvcreate -l 60%VG -n mylv testvg
lvcreate -l 100%FREE -n yourlv testvg
删除逻辑卷
lvremove /dev/VG_NAME/LV_NAME
重设文件系统大小
fsadm [options] resize device [new_size[BKMGTEP]]
resize2fs [-f] [-F] [-M] [-P] [-p] device [new_size]
xfs_growfs /mountpoint
1 在dev/sda新建分区
[root@centos6 ~]#fdisk /dev/sda
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n
First cylinder (14055-26109, default 14055):
Using default value 14055
Last cylinder, +cylinders or +size{K,M,G} (14055-26109, default 26109): +10G
Command (m for help): T
Partition number (1-7): 7
Hex code (type L to list codes): l
5 Extended 42 SFS 86 NTFS volume set da Non-FS data
6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility
8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt
#选择8e
Hex code (type L to list codes): 8e
Changed system type of partition 7 to 8e (Linux LVM)
Command (m for help): p
Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000515e0
Device Boot Start End Blocks Id System
/dev/sda1 * 1 1275 10240000 83 Linux
/dev/sda2 1275 7650 51200000 83 Linux
/dev/sda3 7650 11474 30720000 83 Linux
/dev/sda4 11474 26109 117554176 5 Extended
/dev/sda5 11474 12749 10240000 82 Linux swap / Solaris
/dev/sda6 12749 14054 10485651+ 83 Linux
#查看一下 #id
/dev/sda7 14055 15360 10490413+ 8e Linux LVM
#存盘退出
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.
#同步内存信息
[root@centos6 ~]#partx -a /dev/sda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
BLKPG: Device or resource busy
error adding partition 6
2把两块分区/磁盘设置为物理卷
#查看是否有物理卷
[root@centos6 ~]#pvdisplay
#更详细一些
[root@centos6 ~]#pvdisplay
#设置为物理卷
[root@centos6 ~]#pvcreate /dev/sd{a7,g}
Physical volume "/dev/sda7" successfully created
Physical volume "/dev/sdg" successfully created
#查看一下
[root@centos6 ~]#pvs
PV VG Fmt Attr PSize PFree
/dev/sda7 lvm2 ---- 10.00g 10.00g
/dev/sdg lvm2 ---- 20.00g 20.00g
3 把两块磁盘、分区加入同一个卷组中
#查看是否已经有卷组
[root@centos6 ~]#vgs
#详细查看
[root@centos6 ~]#vgdisplay
#开始添加同一个卷组
[root@centos6 ~]#vgcreate vg0 /dev/sd{a7,g}
Volume group "vg0" successfully created
#查看逻辑卷
[root@centos6 ~]#pvs
PV VG Fmt Attr PSize PFree
/dev/sda7 vg0 lvm2 a--u 10.00g 10.00g
/dev/sdg vg0 lvm2 a--u 20.00g 20.00g
[root@centos6 ~]#pvdisplay
--- Physical volume ---
PV Name /dev/sda7
VG Name vg0
PV Size 10.00 GiB / not usable 4.54 MiB
Allocatable yes
PE Size 4.00 MiB #物理盘区
Total PE 2560
Free PE 2560
Allocated PE 0
PV UUID 9NhGcB-1CJo-y2B3-vh2G-iNoY-ejSG-b2dbjT
--- Physical volume ---
PV Name /dev/sdg
VG Name vg0
PV Size 20.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 5119
Free PE 5119
Allocated PE 0
PV UUID RzAO6Q-dp6a-cxmW-M6ZR-bofz-d02c-v0AJ8r
4.创建逻辑卷组
[root@centos6 ~]#lvs
[root@centos6 ~]#lvdisplay
#使用lvs命令显示出逻辑卷组
[root@centos6 ~]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_mysql vg0 -wi-a----- 15.00g
5 创建文件系统
[root@centos6 ~]#mkfs.ext4 /dev/vg0/lv_mysql
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
983040 inodes, 3932160 blocks
196608 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4026531840
120 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 36 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
6挂载
[root@centos6 ~]#mkdir /mnt/mysql
[root@centos6 ~]#mount /dev/vg0/lv_mysql /mnt/mysql/
7.测试一下速度
#测试读速度
[root@centos6 ~]#dd if=/dev/zero of=/mnt/mysql/db1 bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 2.44978 s, 438 MB/s
#测试写速度
[root@centos6 ~]#dd if=/mnt/mysql/db1 of=/dev/null
2097152+0 records in
2097152+0 records out
1073741824 bytes (1.1 GB) copied, 3.76035 s, 286 MB/s
3.0扩展和缩减lvm
3.1扩展
[root@centos6 ~]#lvextend -l +100%FREE /dev/vg0/lv_mysql
Size of logical volume vg0/lv_mysql changed from 15.00 GiB (3840 extents) to 30.00 GiB (7679 extents).
Logical volume lv_mysql successfully resized.
#ext4文件系统同步
[root@centos6 ~]#resize2fs /dev/vg0/lv_mysql
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg0/lv_mysql to 7863296 (4k) blocks.
The filesystem on /dev/vg0/lv_mysql is now 7863296 blocks long.
#挂载一下
[root@centos6 ~]#mount /dev/vg0/lv_mysql /mnt/mysql
#查看多了30G
[root@centos6 ~]#df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 48G 5.0G 41G 11% /
tmpfs tmpfs 498M 72K 498M 1% /dev/shm
/dev/sda1 ext4 9.5G 55M 9.0G 1% /boot
/dev/sda3 ext4 29G 1.1G 27G 4% /data
/dev/md126 ext4 3.9G 1.1G 2.7G 28% /mnt/raid
/dev/md127 ext4 2.0G 1.1G 840M 56% /mnt/raid5
/dev/mapper/vg0-lv_mysql
ext4 30G 1.1G 27G 4% /mnt/mysql
3.2缩减
#取消挂载
[root@centos6 ~]#umount /dev/vg0/lv_mysql
#检查文件系统
[root@centos6 ~]#e2fsck -f /dev/vg0/lv_mysql
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/vg0/lv_mysql: 12/1966080 files (0.0% non-contiguous), 428850/7863296 blocks
#缩减文件系统到20G
[root@centos6 ~]#resize2fs /dev/vg0/lv_mysql 20G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg0/lv_mysql to 5242880 (4k) blocks.
The filesystem on /dev/vg0/lv_mysql is now 5242880 blocks long.
#缩减逻辑卷
[root@centos6 ~]#lvreduce -L 20G /dev/vg0/lv_mysql
WARNING: Reducing active logical volume to 20.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg0/lv_mysql? [y/n]: y
Size of logical volume vg0/lv_mysql changed from 30.00 GiB (7679 extents) to 20.00 GiB (5120 extents).
Logical volume lv_mysql successfully resized.
#挂载逻辑卷
root@centos6 ~]#mount /dev/vg0/lv_mysql /mnt/mysql/
4.0 跨主机迁移卷组
源计算机上
umont 所有卷组上的逻辑卷
[root@centos6 ~]#umount /mnt/mysql/
禁用卷组,设置为不可用状态
vgchange -an nvg0
#先修改一下卷组名称
[root@centos6 ~]#vgrename newvg0
[root@centos6 ~]#vgchange -an newvg0
0 logical volume(s) in volume group "newvg0" now active
导出卷组
vgexport nwevg0
[root@centos6 ~]#vgexport nwevg0
Volume group "nwevg0" not found
pvscan
[root@centos6 ~]#pvscan
PV /dev/sda7 VG newvg0 lvm2 [10.00 GiB / 10.00 GiB free]
PV /dev/sdg VG newvg0 lvm2 [20.00 GiB / 0 free]
Total: 2 [30.00 GiB] / in use: 2 [30.00 GiB] / in no VG: 0 [0 ]
vgdisplay查看一下拆除哪一块硬盘
--- Physical volume ---
PV Name /dev/sdg
VG Name newvg0
PV Size 20.00 GiB / not usable 4.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 5119
Free PE 0
Allocated PE 5119
PV UUID RzAO6Q-dp6a-cxmW-M6ZR-bofz-d02c-v0AJ8r
拆下旧硬盘
在新系统中安装旧硬盘,导入卷组
vgchange -ay vg0启用
mount所有卷组上的逻辑卷
新添加计算机
创建物理卷pvcreate /dev/sdd
[root@centos7 ~]#pvcreate /dev/sdd
Physical volume "/dev/sdd" successfully created.
为卷组分配物理卷vgcreate vg0 /dev/sdd
[root@centos7 ~]#vgcreate vg0 /dev/sdd
Volume group "vg0" successfully created
从卷组创建逻辑卷lvcreate -n lv_data -L 1G vg0
[root@centos7 ~]#lvcreate -n lv_data -L 1G vg0
Logical volume "lv_data" created.
格式化mkfs.xfs /dev/vg0/data
[root@centos7 ~]#mkfs.xfs /dev/vg0/data
/dev/vg0/data: No such file or directory
Usage: mkfs.xfs
/* blocksize */ [-b log=n|size=num]
/* metadata */ [-m crc=0|1,finobt=0|1,uuid=xxx]
/* data subvol */ [-d agcount=n,agsize=n,file,name=xxx,size=num,
(sunit=value,swidth=value|su=num,sw=num|noalign),
sectlog=n|sectsize=num
/* force overwrite */ [-f]
/* inode size */ [-i log=n|perblock=n|size=num,maxpct=n,attr=0|1|2,
projid32bit=0|1]
/* no discard */ [-K]
/* log subvol */ [-l agnum=n,internal,size=num,logdev=xxx,version=n
sunit=value|su=num,sectlog=n|sectsize=num,
lazy-count=0|1]
/* label */ [-L label (maximum 12 characters)]
/* naming */ [-n log=n|size=num,version=2|ci,ftype=0|1]
/* no-op info only */ [-N]
/* prototype file */ [-p fname]
/* quiet */ [-q]
/* realtime subvol */ [-r extsize=num,size=num,rtdev=xxx]
/* sectorsize */ [-s log=n|size=num]
/* version */ [-V]
devicename
<devicename> is required unless -d name=xxx is given.
<num> is xxx (bytes), xxxs (sectors), xxxb (fs blocks), xxxk (xxx KiB),
xxxm (xxx MiB), xxxg (xxx GiB), xxxt (xxx TiB) or xxxp (xxx PiB).
<value> is xxx (512 byte blocks).
挂载mount
lvm搬家
pvmove /dev/sdf
vgreduce vg0 /dev/sdf
pvremove /dev/sdf
5.0 lvm快照
准备一个逻辑卷为快照
为现有逻辑卷创建快照
xfs文件系统可读可写
ext4文件系统必须只读 (-p r)
[root@centos6 ~]#lvcreate -n lv_data_snap -s -p r -L 1G
挂载快照
mkdir -p /mnt/snap #只读挂载
mount -o ro /dev/vg0/data_snap /mnt/snap
恢复快照
umount /dev/vg0/data_snap
umount /dev/vg0/data
lvconvert --merge /dev/vg0/data_snap
删除快照
umount /mnt/databackup
lvermove /dev/vg0/fatabackup