Author: Eric Newberry
*HOWTO: Set up Logical Volume Manager (LVM) in NetBSD*
NOTE: All commands in this howto need to be run as root.
**What is LVM?**
LVM is a software-based logical disk partioning system.
Multiple disks can be easily combined to form a partionable
continuous volume. This would allow you to, for example,
split a one terabyte partition over two 500 gigabyte
hard drives.
**Setting up LVM**
***Before We Begin***
WARNING: Setting up LVM is not a simple task. However, it
shouldn't be too hard to acomplish using this HOWTO.
***Determining Hard Drive Device IDs***
The hard drive IDs (/dev/[device]) can vary by the
controller type (SATA, SAS, IDE, SCSI, and so on). In
addition, they can vary by the port on the controller that
the device is plugged into. Hard drive IDs have the
format [controller prefix][port]. For example, a SATA drive
might have the ID wd0.
However, some controllers share a prefix with other
controllers (i.e. SATA and IDE share wd and SCSI and SAS
share sd). If you have drives connected to multiple
controllers, they'll be numbered in the order the drives
were detected. For example, if you have one drive plugged
into an IDE controller and two drives connected to a SATA
controller, they will be numbered in the wd0 to wd2.
However, there is no way to determine which drive(s) are on
a specific controller by just looking at them.
It is possible to determine which drive is which by looking
at the output of the dmesg command.
dmseg | grep ": <"
This command will search the dmesg output to find any
mentions of a hard drive name. The output should be a
list of drive names associated with the hard drive name
(usually mentioning the manufacturer and/or the product
number). These can usually be associated with the
particular drive you're searching for.
Hard drives can be divided (or partitioned in technical
terms) into partitions. NetBSD has a method of dividing
hard drives into partitions using "disklabels". These are
indicated with a letter after the hard drive ID. For
example, wd1a. The whole drive is represented by
[drive ID]d. For example, wd1d.
For our purposes in this tutorial, we'll presume that
you're setting up LVM on two SATA drives with IDs of wd1
and wd2.
***Configure Kernel***
First, you need to enable the LVM kernel module. This can
be accomplished by typing:
modload dm
***Disklabel***
Next, you need to clear the Master Boot Records of the
disk(s) you wish to use with LVM. You can do this for each
disk by typing:
dd if=/dev/zero of=/dev/[device name] bs=8k count=1
Be sure to replace "[device name]" with the character
device representing the entire disk (ending with "d" on
i386, amd64, and some other platforms and ending with c on
most other platforms).
Now, edit the disklabel for each disk.
disklabel -e [device name]
Using this program, create a whole-disk partition on d or
c (explained above). Be sure to remove any other disklabel
partitions. When you are finished, the last lines of the
file should look similar to this:
# size offset fstype [fsize bsize cpg]
d: 4194239 65 4.2BSD 0 0
***Physical Volumes***
After the disks have been properly set up, we can start
configuring LVM.
First, you'll need to create a physical volume on each disk.
Be sure to replace rwd1d with the name of the disk that
you're creating a volume on.
lvm pvcreate /dev/rwd1d
***Volume Group***
Next, you need to create a volume group from the physical
volume(s) that were created earlier. A physical volume is
basically a virtual disk.
Create a volume group with:
lvm vgcreate vg0 /dev/rwd1d
If you have additional disks that you created physical
volumes on in the last step, you can add them to the volume
group with:
lvm vgextend vg0 /dev/rwd2d
***Logical Volume***
Now, you need to create a logical volume from the volume
group. A logical volume is equivalent to a partition.
lvm lvcreate -L 3.5G -n lv1 vg0
This command will create a 3.5 gigabyte logical volume
named lv1 in our vg0 volume group.
Now, the volume group needs to be activated.
lvm vgchange -a y
***Filesystem***
The previous steps have created a partition on a virtual
disk. Before you can use that partition, you need to format
it.
newfs -O2 /dev/vg0/rlv1
This will create a new FFSv2 fileystem on lv0.
***Enabling Logical Volumes Automatically***
You can set NetBSD to enable logical volumes automatically
in two simple steps.
First, add this to the end of /etc/rc.conf:
lvm=yes
Then, add an entry in /etc/fstab for the logical volume.
/dev/vg0/lv1 [mountpoint] ffs rw 1 2
Be sure to replace [mountpoint] with the place you wish to
mount the logical volume.
**Resizing the Logical Volume**
If you run out of space in your logical volume, resizing is
a very simple process. However, you must still have enough
hard drive space to hold the logical volume.
***Extending the Logical Volume***
lvm lvextend -L+500G /dev/vg0/lv1
This command will add 500 gigabytes to the logical volume.
You will also need to resize the filesystem to fit the
larger partition. This can be accomplished with resize_ffs.
resize_ffs /dev/vg0/lv1
***Shrinking the Logical Volume***
WARNING: Be aware that some filesystems cannot currently
be shrunk, like UFS2. UFS2 was the filesystem that we set
up in the logical volume above.
You can also shrink the logical volume. Before doing this,
you must first shrink the filesystem contained in the
logical volume. If the filesystem is FFS, you can use the
following command to shrink the lv1 logical volume. However,
you are required to specify the size in disk sectors.
resize_ffs -s [sectors] /dev/vg0/lv1
Then, you can shrink the logical volume.
lvm lvreduce -L-500G /dev/vg0/lv1
This command will remove 500 gigabytes from the logical
volume.
**More Information**
More information can be found in
Chapter 17 of the NetBSD Guide,
which is dedicated to LVM.
**References**
NetBSD Guide, Chapter 17: NetBSD Logical Volume Manager (LVM) configuration
http://www.netbsd.org/docs/guide/en/chap-lvm.html