Using sparse files in lieu of disks
If you want to play around with LVM without making changes to the partitions on your disks, yes, this can be done. Create a couple of sparse files that can serve as your "virtual disks." Then create the partitions on those files and setup loop devices using your disk images. From there, you can play around with LVM on the loop devices that point to your disk images.
Make your sparse files
Run the following in a terminal to create two sparse files. Sparse files always report that they are a predetermined, fixed size, but in reality, they initially use 0 bytes of disk space. As they are written to, they can grow until either they reach their predetermined size or the disk containing them has no more space.
truncate -s 1T /tmp/disk1.img
truncate -s 1T /tmp/disk2.img
Create partitions on the disk images
Now you have two files that you can treat like disks. Use fdisk to partition these images. If you don't know how to use fdisk to create partitions, you can check out this tutorial or run the commands below which will create two partitions of 512G each on both disk images.
sudo sfdisk /tmp/disk1.img <<< "$(base64 -d <<< "bGFiZWw6IGRvcwpsYWJlbC1pZDogMHhkODdlMWQ1MwpkZXZpY2U6IC90bXAvZGlzazEuaW1nCnVuaXQ6IHNlY3RvcnMKCi90bXAvZGlzazEuaW1nMSA6IHN0YXJ0PSAgICAgICAgMjA0OCwgc2l6ZT0gIDEwNzM3NDE4MjQsIHR5cGU9ODMKL3RtcC9kaXNrMS5pbWcyIDogc3RhcnQ9ICAxMDczNzQzODcyLCBzaXplPSAgMTA3MzczOTc3NiwgdHlwZT04Mwo=")"
sudo sfdisk /tmp/disk2.img <<< "$(base64 -d <<< "bGFiZWw6IGRvcwpsYWJlbC1pZDogMHg4ZWMwYWYyZgpkZXZpY2U6IC90bXAvZGlzazIuaW1nCnVuaXQ6IHNlY3RvcnMKCi90bXAvZGlzazIuaW1nMSA6IHN0YXJ0PSAgICAgICAgMjA0OCwgc2l6ZT0gIDEwNzM3NDE4MjQsIHR5cGU9ODMKL3RtcC9kaXNrMi5pbWcyIDogc3RhcnQ9ICAxMDczNzQzODcyLCBzaXplPSAgMTA3MzczOTc3NiwgdHlwZT04Mwo=")"
Setup images on loop devices
Next we lets get our disk images connected to some loop devices so that we can work with the partitions. In a terminal, run the following commands (losetup -f
returns the next available loop device)
sudo losetup -P $(losetup -f) /tmp/disk1.img
sudo losetup -P $(losetup -f) /tmp/disk2.img
Determine what devices our partitions are connected to
Now we need to figure out which loop devices point to our partitions. Run the following in a terminal:
Example LVM setup
I'm only going to use disk1, partition 1 and disk2 partition 2 in my LVM configuration. From terminal run the following, replacing the paths to the loop devices as necessary:
- Setup physical volumes:
sudo pvcreate /dev/loop3p1 /dev/loop4p2
- Setup volume group:
vgcreate my_volume_group /dev/loop3p1 /dev/loop4p2
- Setup a couple partitions:
- Partition1:
sudo lvcreate --name root --size 30G my_volume_group
- Partition2:
sudo lvcreate --name swap --size 4G my_volume_group
- Partition3:
sudo lvcreate --name home --extents +100%free my_volume_group
Now I have a single volume group with 1TB of space that is taken from 512G on disk1.img and 512G on disk2.img. This can be verified by running:
sudo pvs
which outputs
PV VG Fmt Attr PSize PFree
/dev/loop3p1 my_volume_group lvm2 a-- 512.00g 0
/dev/loop4p2 my_volume_group lvm2 a-- 512.00g 0
sudo vgs
which outputs
VG #PV #LV #SN Attr VSize VFree
my_volume_group 2 3 0 wz--n- 1023.99g 0
sudo lvs
which outputs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home my_volume_group -wi-a----- 989.99g
root my_volume_group -wi-a----- 30.00g
swap my_volume_group -wi-a----- 4.00g
system-config-lvm
is a GUI app to do this. Why write a script? Virtualbox is the way to play.