I want to write a script that works with LVM (creates physical volumes, volume groups, etc) on computer with two hard drives. However, I do not have a second hard drive in my PC, nor I want to add one.

Are there any ways to emulate such conditions, besides using Virtual Box?

I want to be able to manipulate some virtual physical volumes, volume groups, i.e use pvcreate, vgcreate, lvcreate, etc.

  • system-config-lvm is a GUI app to do this. Why write a script? Virtualbox is the way to play.
    – heynnema
    Jun 12, 2017 at 23:44
  • @heynnema I'm not sure, but I heard that disks can be somehow replaced by files. Jun 12, 2017 at 23:48

1 Answer 1


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:

  • sudo losetup -a | grep -P "disk[12].img | cut -f1 -d:"
    These are the loop devices of interest that we will look for in the output of the next command
  • sudo blkid
    Output that corresponds to my loop devices
    /dev/loop3: PTUUID="d87e1d53" PTTYPE="dos"
    /dev/loop3p1: PARTUUID="d87e1d53-01"
    /dev/loop3p2: PARTUUID="d87e1d53-02"
    /dev/loop4: PTUUID="8ec0af2f" PTTYPE="dos"
    /dev/loop4p1: PARTUUID="8ec0af2f-01"
    /dev/loop4p2: PARTUUID="8ec0af2f-02"
    So my disk1 partitions are at /dev/loop3p1 and /dev/loop3p2, and my disk2 partitions are at /dev/loop4p1 and /dev/loop4p2. You're probably good to go from here, but I'll continue with an example LVM configuration.

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                                                    
  • Thanks for your effort! I really appreciate your answer. Jun 13, 2017 at 1:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .