MD(4)
πΎ MD(4) β memory disk, bestie edition β¨
okay so this is the md manual page from the Kernel Interfaces Manual (that's the section 4 girlies π«Ά) and honestly? she's iconic. sit down, grab a strawberry drink π, we're gonna talk about memory backed virtual disks and you're gonna understand it. no cap.
πΈ NAME πΈ
md β memory disk π
β¨ SYNOPSIS β¨
so like, if you wanna compile this driver straight into the kernel, you drop these lines into your kernel configuration file:
device md
OR β alternatively, bestie β if you'd rather load the driver as a module at boot time, you put this line in loader.conf(5):
geom_md_load="YES"
either way works, pick your vibe π¦
π DESCRIPTION π
the md driver provides support for four kinds of memory backed virtual disks. four. a whole squad. let's meet them π
mallocβ the backing store is allocated usingmalloc(9). only one malloc-bucket is used, which means that allmddevices withmallocbacking must share the malloc-per-bucket-quota. the exact size of this quota varies, in particular with the amount of RAM in the system. you can figure out the exact value withvmstat(8). it's giving "we all share one charger" energy ngl πpreloadβ a module loaded byloader(8)with type βmd_imageβ is used for backing store. for backwards compatibility the type βmfs_rootβ is also recognized. see the description of module loading directives inloader.conf(5)and note that the module name will either be an absolute path to the image file or the name of a file in themodule_path.
and listen β if the kernel is created with option MD_ROOT, the first preloaded image found will become the root file system. first one. no takebacks. π
vnodeβ a regular file is used as backing store. this allows for mounting ISO images without the tedious detour over actual physical media. literally the main character move, we love her πswapβ the backing store is allocated from buffer memory. pages get pushed out to the swap when the system is under memory pressure, otherwise they stay in the operating memory. usingswapbacking is generally preferable overmallocbacking. so like... chooseswap. it's the better one. periodt. π
for more information, please see mdconfig(8). π«Ά
π₯ EXAMPLES π₯
to create a kernel with a ramdisk or MD file system, your kernel config needs the following options:
options MD_ROOT # MD is a potential root device
options MD_ROOT_READONLY # disallow mounting root writeable
options MD_ROOT_SIZE=8192 # 8MB ram disk
makeoptions MFS_IMAGE=/h/foo/ARM-MD
options ROOTDEVNAME=\"ufs:md0\"
the image in /h/foo/ARM-MD will be loaded as the initial image each boot. to create the image to use, please follow the steps to create a file-backed disk found in the mdconfig(8) man page. other tools will also create these images, such as NanoBSD. slay β¨
π§ ARM KERNEL OPTIONS π§
on armv6 and armv7 architectures, an MD_ROOT image larger than approximately 55 MiB may require building a custom kernel using several tuning options related to kernel memory usage. okay so this is the part where you gotta pay attention bestie, no scrolling π
options LOCORE_MAP_MB=<num>β this configures how much memory is mapped for the kernel during the early initialization stages. the value must be at least as large as the kernel plus all preloaded modules, including the root image. there is no downside to setting this value too large, as long as it does not exceed the amount of physical memory. the default is 64 MiB.options NKPT2PG=<num>β this configures the number of kernel L2 page table pages to preallocate during kernel initialization. each L2 page can map 4 MiB of kernel space. the value must be large enough to map the kernel plus all preloaded modules, including the root image. the default value is 32, which is sufficient to map 128 MiB.options VM_KMEM_SIZE_SCALE=<num>β this configures the amount of kernel virtual address (KVA) space to dedicate to thekmem_arenamap. the scale value is the ratio of physical to virtual pages. the default value of 3 allocates a page of KVA for each 3 pages of physical ram in the system.
the kernel and modules, including the root image, also consume KVA. the combination of a large root image and the default scaling may preallocate so much KVA that there is not enough remaining address space to allocate kernel stacks, IO buffers, and other resources that are not part of kmem_arena.
overallocating kmem_arena space is likely to manifest as failure to launch userland processes with "cannot allocate kernel stack" messages. π
setting the scale value too high may result in kernel failure to allocate memory because kmem_arena is too small, and the failure may require significant runtime to manifest. empirically, a value of 5 works well for a 200 MiB root image on a system with 2 GiB of physical ram.
like... tune responsibly bestie, these numbers are not a vibe check you can just wing π
π SEE ALSO π
gpart(8), loader(8), mdconfig(8), mdmfs(8), newfs(8), vmstat(8)
(go read them, they're all bangers β)
π HISTORY π
the md driver first appeared in FreeBSD 4.0 as a cleaner replacement for the MFS functionality previously used in PicoBSD and in the FreeBSD installation process.
the md driver did a hostile takeover of the vn(4) driver in FreeBSD 5.0. not me casually dropping the most iconic line in kernel history ππ₯ she really said "this is mine now"
π AUTHORS π
the md driver was written by Poul-Henning Kamp <phk@FreeBSD.org>. absolute legend, we stan π«Ά
and that's the whole page bestie!! this one came from the Debian build, dated January 8, 2020, and it's the MD(4) page β go forth and make memory disks, you're gonna eat π
β¨
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms