Linux LVM 简单操作

时间:2023-12-30 22:27:32

查看当前磁盘分区情况
fdisk -l

磁盘分区
fdisk /dev/sdb
# 可能用到的Type :
# 8e Linux LVM
# fd Linux raid auto

创建PV
pvcreate /dev/sdb1 /dev/sdb2

确认PV
pvdisplay
pvdisplay /dev/sdb1
pvdisplay /dev/sdb2

删除PV
pvremove /dev/sdb1 /dev/sdb2

创建VG
vgcreate myVG /dev/sdb1 /dev/sdb2
or
vgcreate myVG /dev/sdb1; vgextend myVG /dev/sdb2

确认VG
vgdisplay
vgscan # vgscan scans all SCSI(sd[a-t]), (E)IDE disks([hd[a-t]]), multiple devices(raid) and a bunch of other disk devices in the system looking for LVM physical volumes and volume groups.

重命名VG
vgrename myVG myNewVG

删除VG
vgremove myVG

创建LV
lvcreate -L 400 -n vol01 myVG; lvcreate --name vol01 --size 400M myVG
lvcreate -L 1000 -n vol02 myVG
# -L size, 400M, 1000M
# -n lv's name

确认LV
lvdisplay
lvscan

重命名LV
lvrename myVG vol01 vol01_new; lvrename /dev/myVG/vol01 myVG/vol01_new

删除LV
lvremove

创建Filesystem
mkfs.ext3 /dev/myVG/vol01
mkfs.ext3 /dev/myVG/vol02
mkfs.xfs /dev/myVG/vol03
mkfs.reiserfs /dev/myVG/vol04

挂载Mount Filesystem
mkdir -p /data1 /data2
mount /dev/myVG/vol01 /data1
mount /dev/myVG/vol02 /data2

卸载FS
umount /data1

确认
df -h

编辑写入/etc/fstab
# file system|mount point|type|options|dump|pass
/dev/myVG/vol01 /data1 ext3 rw,noatime 0 0
/dev/myVG/vol02 /data2 ext3 rw,noatime 0 0

-------扩展文件系统---------------
先卸载
umount /data1

扩展LV
lvextend -L +800 /dev/myVG/vol01 # -L size+ 800M
lvextend -L 1.5G /dev/myVG/vol01 # -L size= 1.5G

检查
e2fsck -f /dev/myVG/vol01
# -f Force checking even if the file system seems clean.

重置FS大小(对于fs,扩张此时才实际生效)
resize2fs /dev/myVG/vol01 # ext3
resize_reiserfs /dev/fileserver/media # reiserfs
xfs_growfs /dev/fileserver/backup # xfs
meta-data=/dev/fileserver/backup isize=256 agcount=8, agsize=163840 blks
= sectsz=512 attr=0
data = bsize=4096 blocks=1310720, imaxpct=25
= sunit=0 swidth=0 blks, unwritten=1
naming =version 2 bsize=4096
log =internal bsize=4096 blocks=2560, version=1
= sectsz=512 sunit=0 blks
realtime =none extsz=65536 blocks=0, rtextents=0

装载
mount /data1
------缩减文件系统----------------
现在卸载
umount /data1

检查
e2fsck -f /dev/myVG/vol01

缩减fs
resize2fs /dev/myVG/vol01 2G

删除LV
lvreduce -L 1G dev/myVG/vol01 # -L size= 1G
--添加新的PV到已知VG中----------------------------------
创建partition
fdisk /dev/sdf # type 8e Linux LVM

创建PV
pvcreate /dev/sdf1

添加
vgextend myVG /dev/sdf1

确认
vgdisplay
--删除PV从VG中----------------------------------
先将数据转移
pvmove /dev/sdb_old /dev/sdf_new

再从vg中删除old pv
vgreduce myVG /dev/sdb_old

确认vg中无old pv
vgdisplay

删除old pv
pvremove /dev/sdb_old

确认
pvdisplay
-----清除所有操作--------------------------------------
umount /data1

lvremove /dev/myVG/vol01

vgremove myVG

pvremove /dev/sdb1 /dev/sdb2

restore /etc/fstab manually

shutdown -r now

确认
vgdisplay
pvdisplay
lvdisplay
df -h

===========LVM On RAID1===========
LV |/dev/myVG/share | /dev/myVG/backup   | /dev/myVG/unused
---|-------------------|----------------------|------------------------
VG |                                  myVG
---|----------------------------------------------------------------
PV |        /dev/md0                     |                   /dev/md1
    |----------------------------------------------------------------
    |/dev/sdb1     |     /dev/sdc1    |       /dev/sdd1     |    /dev/sde1
---|----------------------------------------------------------------

构建初始状态
pvcreate /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
vgcreate myVG /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1
lvcreate --name share --size 40G myVG
lvcreate --name backup --size 5G myVG
lvcreate --name media --size 1G myVG

mkfs.ext3 /dev/myVG/share
mkfs.xfs /dev/myVG/backup
mkfs.reiserfs /dev/myVG/media

mount /dev/myVG/share /var/share
mount /dev/myVG/backup /var/backup
mount /dev/myVG/media /var/media

df -h

开始构筑

清除sdc1 sde1
modprobe dm-mirror
pvmove /dev/sdc1
pvmove /dev/sde1

vgreduce myVG /dev/sdc1
vgreduce myVG /dev/sde1

pvremove /dev/sdc1
pvremove /dev/sde1

重新设定partition为raid
fdisk /dev/sdc # type fd Linux raid auto
fdisk /dev/sde # type fd Linux raid auto

创建raid
# add /dev/sdc1 to /dev/md0 and /dev/sde1 to /dev/md1.
# Because the second nodes (/dev/sdb1 and /dev/sdd1) are not ready yet, we must specify missing in the following commands:
mdadm --create /dev/md0 --auto=yes -l 1 -n 2 /dev/sdc1 missing
# -l level
# -n Specify the number of active devices in the array.

mdadm --create /dev/md1 --auto=yes -l 1 -n 2 /dev/sde1 missing

创建raid PV
pvcreate /dev/md0 /dev/md1

将新的raid PV加入到VG
vgextend myVG /dev/md0 /dev/md1

确认
pvdisplay
# PV Name /dev/sdb1
# PV Name /dev/sdd1
# PV Name /dev/md0
# PV Name /dev/md1

迁移数据
pvmove /dev/sdb1 /dev/md0
pvmove /dev/sdd1 /dev/md1

移除/dev/sdb1 /dev/sdd1从vg中,并删除
vgreduce myVG /dev/sdb1 /dev/sdd1
pvremove /dev/sdb1 /dev/sdd1

确认
pvdisplay
# PV Name /dev/md0
# PV Name /dev/md1

设置dev/sdb和dev/sdd为raid type
fdisk /dev/sdb # type fd Linux raid auto
fdisk /dev/sdd # type fd Linux raid auto

# add /dev/sdb1 to /dev/md0 and /dev/sdd1 to /dev/md1:
mdadm --manage /dev/md0 --add /dev/sdb1
mdadm --manage /dev/md1 --add /dev/sdd1

检查同步进度,直到100%
cat /proc/mdstat

=========Replacing The Hard Disks With Bigger Ones=====
# The procedure is as follows:
# first we remove /dev/sdb and /dev/sdd from the RAID arrays, replace them with bigger hard disks,
# put them back into the RAID arrays, and then we do the same again with /dev/sdc and /dev/sde.

# First we mark /dev/sdb1 as failed:
mdadm --manage /dev/md0 --fail /dev/sdb1

确认
cat /proc/mdstat

移除
mdadm --manage /dev/md0 --remove /dev/sdb1

确认
cat /proc/mdstat

对/dev/sdd1做同样的操作
mdadm --manage /dev/md1 --fail /dev/sdd1
cat /proc/mdstat
mdadm --manage /dev/md1 --remove /dev/sdd1
cat /proc/mdstat

# now shut it down, pull out the 25GB /dev/sdb and /dev/sdd and replace them with 80GB ones.
格式化新的sdb sdd
fdisk /dev/sdb # type fd Linux raid auto
fdisk /dev/sdd # type fd Linux raid auto

添加新的pv到raid vg中
mdadm --manage /dev/md0 --add /dev/sdb1
mdadm --manage /dev/md1 --add /dev/sdd1

检查同步进度,直到100
cat /proc/mdstat

# Now we do the same process again, this time replacing /dev/sdc and/dev/sde:

mdadm --manage /dev/md0 --fail /dev/sdc1
mdadm --manage /dev/md0 --remove /dev/sdc1
mdadm --manage /dev/md1 --fail /dev/sde1
mdadm --manage /dev/md1 --remove /dev/sde1

插入新的 sdc sde
fdisk /dev/sdc # type fd Linux raid auto
fdisk /dev/sde # type fd Linux raid auto

mdadm --manage /dev/md0 --add /dev/sdc1
mdadm --manage /dev/md1 --add /dev/sde1

# Wait until the synchronization has finished.
cat /proc/mdstat

=====如果事先准备好pv,直接构建raid VG时如下步骤=============================
mdadm --create /dev/md2 --auto=yes -l 1 -n 2 /dev/sdb2 /dev/sdc2
mdadm --create /dev/md3 --auto=yes -l 1 -n 2 /dev/sdd2 /dev/sde2
cat /proc/mdstat # Wait until the synchronization has finished.