LVM

关于LVM:
1、第一次创建,按照如下的命令顺序进行:
pvcreate -> vgcreate -> lvcreate -> mkfs.ext3
解释:
pvcreate
--创建pv。示例:pvcreate /dev/xvde
vgcreate
--创建逻辑磁盘,相对于真实的分区,逻辑磁盘还可以包括几个分区哦。一个vg可以包含几个pv。示例:vgcreate myvg /dev/xvde
这个命令,要指定一个/dev/xvde,创建myvg的同时也加上第一个分区/dev/xvde。
lvcreate
--创建逻辑卷,使用上就类似于真实分区了。  创建快照也是此命令。示例:lvcreate -n mydata -L 9G myvg
9G就是要创建的空间大小,小于等于pvs里显示的空间大小。此空间大小后面还会有解释。
mkfs.ext3
--创建文件系统,格式化。示例:mkfs.ext3 /dev/myvg/mydata
这个过程会持续一段比较长的时间。

2、再挂上一个磁盘,进行扩容:
pvcreate -> vgextend -> lvextend -> resize2fs
解释:
vgextend
--扩展vg,因为多了pv了。示例:vgextend myvg /dev/xvdf
lvextend
--扩容lv,因为多了pv,多了的pv又加到了vg中了(vg相当于逻辑磁盘,空间会变大的)。示例:lvextend -L +100G /dev/myvg/mydata
resize2fs
--创建文件系统,格式化。示例:resize2fs /dev/myvg/mydata
不带空间大小,会把lvextend里增加的所有空间都格式化掉。这个过程持续时间比较长,如果挂载到df了,还能实时看到变化。

3、减少磁盘:
umount -> e2fsck -> resize2fs -> lvresize -> pvs -> pvmove -> vgreduce -> pvremove
这个流程在容量上T的时候,到e2fsck会卡住,暂时不知道为啥。
解释:
e2fsck
--检查ext2、ext3、ext4等文件系统的正确性。示例:e2fsck /dev/myvg/mydata
resize2fs
--减少文件系统占用的空间大小。示例:resize2fs /dev/myvg/mydata 8G
lvresize
--调整逻辑卷空间大小。示例:lvresize -L 8G /dev/myvg/mydata
pvs
--查看有多少pv,每个pv剩余空间有多少。示例:
  PV         VG   Fmt  Attr PSize    PFree   
  /dev/xvde  myvg lvm2 a--    10.00g       0
  /dev/xvdf  myvg lvm2 a--  1000.00g       0
  /dev/xvdg  myvg lvm2 a--  1000.00g 1012.00m
PFree里就是每个pv里的剩余空间,如果剩余空间跟PSize一样,说明这个盘是可以被摘下来的;如果不一样,其他空间又够空间可以容纳,也是可以摘下来的。这时候需要依赖下一个命令pvmove。
pvmove
--把某个pv里的空间移动到别的地方去。示例:pvmove /dev/xvde
vgreduce
--从vg里删掉一个pv。示例:vgreduce myvg /dev/xvde
pvremove
--删除pv。示例:pvremove /dev/xvde

4、其他命令:
lvremove /dev/myvg/mydata  删除lv
vgremove myvg    删除卷组
fdisk -l  可以看到所有磁盘,包括使用lvm的虚拟磁盘
dumpe2fs -h /dev/myvg/mydata  根据lvm使用的单位来统计大小,关注block count和block size
### LVM in Linux: Configuration, Management, and Troubleshooting LVM (Logical Volume Manager) is a powerful tool in Linux that allows for flexible disk management. It provides a layer of abstraction over physical disks, enabling dynamic resizing and allocation of storage resources. Below is an overview of configuration, management, and troubleshooting aspects of LVM. #### Configuration of LVM To configure LVM, the process involves creating physical volumes, volume groups, and logical volumes. When using LVM with initramfs, specific hooks must be added to ensure proper initialization during boot. For systemd-based initramfs, the file `/etc/mkinitcpio.conf` should be edited to include the `sd-lvm2` hook[^1]. This ensures that the system can recognize and mount LVM volumes during the boot process. The updated line in the configuration file would look like this: ```bash HOOKS=(base systemd ... block sd-lvm2 filesystems) ``` For busy-box based initramfs, the `lvm2` hook should be added instead of `sd-lvm2`. #### Management of LVM Managing LVM involves several key operations such as creating, extending, and reducing logical volumes. These operations are performed using commands like `pvcreate`, `vgcreate`, `lvcreate`, `lvextend`, and `lvreduce`. Here is an example of creating a logical volume: ```bash # Create a physical volume pvcreate /dev/sdb # Create a volume group named 'myvg' vgcreate myvg /dev/sdb # Create a logical volume named 'mylv' with a size of 10GB lvcreate -L 10G -n mylv myvg ``` Once created, file systems can be formatted on the logical volumes and mounted as needed. #### Troubleshooting LVM Troubleshooting LVM often involves resolving issues related to volume recognition, mounting, or resizing. Common problems include missing devices, incorrect configurations, or insufficient space. Tools like `lvdisplay`, `vgdisplay`, and `pvdisplay` can help diagnose issues by providing detailed information about the current state of LVM components. For instance, if a logical volume fails to mount, checking the status of the volume group and logical volume can provide insights: ```bash # Display information about the volume group vgdisplay myvg # Display information about the logical volume lvdisplay /dev/myvg/mylv ``` If the issue persists, verifying the integrity of the file system using tools like `fsck` may be necessary. ```bash fsck /dev/myvg/mylv ``` #### Example Playbook for Automating LVM Tasks with Ansible Ansible can automate LVM tasks by defining playbooks. Below is an example playbook that creates a physical volume, volume group, and logical volume: ```yaml --- - name: Configure LVM hosts: all become: yes tasks: - name: Create physical volume community.general.lvol: vg: myvg lv: mylv size: 10G state: present ``` This playbook uses the `community.general.lvol` module to manage LVM volumes[^3]. ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值