LVM: Difference between revisions
Add ArchWiki link and document PE to explain that LVs don't have to be contiguous on a PV. |
Troubleshooting: LVM keeps ZFS zvol device busy |
||
| (One intermediate revision by one other user not shown) | |||
| Line 11: | Line 11: | ||
=== Create an LV === | === Create an LV === | ||
Below are shell commands to setup a PV, a VG and an LV from scratch. LVM comes enabled by default on NixOS.<syntaxhighlight lang="shell"> | |||
< | # format the partion into a physical volume (check with pvdisplay) | ||
# | |||
pvcreate /dev/sda2 | pvcreate /dev/sda2 | ||
# | # create a new volume group named pool (check with vgdisplay) | ||
vgcreate pool /dev/sda2 | vgcreate pool /dev/sda2 | ||
# | # create a new logical volume named "home" with the size of 10GB (check with lvdisplay) | ||
# makes /dev/pool/home available | # makes /dev/pool/home available | ||
lvcreate --size 10G --name home pool | lvcreate --size 10G --name home pool | ||
# finally, create a filesystem inside the logical volume | |||
mkfs.ext4 /dev/pool/home | mkfs.ext4 /dev/pool/home | ||
</ | </syntaxhighlight> | ||
=== Use the LV === | === Use the LV === | ||
Mount the filesystem on the logical volume by adding an entry to the {{Nixos:option|fileSystems}} NixOS option. | |||
{{File|3=fileSystems."/home" = { | |||
fileSystems."/home" = { | |||
device = "/dev/pool/home"; | device = "/dev/pool/home"; | ||
fsType = "ext4"; | fsType = "ext4"; | ||
}; | };|name=/etc/nixos/configuration.nix|lang=nix}} | ||
== Booting with special LVM Modes == | == Booting with special LVM Modes == | ||
LVM provides a number of special features such as creating snapshots, RAID for single LVs and much more. If you want to use these devices on bootup, the associated <code>dm-*</code> kernel module must be provided in the initrd (see for example {{Issue|33646}}). | LVM provides a number of special features such as creating snapshots, RAID for single LVs and much more. If you want to use these devices on bootup, the associated <code>dm-*</code> kernel module must be provided in the initrd (see for example Nixpkgs issue {{Issue|33646}}). Here is a non-exhaustive list of features and the corresponding kernel module and other options to put into your NixOS configuration: | ||
boot.initrd.kernelModules = [ | {{File|3=boot.initrd.kernelModules = [ | ||
"dm-snapshot" # when you are using snapshots | "dm-snapshot" # when you are using snapshots | ||
"dm-raid" # e.g. when you are configuring raid1 via: `lvconvert -m1 /dev/pool/home` | "dm-raid" # e.g. when you are configuring raid1 via: `lvconvert -m1 /dev/pool/home` | ||
| Line 42: | Line 39: | ||
]; | ]; | ||
services.lvm.boot.thin.enable = true; # when using thin provisioning or caching | services.lvm.boot.thin.enable = true; # when using thin provisioning or caching|name=/etc/nixos/configuration.nix|lang=nix}} | ||
== Automated Partitioning == | == Automated Partitioning == | ||
| Line 54: | Line 50: | ||
[[Disko]] provides means to automatically generate the creation and configuration of logical volumes, see https://github.com/nix-community/disko | [[Disko]] provides means to automatically generate the creation and configuration of logical volumes, see https://github.com/nix-community/disko | ||
== Troubleshooting == | |||
=== LVM keeps ZFS zvol busy === | |||
The LVM service will possibly manage zfs zvol devices. If you want to destroy a zvol which contains a vm which uses LVM it can give an error. | |||
<syntaxhighlight lang="shell"> | |||
zfs destroy -r vmstorage/vm-alma | |||
dataset is busy | |||
</syntaxhighlight> | |||
Issue <code>lvs</code> to see if the zvol device is in use. | |||
The nixpkgs options of LVM (26.05) does not provide an entry where you could enter your own configurations in /etc/lvm/lvm.conf. Other nixpkgss options will use the lvm.conf also. | |||
A workaround is to define a device filter which instructs lvm no to use zfs zvols: | |||
<syntaxhighlight lang="shell"> | |||
devices { | |||
filter="r|^/dev/zvol*|" | |||
global_filter="r|^/dev/zvol*|" | |||
} | |||
</syntaxhighlight> | |||
Setup a lvm.conf in NixOS such as: | |||
<syntaxhighlight lang="shell"> | |||
environment.etc."lvm/lvm.conf".text = pkgs.lib.mkForce '' | |||
devices { | |||
filter = [ "r|^/dev/zvol*|" ] | |||
global_filter = [ "r|^/dev/zvol*|" ] | |||
} | |||
''; | |||
</syntaxhighlight> | |||
After reboot remove the zfs zvol. The workaround needs to be removed if NixOS previousely had a lvm.conf. | |||
== See also == | == See also == | ||
Latest revision as of 15:26, 13 August 2026
Linux's Logical Volume Manager (LVM) provides means to dynamically organize partitions. For example, partitions can be created, moved, resized and deleted regardless of fragmentation on disk.
Basic Setup
LVM manages four basic building blocks:
- Physical volume (PV)
- Unix block device node reserved for use by LVM. Examples: a disk partition, a whole disk (i.e. without a partition table), a meta device, or a loopback file.
- Volume group (VG)
- Group of PVs for allocating PEs to LVs.
- Logical volume (LV)
- "Virtual partition" composed of PEs inside a VG.
- Physical extent (PE)
- The smallest contiguous extent (default 4 MiB) in the physical volume that can be allocated to an LV.
Create an LV
Below are shell commands to setup a PV, a VG and an LV from scratch. LVM comes enabled by default on NixOS.
# format the partion into a physical volume (check with pvdisplay)
pvcreate /dev/sda2
# create a new volume group named pool (check with vgdisplay)
vgcreate pool /dev/sda2
# create a new logical volume named "home" with the size of 10GB (check with lvdisplay)
# makes /dev/pool/home available
lvcreate --size 10G --name home pool
# finally, create a filesystem inside the logical volume
mkfs.ext4 /dev/pool/home
Use the LV
Mount the filesystem on the logical volume by adding an entry to the fileSystems NixOS option.
fileSystems."/home" = {
device = "/dev/pool/home";
fsType = "ext4";
};
Booting with special LVM Modes
LVM provides a number of special features such as creating snapshots, RAID for single LVs and much more. If you want to use these devices on bootup, the associated dm-* kernel module must be provided in the initrd (see for example Nixpkgs issue 🚩︎#33646). Here is a non-exhaustive list of features and the corresponding kernel module and other options to put into your NixOS configuration:
boot.initrd.kernelModules = [
"dm-snapshot" # when you are using snapshots
"dm-raid" # e.g. when you are configuring raid1 via: `lvconvert -m1 /dev/pool/home`
"dm-cache-default" # when using volumes set up with lvmcache
];
services.lvm.boot.thin.enable = true; # when using thin provisioning or caching
Automated Partitioning
People have created a number of tools to automate the partitioning in NixOS:
NixOps
NixOps can repartition Hetzner Physical Machines, see NixOps Manual.
Disko
Disko provides means to automatically generate the creation and configuration of logical volumes, see https://github.com/nix-community/disko
Troubleshooting
LVM keeps ZFS zvol busy
The LVM service will possibly manage zfs zvol devices. If you want to destroy a zvol which contains a vm which uses LVM it can give an error.
zfs destroy -r vmstorage/vm-alma
dataset is busy
Issue lvs to see if the zvol device is in use.
The nixpkgs options of LVM (26.05) does not provide an entry where you could enter your own configurations in /etc/lvm/lvm.conf. Other nixpkgss options will use the lvm.conf also.
A workaround is to define a device filter which instructs lvm no to use zfs zvols:
devices {
filter="r|^/dev/zvol*|"
global_filter="r|^/dev/zvol*|"
}
Setup a lvm.conf in NixOS such as:
environment.etc."lvm/lvm.conf".text = pkgs.lib.mkForce ''
devices {
filter = [ "r|^/dev/zvol*|" ]
global_filter = [ "r|^/dev/zvol*|" ]
}
'';
After reboot remove the zfs zvol. The workaround needs to be removed if NixOS previousely had a lvm.conf.