Arch Linux 安装流程

First Post:

Last Update:

镜像准备

前往镜像站下载

1
https://mirrors.tuna.tsinghua.edu.cn/archlinux/iso

安装流程

基础安装

加载镜像

进入 VirtualBox 加载镜像

校准时间

1
2
3
4
5
6
7
8
9
10
11
# 列出时区
timedatectl list-timezones

# 设置时区
timedatectl set-timezone Asia/Hong_Kong

# 将系统时间与网络时间进行同步
timedatectl set-ntp true

# 检查服务状态
timedatectl status

切换镜像源

1
vim /etc/pacman.d/mirrorlist
1
2
3
4
5
# 中国科学技术大学开源镜像站
Server = https://mirrors.ustc.edu.cn/archlinux/$repo/os/$arch

# 清华大学开源软件镜像站
Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$arch

全新安装

1
lsblk # 显示当前分区情况

建立分区表

1
2
3
4
parted /dev/sdx # 执行 parted,进行磁盘类型变更
(parted) mktable # 输入 mktable,建立分区表
New disk label type? gpt # 输入 gpt,将磁盘类型转换为 GPT 类型。如磁盘有数据会警告,输入 Yes 即可
(parted) quit # 退出 parted 命令行交互

建立EFI分区

具体的磁盘设备各有差异,SATA名称为sdx,NVME硬盘有另外的名称nvmexn1

此处以 SATA 为例

  • EFI分区:300M
  • 剩余空间自行分配
  • Swap可选
1
cfdisk /dev/sdx # 对安装 archlinux 的磁盘分区
1
fdisk -l # 复查磁盘情况

格式化 EFI 分区

1
mkfs.fat -F32 /dev/sdx1

格式化 Swap 分区(若有)

1
mkswap /dev/sdxn

格式化 Btrfs 分区

将整一个分区格式化为 Btrfs 文件系统

1
mkfs.btrfs -fL Lingmo /dev/sdxn
> -L 选项后指定该分区的 LABLE,可以自定义,但不能使用特殊字符以及空格,且最好有意义

挂载 Btrfs 分区

1
2
# 将 Btrfs 分区挂载到 /mnt 下
mount -t btrfs -o compress=zstd /dev/sdxn /mnt
1
2
# 复查挂载情况
df -h

为后续创建子卷做准备

相关参数: - -t:选项后指定挂载分区文件系统类型 - -o:选项后添加挂载参数: - compress=zstd:开启透明压缩

创建 Btrfs 子卷

1
2
3
4
5
# 创建 / 目录子卷
btrfs subvolume create /mnt/@

# 创建 /home 目录子卷
btrfs subvolume create /mnt/@home
1
2
# 复查子卷情况
btrfs subvolume list -p /mnt

卸载 /mnt

1
umount /mnt

挂载各目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 挂载 / 目录
mount -t btrfs -o subvol=/@,compress=zstd /dev/sdxn /mnt

# 创建 /home 目录
mkdir /mnt/home

# 挂载 /home 目录
mount -t btrfs -o subvol=/@home,compress=zstd /dev/sdxn /mnt/home

# 创建 /boot 目录
mkdir -p /mnt/boot

# 挂载 /boot 目录
mount /dev/sdxn /mnt/boot

# 挂载交换分区(如有)
swapon /dev/sdxn

安装系统

  1. 使用 pacstrap 脚本安装基础包:

Pacstrap is designed to create a new system installation from scratch.

1
2
# 如果使用btrfs文件系统,额外安装一个btrfs-progs包
pacstrap /mnt base base-devel linux linux-firmware btrfs-progs

相关参数: - base-devel:在 AUR 包的安装过程中是必须用到的 - linux:内核软件包,这里建议先不要替换为其它内核

  1. 如果提示 GPG 证书错误,可能是因为使用的不是最新的镜像文件
1
2
# 通过更新 archlinux-keyring 解决此问题
pacman -S archlinux-keyring
  1. 安装其它必要的功能性软件:
1
pacstrap /mnt networkmanager vim sudo zsh zsh-completions

生成 fstab 文件

fstab 用来定义磁盘分区。它是 Linux 系统中重要的文件之一。

1
2
# 使用 genfstab 自动根据当前挂载情况生成并写入 fstab 文件:
genfstab -U /mnt > /mnt/etc/fstab
1
2
# 复查 /mnt/etc/fstab
cat /mnt/etc/fstab

如下:

1
2
3
4
5
6
7
8
# /dev/sdb2 LABEL=Lingmo
UUID=d34ad4d9-8c4f-4ffc-adf4-6d5985dd4da3 / btrfs rw,relatime,compress=zstd:3,space_cache=v2,subvolid=256,subvol=/@ 0 0

# /dev/sdb2 LABEL=Lingmo
UUID=d34ad4d9-8c4f-4ffc-adf4-6d5985dd4da3 /home btrfs rw,relatime,compress=zstd:3,space_cache=v2,subvolid=257,subvol=/@home 0 0

# /dev/sdb1
UUID=4CF3-8291 /boot vfat rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro 0 2

进入 chroot

1
arch-chroot /mnt

修改主机名

1
2
# 填入主机名
vim /etc/hostname
1
2
# 设置hosts
vim /etc/hosts
1
2
3
4
# 加入以下
127.0.0.1 localhost
::1 localhost
127.0.1.1 myarch.localdomain myarch

设置时区

1
2
# 在 /etc/localtime 下用 /usr 中合适的时区创建符号链接
ln -sf /usr/share/zoneinfo/Asia/Hong_Kong /etc/localtime

硬件时间设置

1
hwclock --systohc

设置 Locale

Locale 决定了软件使用的语言、书写习惯和字符集。

1
2
# 编辑 /etc/locale.gen,去掉 en_US.UTF-8 UTF-8 以及 zh_CN.UTF-8 UTF-8 行前的注释符号(#):
vim /etc/locale.gen
1
2
# 生成 locale
locale-gen
1
2
3
# 配置 locale.conf
# 不推荐在此设置任何中文 locale,可能会导致 tty 乱码。
echo 'LANG=en_US.UTF-8' > /etc/locale.conf

为 root 用户设置密码

1
passwd root

安装微码

1
2
pacman -S intel-ucode   # Intel
pacman -S amd-ucode # AMD

安装引导程序

  1. 安装相应的包
1
pacman -S grub efibootmgr os-prober

相关参数 - -S:选项后指定要通过 pacman 包管理器安装的包: - grub:启动引导器 - efibootmgr:efibootmgr 被 grub 脚本用来将启动项写入 NVRAM - os-prober:为了能够引导 win10,需要安装 os-prober 以检测到它

  1. 安装 GRUBEFI 分区
1
2
grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=ARCH 
# 可选参数:--removable

相关参数 - --efi-directory=/boot:将 grubx64.efi 安装到之前的指定位置(EFI 分区) - --bootloader-id=ARCH:取名为 ARCH - --removable:用于安装到移动介质

  1. 编辑 /etc/default/grub 文件
1
vim /etc/default/grub

进行如下修改:

  • 去掉 GRUB_CMDLINE_LINUX_DEFAULT 一行中最后的 quiet 参数
  • loglevel 的数值从 3 改成 5。这样是为了后续如果出现系统错误,方便排错
  • 加入 nowatchdog 参数,这可以显著提高开关机速度
  1. 生成 GRUB 所需的配置文件
1
grub-mkconfig -o /boot/grub/grub.cfg

完成安装

1
2
3
exit 
umount -R /mnt
reboot

设置开机启动项

1
2
# 启动 networkmanager 服务即可连接网络
systemctl enable --now NetworkManager
1
2
3
4
5
6
7
# 若为无线连接,则需要在启动 networkmanager 后使用 nmcli 连接网络:

# 显示附近的 Wi-Fi 网络
nmcli dev wifi list

# 连接指定的无线网络
nmcli dev wifi connect "SSID" password "PWD"

也可以使用 nmtui 来配置网络

1
nmtui

更新系统所有包

1
pacman -Syu

使用 neofetch 打印系统信息

1
2
pacman -S neofetch
neofetch

添加用户 & 进组

1
2
3
4
5
6
7
8
# -m: 建立 home 目录
useradd -m username

# 设置密码
passwd username

# -a: 在其之后可添加用户群组
gpasswd -a user groupname

安装 openssh 并启用

1
2
pacman -S openssh
systemctl enable --now sshd

后日谈

Why Btrfs ?

纵观 Btrfs 的历史,可以说 Btrfs 未来的发展是道阻且长的。也让我们感受到开源社区也并不是一根绳上的蚂蚱 —— 开源社区之间也有着各种各样的分歧。

但不管怎么说,Btrfs 的未来现在来看是光明的;我们也可以在 archlinux 上享受到 Btrfs 文件系统的特性带来的好处:

  1. 快照 archlinux 作为滚动发行版,若滚挂了可以使用 Btrfs 的快照特性快速回滚 若使用传统的 ext4 文件系统,我们可以使用 timeshiftRSYNC 模式进行增量备份。但是,一般来说 RSYNC 方式的快照大小略大于当前实际使用大小,也就是说实际上开启了 timeshiftRSYNC 模式快照相当于磁盘可用空间直接少了一半多。因为虽然 RSYNC 方式的快照是增量的,但历史最久远的快照依然是完整备份,随后才是增量的
  2. 透明压缩 可以大大减少磁盘的使用空间(压缩率大概在 10% 左右)