寫在前面
本文將會詳細記錄LFS中,構建分區,構建文件系統和掛載分區的全過程
準備新硬盤
爲了更加符合“從零開始構建Linux”的要求,我在虛擬機中,新建了一個磁盤
我們將會在這個新磁盤上構建所需的分區和文件系統,並對其進行掛載
創建新磁盤後,我們啓動虛擬機,輸入sudo fdisk -l,查看當前虛擬機磁盤的情況
複製出來的信息如下所示:
1 alphainf@ubuntu:~$ sudo fdisk -l 2 [sudo] password for alphainf: 3 Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors 4 Units: sectors of 1 * 512 = 512 bytes 5 Sector size (logical/physical): 512 bytes / 512 bytes 6 I/O size (minimum/optimal): 512 bytes / 512 bytes 7 Disklabel type: dos 8 Disk identifier: 0x57e14c18 9 10 Device Boot Start End Sectors Size Id Type 11 /dev/sda1 * 2048 39942143 39940096 19G 83 Linux 12 /dev/sda2 39944190 41940991 1996802 975M 5 Extended 13 /dev/sda5 39944192 41940991 1996800 975M 82 Linux swap / Solaris 14 15 16 Disk /dev/sdb: 64 GiB, 68719476736 bytes, 134217728 sectors 17 Units: sectors of 1 * 512 = 512 bytes 18 Sector size (logical/physical): 512 bytes / 512 bytes 19 I/O size (minimum/optimal): 512 bytes / 512 bytes
如上所示,有sda和sdb兩個硬盤
其中sda所掛載的是當前系統,分了三個區,分別是Linux,Extended和Swap
sdb爲我們剛創建的新硬盤,尚未進行分區
分區
根據書中的要求,我們至少需要完成三個分區的構造
分別爲boot,根分區,交換分區
由於磁盤空間足夠,boot和根分區我們都將分配20G,交換分區分配8G
構建boot分區的過程如下,注意,我輸入的內容均在冒號的後面
比如Command (m for help): p中的p
alphainf@ubuntu:~$ sudo fdisk /dev/sdb Welcome to fdisk (util-linux 2.27.1). 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 0xd868f5e0. Command (m for help): p Disk /dev/sdb: 64 GiB, 68719476736 bytes, 134217728 sectors 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: 0xd868f5e0 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): 1 First sector (2048-134217727, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-134217727, default 134217727): +20G Created a new partition 1 of type 'Linux' and of size 20 GiB. Command (m for help): p Disk /dev/sdb: 64 GiB, 68719476736 bytes, 134217728 sectors 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: 0xd868f5e0 Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 41945087 41943040 20G 83 Linux
我們可以看到/dev/sdb1已經出現
我們可以通過同樣的方法,構造出/dev/sdb2,也是20GB,/sdb2將作爲根分區
接下來,我們構建/dev/sdb3,並將該分區的類型調整爲交換分區
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 (83888128-134217727, default 83888128): Last sector, +sectors or +size{K,M,G,T,P} (83888128-134217727, default 134217727): +8G Created a new partition 3 of type 'Linux' and of size 8 GiB. Command (m for help): t #注意這裏,用於調整分區類型 Partition number (1-3, default 3): Partition type (type L to list all types): 82 Changed type of partition 'Linux' to 'Linux swap / Solaris'. Command (m for help): p Disk /dev/sdb: 64 GiB, 68719476736 bytes, 134217728 sectors 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: 0xd868f5e0 Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 41945087 41943040 20G 83 Linux /dev/sdb2 41945088 83888127 41943040 20G 83 Linux /dev/sdb3 83888128 100665343 16777216 8G 82 Linux swap / Solaris
我們可以通過以下指令實現
在完成上述設置後,記得輸入w並回車,以保存對磁盤分區的修改
修改完成後將出山以下提示:
Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.
我們最後再輸入sudo fdisk -l 確認分區情況
alphainf@ubuntu:~$ sudo fdisk -l [sudo] password for alphainf: Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors 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: 0x57e14c18 Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 39942143 39940096 19G 83 Linux /dev/sda2 39944190 41940991 1996802 975M 5 Extended /dev/sda5 39944192 41940991 1996800 975M 82 Linux swap / Solaris Disk /dev/sdb: 64 GiB, 68719476736 bytes, 134217728 sectors 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: 0xd868f5e0 Device Boot Start End Sectors Size Id Type /dev/sdb1 2048 41945087 41943040 20G 83 Linux /dev/sdb2 41945088 83888127 41943040 20G 83 Linux /dev/sdb3 83888128 100665343 16777216 8G 82 Linux swap / Solaris
我們發現,sdb出現了分區,這是我們期望的狀態
在分區上創建文件系統
我們可以依次輸入下列指令,實現文件系統的創建
sudo mkfs -v -t ext4 /dev/sdb1 sudo mkfs -v -t ext4 /dev/sdb2 sudo mkswap /dev/sdb3
以下是輸入指令後的輸出信息(以根目錄的創建和交換分區創建的輸出爲例)
我們還可以打開文件夾,看到這兩個剛生成的磁盤
創建完成後,我們可以先輸入sudo parted /dev/sdb ,再輸入print list查看分區文件系統類型
設置$LFS環境變量
在接下來的配置中,爲了方便設置,我們將多次使用LFS變量
設置LFS的代碼如下:
export LFS=/mnt/lfs
我們可以使用echo $LFS進行確認
經確認,環境變量已正確設置
掛載分區
將分區/dev/sdb2掛載到/mnt/lfs中,代碼如下:
sudo mkdir -pv $LFS sudo mount -v -t ext4 /dev/sdb2 $LFS
設置交換分區代碼如下
sudo /sbin/swapon -v /dev/sdb3
以上構建LFS分區的準備工作已完成
記得進行快照的保存,命名爲STEP 2,並進行備註