Skip to content

Instantly share code, notes, and snippets.

@alex2600
Last active September 19, 2024 10:56
Show Gist options
  • Save alex2600/3b2ebd16fa5b088358eda7597f1be196 to your computer and use it in GitHub Desktop.
Save alex2600/3b2ebd16fa5b088358eda7597f1be196 to your computer and use it in GitHub Desktop.
Create virtual device from file and use it in LVM
fallocate -l 256M /image.img # create file to store a filesystem
losetup /dev/loop0 # check if loop0 is free to use, if not continue with loop1, ...
losetup /dev/loop0 /image.img # create device using file
pvcreate /dev/loop0 # create lvm physical volume
vgcreate lvmstorage /dev/loop0 # create volume group "lvmstorage" using device
lvcreate -L 128M -n lv1 lvmstorage # create logical volume "lv1" with size 128M
mkfs.ext4 /dev/lvmstorage/lv1 # create file system
e2label /dev/lvmstorage/lv1 LV1 # set label "LV1" for device
mkdir /mnt/stuff
mount /dev/lvmstorage/lv1 /mnt/stuff # mount device
df -hT # check result
@stepanovmm1992
Copy link

stepanovmm1992 commented Nov 20, 2020

Hello, you can change 2 and 3 line to losetup -fP /image.img
If you want find loop with your file, you can run some like this losetup -a | grep image.img

@Et7f3
Copy link

Et7f3 commented Sep 19, 2024

Do you need the "loop" module loaded ?

@stepanovmm1992
Copy link

@Et7f3 I'm really sorry, but I completely forgot what I meant))
This was the beginning of my career in information technology, and I was stupid)
Anyway, thanks for this instruction, it was really helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment