avatar

gentoo安装记

Larry the cow beckons you to explore gentoo!

用了三年的MacBook开始出现电池问题,来回从公司和家里带也很不方便,索性给自己在公司的主机装个linux。思前想后,还是选择了gentoo,毕竟公司的机器编译起来也很快:)。gentoo和arch简称两大自由系统,你可以定制你的系统,和其他版本的比较向来都是一个话题,这里不展开聊,只记录一下自己的安装过程。如果对linux熟悉的话,安装应该不是难事。先show一下我的新系统。
8Lp976.png

简介

整个安装的过程包括了usb引导镜像的制作,磁盘分区,安装内核代码,编译内核,安装基本软件,配置系统引导这几个过程。不熟悉的同学最好在安装前过一遍Gentoo Handbook里面的内容,确保自己已经理解,handbook一般都是update to date的指南

安装过程

1. usb镜像制作

安装gentoo我一般都用system rescue cd 来做为live cd的系统,system rescue cd 默认搭配了很多系统安装常用的软件,包括gparted, cgdisk,一些常用的配置和网络命令等。非UEFI系统直接dd命令就行了。

1
$ dd if=/path/to/systemrescuecd-x.y.z.iso of=/dev/sdx

if 后面放的是下载好的iso文件路径,of 后面放的是你想写入的usb盘的名称,一般情况下为 /dev/sdb

如果是需要在UEFI模式下启动的,按照上面链接里面的,找到对应的UEFI模式说明就好了。

2. 系统分区

启动live cd后,直接在终端输入:

1
$ startx

便可以进入system rescue cd的xfce桌面环境,操作起来会方便很多。这个时候你可以直接打开终端,然后查看一下自己的硬盘。

1
2
3
4
5
6
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 238.5G 0 disk
├─sda1 8:1 0 300M 0 part /boot
├─sda2 8:2 0 16G 0 part [SWAP]
└─sda3 8:3 0 222.2G 0 part /

可以看到这个是一块硬盘,/sda, 分了三个部分,一个是启动用的,一个swap系统交换文件分区,一个是挂载在根目录下的盘,
最后我们就是要分区成这个样子,如果你的硬盘不是这样的话,也可以通过删除分区重新建立分区来完成。

或者你也可以通过自带的GParted来分区,GUI界面操作起来比较方便,GParted用法可以直接在网上查看一下教程

命令行的话,可以通过 cgdisk 来实现分区。

1
$ cgdisk /dev/sda

可以得到cgdisk的启动页面,大概如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
                                         cgdisk 1.0.5

Disk Drive: /dev/sda
Size: 500118192, 238.5 GiB

Part. # Size Partition Type Partition Name
----------------------------------------------------------------
1007.0 KiB free space
1 300.0 MiB EFI system partition
2 16.0 GiB Linux swap
3 222.2 GiB Linux filesystem

[ Align ] [ Backup ] [ Delete ] [ Help ] [ Info ] [ Load ] [ naMe ]
[ Quit ] [ Type ] [ Verify ] [ Write ]

光标可以移动到你想处理的盘上,比如上面的1,2,3.下面的New,Write,Delete分别表示新建一个盘,写盘和删除一个盘。
新建的时候会问几个问题,#是加上的注释:

1
2
3
4
5
6
7
8
9
10
11
12
# 起始扇区的位置,直接 Enter 就行
First sector (749424640-972906511, default = 749424640):
# 大小,可以是扇区数,也可以是实际的大小(例如 100M,20G一类的),要用掉整个剩余空闲空间的话,直接 Enter 就行。
Size in sectors or {KMGTP} (default = 223481872):
# 分区类型,默认的就好
# 但是如果要建立新的 EFI 系统分区的话 ,分区类型是 :code:`ef00`
# 但是如果要建立新的 交换空间(就是虚拟内存啦)的话 ,分区类型是 :code:`8200`
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):
# 设置卷标,不设置也行。
Current partition name is ''
Enter new partition name, or <Enter> to use the current name:

这里的话,一般情况下需要三个分区,一个是EFI,一个是交换分区,一个是根目录区,按照提示建立好保存退出就行了。

3. 文件系统挂载

每个硬盘分区都会有自己对应的文件系统,EFI分区需要FAT32文件系统,根目录一般用ext4文件系统,依次创建文件系统。

1
2
3
$ mkfs.ext4 -T small /dev/sda3
$ mkswap /dev/sda2 && swapon /dev/sda2
$ mkfs.fat -F 32 /dev/sda1

接下来就是挂载文件系统了

1
2
$ mount /dev/sda3 /mnt/gentoo
$ mount /dev/sda2 /mnt/gentoo/boot

4. 安装stage3

先同步好时钟, 手动设置的话,可以用date命令

1
$ date 032416082020 # 表示 2020-03-24 16:08

自动的话可以使用ntpd

1
$ ntpd -q -g

进入主目录,下载stage3文件,国内的话可以到腾讯云或者清华的镜像

1
2
3
$ cd /mnt/gentoo
$ wget -c https://mirrors.tuna.tsinghua.edu.cn/gentoo/releases/amd64/autobuilds/current-stage3-amd64/stage3-amd64-20200322T214504Z.tar.xz
$ tar -xpvf stage3-amd64-20200322T214504Z.tar.xz --xattrs-include='*.*' --numeric-owner

5. 配置编译参数

Gentoo是一个高度自由定制的系统,所以所有的软件包都是需要根据用户特定的参数来编译的,编译配置文件在/etc/portage/make.conf下。每个人每台电脑配置一般都不一样。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# These settings were set by the catalyst build script that automatically
# built this stage.
# Please consult /usr/share/portage/config/make.conf.example for a more
# detailed example.
COMMON_FLAGS="-march=native -O2 -pipe"
CFLAGS="${COMMON_FLAGS}"
CXXFLAGS="${COMMON_FLAGS}"
FCFLAGS="${COMMON_FLAGS}"
FFLAGS="${COMMON_FLAGS}"

MAKEOPTS="-j9" # j后面的数目,一般是你电脑cpu数目+1

GENTOO_MIRRORS="https://mirrors.cloud.tencent.com/gentoo/ https://mirrors.tuna.tsinghua.edu.cn/gentoo"

# NOTE: This stage was built with the bindist Use flag enabled
PORTDIR="/var/db/repos/gentoo"
DISTDIR="/var/cache/distfiles"
PKGDIR="/var/cache/binpkgs"

# This sets the language of build output to English.
# Please keep this setting intact when reporting bugs.
LC_MESSAGES=C

USE="elogind xft clang -consolekit -systemd networkmanager"

6. 安装基本系统

选择合适的软件仓库rsync地址,推荐国内地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ mkdir --parents /mnt/gentoo/etc/portage/repos.conf
$ cp /mnt/gentoo/usr/share/portage/config/repos.conf /mnt/gentoo/etc/portage/repos.conf/gentoo.conf
$ cat /mnt/gentoo/etc/portage/repos.conf/gentoo.conf
[DEFAULT]
main-repo = gentoo

[gentoo]
location = /var/db/repos/gentoo
sync-type = rsync
sync-uri = rsync://rsync.cn.gentoo.org/gentoo-portage/ # 国内任意一个可用的源
auto-sync = yes
sync-rsync-verify-jobs = 1
sync-rsync-verify-metamanifest = yes
sync-rsync-verify-max-age = 24
sync-openpgp-key-path = /usr/share/openpgp-keys/gentoo-release.asc
sync-openpgp-keyserver = hkps://keys.gentoo.org
sync-openpgp-key-refresh-retry-count = 40
sync-openpgp-key-refresh-retry-overall-timeout = 1200
sync-openpgp-key-refresh-retry-delay-exp-base = 2
sync-openpgp-key-refresh-retry-delay-max = 60
sync-openpgp-key-refresh-retry-delay-mult = 4
sync-webrsync-verify-signature = yes

然后复制dns信息

1
$ cp --dereference /etc/resolv.conf /mnt/gentoo/etc/

挂载必要的文件系统

1
2
3
4
5
$ mount --types proc /proc /mnt/gentoo/proc
$ mount --rbind /sys /mnt/gentoo/sys
$ mount --make-rslave /mnt/gentoo/sys
$ mount --rbind /dev /mnt/gentoo/dev
$ mount --make-rslave /mnt/gentoo/dev

chroot进入准备安装的系统目录

1
2
3
$ chroot /mnt/gentoo /bin/bash
$ source /etc/profile
$ export PS1="(chroot) ${PS1}"

挂载/boot目录,如果挂载过了,则不需要

1
$ mount /dev/sda2 /boot

同步gentoo ebuild仓库信息

1
2
$ emerge-webrsync
$ emerge --sync --quiet

选择默认profile, gentoo支持系统同时存在多个版本的软件,系统也同样会有多种配置,可以选择一个

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
$ eselect profile list
Available profile symlink targets:
[1] default/linux/amd64/17.0 (stable)
[2] default/linux/amd64/17.0/selinux (stable)
[3] default/linux/amd64/17.0/hardened (stable)
[4] default/linux/amd64/17.0/hardened/selinux (stable)
[5] default/linux/amd64/17.0/desktop (stable)
[6] default/linux/amd64/17.0/desktop/gnome (stable)
[7] default/linux/amd64/17.0/desktop/gnome/systemd (stable)
[8] default/linux/amd64/17.0/desktop/plasma (stable)
[9] default/linux/amd64/17.0/desktop/plasma/systemd (stable)
[10] default/linux/amd64/17.0/developer (stable)
[11] default/linux/amd64/17.0/no-multilib (stable)
[12] default/linux/amd64/17.0/no-multilib/hardened (stable)
[13] default/linux/amd64/17.0/no-multilib/hardened/selinux (stable)
[14] default/linux/amd64/17.0/systemd (stable)
[15] default/linux/amd64/17.0/x32 (dev)
[16] default/linux/amd64/17.1 (stable)
[17] default/linux/amd64/17.1/selinux (stable)
[18] default/linux/amd64/17.1/hardened (stable)
[19] default/linux/amd64/17.1/hardened/selinux (stable)
[20] default/linux/amd64/17.1/desktop (stable) *
[21] default/linux/amd64/17.1/desktop/gnome (stable)
[22] default/linux/amd64/17.1/desktop/gnome/systemd (stable)
[23] default/linux/amd64/17.1/desktop/plasma (stable)
[24] default/linux/amd64/17.1/desktop/plasma/systemd (stable)
[25] default/linux/amd64/17.1/developer (stable)
[26] default/linux/amd64/17.1/no-multilib (stable)
[27] default/linux/amd64/17.1/no-multilib/hardened (stable)
[28] default/linux/amd64/17.1/no-multilib/hardened/selinux (stable)
[29] default/linux/amd64/17.1/systemd (stable)
[30] default/linux/amd64/17.0/musl (exp)
[31] default/linux/amd64/17.0/musl/hardened (exp)
[32] default/linux/amd64/17.0/musl/hardened/selinux (exp)
[33] default/linux/amd64/17.0/uclibc (exp)
[34] default/linux/amd64/17.0/uclibc/hardened (exp)
$ eselect profile set 20 # 根据自己的选择设置

更新整个系统的包到最新

1
$ emerge --ask --verbose --update --newuse --deep @world

设置自己的USE flag和对应accept_license, USE是gentoo控制包定制化的一个参数,具体可以查看gentoo use wiki, 而accept_license则表示你可以接受的包的license, 具体的各种licese参数
以及对应的license,可以参考gentoo licenses wiki, 我的配置如下:

1
2
3
# 仅供参考
ACCEPT_LICENSE="* -@EULA"
USE="elogind xft clang -consolekit -systemd networkmanager"

配置时区

1
$ echo "Asia/Shanghai" > /etc/timezone

配置loales

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ vim /etc/locale.gen
en_US.UTF-8 UTF-8
zh_CN.UTF-8 UTF-8
zh_TW.UTF-8 UTF-8

$ locale-gen
$ eselect locale list
Available targets for the LANG variable:
[1] C
[2] C.utf8
[3] en_US.utf8 *
[4] POSIX
[5] zh_CN.utf8
[6] zh_TW.utf8
[ ] (free form)
$ eselect locale set 3

重新加载所有更新后的配置

1
$ env-update && source /etc/profile && export PS1="(chroot) ${PS1}"

7. 内核编译

Gentoo是高度定制的Linux版本,所以一般情况下,内核也是自己配置,这样能减少不必要的内核模块和驱动,从而编译一个只适合自己机器的内核。

手动自己编译内核

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
下载内核代码
$ emerge --ask sys-kernel/gentoo-sources

下载必要的电脑设备检测工具,以知道自己设备对应的驱动
$ emerge --ask sys-apps/pciutils sys-apps/usbutils

查看对应设备
$ lspci
00:00.0 Host bridge: Intel Corporation Xeon E3-1200 v6/7th Gen Core Processor Host Bridge/DRAM Registers (rev 05)
00:02.0 VGA compatible controller: Intel Corporation HD Graphics 630 (rev 04)
00:14.0 USB controller: Intel Corporation 200 Series/Z370 Chipset Family USB 3.0 xHCI Controller
00:16.0 Communication controller: Intel Corporation 200 Series PCH CSME HECI #1
00:17.0 SATA controller: Intel Corporation 200 Series PCH SATA controller [AHCI mode]
00:1c.0 PCI bridge: Intel Corporation 200 Series PCH PCI Express Root Port #5 (rev f0)
00:1c.5 PCI bridge: Intel Corporation 200 Series PCH PCI Express Root Port #6 (rev f0)
00:1c.7 PCI bridge: Intel Corporation 200 Series PCH PCI Express Root Port #8 (rev f0)
00:1d.0 PCI bridge: Intel Corporation 200 Series PCH PCI Express Root Port #9 (rev f0)
00:1f.0 ISA bridge: Intel Corporation 200 Series PCH LPC Controller (B250)
00:1f.2 Memory controller: Intel Corporation 200 Series/Z370 Chipset Family Power Management Controller
00:1f.3 Audio device: Intel Corporation 200 Series PCH HD Audio
00:1f.4 SMBus: Intel Corporation 200 Series/Z370 Chipset Family SMBus Controller
02:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 15)
03:00.0 PCI bridge: ASMedia Technology Inc. ASM1083/1085 PCIe to PCI Bridge (rev 04)
$ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 121: ID 05ac:12a8 Apple, Inc. iPhone5/5C/5S/6
Bus 001 Device 096: ID 0853:0100 Topre Corporation HHKB Professional
Bus 001 Device 095: ID 0409:005a NEC Corp. HighSpeed Hub
Bus 001 Device 003: ID 148f:7601 Ralink Technology, Corp. MT7601U Wireless Adapter
Bus 001 Device 094: ID 046d:c52b Logitech, Inc. Unifying Receiver
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

开始编译内核, menuconfig勾选对应的内核选项,包括驱动,docker支持等等
$ make menuconfig
$ make && make modules_install
$ make install
$ emerge --ask sys-kernel/genkernel
$ genkernel --install initramfs
$ emerge --ask sys-kernel/linux-firmware

利用genkernel编译

新手第一次安装一般都不知道内核参数,很难正确设置对,这个时候可以用genkernel,生成和编译一个通用的内核

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
安装genkernel
$ emerge --ask sys-kernel/genkernel

配置fstab
$ vim /etc/fstab
#LABEL=boot /boot ext4 noauto,noatime 1 2
#UUID=58e72203-57d1-4497-81ad-97655bd56494 / ext4 noatime0 1
#LABEL=swap none swap sw 0 0
#/dev/cdrom /mnt/cdrom auto noauto,ro 0 0
/dev/sda1 /boot vfat defaults,noatime 0 2
/dev/sda2 none swap sw 0 0
/dev/sda3 / ext4 noatime 0 1

开始编译
$ genkernel all
安装firmware
$ emerge --ask sys-kernel/linux-firmware

内核编译完成后,我们就可以开始配置自己的系统了。

8. 系统配置

网络配置

1
2
3
4
5
6
7
8
9
10
11
$ vim /etc/conf.d/hostname
# Set to the hostname of this machine
hostname="gentoo"

$ vim /etc/conf.d/net
config_enp2s0="dhcp"

$ vim /etc/hosts
# IPv4 and IPv6 localhost aliases
127.0.0.1 localhost gentoo
::1 localhost

修改默认密码

1
$ passwd

9. 安装必要软件

系统日志

1
2
$ emerge --ask app-admin/sysklogd
$ rc-update add sysklogd default

cron组件

1
2
$ emerge --ask sys-process/cronie
$ rc-update add cronie default

sshd

1
$ rc-update add sshd default

网络相关

1
2
$ emerge --ask net-misc/dhcpcd
$ emerge --ask net-wireless/iw net-wireless/wpa_supplicant

10. 配置系统引导

grub安装

1
2
3
若是uefi,需要先在make.conf配置好
$ echo 'GRUB_PLATFORMS="efi-64"' >> /etc/portage/make.conf
$ emerge --ask --verbose sys-boot/grub:2

grub配置

1
2
3
4
5
6
7
8
9
10
# for bios
$ grub-install /dev/sda
# for uefi
$ grub-install --target=x86_64-efi --efi-directory=/boot

$ grub-mkconfig -o /boot/grub/grub.cfg
Generating grub.cfg ...
Found linux image: /boot/vmlinuz-4.9.16-gentoo
Found initrd image: /boot/initramfs-genkernel-amd64-4.9.16-gentoo
done

重启系统

1
2
3
4
5
$ exit
$ cd
$ umount -l /mnt/gentoo/dev{/shm,/pts,}
$ umount -R /mnt/gentoo
$ reboot

重启后

1
2
3
4
5
6
7
8
# 添加新用户
$ useradd -m -G users,wheel,audio -s /bin/bash jenenliu
$ passwd jenenliu

# 配置sudo
$ EDITOR=nano visudo
## Uncomment to allow members of group wheel to execute any command
# %wheel ALL=(ALL) ALL 去掉这一行的注释,你就拥有sudo权限了

完成

自此,你的Genoo系统已经安装完成了,Welcome to the Gentoo world ;)

文章作者: 水哥
文章链接: https://jenenliu.github.io/2020/03/24/gentoo-installation-tutorial/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Life is short
打赏
  • 微信
    微信
  • 支付寶
    支付寶