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 using malloc(9). only one malloc-bucket is used, which means that all md devices with malloc backing 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 with vmstat(8). it's giving "we all share one charger" energy ngl 😭
  • preload β€” a module loaded by loader(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 in loader.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 the module_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. using swap backing is generally preferable over malloc backing. so like... choose swap. 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 the kmem_arena map. 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