linux cloud安装_在Google Cloud虚拟机上从头开始构建Linux

linux cloud安装

In Part 1 of this series, we will set up a build environment to build Linux From Scratch.

在本系列的第1部分中,我们将建立一个构建环境来构建Linux From Scratch。

Links to Part 2, Part 3

链接到第2 部分 , 第3部分

I decided to build Linux from scratch as a learning project to gain a better understanding of Linux internals. I spend a lot of time developing in the cloud, and cloud technology seems to have Linux at its heart, so it seemed worthwhile to give this a shot. Besides, I have major graybeard yearnings.

我决定从头开始构建Linux作为一个学习项目,以更好地了解Linux内部。 我花了大量时间在云中进行开发,而云技术似乎以Linux为核心,因此值得一试。 此外,我有主要的白胡子向往。

Note: we will be following LFS Version 9.1-systemd. I will use “LFS x.x.x…” when referring to specific parts. Many of the commands are copied verbatim.

注意:我们将遵循 LFS版本9.1-systemd 当提到特定部分时,我将使用“ LFS xxx…”。 许多命令是逐字复制的。

Some of my specific learning goals:

我的一些具体学习目标:

  • source compilation and debugging — I’m C-illiterate

    源代码编译和调试—我是文盲
  • increased use of binutils and coreutils

    增加使用binutils和coreutils
  • low-level kernel features like namespaces — what make containers tick

    诸如名称空间之类的低级内核功能-使容器滴答作响的原因

Building a new Linux system is done most easily within an existing Linux system. The easiest way to get up and running with a Linux system is to fire up a virtual machine on one of the many cloud services. I use Google Cloud Platform at my job, so we will use it here since I’m familiar with it.

在现有Linux系统中,构建新Linux系统最容易。 建立和运行Linux系统的最简单方法是在众多云服务之一上启动虚拟机。 我在工作中使用Google Cloud Platform,因此我们会在这里使用它,因为我对此很熟悉。

Start a GCP VM Instance

启动GCP VM实例

1. Sign up for the free tier of Google Cloud Platform and create a new project. Google’s documentation is excellent, so if you’re stuck somewhere, just google it.

1.注册免费的Google Cloud Platform层并创建一个新项目。 Google的文档非常出色,因此,如果您被困在某个地方,只需对其进行搜索即可。

I will use gcloud commands in this series for consistency because sharing text commands is clearer than point and click descriptions. Anything done with these commands can also be done with the Cloud Console GUI.

我将在本系列文章中使用gcloud命令来保持一致性,因为共享文本命令比指向和单击的描述更清晰。 使用这些命令完成的所有操作也可以通过Cloud Console GUI完成。

2. Install the gcloud SDK. It is a command line utility that makes interacting with Google Cloud facilities easy. Follow the documentation to authenticate and set the configuration.

2.安装gcloud SDK 。这是一个命令行实用程序,可简化与Google Cloud设施的交互。 请按照文档进行身份验证并设置配置。

Here’s the output gcloud config list for my configuration:

这是我的配置的输出gcloud config list

3. Create a VM with the following command. Replace the instance name, zone, and disk name accordingly.

3.使用以下命令创建VM。 相应地替换实例名称,区域和磁盘名称。

This command creates a Google Cloud VM 此命令创建一个Google Cloud VM

That command did a number of important things:

该命令做了很多重要的事情:

  • Provisioned a 1-cpu, 3.75GB VM running Debian Linux. We can (and will) increase the compute resources when we start compiling source code.

    配备了运行Debian Linux的1-cpu,3.75GB VM。 当我们开始编译源代码时,我们可以(并且将)增加计算资源。
  • Enabled a serial port that will let us interact with the VM during boot time. This will be critical when we want to switch between Linux kernel installations. You can read more about serial ports and consoles here.

    启用了一个串行端口,该端口可让我们在启动期间与VM进行交互。 当我们要在Linux内核安装之间进行切换时,这将至关重要。 您可以在此处阅读有关串行端口和控制台的更多信息。

  • Created and attached a secondary persistent disk. We will need to do some disk and filesystem manipulation throughout the course of the build, and it doesn’t seem possible to partition the primary boot disk without borking the VM. Using a secondary disk is a way around this. (GCP sets 200GB as the default minimum for performance reasons)

    创建并附加了辅助永久磁盘。 在整个构建过程中,我们将需要对磁盘和文件系统进行一些操作,并且在不破坏VM的情况下似乎无法对主引导磁盘进行分区。 使用辅助磁盘可以解决此问题。 (出于性能考虑,GCP将200GB设置为默认最小值)

4. Connect to the VM using gcloud.

4.使用gcloud连接到VM。

Use this command to shell into the Linux os:

使用以下命令可将其安装到Linux操作系统中:

gcloud compute ssh [INSTANCE-NAME]

gcloud compute ssh [INSTANCE-NAME]

This gcloud feature is very convenient. It saves us from having to manually configure ssh and transfer keys.

这个gcloud功能非常方便。 它使我们不必手动配置ssh和传输密钥。

Set up a user password. It could save us a headache down the road.

设置用户密码。 这可以使我们免于头痛。

sudo passwd $(whoami)

sudo passwd $(whoami)

Update the package repos and upgrade packages.

更新软件包存储库并升级软件包。

sudo apt-get update

sudo apt-get update

In another terminal, use this command to open a serial console:

在另一个终端中,使用以下命令打开串行控制台:

gcloud compute connect-to-serial-port [INSTANCE-NAME]

gcloud compute connect-to-serial-port [INSTANCE-NAME]

Notice the output of the serial console is different from the shell. It displays hardware and system-level diagnostic information. We will use it primarily to interact with the bootloader and to get ourselves out of any goofs should we make them.

请注意,串行控制台的输出与Shell不同。 它显示硬件和系统级诊断信息。 我们将主要使用它与引导加载程序进行交互,并让我们脱离任何需要的东西。

The default system configuration has the bootloader timeout set to 0s, which means we can’t interact with it. Let’s change that and then reboot so we can see the bootloader menu in the serial console.

默认的系统配置将引导加载程序超时设置为0s,这意味着我们无法与其进行交互。 让我们进行更改,然后重新启动,以便在串行控制台中看到bootloader菜单。

In the shell session, edit /etc/default/grub.d/15_timeout.cfg. Set the timeout to 30s. Update GRUB(the bootloader) with sudo update-grub and then reboot the VM with sudo reboot .

在Shell会话中,编辑/etc/default/grub.d/15_timeout.cfg。 将超时设置为30s。 使用sudo update-grub更新GRUB(引导加载程序),然后使用sudo reboot重启虚拟机。

Monitor the serial console. You should see something like this:

监视串行控制台。 您应该会看到以下内容:

That’s the GRUB bootloader menu, and access to it will be key in later steps.

这就是GRUB引导程序菜单,在以后的步骤中,对其的访问至关重要。

Feel free to keep the serial console open, but reconnect a shell session for the next part.

随时保持串行控制台打开,但为下一部分重新连接Shell会话。

Format and Partition the Attached Disk

格式化和分区附加磁盘

The secondary disk we attached needs to formatted, partitioned, and mounted before we can do anything with it.

我们附加的辅助磁盘需要进行格式化,分区和装入,然后才能对其执行任何操作。

  1. Find your secondary disk with the lsblk command.

    使用lsblk命令找到辅助磁盘。

You should see a tree structure with the primary disk, sda, and its partitions, and the secondary disk, sdb, which has no partitions.

您应该看到一个具有主磁盘sda及其分区以及不具有分区的辅助磁盘sdb的树结构。

2. The disk needs to be labeled before it is recognized by the system and bootloader. We will use the parted utility to label sdb as gpt (GUID Partition Table)

2.在系统和引导加载程序识别磁盘之前,需要对其进行标记。 我们将使用parted实用程序将sdb标记为gpt(GUID分区表)

sudo parted /dev/sdb mklabel gpt

You might see an informational warning about updating /etc/fstab. We will address that later.

您可能会看到有关更新/ etc / fstab的信息性警告。 我们稍后会解决。

We need to partition sdb before we start working on it. LFS 2.4 recommends a main or ‘root’ partition and two additional partitions: boot and home. Our disk is 200GB, so will assign 4GB to boot, 96 GB to home, and 100GB to main.

在开始处理sdb之前,需要对其进行分区。 LFS 2.4建议使用主分区或“根”分区以及两个附加分区:引导分区和主分区。 我们的磁盘为200GB,因此将为启动分配4GB,为家庭分配96GB,为主分配100GB。

3. Create the partitions.

3.创建分区。

#boot
sudo parted -a opt /dev/sdb mkpart primary ext4 0% 2%#primary
sudo parted -a opt /dev/sdb mkpart primary ext4 2% 52%#home
sudo parted -a opt /dev/sdb mkpart primary ext4 52% 100%

The output of lsblk should look like this:

lsblk的输出应如下所示:

Sweet, sweet partitions. 甜美,甜美的隔板。

4. Make file systems on the partitions.

4.在分区上制作文件系统。

#boot
sudo mkfs.ext4 -L datapartition /dev/sdb1#main
sudo mkfs.ext4 -L datapartition /dev/sdb2#home
sudo mkfs.ext4 -L datapartition /dev/sdb3

5. Set the LFS environment variable.

5.设置LFS环境变量。

This points to the mount point for the LFS installation. We will need to set it for both user and root on the build system.

这指向LFS安装的安装点。 我们将需要在构建系统上同时为用户和root设置它。

#user (i.e., your current login)
cd ~
echo 'export LFS=/mnt/lfs' >> .bashrc
source .bashrc#root
sudo -s #start a root session
cd /root
echo 'export LFS=/mnt/lfs' >> .bashrc
source .bashrc
exit #leave root session#test environment variable -- should return '/mnt/lfs'
echo $LFS

6. Mount the partitions.

6.安装分区。

#make the mount directories
sudo mkdir $LFS
sudo mkdir $LFS/boot
sudo mkdir $LFS/home#mount 'root' partition
sudo mount -v -t ext4 /dev/sdb2 $LFS#mount boot partition
sudo mount -v -t ext4 /dev/sdb1 $LFS/boot#mount home partition
sudo mount -v -t ext4 /dev/sdb3 $LFS/home

The output of lsblk should look like this:

lsblk的输出应如下所示:

I’m working from a different computer, hence the minor differences in terminal appearance. 我在另一台计算机上工作,因此终端外观略有不同。

7. Update /etc/fstab so the system mounts the partitions automatically at boot

7.更新/ etc / fstab,以便系统在启动时自动挂载分区

echo UUID=`sudo blkid -s UUID -o value /dev/sdb2` $LFS ext4 discard,defaults,nofail 0 2 | sudo tee -a /etc/fstabecho UUID=`sudo blkid -s UUID -o value /dev/sdb1` $LFS/boot ext4 discard,defaults,nofail 0 2 | sudo tee -a /etc/fstabecho UUID=`sudo blkid -s UUID -o value /dev/sdb3` $LFS/home ext4 discard,defaults,nofail 0 2 | sudo tee -a /etc/fstab

Prepare the host system requirements (build tools).

准备主机系统要求(构建工具)。

  1. Run the script provided in LFS 2.2 to check if all the needed build tools are installed.

    运行LFS 2.2中提供的脚本以检查是否已安装所有必需的构建工具。

Your output should be similar to this:

您的输出应类似于以下内容:

bash, version 5.0.3(1)-release
/bin/sh -> /bin/dash
ERROR: /bin/sh does not point to bash
Binutils: version-check.sh: line 10: ld: command not found
version-check.sh: line 11: bison: command not found
yacc not found
bzip2, Version 1.0.6, 6-Sept-2010.
Coreutils: 8.30
diff (GNU diffutils) 3.7
find (GNU findutils) 4.6.0.225-235f
version-check.sh: line 25: gawk: command not found
/usr/bin/awk -> /usr/bin/mawk
version-check.sh: line 35: gcc: command not found
version-check.sh: line 36: g++: command not found
(Debian GLIBC 2.28-10) 2.28
grep (GNU grep) 3.3
gzip 1.9
Linux version 4.19.0-10-cloud-amd64 ([email protected]) (gcc version 8.3.0 (Debian 8.3.0-6)) #1 SMP Debian 4.19.132-1 (2020-07-24)
version-check.sh: line 41: m4: command not found
version-check.sh: line 42: make: command not found
version-check.sh: line 43: patch: command not found
Perl version='5.28.1';
Python 3.7.3
sed (GNU sed) 4.7
tar (GNU tar) 1.30
version-check.sh: line 48: makeinfo: command not found
xz (XZ Utils) 5.2.4
version-check.sh: line 51: g++: command not found
g++ compilation failed

Note that some packages are not found. We will have to install them. For those packages that are installed, a version is listed. We need to ensure that the version meets or exceeds those specified in LFS 2.2. Since we are working on an up-to-date Debian system, the versions all exceed the recommendations.

请注意,找不到某些软件包。 我们将必须安装它们。 对于已安装的那些软件包,列出了一个版本。 我们需要确保该版本达到或超过LFS 2.2中指定的版本。 由于我们正在使用最新的Debian系统,因此所有版本都超出了建议。

The missing packages can be installed with:

缺少的软件包可以通过以下方式安装:

sudo apt-get install build-essential, bison, gawk, texinfo

2. Download the source files needed to build Linux from scratch.

2.下载从头开始构建Linux所需的源文件。

LFS 3.1 provides a manifest of source file download URIs that can be used with wget. You might need to install it first: sudo apt-get install wget.

LFS 3.1提供了可与wget一起使用的源文件下载URI的清单。 您可能需要先安装它: sudo apt-get install wget

Create the source directory for the files and modify its file permissions.

创建文件的源目录并修改其文件权限。

sudo mkdir -v $LFS/sources 
sudo chmod -v a+wt $LFS/sources

Obtain the package manifest ‘wget-list’.

获取软件包清单“ wget-list”。

wget http://www.linuxfromscratch.org/lfs/view/stable-systemd/wget-list

The file ‘wget-list’ should be in your home directory. Now download the source files to the source directory.

文件“ wget-list”应位于您的主目录中。 现在将源文件下载到源目录。

sudo wget --input-file=wget-list --continue --directory-prefix=$LFS/sources

To check that everything was downloaded, we can make a list of all the packages in the wget-list and compare it to the contents of the sources directory. One of my downloads timed out on the first pass and was missing.

为了检查所有内容是否都已下载,我们可以在wget-list中列出所有软件包的列表,并将其与源目录的内容进行比较。 我的一个下载在第一次通过时超时,并且丢失了。

#create a the list of expected file names
grep -oP “([^\/]+$)” wget-list | sort -f > manifest#create the list of downloaded file names
ls /mnt/lfs/sources | sort -f > downloaded#compare the lists
diff manifest downloaded

3. Make the tools directory and symlink it to the host system.

3.创建工具目录,并将其符号链接到主机系统。

sudo mkdir -v $LFS/toolssudo ln -sv $LFS/tools /

The reason for the symlink as directly described in LFS 4.2:

LFS 4.2中直接描述了符号链接的原因:

The created symlink enables the toolchain to be compiled so that it always refers to /tools, meaning that the compiler, assembler, and linker will work both in Chapter 5 (when we are still using some tools from the host) and in the next (when we are “chrooted” to the LFS partition).

创建的符号链接使工具链得以编译,因此它始终引用/tools ,这意味着编译器,汇编器和链接器将在第5章(当我们仍在使用主机中的某些工具时)和下一章(当我们被“ chroot”到LFS分区时)。

4. Set up the lfs user.

4.设置lfs用户。

sudo groupadd lfssudo useradd -s /bin/bash -g lfs -m -k /dev/null lfssudo passwd lfs #set passwordsudo chown -v lfs $LFS/toolssudo chown -v lfs $LFS/sources#switch to lfs login shell
su - lfs

5. Continue the environment set up as described in LFS 4.4

5.继续按照LFS 4.4中所述设置环境

This completes Part 1.

至此完成了第1部分。

翻译自: https://medium.com/swlh/building-linux-from-on-a-google-cloud-virtual-machine-5785d6115877

linux cloud安装

你可能感兴趣的:(linux cloud安装_在Google Cloud虚拟机上从头开始构建Linux)