Skip to content

Commit 5e7416f

Browse files
committed
docs(Arch Linux 安装): 删除 Btrfs,将 swap 替换为随机临时密码
1 parent 3e6ecca commit 5e7416f

File tree

1 file changed

+22
-147
lines changed

1 file changed

+22
-147
lines changed

src/content/posts/arch-installation.md

Lines changed: 22 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: 全盘加密安装 Arch Linux
3-
description: 使用 LUKS+LVM/btrfs
3+
description: 使用 LUKS+LVM
44
published: 2025-07-04
55
tags: [Arch Linux, 安全]
66
category: GNU/Linux
@@ -259,10 +259,8 @@ cryptsetup open /dev/sda2 cry0 6.74s user 0.09s system 123% cpu 5.556 total
259259
```
260260
输入密码,完成解密,解密后的分区映射在`/dev/mapper/cry0`
261261
#### 创建文件系统
262-
选择喜欢的文件系统格式,LVM 和 btrfs 都比较灵活,可以创建跨盘文件系统,且都支持写时复制
263-
##### 1. LVM + ext4
264-
其实 LVM 可以搭配任何格式,这里以 ext4 为例
265-
###### 创建 LVM 卷组
262+
我们使用 LVM+ext4 作为我们的文件系统,其实 LVM 可以搭配任何格式,这里只是以 ext4 为例
263+
##### 创建 LVM 卷组
266264
使用`pvcreate`在解密后的分区上创建物理卷
267265
```ansi
268266
root@archiso ~ # pvcreate /dev/mapper/cry0
@@ -309,7 +307,7 @@ sda 8:0 0 476.9G 0 disk
309307
└─vg0-home 253:5 0 395.9G 0 lvm
310308
root@archiso ~ #
311309
```
312-
###### 格式化逻辑卷并挂载
310+
##### 格式化逻辑卷并挂载
313311
接下来,分别格式化创建好的各个逻辑卷(即分区)
314312
```ansi
315313
root@archiso ~ # mkfs.ext4 /dev/vg0/root
@@ -339,17 +337,13 @@ Writing inode tables: done
339337
Creating journal (262144 blocks): done
340338
Writing superblocks and filesystem accounting information: done
341339
342-
root@archiso ~ # mkswap /dev/vg0/swap
343-
Setting up swapspace version 1, size = 16 GiB (17179865088 bytes)
344-
no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxx
345340
root@archiso ~ #
346341
```
347-
接下来按顺序挂载所有分区,并启用`swap`
342+
接下来按顺序挂载所有分区
348343
```ansi
349344
root@archiso ~ # mount /dev/vg0/root /mnt
350345
root@archiso ~ # mount --mkdir /dev/vg0/home /mnt/home
351346
root@archiso ~ # mount --mkdir /dev/sda1 /mnt/boot
352-
root@archiso ~ # swapon /dev/vg0/swap
353347
root@archiso ~ #
354348
```
355349
> [!WARNING]
@@ -363,127 +357,15 @@ sda 8:0 0 476.9G 0 disk
363357
├─sda1 8:1 0 1G 0 part /mnt/boot
364358
└─sda2 8:2 0 475.9G 0 part
365359
└─cry0 253:2 0 475.9G 0 crypt
366-
├─vg0-swap 253:3 0 16G 0 lvm [SWAP]
360+
├─vg0-swap 253:3 0 16G 0 lvm
367361
├─vg0-root 253:4 0 64G 0 lvm /mnt
368362
└─vg0-home 253:5 0 395.9G 0 lvm /mnt/home
369363
root@archiso ~ #
370364
```
371-
##### 2. btrfs
372-
将解密后的分区格式化为 btrfs 格式
373-
374-
输入`mkfs.btrfs /dev/mapper/cry0`进行格式化
375-
> [!WARNING]
376-
> 此处要格式化的是`/dev/mapper/cry0`而不是`/dev/sda2`,不要搞错了
377-
```ansi
378-
root@archiso ~ # mkfs.btrfs /dev/mapper/cry0
379-
btrfs-progs v6.15
380-
See https://btrfs.readthedocs.io for more information.
381-
382-
Performing full device TRIM /dev/mapper/cry0 (475.92GiB) ...
383-
NOTE: several default settings have changed in version 5.15, please make sure
384-
this does not affect your deployments:
385-
- DUP for metadata (-m dup)
386-
- enabled no-holes (-O no-holes)
387-
- enabled free-space-tree (-R free-space-tree)
388-
389-
Label: (null)
390-
UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx
391-
Node size: 16384
392-
Sector size: 4096 (CPU page size: 4096)
393-
Filesystem size: 475.92GiB
394-
Block group profiles:
395-
Data: single 8.00MiB
396-
Metadata: DUP 1.00GiB
397-
System: DUP 8.00MiB
398-
SSD detected: yes
399-
Zoned device: no
400-
Features: extref, skinny-metadata, no-holes, free-space-tree
401-
Checksum: crc32c
402-
Number of devices: 1
403-
Devices:
404-
ID SIZE PATH
405-
1 475.92GiB /dev/mapper/cry0
406-
407-
root@archiso ~ #
408-
```
409-
###### 创建 btrfs 子卷
410-
411-
首先把格式化完成的分区挂载到`/mnt`
412-
413-
使用命令`mount /dev/mapper/cry0 /mnt`挂载
414-
415-
然后使用`btrfs subvolume create`命令创建子卷,我们需要分别创建`@``@home``@var``@swap`四个子卷
416-
```ansi
417-
root@archiso ~ # btrfs subvolume create /mnt/@
418-
Create subvolume '/mnt/@'
419-
root@archiso ~ # btrfs subvolume create /mnt/@home
420-
Create subvolume '/mnt/@home'
421-
root@archiso ~ # btrfs subvolume create /mnt/@var
422-
Create subvolume '/mnt/@var'
423-
root@archiso ~ # btrfs subvolume create /mnt/@swap
424-
Create subvolume '/mnt/@swap'
425-
root@archiso ~ #
426-
```
427-
创建好后可以使用`btrfs subvolume list -t /mnt`列出所有子卷
428-
```ansi
429-
root@archiso ~ # btrfs subvolume list -t /mnt
430-
ID gen top level path
431-
-- --- --------- ----
432-
256 10 5 @
433-
257 10 5 @home
434-
258 10 5 @var
435-
259 10 5 @swap
436-
root@archiso ~ #
437-
```
438-
确认好后卸载掉分区
439-
```ansi
440-
root@archiso ~ # umount /mnt
441-
```
442-
###### 挂载所有分区
443-
接下来按照顺序挂载我们创建好的所有分区
444-
445-
从根分区开始
446-
> [!NOTE]
447-
> `swap`子卷挂载时不要启用压缩
448-
```ansi
449-
root@archiso ~ # mount -o compress=zstd,subvol=@ /dev/mapper/cry0 /mnt
450-
root@archiso ~ # mount --mkdir -o compress=zstd,subvol=@home /dev/mapper/cry0 /mnt/home
451-
root@archiso ~ # mount --mkdir -o compress=zstd,subvol=@var /dev/mapper/cry0 /mnt/var
452-
root@archiso ~ # mount --mkdir -o subvol=@swap /dev/mapper/cry0 /mnt/swap
453-
root@archiso ~ # mount --mkdir /dev/sda1 /mnt/boot
454-
```
455-
> [!WARNING]
456-
> 如果你要使用`systemd-boot`作为你的引导加载器,那么建议把`esp`分区挂载到`/mnt/efi`而非`/mnt/boot`
457-
458-
挂载好后的`lsblk`输出应该是这样的
459-
```ansi
460-
root@archiso ~ # lsblk
461-
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
462-
sda 8:0 0 476.9G 0 disk
463-
├─sda1 8:1 0 1G 0 part /mnt/boot
464-
└─sda2 8:2 0 475.9G 0 part
465-
└─cry0 253:2 0 475.9G 0 crypt /mnt/swap
466-
/mnt/var
467-
/mnt/home
468-
/mnt
469-
root@archiso ~ #
470-
```
471-
接下来创建`swapfile`并启用(推荐设置为物理内存的一到二倍)
472-
```ansi
473-
root@archiso ~ # btrfs filesystem mkswapfile --size 16g --uuid clear /mnt/swap/swapfile
474-
create swapfile /mnt/swap/swapfile size 16.00GiB (17179869184)
475-
root@archiso ~ # swapon /mnt/swap/swapfile
476-
root@archiso ~ #
477-
```
478-
`btrfs filesystem mkswapfile --size 16g --uuid clear /mnt/swap/swapfile`:在`/mnt/swap/`下创建大小为 16GB 的`swapfile`
479-
480-
`swapon /mnt/swap/swapfile`:启用`swapfile`
481-
482-
恭喜你,最耗时的一步分区到这里已经完成了
483365
### 开始安装系统
484366
首先切换镜像源
485367
```ansi
486-
[31mroot[0m@archiso [34m~[0m # echo "Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/\$repo/os/\$arch" > /etc/pacman.d/mirrorlist
368+
[31mroot[0m@archiso [34m~[0m # echo 'Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$arch' > /etc/pacman.d/mirrorlist
487369
```
488370
这行命令的意思是将镜像源网站`Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$arch"`写入`/etc/pacman.d/mirrorlist`内并替换原来的内容
489371

@@ -527,13 +409,11 @@ Created symlink '/etc/systemd/system/sysinit.target.wants/systemd-timesyncd.serv
527409

528410
接下来安装一些基本工具
529411
```ansi
530-
[root@archiso /]# pacman -S base-devel btrfs-progs lvm2 neovim networkmanager
412+
[root@archiso /]# pacman -S base-devel lvm2 neovim networkmanager
531413
```
532414
`base-devel`:一些基本的工具包(包括`sudo`
533415

534-
`btrfs-progs``btrfs`文件系统工具(选择 btrfs 文件系统需装)
535-
536-
`lvm2`:LVM 管理工具(选择 LVM 卷需装)
416+
`lvm2`:LVM 管理工具
537417

538418
`neovim`:文本编辑器(不会用`vim`可以替换成`nano`
539419

@@ -597,14 +477,21 @@ Shiori-archlinux
597477
::1 localhost
598478
127.0.1.1 Shiori-archlinux.localdomain Shiori-archlinux
599479
```
600-
`btrfs`)然后我们修改一下刚刚生成的`fstab`
480+
编辑`crypttab`
481+
```ansi
482+
[root@archiso /]# nvim /etc/crypttab
483+
```
484+
在里面写下一行
485+
```
486+
swap /dev/vg0/swap /dev/urandom swap,cipher=aes-xts-plain64,size=512,sector-size=4096
487+
```
488+
然后我们修改一下刚刚生成的`fstab`
601489
```ansi
602490
[root@archiso /]# nvim /etc/fstab
603491
```
604-
`btrfs`)找到`/swap`的行,把relatime改成noatime,并把`compress=zstd:3`删掉,注意删掉一个逗号
492+
新增一行,启用`swap`
605493
```
606-
# /dev/mapper/cry0
607-
UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX /swap btrfs rw,noatime,ssd,space_cache=v2,subvol=/@swap 0 0
494+
/dev/mapper/swap none swap defaults 0 0
608495
```
609496
`root`用户设置一个密码
610497
```ansi
@@ -624,11 +511,9 @@ passwd: password updated successfully
624511
```
625512
找到`HOOKS=`这一行,我们将钩子替换为`systemd`提供的
626513
```
627-
HOOKS=(base systemd autodetect microcode modconf kms keyboard sd-vconsole block sd-encrypt filesystems fsck)
514+
HOOKS=(base systemd autodetect microcode modconf kms keyboard sd-vconsole block sd-encrypt lvm2 filesystems fsck)
628515
```
629-
使用`systemd`替换了`udev`,使用`sd-vconsole`替换了`keymap``consolefont`,在`block``filesystems`之间加入`sd-encrypt`
630-
631-
(如果你使用 LVM,那么还要在`sd-encrypt`后添加`lvm2`
516+
使用`systemd`替换了`udev`,使用`sd-vconsole`替换了`keymap``consolefont`,在`block``filesystems`之间加入`sd-encrypt`,在`sd-encrypt`后添加`lvm2`
632517

633518
创建`/etc/vconsole.conf`
634519
```ansi
@@ -715,14 +600,9 @@ Random seed file /efi/loader/random-seed successfully written (32 bytes).
715600
```
716601
写入以下内容(注意把 UUID 换成你刚刚获取的)
717602

718-
(LVM)
719603
```
720604
rd.luks.name=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx=cry0 root=/dev/vg0/root
721605
```
722-
(btrfs)
723-
```
724-
rd.luks.name=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx=cry0 root=/dev/mapper/cry0 rootflags=subvol=@
725-
```
726606

727607
接下来我们更改内核配置文件让它在 EFI 分区内生成 UKI 镜像
728608
```ansi
@@ -781,14 +661,9 @@ default_uki="/efi/EFI/Linux/arch-linux-fallback.efi"
781661
```
782662
写入以下内容(注意把 UUID 换成你刚刚获取的)
783663

784-
(LVM)
785664
```
786665
"Arch Linux" "rd.luks.name=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx=cry0 root=/dev/vg0/root rw initrd=intel-ucode.img initrd=initramfs-linux.img"
787666
```
788-
(btrfs)
789-
```
790-
"Arch Linux" "rd.luks.name=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx=cry0 root=/dev/mapper/cry0 rootflags=subvol=@ rw initrd=intel-ucode.img initrd=initramfs-linux.img"
791-
```
792667
注意最后一个参数的`initrd=initramfs-linux.img`要改成你实际的`initramfs`镜像的名称,可以使用`ls /boot`查看
793668
### 创建新用户
794669
重启前我们先创建一个普通用户,以便我们后续不用一直使用`root`用户进行操作

0 commit comments

Comments
 (0)