NetBSD LVM allows logical volume management on NetBSD systems,
with a well known user interface, which is the same as the Linux
LVM2 tools.

NetBSD LVM is built on Linux lvm2tools and libdevmapper,
together with a BSD-licensed device-mapper kernel driver
specially written for NetBSD.

The LVM driver allows the user to manage
available disk space effectively and efficiently. Disk space from
several disks, and partitions, known as “Physical Volumes”, can
be added to “Volume Groups”, which is the pool of available
disk space for “Logical Partitions”aka Logical Volumes.

Logical Volumes can be grown and shrunk at will using the
LVM utilities.

The basic building block is the Physical Volume. This is a disk,
or a part of a disk, which is used to store data.

Physical Volumes are aggregated together to make Volume Groups, or
VGs. Typically, Volume Groups are used to aggregate
storage for the same functional unit. Typical Volume Groups could thus
be named “Audio”, “Multimedia” or “Documents”.
By segregating storage requirements in this functional way, the same type
of resilience and redundancy is applied to the whole of the functional
unit.

The steps required to setup a LVM are as follows:

Install physical media

Configure kernel support

Configure system, install tools

Optional step

Disklabel each volume member of the LVM

Initialize the LVM disk devices

Create a volume group from initialized disks

Create Logical volume from created Volume group

Create a filesystem on the new LV device

Mount the LV filesystem

This example features a LVM setup on NetBSD/i386.

17.1. Anatomy of NetBSD Logical Volume Manager

Figure 17.1. Anatomy of Logical Volume Management

Volume Group

The Volume Group is a disk space pool from which the user creates Logical Volumes and
to which Physical Volumes can be added. It is the basic administration unit of the NetBSD
LVM implementation.

Physical Volume

A physical volume is the basic unit in a LVM structure. Every PV consists of small
disk space chunks called Physical Extends. Every Volume Group must have at least one PV.
A PV can be created on hard disks or hard disk like devices such as raid, ccd, or cgd device.

Logical Volume

The Logical Volume is a logical partition created from disk space assigned to the Volume Group.
LV can be newfsed and mounted as any other pseudo-disk device. Lvm tools use functionality exported by
the device-mapper driver in the kernel to create the LV.

Physical Extents

Each physical volume is divided chunks of disk space. The default size is 4MB. Every LV
size is rounded by PE size. The LV is created by mapping Logical Extends in the LV to Physical
extends in a Volume group.

Logical Extents

Each logical volume is split into chunks of disk space, known as logical extents. The extent
size is the same for all logical volumes in the volume group.

Physical Extents mapping

Every LV consists of “LEs” mapped to “PEs” mapped by a target mapping.
Currently, the following mappings are defined.

Linear Mapping

will linearly assign range of PEs to LEs.

For example it can map 100 PEs from PV 1 to LV 1 and
another 100 PEs from PV 0.

Stripe Mapping

will interleave the chunks of the logical extents across a number of physical
volumes.

Snapshots

A facility provided by LVM is 'snapshots'. Whilst in standard NetBSD, the “fss”
driver can be used to provide filesystem snapshots at a filesystem level,
the snapshot facility in the LVM allows the administrator to
create a logical block device which presents an exact copy of a logical volume, frozen at some
point in time. This facility does require that the snapshot be made at a time when the data on
the logical volume is in a consistent state.

Warning

Snapshot feature is not fully implemented in LVM in NetBSD and should not be used in
production.

17.2. Install physical media

This step is at your own discretion, depending on your platform and the hardware at your
disposal. LVM can be used with disklabel partitions or even with standard
partitions created with fdisk.

17.3. Configure Kernel Support

The following kernel configuration directive is needed to provide LVM device support. It
is provided as a kernel module, so that no extra modifications need be made to a standard
NetBSD kernel.
The dm driver is provided as a kernel module, it first appeared in
the NetBSD 6.0 release.

If your system doesn't use modules you can enable dm driver in NetBSD by adding
this line to kernel configuration file. This will add device-mapper driver to
kernel and link it as statically linked module.

pseudo-device dm

If you do not want to rebuild your kernel only because of LVM support
you can use dm kernel module. The devmapper kernel module can be loaded on
your system. To get the current status of modules in the kernel, the
modstat is used:

If your port uses a MBR (Master Boot Record) to partition the disks so that the NetBSD
partitions are only part of the overall disk, and other OSs like Windows or Linux use other
parts, you can void the MBR and all partitions on disk by using the command:

This will make all data on the entire disk inaccessible. Note that the entire disk is
slice d on i386 (and some other ports), and c
elsewhere (e.g. on sparc). See the “kern.rawpartition” sysctl - "3" means "d",
"2" means "c".

You will need to create one “slice” on the NetBSD partition of the disk
that consumes the entire partition. The slice must begin at least two sectors after end of
disklabel part of disk. On i386 it is sector “63”. Therefore, the
“size” value should be “total sectors” minus 2x
“sectors”. Edit your disklabel accordingly:

#disklabel -e sd0

Note

The offset of a slice of type “4.2BSD” must be a multiple of the
“sectors” value.

Note

Be sure to export EDITOR=[path to your favorite editor] before
editing the disklabels.

Note

The slice must be fstype 4.2BSD.

Because there will only be one slice on this partition, you can recycle the
d slice (normally reserved for symbolic uses). Change your disklabel to the
following:

Be sure to write the label when you have completed. Disklabel will object to your
disklabel and prompt you to re-edit if it does not pass its sanity checks.

17.6. Create Physical Volumes

Once all disks are properly labeled, you will need to create physical volume on them.
Every partition/disk added to LVM must have physical volume header on start
of it. All informations, like Volume group where Physical volume belongs are stored in this
header.

#lvm pvcreate /dev/rwd1[ad]

Status of physical volume can be viewed with pvdisplay command.

#lvm pvdisplay

17.7. Create Volume Group

Once all disks are properly labeled with physical volume header, volume group must be
created from them. Volume Group is pool of PEs from which administrator can create Logical
Volumes “partitions”.

#lvm vgcreate vg0 /dev/rwd1[ad]

vg0 is name of Volume Group

/dev/rwd1[ad] is Physical Volume

Volume group can be later extended/reduced with vgextend and vgreduce commands. These
commands adds physical volumes to VG.

#lvm vgextend vg0 /dev/rwd1[ad]#lvm vgreduce vg0 /dev/rwd1[ad]

Status of Volume group can be viewed with vgdisplay command.

#lvm vgdisplay vg0

17.8. Create Logical Volume

Once Volume Group was created administrator can create “logical partitions”
volumes.

#lvm lvcreate -L 20M -n lv1 vg0

vg0 is name of Volume Group

-L 20M is size of Logical Volume

-n lv1 is name of Logical Volume

Logical Volume can be later extended/reduced with lvextend and lvreduce commands.

#lvm lvextend -L+20M /dev/vg0/lv1#lvm lvreduce -L-20M /dev/vg0/lv1

Note

To shrink the lv partition, you must first shrink the filesystem
using resize_ffs(8) (which as of NetBSD 6.x does not support shrinking of FFSv2 yet).

Status of Logical Volume can be viewed with lvdisplay command.

#lvm lvdisplay lv0/lv1

After reboot all functional LV's in defined Volume group can be activated with command

#lvm vgchange -a y

17.9. Example: LVM with Volume groups located on raid1

Motivation for using raid 1 disk as physical volume disk for Volume Group is disk reliability.
With PV on raid 1 disk it is possible to use Logical Volumes even after disk failure.

17.9.1. Loading Device-Mapper driver

Before we can start work with the LVM tools. We have to be sure that
NetBSD dm driver was properly compiled into the kernel or loaded as a module.
Easiest way how to find if we have dm driver available is run modstat.
For more information see
Configure Kernel Support chapter.

17.9.2. Preparing raid1 installation

Following example raid configuration defined in
Raid 1 configuration user will set up clean
raid1 disk device. With 2 disks in a mirror mode.

Partitions should be created with offset 65, because sectors <
than 65 sector are marked as readonly and therefore can't be rewritten.

17.9.3. Creating PV, VG on raid disk

Physical volumes can be created on any disk like device and on any
partition on it we can use a or d on sparc64 c partitions. PV will label
selected partition as LVM used and add needed information to it.

PV is created on char disk device entry. As any other disk operation
in the NetBSD.

#lvm pvcreate /dev/rraid0a

For our example purpose I will create vg00 Volume Group. The first
parameter of vgcreate command is Volume Group name and second is PV
created on raid. If you later found that VG size is no sufficient and you
need more space we will can add it with vgextend
command.

#lvm vgcreate vg00 /dev/rraid0a#lvm vgextend vg00 /dev/rraid1a

Warning

If you add non-raid PV to your Volume Group your data are not safe
anymore. Therefore you should add raid based PV to VG if you want to
keep your data safe.

17.9.4. Creating LV's from VG located on raid disk

For our example purpose we will create Logical Volume named lv0. If
you later found that LV size is not sufficient for you can add it with
lvresize command.

Note

You must also resize the filesystem, when you resize LV,
otherwise you will not see any filesystem change when you mount LV.

Warning

Be aware that to shrink LV you must first shrink the filesystem
(and shrinking of FFSv2 filesystems is not supported yet in NetBSD 6.x).

This means that for FFSv2 filesystems, the -L-* option is not available in NetBSD.

#lvm lvcreate -n lv0 -L 2G vg00#lvm lvresize -L+2G vg00/lv0

All lv device nodes are created in the /dev/vg00/
directory. File system can be create on LV with this command. After
filesystem creation LV can be mounted to system.

#newfs -O2 /dev/vg00/rlv0#mount /dev/vg00/lv0 /mnt/

17.9.5. Integration of LV's in to the system

For Proper LVM integration you have to enable lvm rc.d script,
which detect LVs during boot and enables them. You have to add entry
for Logical Volume to the /etc/fstab file.