确保已经在虚拟中添加了 第二个虚拟硬盘, 接下来在这个新磁盘上构建所需的分区文件系统, 并对其进行挂载

查看虚拟机磁盘

启动虚拟机,输入sudo fdisk -l, 查看当前虚拟机磁盘的情况:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
laohoo@laohoo-virtual-machine:~/Downloads$ sudo fdisk -l
[sudo] password for laohoo:
Disk /dev/sda: 60 GiB, 64424509440 bytes, 125829120 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 380AC38B-BB33-4A84-B199-14F3B7506152

Device Start End Sectors Size Type
/dev/sda1 2048 4095 2048 1M BIOS boot
/dev/sda2 4096 1054719 1050624 513M EFI System
/dev/sda3 1054720 125827071 124772352 59.5G Linux filesystem


Disk /dev/sdb: 80 GiB, 85899345920 bytes, 167772160 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
laohoo@laohoo-virtual-machine:~/Downloads$

新磁盘分区

现在对第二个磁盘进行分区, 需要将其分成三个分区,分别为boot, swap, root。其中:

  • boot分区用于进行系统引导,且是sdb的第一个分区,分区大小为256MB。设置为第一个分区的原因是:用于系统引导的分区一定要是第一块磁盘的第一个分区。

sdb现在并不是第一个分区,到后面会将sdasdb互换)

  • swap分区用于进行内存交换,分区大小为一般为内存大小的2倍。该分区将用于进行内存交换(当运行内存不足时将会发生交换)

  • root分区为主分区,整个LFS系统将会构造在其中

输入命令对第二个磁盘(/dev/sdb)进行分区: sudo fdisk /dev/sdb

操作过程如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
laohoo@laohoo-virtual-machine:~$ sudo fdisk /dev/sdb
[sudo] password for laohoo:

Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x43d3de27.

Command (m for help): p
Disk /dev/sdb: 80 GiB, 85899345920 bytes, 167772160 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x43d3de27

Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-167772159, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-167772159, default 167772159): +256m

Created a new partition 1 of type 'Linux' and of size 256 MiB.

Command (m for help): p
Disk /dev/sdb: 80 GiB, 85899345920 bytes, 167772160 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x43d3de27

Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 526335 524288 256M 83 Linux

Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2):
First sector (526336-167772159, default 526336):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (526336-167772159, default 167772159): +4g

Created a new partition 2 of type 'Linux' and of size 4 GiB.

Command (m for help): p
Disk /dev/sdb: 80 GiB, 85899345920 bytes, 167772160 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x43d3de27

Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 526335 524288 256M 83 Linux
/dev/sdb2 526336 8914943 8388608 4G 83 Linux

Command (m for help): n
Partition type
p primary (2 primary, 0 extended, 2 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (3,4, default 3):
First sector (8914944-167772159, default 8914944):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (8914944-167772159, default 167772159):

Created a new partition 3 of type 'Linux' and of size 75.7 GiB.

Command (m for help): p
Disk /dev/sdb: 80 GiB, 85899345920 bytes, 167772160 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x43d3de27

Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 526335 524288 256M 83 Linux
/dev/sdb2 526336 8914943 8388608 4G 83 Linux
/dev/sdb3 8914944 167772159 158857216 75.7G 83 Linux

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

操作过程分段说明:

  1. 创建boot分区
  1. 创建 swap 分区
  1. 创建 root 分区

在分区上创建文件系统

完成分区之后, 需要将boot分区(/dev/sdb1)和root分区(/dev/sdb3)设置为ext4文件系统

对于swap分区(/dev/sdb2),我们需要将其设置为交换分区

  1. boot分区(/dev/sdb1) 设置为ext4 文件系统
1
sudo mkfs -v -t ext4 /dev/sdb1

操作过程如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
laohoo@laohoo-virtual-machine:~$ sudo mkfs -v -t ext4 /dev/sdb1
[sudo] password for laohoo:
mke2fs 1.46.5 (30-Dec-2021)
fs_types for mke2fs.conf resolution: 'ext4', 'small'
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 65536 blocks
3276 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=67108864
2 block groups
32768 blocks per group, 32768 fragments per group
32768 inodes per group
Filesystem UUID: b7750a3f-1ad6-4c4a-b288-c70348502c3c
Superblock backups stored on blocks:
32768

Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

2。 将 root 分区(/dev/sdb3)设置为 ext4 文件系统

1
sudo mkfs -v -T small -t ext4 /dev/sdb3

操作过程如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
laohoo@laohoo-virtual-machine:~$ sudo mkfs -v -T small -t ext4 /dev/sdb3
mke2fs 1.46.5 (30-Dec-2021)
fs_types for mke2fs.conf resolution: 'ext4', 'small'
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
19857408 inodes, 19857152 blocks
992857 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2168455168
606 block groups
32768 blocks per group, 32768 fragments per group
32768 inodes per group
Filesystem UUID: ffb2ba6a-a067-4a61-94fc-8e20787a412d
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424

Allocating group tables: done
Writing inode tables: done
Creating journal (131072 blocks): done
Writing superblocks and filesystem accounting information: done

  1. swap 分区(/dev/sdb2)设置为交换分区
1
sudo mkswap /dev/sdb2

操作过程如下:

1
2
3
4
laohoo@laohoo-virtual-machine:~$ sudo mkswap /dev/sdb2
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=1c107759-3120-4944-ac3f-2d686cb658d2

查看分区文件系统类型

创建完成后,我们可以先输入parted /dev/sdb, 再输入print list查看分区文件系统类型

设置$LFS环境变量

在以后的操作中, 为了方便访问相关文件夹,需要使用环境变量 LFS。该变量都被定义且设置为构建 LFS 使用的目录 /mnt/lfs , 使用以下命令设置环境变量:

1
export LFS=/mnt/lfs

设置该环境变量的好处是, 我们可以直接输入书中的命令, 例如 mkdir -v $LFS/toolsShell 在解析命令时会自动将 $LFS 替换成 /mnt/lfs

检查 LFS 的设置是否正确:

1
echo $LFS

操作过程如下:

1
2
3
laohoo@laohoo-virtual-machine:~$ export LFS=/mnt/lfs
laohoo@laohoo-virtual-machine:~$ echo $LFS
/mnt/lfs

无论何时,在离开并重新进入了工作环境,一定要确认 LFS 的设定值和您离开工作环境时相同

挂载新的分区

现在已经在分区上建立了文件系统,为了访问分区,需要把分区挂载到选定的挂载点上。这里将文件系统挂载到 LFS 环境变量指定的目录中。

输入以下命令以创建挂载点,并挂载 LFS 文件系统:

  1. 创建文件夹 /mnt/lfs, 并将root分区(/dev/sdb3)挂载到/mnt/lfs
1
2
sudo mkdir -pv $LFS
sudo mount -v -t ext4 /dev/sdb3 $LFS

操作过程如下:

1
2
3
4
5
laohoo@laohoo-virtual-machine:~$ sudo mkdir -pv $LFS
mkdir: created directory '/mnt/lfs'
laohoo@laohoo-virtual-machine:~$ sudo mount -v -t ext4 /dev/sdb3 $LFS
mount: /dev/sdb3 mounted on /mnt/lfs.

  1. 创建文件夹LFS/boot,并将boot分区(/dev/sdb1)挂载到LFS/boot
1
2
sudo mkdir -v $LFS/boot
sudo mount -v -t ext4 /dev/sdb1 $LFS/boot

操作过程如下:

1
2
3
4
5
laohoo@laohoo-virtual-machine:~$ sudo mkdir -v $LFS/boot
mkdir: created directory '/mnt/lfs/boot'
laohoo@laohoo-virtual-machine:~$ sudo mount -v -t ext4 /dev/sdb1 $LFS/boot
mount: /dev/sdb1 mounted on /mnt/lfs/boot.

  1. 挂载swap分区
1
sudo /sbin/swapon -v /dev/sdb2

操作过程如下:

1
2
3
4
5
laohoo@laohoo-virtual-machine:~$ sudo /sbin/swapon -v /dev/sdb2
swapon: /dev/sdb2: found signature [pagesize=4096, signature=swap]
swapon: /dev/sdb2: pagesize=4096, swapsize=4294967296, devsize=4294967296
swapon /dev/sdb2

  1. 使用 df -h 命令查看分区挂载情况
1
2
3
4
5
6
7
8
9
10
11
laohoo@laohoo-virtual-machine:~$ df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 792M 1.7M 790M 1% /run
/dev/sda3 59G 11G 46G 19% /
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
/dev/sda2 512M 5.3M 507M 2% /boot/efi
tmpfs 792M 84K 792M 1% /run/user/112
tmpfs 792M 80K 792M 1% /run/user/1000
/dev/sdb3 71G 28K 67G 1% /mnt/lfs
/dev/sdb1 224M 24K 206M 1% /mnt/lfs/boot

新磁盘的分区操作结束。建议使用 快照 对当前虚拟机进行备份。

参考资料:

  1. Linux From Scratch Version 11.2

  2. Linux From Scratch 版本 11.2-systemd-中文翻译版