lvm cheatsheet

  • apt-get install lvm2
  • pvcreate /dev/sda4 /dev/sdb1
  • vgcreate vg0 /dev/sda4 /dev/sdb1
  • lvcreate -n srv -l 100%FREE vg0
  • mkfs.ext4 /dev/vg0/srv

 

  • pvdisplay
    • Display physical volume information
  • vgdisplay
    vgs
    • Display volume group information
  • lvdisplay
    lvs
    • Display logical volume information
  • lvcreate -L108G -n goldfish vg0
    • Create volume
  • lvextend -L+3T /dev/mapper/vg0-lv--srv
    • -> Enlarges volume by 3GB. Then:
    • umount /dev/mapper/vg0-lv--srv
    • e2fsck -f /dev/mapper/vg0-lv--srv
    • resize2fs /dev/mapper/vg0-lv--srv
    • mount /dev/mapper/vg0-lv--srv

Mount lvm on external harddisk

  • vgscan
  • vgdisplay
    • If the external harddisk has the same VG name we need to rename it.
    • If you connected an external harddisk via USB it can be identified by "Open LV = 0"
    • Note VG UUID and use it in the following command to rename the volume group:
    • vgrename AAA-BBB....-000 old-crypt
    •  
  • vgchange --activate y
    • Activates all volume groups
  • lvdisplay
    • LV Path                /dev/old-crypt/root
  • mount -o ro /dev/old-crypt/root /mnt
    • In case of errors: "mount: /mnt: mount(2) system call failed: Structure needs cleaning."
    • fsck -cy /dev/old-crypt/root

Snapshots

  • lvcreate --size 1G --snapshot --name test /dev/vg0/vm_images
    • create snapshot
  • lvremove /dev/vg0/test
    • remove snapshot

Shrink logical volume

https://blog.shadypixel.com/how-to-shrink-an-lvm-volume-safely/

Example: Reduce /srv from 460G to 422G

  • umount /srv
  • fsck.ext4 -f -y /dev/vg0/srv
  • resize2fs /dev/vg0/srv 400G
    • A good amount smaller than the final size, because it is unclear if the tools work with 1000 or 1024 multipliers
  • lvreduce -L 422G /dev/vg0/srv
  • resize2fs /dev/vg0/srv
    • Resize filesystem to max size
  • fsck.ext4 -f -y /dev/vg0/srv
  • vgs
  • mount /srv

Moving a volume group

http://www.linuxquestions.org/questions/linux-software-2/ubuntu-move-kvm-guest-from-one-host-to-another-870211/

http://www.linuxquestions.org/questions/linux-general-1/cloning-logical-volume-with-dd-341961/

Resizing

http://libguestfs.org/virt-resize.1.html

 

Troubleshooting

/dev/dm-3: read failed after 0 of 4096 at 0: Input/output error

  • dmsetup ls
  • dmsetup info /dev/dm-3
  • lsblk
  • pvs
  • lvdisplay
    •  LV snapshot status     INACTIVE destination for /dev/vg0/vm_images
  • -> deleted snapshot "lvremove /dev/vg0/vm_xxx"