From WTFwiki
Jump to navigation Jump to search

Using vnconfig to add vnode-backed swap on NetBSD

If, when initially slicing the drive, you find you've allocated insufficent space for swap you can add more swap by using vnconfig(8) - a tool which allows you to create-pseudo disk devices from vnodes.


Add the line

 pseudo-device    vnd    4

to your kernel config. This will create 4 devices /dev/vndN which you can then attach vnodes to to act as disk devices. The GENERIC kernel should have this option already enabled.

Creating a disk image

Next, you need to make a disk image that you can use as your swap. We will use dd(1):

 dd if=/dev/zero of=swapfile1 bs=1024k count=256

This will make a 256 meg file that we can use as our new swap device. It may take some time to create.

Attaching the vnode to a vnd device

To attach the vnode to one of your /dev/vnd devices you need to use vnconfig:

 vnconfig vnd0 swapfile1

To check that things are all working right:

 vnconfig -l

That should list the available vnd devices and if/how they're being used.

Labeling the device

Now, we need to put a label on the device, we use disklabel(8) for this:

 disklabel -e -I /dev/vnd0

$EDITOR will open and allow us to edit the disklabel. Most of the information is irrelevant, all we need to do is change the fstype of a: from 4.2BSD to swap. Save and exit (you may get errors about Invalid signatures in MBR record, this seems to be OK).

Enabling the device for swap

We're almost done. All we need to do is use swapon(8) to start using the device for swap:

 swapon /dev/vnd0a

The 'a' corresponds to the slice of the disk we saw in the disklabel process. If you'd renamed it to be 'b:' (the traditional place for swap) it would be /dev/vnd0b.

Now, just check everything is okay:

 swapctl -l

You should see your original swap as well as the new swap on /dev/vnd0a:

 Device      512-blocks     Used    Avail Capacity  Priority
 /dev/wd0b       263088        0   263088     0%    0
 /dev/vnd0a      524288        0   524288     0%    0
 Total           787376        0   787376     0%

Proceed to do whatever eats all your RAM safe in the knowledge that you now have additional swap to cope.

Mounting swapfiles on bootup

In /etc/rc.local:

 vnconfig vnd0 /usr/swap/swapfile1
 swapon /dev/vnd0a