I’ve been using LVM to work around the hassle of constantly changing hard disks, partitions, mounting shared data, etc.
# sfdisk --show-size # lshw -short -class disk
- List briefly (“partprobe probes partition table for changes and informs OS”. Foo?):
# partprobe -s /dev/hda: msdos partitions 1 2 3 4 <5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25> /dev/hdb: msdos partitions 1 2 <5 6 7 8 9 10 11 12 13 14 15 16 17 18>
- List with geometry and sizes in MiB (-uM) or sectors (-uS):
# sfdisk --list -uM /dev/hda
Note MiB involves rounding. Use sectors for precision.
- Analyze content of (unmounted) partition — filtering through hexdump to see raw data, or through strings to look for text; looking in first 10 MiB:
# head --bytes=10m /dev/hda7 | hexdump -C | less # head --bytes=10m /dev/sda3 | strings --all | less
If using less, also use head to limit amount of data read, because with large empty partitions these commands take forever to start displaying anything (I guess because less waits to fill the first page?):
# hexdump -C /dev/hda14 | less # cat /dev/hda14 | strings --all | less
- An empty (zeroed) partition produces this hexdump:
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| * 00a00000 (END)
(Isn’t the first cylinder (of every partition) ever used? Its first sector contains a partitions table, but the rest, which can be several megabytes long, is wasted?)
- Overwrite with random data (generated by /dev/urandom):
# shred --verbose /dev/sda1
- Scan for physical volumes (PV) (grep to filter device):
# pvscan PV /dev/hda9 VG moved lvm2 [11.18 GB / 0 free] PV /dev/hda10 VG moved lvm2 [3.73 GB / 0 free] PV /dev/hda11 VG moved lvm2 [7.46 GB / 0 free] PV /dev/hda12 VG moved lvm2 [11.18 GB / 4.06 GB free] PV /dev/hda13 VG moved lvm2 [18.64 GB / 7.13 GB free] Total: 5 [52.19 GB] / in use: 5 [52.19 GB] / in no VG: 0 [0 ]
- Scan for logical volumes (LV):
# lvscan --verbose Finding all logical volumes ACTIVE '/dev/moved/sound' [1.00 GB] inherit ACTIVE '/dev/moved/star' [1.00 GB] inherit ACTIVE '/dev/moved/age' [9.00 GB] inherit ACTIVE '/dev/moved/knowledge' [8.00 GB] inherit ...
- List volume groups (VG):
# vgs VG #PV #LV #SN Attr VSize VFree moved 6 20 0 wz--n 33.51G 3.51G
- List LVs:
# lvs LV VG Attr LSize Origin Snap% Move Copy% abstract moved -wi-ao 1.00G age moved -wi-ao 9.00G critic moved -wi-ao 2.00G cubism moved -wi-ao 1.00G fiction moved -wi-ao 1.00G knowledge moved -wi-ao 8.00G mess moved -wi-ao 1.00G ...
- Displaying a LV’s attriubtes:
# lvdisplay /dev/moved/abstract --- Logical volume --- LV Name /dev/moved/abstract VG Name moved LV UUID Wd2DvY-UoSb-xsUY-Ywry-s7Cv-raXl-v5km3L LV Write Access read/write LV Status available # open 2 LV Size 1.00 GB Current LE 256 Segments 1 Allocation inherit Read ahead sectors 0 Block device 254:13
--mapsshows which physical volumes (partitions) and logical extents this volume occupies.
Create physical volumes (PV)
- Formating a partition to make it a PV:
# pvcreate /dev/hda14 Physical volume "/dev/hda14" successfully created
Create volume groups (VG)
- Adding a (formatted) PV to a VG:
# vgextend --verbose moved /dev/hda14 Checking for volume group "moved" Archiving volume group "moved" metadata. Adding physical volume '/dev/hda14' to volume group 'moved' Volume group "moved" will be extended by 1 new physical volumes Creating volume group backup "/etc/lvm/backup/moved" Volume group "moved" successfully extended
Create logical volumes (LV)
# lvcreate --size 80G --name progress heavy Logical volume "progress" created
- Moving a PV between VGs:
# vgreduce marf /dev/sdb9 # vgextend heavy /dev/sdb9 Volume group "heavy" successfully extended
- Removing LV from VG so can extend other LVs:
# lvs moved LV VG Attr LSize Origin Snap% Move Log Copy% abstract moved -wi-ao 5.46G age moved -wi-ao 22.00G ... # lvremove moved/mirror Do you really want to remove active logical volume "mirror"? [y/n]: y Logical volume "mirror" successfully removed # vgdisplay moved --- Volume group --- VG Name moved System ID Format lvm2 ... Alloc PE / Size 37404 / 146.11 GB Free PE / Size 256 / 1.00 GB
- Extending LV and contained FS: first resize the LV, then the filesystem. With reiserfs, extending can be done while mounted, but for shrinking must unmount.
- Resize (extend) LV (assumes VG has enough free space; specify final size with
--size=9Gor relative change — use extents, not size, for perfect sizing!):
# lvresize --verbose --size +1G /dev/moved/abstract Finding volume group moved Archiving volume group "moved" metadata. Extending logical volume abstract to 2.00 GB Creating volume group backup "/etc/lvm/backup/moved" Found volume group "moved" Found volume group "moved" Loading moved-abstract Logical volume abstract successfully resized
- Resize (extend) FS. Given no parameters, resize_reiserfs stretches the FS to fill the partition (LV). For ext3: resize2fs. Note: do not add trailing slash.
# resize_reiserfs /dev/moved/abstract resize_reiserfs 3.6.19 (2003 www.namesys.com) resize_reiserfs: On-line resizing finished successfully.
- Could have told LVM to
--resizefsitself, but that fails:
# lvresize --verbose --size=4G --resizefs /dev/moved/surreal Finding volume group moved Executing: fsadm check /dev/moved/surreal (null) fsadm: execlp failed: No such file or directory fsadm failed: 2
# df /vol/abstract/ Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/moved-abstract 2097084 1013168 1083916 49% /vol/abstract
- Resize (extend) LV (assumes VG has enough free space; specify final size with
- To extend an LV to fill the VG: check how many free extents (PE) remain, and resize:
# vgdisplay moved --- Volume group --- VG Name moved System ID Format lvm2 Metadata Areas 6 Metadata Sequence No 139 VG Access read/write VG Status resizable MAX LV 255 Cur LV 20 Open LV 20 Max PV 255 Cur PV 6 Act PV 6 VG Size 55.91 GB PE Size 4.00 MB Total PE 14314 Alloc PE / Size 13568 / 53.00 GB Free PE / Size 746 / 2.91 GB VG UUID vYRQuG-FKKQ-m937-Bmnr-ti0l-aWNi-4j1HcI # lvresize --verbose --size +3G /dev/moved/noise Finding volume group moved Archiving volume group "moved" metadata. Extending logical volume noise to 15.00 GB Insufficient allocatable logical extents (3818) for logical volume noise: 3840 required # lvresize --verbose --extents +746 /dev/moved/noise Finding volume group moved Archiving volume group "moved" metadata. Extending logical volume noise to 14.91 GB Creating volume group backup "/etc/lvm/backup/moved" Found volume group "moved" Found volume group "moved" Loading moved-noise Logical volume noise successfully resized
Followed by the usual resize_reiserfs (or resize2fs) and df.
- Shrinking an LV and the file-system it contains: unmount, shrink the FS, then shrink the LV:
# resize_reiserfs -s 2G /dev/moved/age resize_reiserfs 3.6.19 (2003 www.namesys.com) Processing the tree: 0%....20%....40%....60%....80%....100% left 0, 8235 /sec nodes processed (moved): int 94 (1), leaves 14490 (148), unfm 298355 (143925), total 312939 (144074). check for used blocks in truncated region ReiserFS report: blocksize 4096 block count 524288 (2359296) free blocks 203123 (2038075) bitmap block count 16 (72) Syncing..done resize_reiserfs: Resizing finished successfully. # lvresize --verbose --size 2G /dev/moved/age Finding volume group moved WARNING: Reducing active logical volume to 2.00 GB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce age? [y/n]: y Archiving volume group "moved" metadata. Reducing logical volume age to 2.00 GB Creating volume group backup "/etc/lvm/backup/moved" Found volume group "moved" Found volume group "moved" Loading moved-age Logical volume age successfully resized # resize_reiserfs /dev/moved/age resize_reiserfs 3.6.19 (2003 www.namesys.com) /dev/moved/age already is of the needed size. Nothing to be done
Can't shrink filesystem on-line.
resize_reiserfs: can't shrink fs; too many blocks already allocated resize_reiserfs: Resizing failed.
- Removing LV from VG:
# umount /vol/critic/ # lvremove moved/critic Do you really want to remove active logical volume "critic"? [y/n]: n Logical volume "critic" not removed # lvdisplay moved/critic --- Logical volume --- LV Name /dev/moved/critic VG Name moved LV UUID mh6nr5-ZzuC-lG56-uhDB-5y5P-Bxq0-g1PmeX LV Write Access read/write LV Status available # open 0 LV Size 2.00 GB Current LE 512 Segments 1 Allocation inherit Read ahead sectors 0 Block device 254:13 # lvchange --available n moved/critic # lvremove moved/critic Logical volume "critic" successfully removed
- Wiping the PV signature from a partition:
# pvremove /dev/hdc18 Labels on physical volume "/dev/hdc18" successfully wiped