====== LVM - Logical Volume Manager ====== ===== Install logical volumes (LVM) ===== ==== Prepare and install volumes ==== Prepare a single disk or a disk raid: lvm lvm> pvcreate lvm> vgcreate // or several physical volumes: lvm> lvcreate lvm> quit mkfs.ext4 /dev/mapper/ mount /dev/mapper/ Auto-mount during boot: vim /etc/fstab /dev/mapper/ ext3 noatime,user_xattr 0 0 ===== Replace LVM disk to upgrade capacity ===== This example will copy /dev/sde1 mounted on /backup to /dev/sdf1 mounted on /replace and afterwards change mount point, logical volume and volume group names to the source names. ==== Prepare new disk ==== * fdisk /dev/sdf * **n** to create a new partition, select **1** and accept all defaults * **w** to write the new partition table and quit * mkfs.ext3 /dev/sdf1 * lvm * lvm > pvcreate /dev/sdf1 * lvm > vgcreate vg_replace /dev/sdf1 * lvm > vgdisplay vg_replace to check ### of free extents * lvm > lvcreate -l### vg_replace (do not use -LxxGB, use size with -l### which is number of extents) * lvm > lvrename vg_replace lvol0 lv_replace * lvm > vgcfgbackup to backup volume group configurations * lvm > exit * mkfs -t ext3 /dev/vg_replace/lv_replace * mkdir /replace * mount /dev/vg_replace/lv_replace /replace ==== Copy content and assign new disk ==== * rsync -avH /backup/* /replace/ * for an entire BackupPC structurersync -avxHAWX --info=progress2 /backup/ /new-backup/ * verify the structure between source and destinationrsync -avcn /backup/ /new-backup/ * umount ''/backup'' * umount ''/new-backup'' * lvm * lvm > lvrename vg_backup/lv_backup lv_backup1 * lvm > lvchange vg_backup/lv_backup1 -an * lvm > vgrename vg_backup vg_backup1 * lvm > lvchange vg_backup1/lv_backup1 -ay * lvm > lvrename vg_replace/lv_replace lv_backup * lvm > lvchange vg_replace/lv_backup -an * lvm > vgrename vg_replace vg_backup * lvm > lvchange vg_backup/lv_backup -ay * lvm > exit * mount /dev/vg_backup/lv_backup /backup -o noatime,user_xattr ==== Setting up LVM on top of a Linux Software Raid ==== * prepare the new disks with a partition (see above), do not create the filesystem yet * mdadm --create --verbose /dev/md5 --level=1 --raid-devices=2 /dev/sde /dev/sdf * watch the progress with cat /proc/mdstat * when finished, restart the computer (shutdown -r now) ==== Merge 2 volume groups ==== - Unmount and remove LV0 and LV1 from VG1 with umount/lvremove - Remove VG1 with vgremove - Unmount LV0 and LV1 from VG0 with umount - Extend VG0 with any available PVs if necessary - Mount LV0 and LV1 on VG0 with mount lvcreate -l -n testlv testvg mkfs.ext4 /dev/mapper/vg-lv ==== Increase size of physical volume and/or logical volume ==== - Replace hard disks in a RAID1 array, or change from a RAID1 to a RAID5 array, the resulting array being MD127 (see [[deb9:mdadm|mdadm - Software RAID]]) - Use pvresize to extend the physical volume to consume all available spacepvresize /dev/md127 - Use lvresize to extend the logical volume to consume all available spacelvresize -l +100%FREE /dev/mapper/- - Or, unmount the volume, delete the logical volumes and the volume group first, then create a new logical volume consuming all available spacelvremove /dev/mapper/- vgremove lvcreate -l 100%FREE -n ===== Increase / decrease size of logical volume ===== First, check the status with: # pvs # vgs # lvs * [[https://www.rootusers.com/lvm-resize-how-to-decrease-an-lvm-partition/|LVM Resize – How to Decrease an LVM Partition]] * [[https://www.rootusers.com/lvm-resize-how-to-increase-an-lvm-partition/|LVM Resize – How to Increase an LVM Partition]] * [[http://www.linuxquestions.org/questions/linux-hardware-18/size-in-superblock-is-different-from-the-physical-size-of-the-partition-298175/| Size in superblock is different from the physical size of the partition]] * [[https://www.systutorials.com/124416/shrinking-a-ext4-file-system-on-lvm-in-linux/|Shrinking an Ext4 File System on LVM in Linux]] * [[https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Deployment_Guide/s1-filesystem-ext4-create.html|Creating an ext4 File System]] * [[https://www.tecmint.com/extend-and-reduce-lvms-in-linux/|How to Extend/Reduce LVM’s]] * [[https://we.riseup.net/debian/how-to-grow-ext4+lvm+luks+raid|How to grow ext4+lvm+LUKS+RAID]] ==== Decrease ==== - sudo umount /srv/media - sudo lvresize --resizefs --size -1024GB /dev/vg_data/lv_media ==== Increase ==== - sudo lvresize --resizefs --size +1024GB /dev/vg_data/lv_home ==== Remove a disk from a logical volume ==== * [[https://www.howtogeek.com/howto/40702/how-to-manage-and-use-lvm-logical-volume-management-in-ubuntu/|How to Manage and Use LVM]] * [[https://blog.100tb.com/resize-or-remove-logical-volumes-with-lvm|Resize Or Remove Logical Volumes With LVM]] * [[http://www.microhowto.info/howto/increase_the_size_of_an_lvm_logical_volume.html|Increase the size of an LVM logical volume]] * [[https://serverfault.com/questions/692340/how-can-i-tell-pvresize-to-expand-a-physical-volume-to-include-all-available-spa|How can I tell pvresize to expand a physical volume to include all available space?]] * [[http://www.theitblogg.com/2016/10/create-logical-volume-using-maximum-available-free-space/|How to Create Logical Volume Using Maximum Available Free Space in Volume Group]] * [[https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/5/html/logical_volume_manager_administration/disk_remove_ex|REMOVING A DISK FROM A LOGICAL VOLUME]] * [[https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/logical_volume_manager_administration/vg_remove|REMOVING VOLUME GROUPS]] * [[https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/logical_volume_manager_administration/lv_remove|REMOVING LOGICAL VOLUMES]] * [[https://wiki.debian.org/LVM#Encrypted_LVM|LVM (Debian Wiki)]]