NetBSD 11 from scratch

NetBSD 11 from scratch

In this post I will cover the installation process of the NetBSD operating system, including disk level encryption. Instead of using the standard installation program (sysinst), I decided to install NetBSD “from scratch”, as a way to dive into the internals, and learn more about the nuts and bolts of this great operating system. 在这篇文章中,我将介绍 NetBSD 操作系统的安装过程,包括磁盘级加密。我决定不使用标准的安装程序 (sysinst),而是选择“从零开始”安装 NetBSD,以此深入了解这个伟大操作系统的内部构造和核心机制。

This post is by no means a replacement for the NetBSD official guide. Sysinst is the the facto installer for NetBSD and is the most documented, straightforward way to install the operating system. This post is about having fun and understanding and learning the internals of the operating system. It also offers the highest flexibility to customize and overcome issues that may arise during a conventional installation. 本文绝非 NetBSD 官方指南的替代品。Sysinst 是 NetBSD 的事实标准安装程序,也是安装该系统最直观、文档最齐全的方式。本文旨在通过实践获得乐趣,并深入理解和学习操作系统的内部原理。这种方式也提供了最高的灵活性,以便进行自定义并解决传统安装过程中可能出现的问题。

I will try to establish a chronological order in the process, documenting the most relevant steps with screenshots and references to sources related to the topic. 我将尝试按时间顺序梳理整个过程,并通过截图和相关主题的参考资料记录最关键的步骤。

MBR installation

MBR 安装

NetBSD is notorious for its portability and being able to work in “old” computers. I decided to do the installation in a HP Pavillion (around year 2012) that uses MBR instead of the GPT partitioning found in more modern UEFI systems. (quick digression: Many of the so called “obsolete” or “old” computers are still useful and perform very well. You just need a good operating system ;). By adopting them, you will giving refurbished computers a great second life, you will be saving a lot of money and, most importantly, you will be preserving the environment from polluting waste.). NetBSD 以其可移植性著称,能够在“老旧”计算机上运行。我决定在一台 HP Pavilion(约 2012 年产)上进行安装,该设备使用 MBR 分区,而非现代 UEFI 系统中常见的 GPT 分区。(题外话:许多所谓的“过时”或“老旧”计算机依然非常有用且性能良好。你只需要一个优秀的操作系统 ;)。通过重新利用它们,你可以让翻新的计算机焕发新生,节省大量资金,最重要的是,你还能减少电子垃圾对环境的污染。)

Let’s start 🙂

开始吧 🙂

The installation image

安装镜像

The first step to install NetBSD is to download and copy the image to the USB pen drive. Note: <usb_device> is the assigned device of your USB drive. Use the entire disk (ej sdb) instead of a partition. Please also note that this operation will wipe out your entire flash drive. Triple check that you are actually using the right device and not another disk of your computer. 安装 NetBSD 的第一步是下载镜像并将其复制到 USB 闪存盘中。注意:<usb_device> 是分配给你的 USB 驱动器的设备名称。请使用整个磁盘(例如 sdb),而不是某个分区。另请注意,此操作将清除闪存盘中的所有数据。请务必反复确认你使用的是正确的设备,而不是计算机上的其他磁盘。

Note: I will use the # as the shell prompt to denote root operations while the $ is for regular users. 注意:我将使用 # 作为 shell 提示符来表示 root 操作,而 $ 则用于普通用户。

$ wget https://cdn.netbsd.org/pub/NetBSD/NetBSD-11.0/images/NetBSD-11.0-amd64-install.img.gz
$ gunzip NetBSD-11.0-amd64-install.img.gz
$ sudo dd if=NetBSD-11.0-amd64-install.img of=/dev/<usb_device> bs=2m

Once it finishes, you can plug it in on your USB socket and boot the computer. Make sure you select the device as the first bootable device in your BIOS. 完成后,你可以将其插入 USB 插槽并启动计算机。请确保在 BIOS 中将该设备选为第一启动项。

Preparing the Installation

准备安装

Exit “sysinst” 退出 “sysinst”

When you boot from the usb flash drive, you will get at some point the sysinst installation program. Press CTRL+C to exit from sysinst and get a prompt. You will see the following message followed by the root prompt. 当你从 USB 闪存盘启动时,系统最终会进入 sysinst 安装程序。按下 CTRL+C 退出 sysinst 并进入命令行提示符。你将看到以下消息,随后是 root 提示符。

Sysinst terminated. To return to the installer, quit this shell by typing 'exit' of ^D #

Setup the keyboard layout 设置键盘布局

If you have a non English keyboard, you might want to set the keyboard layout with the following command. For instance, to enable the Spanish keyboard layout, type the following command: 如果你使用的不是英语键盘,可能需要通过以下命令设置键盘布局。例如,要启用西班牙语键盘布局,请输入以下命令:

# wsconsctl -w encoding=es
encoding -> es

Note: The NetBSD console only admit ASCII characters, so letters like “Ñ” won’t be shown, but the layout will be correct. 注意:NetBSD 控制台仅支持 ASCII 字符,因此像“Ñ”这样的字母无法显示,但键盘布局将是正确的。

Setup networking 设置网络

To make the installation process more comfortable, I decided to enable networking and the SSH server in the target computer, so I can also take screenshots and document it better. I will be using a LAN, so connect your ethernet cable to the router and enable DHCP. The following command will retrieve the IP and default gateway from your router DHCP server. 为了让安装过程更舒适,我决定在目标计算机上启用网络和 SSH 服务器,这样我也可以更好地截图并记录文档。我将使用局域网,因此请将以太网线连接到路由器并启用 DHCP。以下命令将从路由器的 DHCP 服务器获取 IP 和默认网关。

# /sbin/dhcpcd -d -n re0

“re0” is the network interface driver. NetBSD uses the driver name instead of the GNU/Linux “eth” nomenclature. In my case, “re0” stands for Realtek ethernet driver. You can check the name of the interface and its status using the ifconfig command. “re0” 是网络接口驱动程序。NetBSD 使用驱动程序名称,而不是 GNU/Linux 的 “eth” 命名法。在我的例子中,“re0” 代表 Realtek 以太网驱动程序。你可以使用 ifconfig 命令查看接口名称及其状态。

Enable SSH server 启用 SSH 服务器

Add a local user. It’s a good practice to create a local user for non-privileged operations, as well as to remotely login without using “root”. We will create the user “malatesta” and make him a member of the “wheel” group, so he can do a “su -” operation and assign a password. Note: This user only lives in the installation usb drive. 添加一个本地用户。创建一个本地用户用于非特权操作,以及在不使用 “root” 的情况下远程登录,是一种良好的实践。我们将创建用户 “malatesta” 并将其设为 “wheel” 组的成员,以便他可以执行 “su -” 操作,并为其分配密码。注意:此用户仅存在于安装 USB 驱动器中。

# useradd -m malatesta
# usermod -G wheel malatesta
# passwd malatesta

Next we need to start the SSH server, so the live system will allow remote connections and we can resume the installation from another computer. 接下来我们需要启动 SSH 服务器,以便实时系统允许远程连接,这样我们就可以从另一台计算机继续安装。

# service sshd onestart

Setting up disks and partitions

设置磁盘和分区

Now we are ready to start configuring important, non-volatile resources, starting with the target disk that will hold the operating system. 现在我们准备开始配置重要的非易失性资源,首先从将承载操作系统的目标磁盘开始。

About Disks, partitions and BSD disklabels 关于磁盘、分区和 BSD 磁盘标签 (disklabels)

Disks 磁盘

In NetBSD, and similar to the network interfaces, physical disks are represented by the driver interface, followed by an integer. The logical and raw devices are under the “/dev” directory. The following are some of the drivers: 在 NetBSD 中,与网络接口类似,物理磁盘由驱动程序接口表示,后跟一个整数。逻辑设备和原始设备位于 “/dev” 目录下。以下是一些驱动程序:

  • wd: IDE drives (atabus). Regular hard drives

  • sd: SCSI drives (scsibus). This includes USB pendrives.

  • cd: CDROM drives

  • wd: IDE 驱动器 (atabus)。普通硬盘

  • sd: SCSI 驱动器 (scsibus)。包括 USB 闪存盘。

  • cd: CDROM 驱动器

To list the current disks in your computer, use systcl hw.disknames command: 要列出计算机中当前的磁盘,请使用 sysctl hw.disknames 命令:

# sysctl hw.disknames
hw.disknames = wd0 cd0 sd0 dk0 dk1

In this example, wd0 is the IDE drive (where we’ll be installing NetBSD), cd0 is the CDROM, sd0 the USB pendrive and finally dk0 and dk1 are “wedges” (GPT partitions) belonging to sd0. Now we know -from the naming conventions- that wd0 is our internal hard drive, and that is where we will install NetBSD. 在此示例中,wd0 是 IDE 驱动器(我们将在此安装 NetBSD),cd0 是 CDROM,sd0 是 USB 闪存盘,最后 dk0 和 dk1 是属于 sd0 的 “wedges”(GPT 分区)。现在我们通过命名约定知道 wd0 是我们的内置硬盘,也就是我们将安装 NetBSD 的地方。

Partitions 分区

As previously mentioned, we will use a non UEFI, BIOS computer, so we’ll be installing NetBSD using the MBR (Master Boot Record) partitioning schema. 如前所述,我们将使用非 UEFI 的 BIOS 计算机,因此我们将使用 MBR(主引导记录)分区方案来安装 NetBSD。