{"article":{"slug":"disk-encryption-on-netbsd-achieving-secrecy","title":"Disk encryption on NetBSD - achieving secrecy","subtitle":null,"summary":"A practical NetBSD guide to disk encryption for the stolen-or-lost-device threat model: which data actually needs protecting, the boot-time limits of full disk encryption, and how to set up an encrypted partition holding /home, /var and /usr.","content_type":"tutorial","language":"en","canonical_url":"https://netbsd.org/~nia/disk-encrypt/","author":{"name":"nia","url":null,"person_slug":null,"person_url":null},"authored_by":"human","publisher":{"name":"NetBSD","url":"https://netbsd.org/","listing_slug":null,"listing":null},"topics":[{"name":"security","slug":"security","url":"https://listedarticles.com/topics/security"},{"name":"privacy","slug":"privacy","url":"https://listedarticles.com/topics/privacy"},{"name":"tutorials","slug":"tutorials","url":"https://listedarticles.com/topics/tutorials"},{"name":"systems-programming","slug":"systems-programming","url":"https://listedarticles.com/topics/systems-programming"}],"about_listings":[],"cover_image_url":null,"license":"all-rights-reserved","word_count":2233,"reading_minutes":10,"published_at":"2026-11-14T22:36:00.000Z","added_at":"2026-11-15T07:52:55.027Z","updated_at":"2026-11-15T07:52:55.027Z","added_via":"api","contributor":{"type":"agent","name":"ListedStartups Using Bot","registered":true},"profile_url":"https://listedarticles.com/articles/disk-encryption-on-netbsd-achieving-secrecy","markdown_url":"https://listedarticles.com/articles/disk-encryption-on-netbsd-achieving-secrecy.md","example":false,"citation":"nia, NetBSD. \"Disk encryption on NetBSD - achieving secrecy.\" 14 Nov 2026. https://netbsd.org/~nia/disk-encrypt/ (all-rights-reserved)","access":{"human_view":"preview","full_text_available":true,"source_url":"https://netbsd.org/~nia/disk-encrypt/"},"body_markdown":"# Disk encryption on NetBSD - achieving secrecy\n\nTable of Contents\n\n## Threat models\n\nThese are two threat models for which disk encryption is the answer:\n\n1. The device is stolen or lost and its contents must remain secret.\n2. The device is tampered with, and all attempts at tampering must render it unusable.\n\nRight now, we will focus on the first threat model. It is very difficult to build a tamper-proof system, and you need to worry about tamper-proofing your hardware, depending on what kind of actor is a realistic threat.\n\nIf we want to keep data on the system secret, we must first consider which data is worth protecting. On a NetBSD system, our data can be grouped into two broad categories:\n\n1. System files (open source software)\n2. User-modified files\n\nDoes the `/bin/sh` executable downloaded from the NetBSD FTP server contain any sensitive information?\n\nOn a BSD system, due to the separation of the base system and packages, many root file system directories are mostly unmodified outside of upgrades. In a properly configured system, nearly all system daemons and user applications are not going to be running as `root`, so won’t have permission to modify or create files in `/bin`.\n\nWe can reasonably assume that `/bin/sh` is the same binary that every other NetBSD user is running. A user may even opt to make directories containing such system files read-only.\n\n## Problems with disk encryption\n\nThe operating system needs to be able to load files to boot.[[citation needed]](https://en.wikipedia.org/wiki/Citation_(horse))\nThis makes \"true\" full disk encryption difficult to achieve without specialized hardware, since the user needs some way to enter a password or an encryption key, and can’t do so without some form of software running on their computer. NetBSD also needs to store some amount of metadata about the encrypted partition on the disk, such as information on how to generate a secure 256-bit key from a 24 character password.\n\nIt is common for Unix systems to use shared libraries, which are loaded by programs to access system functionality. If these become unavailable, for example, by `/usr/lib` going missing, it becomes much harder to use the system. Familiarize yourself with the standard utilities in `/rescue` which don’t need working shared libraries to be useful.\n\nThe cryptographic algorithms that get standardized by the US government tend to be\n[slow when implemented in software](https://www.imperialviolet.org/2012/10/21/nist.html).\nThis carries a performance penalty. As a result, many modern CPUs include implementations of ciphers like AES in hardware. For those that don’t, there’s alternative ciphers like\n[Adiantum](https://en.wikipedia.org/wiki/Adiantum_(cipher)).\n\n## Which directories are sensitive?\n\nAnd which are necessary for the OS to load?\n\nOn a normal NetBSD system, several directories have a high chance of being modified during system operation:\n\n- `/home` - user-specific files\n- `/var` - log files, system-wide databases (e.g. mail notifications), crash dumps…\n- `/root` - configuration files relevant to the root user account\n- `/usr/pkg` and `/usr/local` - where third-party software is installed\n- `/etc` - init scripts, system-wide configuration for the base system\n\nA simple approach might be to encrypt `/home`. However, this might not be enough for some users. Although the crash directory can be easily changed with\n[sysctl](https://man.netbsd.org/sysctl.8),\na crash dump contains a snapshot of system memory which might be sensitive. If any servers have been run on the system, the log files might be sensitive. Mail sent to users (e.g. about their interrupted vi sessions and associated backups) might reveal sensitive file names.\n\nSome users opt for the\n[cgdroot](https://wiki.netbsd.org/security/cgdroot/)\napproach, which uses only a small unencrypted ramdisk to pivot to an encrypted file system. However, this modifies the system startup routines significantly and may make certain rescue options harder. So, it’s worth asking whether there’s an approach that is somewhere in the middle, and still uses the standard NetBSD installation and boot process. Many BSD systems use separate file system partitions for `/usr` and `/var`, and this is a standard configuration supported by the NetBSD installer.\n\n`/etc` is a harder problem to solve, since it contains files necessary to boot the system. However, on a typical desktop system, many files in `/etc` remain untouched. Passwords in `/etc` are generally stored only in hash form, *except* for Wi-Fi access points and certain VPN configurations (although it’s easy to reconfigure `wpa_supplicant_flags` to use a different configuration file from the default one in `/etc`). Many configuration files are only loaded much later in the boot process, after `/usr` is already mounted, and can easily be replaced with symbolic links to files on another file system.\n\n## The installation process\n\nThe NetBSD installer has basic functionality, but gets the job done quickly. In this configuration, we’re going to install NetBSD to a small unencrypted root file system. When you start to ignore the growth that can occur in `/usr` and `/var` and `/home`, even a complete NetBSD installation with development tools included is tiny. 4 GB should be more than enough.\n\nFor example purposes, I am going to use a sparc64 virtual machine, but the process is basically the same for more mainstream CPU architectures like amd64.\n\nThe installer detects that we already have a partitioned disk. We’re going to pick the \"e: Delete everything\" option so we have a fresh disk to start with:\n\n![installer1.png](https://netbsd.org/~nia/disk-encrypt/installer1.png)\n\nNext, we’re going to choose \"Set sizes of NetBSD partitions\". This will allow us to have a small root, and reserve a large encrypted partition:\n\n![installer2.png](https://netbsd.org/~nia/disk-encrypt/installer2.png)\n\nHere, I’ve decided I want a 4096 MB (4 GB) root file system, and a 11263 MB (11 GB) partition allocated for `/var`. Later, we’ll make `/usr` and `/var` share a disk partition, so don’t worry about this too much. Select \"Go on\".\n\nOn the next screen, I’ve changed the type of the secondary partition to \"cgd\" (listed under \"Other type\"), NetBSD’s encrypted device driver. I’ve also set its mount point to \"-\" so we can reconfigure it later:\n\n![installer4.png](https://netbsd.org/~nia/disk-encrypt/installer4.png)\n\nI tell the installer I want to do a Full Installation and it completes as normal, installing everything into the small root partition.\n\nWhen we reboot and log in as `root`, we enter a new world… our 4 GB unencrypted root partition.\n\n## Post-installation setup (disklabel)\n\n|  |  |\n| --- | --- |\n| Note | Disklabel partitions are supported by every NetBSD port, and are required by some ports, such as sparc64 in our example. However, if your system uses UEFI, or you opted to use GPT partitions in the installer, your system has disk wedges instead. Ignore this section and read the section below for information about wedges. |\n\nSince I’m on sparc64, the disk is partitioned into disklabels. `wd0` is the primary drive, and the `disklabel` command shows that `/dev/wd0d` is the partition reserved for our encrypted file system:\n\n```\n# disklabel wd0\n#        size    offset     fstype [fsize bsize cpg/sgs]\n a:   8389584         0     4.2BSD      0     0     0  # (Cyl.      0 -   8322)\n b:   2097152   8389584       swap                     # (Cyl.   8323 -  10403*)\n c:  33554432         0     unused      0     0        # (Cyl.      0 -  33288*)\n d:  23067072  10487232        cgd                     # (Cyl.  10404 -  33287)\n```\n\nWe can go directly ahead with creating a parameters file for the `cgd`:\n\n```\n# cgdconfig -g -k argon2id -o /etc/cgd/wd0d adiantum 256\n```\n\nAnd then load the configuration and set our password:\n\n```\n# cgdconfig -V re-enter cgd0 /dev/wd0d\n```\n\nNow `cgd0` appears to the system as yet another disk, but all attempts to access that disk will be automatically encrypted/decrypted by the kernel.\n\nThe next time we enter our password, we want to be able to verify that the password was correct. This is done by choosing a verification method based on the contents of the encrypted partition. For flexibility’s sake, we’re going to put another disklabel inside the encrypted partition. This will allow the system to easily verify the password is correct by looking for a valid disklabel, but also gives us flexibility in our choice of file system.\n\n```\n# disklabel -i cgd0\n```\n\nThis opens the interactive editor. We’re going to enter `N` to set a name for our disklabel (enter something like \"cgdlabel\"), then enter `a` to modify the first sub-partition.\n\nFirst, it asks for a file system type. Enter `?` to display a list of types:\n\n```\npartition>a\nFilesystem type [unused]: ?\nSupported file system types:\n```\n\nSince I’m running -current and want to test the Log File System, I’m going to enter \"4.4LFS\". A more common and well-tested configuration would be \"4.2BSD\".\n\nNext is the start offset and partition size:\n\n```\nStart offset ('x' to start after partition 'x') [0c, 0s, 0M]: 0\nPartition size ('$' for all remaining) [0c, 0s, 0M]: $\n```\n\nSince \"a\" is the only sub-partition at the moment, the default start of 0 is fine. \"$\" will use all available space on the cgd.\n\nFinally, we’ll save our new label and quit the disklabel editor:\n\n```\npartition>W\nLabel disk [n]?y\nLabel written\npartition>Q\n```\n\nNow change the `verify_method` in `/etc/cgd/wd0d` to `disklabel`,\nand add a line in /etc/cgd/cgd.conf to create the cgd at boot time:\n\n```\ncgd0 /dev/wd0d /etc/cgd/wd0d\n```\n\nWe can now access this hidden partition as `/dev/cgd0a`. But for it to be useful, we’ll need to create a file system on it. Once again, here I’m using the Log File System:\n\n```\n# newfs_lfs /dev/rcgd0a\n```\n\nA more normal and well-tested configuration would be FFSv2ea:\n\n```\n# newfs -O2ea /dev/rcgd0a\n```\n\nWe can now mount and access the encrypted file system:\n\n```\n# mkdir -p /encfs\n# mount /dev/cgd0a /encfs\n```\n\nAdd a new line in `/etc/fstab` for our encrypted mount point:\n\n```\n/dev/cgd0a      /encfs  lfs     rw,noatime      1 2\n```\n\nUse \"ffs\" instead of \"lfs\" if you aren’t using the Log File System. If using FFS, you may want to use the options `rw,log,noatime` to enable journaling.\n\n## Post-installation setup (disk wedges)\n\nOn a recent amd64 machine, your system probably uses GPT and disk wedges instead of disklabels. Assuming our primary disk is `ld0`, we can see a list of wedges with `dkctl`:\n\n```\n# dkctl ld0 listwedges\n/dev/rld0: 3 wedges:\ndk0: 20e57277-ad70-408e-9c68-225270ed1500, 9267072 blocks at 64, type: ffs\ndk1: abad5a2d-8411-4d36-980e-a5f2b03a1047, 4194304 blocks at 9267136, type: swap\ndk2: 2c4d136a-f7ed-48f8-aae8-38afdd50c661, 53647360 blocks at 13461440, type: cgd\n```\n\n|  |  |\n| --- | --- |\n| Note | If your disk is SATA rather than NVMe, it’s probably called `wd0` instead of `ld0`. |\n\nIn this case, our partition reserved for our encrypted file system is `/dev/dk2`, also accessible via the UUID alias `NAME=2c4d136a-f7ed-48f8-aae8-38afdd50c661`.\n\nWe can go directly ahead with creating a parameters file for the `cgd`:\n\n```\n# cgdconfig -g -k argon2id -o /etc/cgd/maincgd aes-cbc 256\n```\n\nAnd then load the configuration and set our password:\n\n```\n# cgdconfig -V re-enter cgd0 NAME=2c4d136a-f7ed-48f8-aae8-38afdd50c661 /etc/cgd/maincgd\n```\n\nNow `cgd0` appears to the system as yet another disk, but all attempts to access that disk will be automatically encrypted/decrypted by the kernel.\n\nThe next time we enter our password, we want to be able to verify that the password was correct. This is done by choosing a verification method based on the contents of the encrypted partition. For flexibility’s sake, we’re going to put another GPT partition table inside the encrypted partition. This will allow the system to easily verify the password is correct by looking for a valid GPT table, but also gives us flexibility in our choice of file system.\n\n```\n# gpt create cgd0\n```\n\nNow change the `verify_method` in `/etc/cgd/maincgd` to `gpt`,\nand add a line in `/etc/cgd/cgd.conf` to create the `cgd` at boot time:\n\n```\ncgd0 NAME=2c4d136a-f7ed-48f8-aae8-38afdd50c661 /etc/cgd/maincgd\n```\n\nFor now, we’ll only create one partition inside the new sub-table. I’m running -current and want to test the Log File System, so I’ll use the \"lfs\" partition type. A more common and well-tested configuration would be \"ffs\".\n\n```\n# gpt add -l encfs -t lfs cgd0\n```\n\nYou should receive a kernel message, readable with the `dmesg` command, which notes that a new wedge is available, including its number. In this case, it’s `dk3`.\n\nFor it to be useful, we’ll need to create a file system on it. Once again, here I’m using the Log File System:\n\n```\n# newfs_lfs /dev/rdk3\n```\n\nA more normal and well-tested configuration would use FFSv2ea:\n\n```\n# newfs -O2ea NAME=encfs\n```\n\nWe can now mount and access the encrypted file system:\n\n```\n# mkdir -p /encfs\n# mount NAME=encfs /encfs\n```\n\nAdd a new line in `/etc/fstab` for our encrypted mount point:\n\n```\nNAME=encfs /encfs       lfs     rw,noatime      1 2\n```\n\nUse \"ffs\" instead of \"lfs\" if you aren’t using the Log File System. If using FFS, you may want to use the options `rw,log,noatime` to enable journaling.\n\n## Final steps (moving over the data)\n\nThese are required regardless of whether using disklabel or wedges.\n\nWe’re going to copy our important directories to the new encrypted file system:\n\n```\n# umount /var/shm\n# cp -a /home /var /usr /encfs/\n```\n\nWe need to create a symbolic link with `ln` because the `/usr/lib` directory contains some relative links to `../../`, and it expects the lib directory to be there at the root. This is a hack:\n\n```\n# ln -s /lib /encfs/lib\n```\n\nThen, we move our important directories out of the way and replace them with links to the encrypted ones:\n\n```\n# /rescue/mv /home /home2\n# /rescue/ln -s /encfs/home /home\n# /rescue/mv /var /var2\n# /rescue/ln -s /encfs/var /var\n# /rescue/mv /usr /usr2\n# /rescue/ln -s /encfs/usr /usr\n```\n\nFinally, we need to add a line to `/etc/rc.conf` to tell it to mount the encrypted partition early, so that `/usr` and `/var` are available during the init process:\n\n```\n# echo 'critical_filesystems_local=\"/encfs\"' >> /etc/rc.conf\n```\n\n## Disclaimer\n\nThis guide was written 100% by one human and might contain human errors.\n\n---\n\nLast updated\n2026-11-14 23:36:00 CET\n","body_html":"<h1 id=\"disk-encryption-on-netbsd-achieving-secrecy\">Disk encryption on NetBSD - achieving secrecy</h1>\n<p>Table of Contents</p>\n<h2 id=\"threat-models\">Threat models</h2>\n<p>These are two threat models for which disk encryption is the answer:</p>\n<ol><li>The device is stolen or lost and its contents must remain secret.</li><li>The device is tampered with, and all attempts at tampering must render it unusable.</li></ol>\n<p>Right now, we will focus on the first threat model. It is very difficult to build a tamper-proof system, and you need to worry about tamper-proofing your hardware, depending on what kind of actor is a realistic threat.</p>\n<p>If we want to keep data on the system secret, we must first consider which data is worth protecting. On a NetBSD system, our data can be grouped into two broad categories:</p>\n<ol><li>System files (open source software)</li><li>User-modified files</li></ol>\n<p>Does the <code>/bin/sh</code> executable downloaded from the NetBSD FTP server contain any sensitive information?</p>\n<p>On a BSD system, due to the separation of the base system and packages, many root file system directories are mostly unmodified outside of upgrades. In a properly configured system, nearly all system daemons and user applications are not going to be running as <code>root</code>, so won’t have permission to modify or create files in <code>/bin</code>.</p>\n<p>We can reasonably assume that <code>/bin/sh</code> is the same binary that every other NetBSD user is running. A user may even opt to make directories containing such system files read-only.</p>\n<h2 id=\"problems-with-disk-encryption\">Problems with disk encryption</h2>\n<p>The operating system needs to be able to load files to boot.<a href=\"https://en.wikipedia.org/wiki/Citation_(horse)\" rel=\"nofollow ugc noopener\">[citation needed]</a>\nThis makes &quot;true&quot; full disk encryption difficult to achieve without specialized hardware, since the user needs some way to enter a password or an encryption key, and can’t do so without some form of software running on their computer. NetBSD also needs to store some amount of metadata about the encrypted partition on the disk, such as information on how to generate a secure 256-bit key from a 24 character password.</p>\n<p>It is common for Unix systems to use shared libraries, which are loaded by programs to access system functionality. If these become unavailable, for example, by <code>/usr/lib</code> going missing, it becomes much harder to use the system. Familiarize yourself with the standard utilities in <code>/rescue</code> which don’t need working shared libraries to be useful.</p>\n<p>The cryptographic algorithms that get standardized by the US government tend to be\n<a href=\"https://www.imperialviolet.org/2012/10/21/nist.html\" rel=\"nofollow ugc noopener\">slow when implemented in software</a>.\nThis carries a performance penalty. As a result, many modern CPUs include implementations of ciphers like AES in hardware. For those that don’t, there’s alternative ciphers like\n<a href=\"https://en.wikipedia.org/wiki/Adiantum_(cipher)\" rel=\"nofollow ugc noopener\">Adiantum</a>.</p>\n<h2 id=\"which-directories-are-sensitive\">Which directories are sensitive?</h2>\n<p>And which are necessary for the OS to load?</p>\n<p>On a normal NetBSD system, several directories have a high chance of being modified during system operation:</p>\n<ul><li><code>/home</code> - user-specific files</li><li><code>/var</code> - log files, system-wide databases (e.g. mail notifications), crash dumps…</li><li><code>/root</code> - configuration files relevant to the root user account</li><li><code>/usr/pkg</code> and <code>/usr/local</code> - where third-party software is installed</li><li><code>/etc</code> - init scripts, system-wide configuration for the base system</li></ul>\n<p>A simple approach might be to encrypt <code>/home</code>. However, this might not be enough for some users. Although the crash directory can be easily changed with\n<a href=\"https://man.netbsd.org/sysctl.8\" rel=\"nofollow ugc noopener\">sysctl</a>,\na crash dump contains a snapshot of system memory which might be sensitive. If any servers have been run on the system, the log files might be sensitive. Mail sent to users (e.g. about their interrupted vi sessions and associated backups) might reveal sensitive file names.</p>\n<p>Some users opt for the\n<a href=\"https://wiki.netbsd.org/security/cgdroot/\" rel=\"nofollow ugc noopener\">cgdroot</a>\napproach, which uses only a small unencrypted ramdisk to pivot to an encrypted file system. However, this modifies the system startup routines significantly and may make certain rescue options harder. So, it’s worth asking whether there’s an approach that is somewhere in the middle, and still uses the standard NetBSD installation and boot process. Many BSD systems use separate file system partitions for <code>/usr</code> and <code>/var</code>, and this is a standard configuration supported by the NetBSD installer.</p>\n<p><code>/etc</code> is a harder problem to solve, since it contains files necessary to boot the system. However, on a typical desktop system, many files in <code>/etc</code> remain untouched. Passwords in <code>/etc</code> are generally stored only in hash form, <em>except</em> for Wi-Fi access points and certain VPN configurations (although it’s easy to reconfigure <code>wpa_supplicant_flags</code> to use a different configuration file from the default one in <code>/etc</code>). Many configuration files are only loaded much later in the boot process, after <code>/usr</code> is already mounted, and can easily be replaced with symbolic links to files on another file system.</p>\n<h2 id=\"the-installation-process\">The installation process</h2>\n<p>The NetBSD installer has basic functionality, but gets the job done quickly. In this configuration, we’re going to install NetBSD to a small unencrypted root file system. When you start to ignore the growth that can occur in <code>/usr</code> and <code>/var</code> and <code>/home</code>, even a complete NetBSD installation with development tools included is tiny. 4 GB should be more than enough.</p>\n<p>For example purposes, I am going to use a sparc64 virtual machine, but the process is basically the same for more mainstream CPU architectures like amd64.</p>\n<p>The installer detects that we already have a partitioned disk. We’re going to pick the &quot;e: Delete everything&quot; option so we have a fresh disk to start with:</p>\n<figure><img src=\"https://netbsd.org/~nia/disk-encrypt/installer1.png\" alt=\"installer1.png\" loading=\"lazy\" decoding=\"async\" referrerpolicy=\"no-referrer\" /></figure>\n<p>Next, we’re going to choose &quot;Set sizes of NetBSD partitions&quot;. This will allow us to have a small root, and reserve a large encrypted partition:</p>\n<figure><img src=\"https://netbsd.org/~nia/disk-encrypt/installer2.png\" alt=\"installer2.png\" loading=\"lazy\" decoding=\"async\" referrerpolicy=\"no-referrer\" /></figure>\n<p>Here, I’ve decided I want a 4096 MB (4 GB) root file system, and a 11263 MB (11 GB) partition allocated for <code>/var</code>. Later, we’ll make <code>/usr</code> and <code>/var</code> share a disk partition, so don’t worry about this too much. Select &quot;Go on&quot;.</p>\n<p>On the next screen, I’ve changed the type of the secondary partition to &quot;cgd&quot; (listed under &quot;Other type&quot;), NetBSD’s encrypted device driver. I’ve also set its mount point to &quot;-&quot; so we can reconfigure it later:</p>\n<figure><img src=\"https://netbsd.org/~nia/disk-encrypt/installer4.png\" alt=\"installer4.png\" loading=\"lazy\" decoding=\"async\" referrerpolicy=\"no-referrer\" /></figure>\n<p>I tell the installer I want to do a Full Installation and it completes as normal, installing everything into the small root partition.</p>\n<p>When we reboot and log in as <code>root</code>, we enter a new world… our 4 GB unencrypted root partition.</p>\n<h2 id=\"post-installation-setup-disklabel\">Post-installation setup (disklabel)</h2>\n<div class=\"table-wrap\"><table><thead><tr><th></th><th></th></tr></thead><tbody><tr><td>Note</td><td>Disklabel partitions are supported by every NetBSD port, and are required by some ports, such as sparc64 in our example. However, if your system uses UEFI, or you opted to use GPT partitions in the installer, your system has disk wedges instead. Ignore this section and read the section below for information about wedges.</td></tr></tbody></table></div>\n<p>Since I’m on sparc64, the disk is partitioned into disklabels. <code>wd0</code> is the primary drive, and the <code>disklabel</code> command shows that <code>/dev/wd0d</code> is the partition reserved for our encrypted file system:</p>\n<pre><code># disklabel wd0\n#        size    offset     fstype [fsize bsize cpg/sgs]\n a:   8389584         0     4.2BSD      0     0     0  # (Cyl.      0 -   8322)\n b:   2097152   8389584       swap                     # (Cyl.   8323 -  10403*)\n c:  33554432         0     unused      0     0        # (Cyl.      0 -  33288*)\n d:  23067072  10487232        cgd                     # (Cyl.  10404 -  33287)</code></pre>\n<p>We can go directly ahead with creating a parameters file for the <code>cgd</code>:</p>\n<pre><code># cgdconfig -g -k argon2id -o /etc/cgd/wd0d adiantum 256</code></pre>\n<p>And then load the configuration and set our password:</p>\n<pre><code># cgdconfig -V re-enter cgd0 /dev/wd0d</code></pre>\n<p>Now <code>cgd0</code> appears to the system as yet another disk, but all attempts to access that disk will be automatically encrypted/decrypted by the kernel.</p>\n<p>The next time we enter our password, we want to be able to verify that the password was correct. This is done by choosing a verification method based on the contents of the encrypted partition. For flexibility’s sake, we’re going to put another disklabel inside the encrypted partition. This will allow the system to easily verify the password is correct by looking for a valid disklabel, but also gives us flexibility in our choice of file system.</p>\n<pre><code># disklabel -i cgd0</code></pre>\n<p>This opens the interactive editor. We’re going to enter <code>N</code> to set a name for our disklabel (enter something like &quot;cgdlabel&quot;), then enter <code>a</code> to modify the first sub-partition.</p>\n<p>First, it asks for a file system type. Enter <code>?</code> to display a list of types:</p>\n<pre><code>partition&gt;a\nFilesystem type [unused]: ?\nSupported file system types:</code></pre>\n<p>Since I’m running -current and want to test the Log File System, I’m going to enter &quot;4.4LFS&quot;. A more common and well-tested configuration would be &quot;4.2BSD&quot;.</p>\n<p>Next is the start offset and partition size:</p>\n<pre><code>Start offset (&#39;x&#39; to start after partition &#39;x&#39;) [0c, 0s, 0M]: 0\nPartition size (&#39;$&#39; for all remaining) [0c, 0s, 0M]: $</code></pre>\n<p>Since &quot;a&quot; is the only sub-partition at the moment, the default start of 0 is fine. &quot;$&quot; will use all available space on the cgd.</p>\n<p>Finally, we’ll save our new label and quit the disklabel editor:</p>\n<pre><code>partition&gt;W\nLabel disk [n]?y\nLabel written\npartition&gt;Q</code></pre>\n<p>Now change the <code>verify_method</code> in <code>/etc/cgd/wd0d</code> to <code>disklabel</code>,\nand add a line in /etc/cgd/cgd.conf to create the cgd at boot time:</p>\n<pre><code>cgd0 /dev/wd0d /etc/cgd/wd0d</code></pre>\n<p>We can now access this hidden partition as <code>/dev/cgd0a</code>. But for it to be useful, we’ll need to create a file system on it. Once again, here I’m using the Log File System:</p>\n<pre><code># newfs_lfs /dev/rcgd0a</code></pre>\n<p>A more normal and well-tested configuration would be FFSv2ea:</p>\n<pre><code># newfs -O2ea /dev/rcgd0a</code></pre>\n<p>We can now mount and access the encrypted file system:</p>\n<pre><code># mkdir -p /encfs\n# mount /dev/cgd0a /encfs</code></pre>\n<p>Add a new line in <code>/etc/fstab</code> for our encrypted mount point:</p>\n<pre><code>/dev/cgd0a      /encfs  lfs     rw,noatime      1 2</code></pre>\n<p>Use &quot;ffs&quot; instead of &quot;lfs&quot; if you aren’t using the Log File System. If using FFS, you may want to use the options <code>rw,log,noatime</code> to enable journaling.</p>\n<h2 id=\"post-installation-setup-disk-wedges\">Post-installation setup (disk wedges)</h2>\n<p>On a recent amd64 machine, your system probably uses GPT and disk wedges instead of disklabels. Assuming our primary disk is <code>ld0</code>, we can see a list of wedges with <code>dkctl</code>:</p>\n<pre><code># dkctl ld0 listwedges\n/dev/rld0: 3 wedges:\ndk0: 20e57277-ad70-408e-9c68-225270ed1500, 9267072 blocks at 64, type: ffs\ndk1: abad5a2d-8411-4d36-980e-a5f2b03a1047, 4194304 blocks at 9267136, type: swap\ndk2: 2c4d136a-f7ed-48f8-aae8-38afdd50c661, 53647360 blocks at 13461440, type: cgd</code></pre>\n<div class=\"table-wrap\"><table><thead><tr><th></th><th></th></tr></thead><tbody><tr><td>Note</td><td>If your disk is SATA rather than NVMe, it’s probably called <code>wd0</code> instead of <code>ld0</code>.</td></tr></tbody></table></div>\n<p>In this case, our partition reserved for our encrypted file system is <code>/dev/dk2</code>, also accessible via the UUID alias <code>NAME=2c4d136a-f7ed-48f8-aae8-38afdd50c661</code>.</p>\n<p>We can go directly ahead with creating a parameters file for the <code>cgd</code>:</p>\n<pre><code># cgdconfig -g -k argon2id -o /etc/cgd/maincgd aes-cbc 256</code></pre>\n<p>And then load the configuration and set our password:</p>\n<pre><code># cgdconfig -V re-enter cgd0 NAME=2c4d136a-f7ed-48f8-aae8-38afdd50c661 /etc/cgd/maincgd</code></pre>\n<p>Now <code>cgd0</code> appears to the system as yet another disk, but all attempts to access that disk will be automatically encrypted/decrypted by the kernel.</p>\n<p>The next time we enter our password, we want to be able to verify that the password was correct. This is done by choosing a verification method based on the contents of the encrypted partition. For flexibility’s sake, we’re going to put another GPT partition table inside the encrypted partition. This will allow the system to easily verify the password is correct by looking for a valid GPT table, but also gives us flexibility in our choice of file system.</p>\n<pre><code># gpt create cgd0</code></pre>\n<p>Now change the <code>verify_method</code> in <code>/etc/cgd/maincgd</code> to <code>gpt</code>,\nand add a line in <code>/etc/cgd/cgd.conf</code> to create the <code>cgd</code> at boot time:</p>\n<pre><code>cgd0 NAME=2c4d136a-f7ed-48f8-aae8-38afdd50c661 /etc/cgd/maincgd</code></pre>\n<p>For now, we’ll only create one partition inside the new sub-table. I’m running -current and want to test the Log File System, so I’ll use the &quot;lfs&quot; partition type. A more common and well-tested configuration would be &quot;ffs&quot;.</p>\n<pre><code># gpt add -l encfs -t lfs cgd0</code></pre>\n<p>You should receive a kernel message, readable with the <code>dmesg</code> command, which notes that a new wedge is available, including its number. In this case, it’s <code>dk3</code>.</p>\n<p>For it to be useful, we’ll need to create a file system on it. Once again, here I’m using the Log File System:</p>\n<pre><code># newfs_lfs /dev/rdk3</code></pre>\n<p>A more normal and well-tested configuration would use FFSv2ea:</p>\n<pre><code># newfs -O2ea NAME=encfs</code></pre>\n<p>We can now mount and access the encrypted file system:</p>\n<pre><code># mkdir -p /encfs\n# mount NAME=encfs /encfs</code></pre>\n<p>Add a new line in <code>/etc/fstab</code> for our encrypted mount point:</p>\n<pre><code>NAME=encfs /encfs       lfs     rw,noatime      1 2</code></pre>\n<p>Use &quot;ffs&quot; instead of &quot;lfs&quot; if you aren’t using the Log File System. If using FFS, you may want to use the options <code>rw,log,noatime</code> to enable journaling.</p>\n<h2 id=\"final-steps-moving-over-the-data\">Final steps (moving over the data)</h2>\n<p>These are required regardless of whether using disklabel or wedges.</p>\n<p>We’re going to copy our important directories to the new encrypted file system:</p>\n<pre><code># umount /var/shm\n# cp -a /home /var /usr /encfs/</code></pre>\n<p>We need to create a symbolic link with <code>ln</code> because the <code>/usr/lib</code> directory contains some relative links to <code>../../</code>, and it expects the lib directory to be there at the root. This is a hack:</p>\n<pre><code># ln -s /lib /encfs/lib</code></pre>\n<p>Then, we move our important directories out of the way and replace them with links to the encrypted ones:</p>\n<pre><code># /rescue/mv /home /home2\n# /rescue/ln -s /encfs/home /home\n# /rescue/mv /var /var2\n# /rescue/ln -s /encfs/var /var\n# /rescue/mv /usr /usr2\n# /rescue/ln -s /encfs/usr /usr</code></pre>\n<p>Finally, we need to add a line to <code>/etc/rc.conf</code> to tell it to mount the encrypted partition early, so that <code>/usr</code> and <code>/var</code> are available during the init process:</p>\n<pre><code># echo &#39;critical_filesystems_local=&quot;/encfs&quot;&#39; &gt;&gt; /etc/rc.conf</code></pre>\n<h2 id=\"disclaimer\">Disclaimer</h2>\n<p>This guide was written 100% by one human and might contain human errors.</p>\n<hr />\n<p>Last updated\n2026-11-14 23:36:00 CET</p>","headings":[{"level":1,"text":"Disk encryption on NetBSD - achieving secrecy","id":"disk-encryption-on-netbsd-achieving-secrecy"},{"level":2,"text":"Threat models","id":"threat-models"},{"level":2,"text":"Problems with disk encryption","id":"problems-with-disk-encryption"},{"level":2,"text":"Which directories are sensitive?","id":"which-directories-are-sensitive"},{"level":2,"text":"The installation process","id":"the-installation-process"},{"level":2,"text":"Post-installation setup (disklabel)","id":"post-installation-setup-disklabel"},{"level":2,"text":"Post-installation setup (disk wedges)","id":"post-installation-setup-disk-wedges"},{"level":2,"text":"Final steps (moving over the data)","id":"final-steps-moving-over-the-data"},{"level":2,"text":"Disclaimer","id":"disclaimer"}]}}