使用Ubuntu Base和Live自动安装程序(subiquity)获取最小的Ubuntu/Xubuntu系统
以下是一个自动安装模板文件,您可以使用它来自动安装Ubuntu Base 20.10(及更高版本),并从中获得相对较小的Xubuntu安装。作为先决条件,您需要已经设置好PXE安装环境的其余部分(请参阅下面的链接以获取逐步指南)。PXE环境由DHCP服务、TFTP服务器、HTTP(S)服务器和可选的NFS服务器组成。当这些组合在一起时,它们为您网络中的设备提供了PXE引导功能。设备本身还需要支持从网络引导(无论是传统BIOS还是UEFI)。
我在我的所有测试中都使用了PXE引导、Ubuntu 20.04.1和20.10以及自动安装文件(请参阅下面的20.10)。要了解有关为20.04及更高版本逐步设置PXE的详细信息,请阅读我在此处的其他指南:
https://askubuntu.com/a/1292097/1080682
注意:下面列出的命令也可以以其他方式执行,例如在Live-CD或任何其他实时引导会话中手动执行,我还使用PXE引导到安装程序(subiquity),然后在subiquity实时会话中通过SSH执行。请确保引导的会话/安装程序与您计划安装的Ubuntu Base的主要版本相同,例如,使用Ubuntu 20.10的Live-CD来创建Ubuntu Base 20.10。
自动安装脚本通过PXE作为"user-data"文件提供。
user-data
#cloud-config
autoinstall:
version: 1
early-commands:
- dd if=/dev/zero of=/dev/sda bs=8M count=30
- (echo o; echo n; echo p; echo 1; echo ""; echo +200M; echo n; echo p; echo 2; echo ''; echo ''; echo a; echo 1; echo p; echo w) | fdisk /dev/sda
- mkfs.fat -F 32 -D 0x80 -M 0xF8 -n BOOT /dev/sda1
- mkfs.ext4 -F /dev/sda2
- mkdir /mnt/boot /mnt/root
- mount /dev/sda2 /mnt/root
- curl http://cdimage.ubuntu.com/ubuntu-base/releases/20.10/release/ubuntu-base-20.10-base-amd64.tar.gz -o /ubuntu-base-20.10-base-amd64.tar.gz
- tar -xzvf /ubuntu-base-20.10-base-amd64.tar.gz -C /mnt/root
- touch /mnt/root/etc/resolv.conf
- echo "nameserver 8.8.8.8" > /mnt/root/etc/resolv.conf
- chroot /mnt/root sh -c "apt-get update"
- chroot /mnt/root sh -c "apt-get install -y linux-image-5.8.0-28-generic initramfs-tools init dbus iproute2 sudo nano --no-install-recommends"
- chroot /mnt/root sh -c "useradd -m ubuntu -s '/bin/bash' && echo ubuntu:ubuntu | chpasswd"
- chroot /mnt/root sh -c "addgroup ubuntu adm"
- chroot /mnt/root sh -c "addgroup ubuntu sudo"
- apt-get update
- apt-get install -y syslinux
- syslinux -i /dev/sda1
- dd if=/usr/lib/syslinux/mbr/mbr.bin of=/dev/sda bs=440 count=1 conv=notrunc
- mount /dev/sda1 /mnt/boot
- touch /mnt/boot/syslinux.cfg
- (echo PROMPT 0; echo DEFAULT base; echo LABEL base; echo KERNEL vmlinuz; echo APPEND root=/dev/sda2 rw; echo INITRD initrd.img) > /mnt/boot/syslinux.cfg
- cp /mnt/root/boot/vmlinuz /mnt/boot
- cp /mnt/root/boot/initrd.img /mnt/boot
- touch /mnt/root/etc/systemd/network/00-wired.network
- (echo [Match]; echo Name=enp0s10f0; echo [Network]; echo Address=10.10.2.101/24; echo Gateway=10.10.2.99; echo DNS=8.8.8.8) > /mnt/root/etc/systemd/network/00-wired.network
- chroot /mnt/root sh -c "systemctl enable systemd-networkd.service"
- chroot /mnt/root sh -c "apt-get clean"
- umount /mnt/boot
- umount /mnt/root
- reboot
上面的脚本只是纯粹的BIOS / 传统引导脚本,原样复制/粘贴应该可以正常工作。
要添加UEFI功能,请在apt-get clean命令之后和umount & reboot命令之前插入以下行。
- mkdir -p /mnt/boot/EFI/BOOT/
- cp /usr/lib/SYSLINUX.EFI/efi64/syslinux.efi /mnt/boot/EFI/BOOT/BOOTX64.EFI
- cp /usr/lib/syslinux/modules/efi64/ldlinux.e64 /mnt/boot/EFI/BOOT/
- cp /mnt/boot/syslinux.cfg /mnt/boot/EFI/BOOT/syslinux.cfg
- cp /mnt/root/boot/vmlinuz /mnt/boot/EFI/BOOT/
- cp /mnt/root/boot/initrd.img /mnt/boot/EFI/BOOT/
需要注意的行,可能需要更改:
useradd
和addgroup
- 更改为您自己的用户名和密码,或者保留为ubuntu/ubuntu
/etc/systemd/network/00-*.network
- 随意更改配置文件名,并确保将接口名称(在我的情况下为enp0s10f0
)、IP(10.10.2.101/24
)和网关(10.10.2.99
)更改为您实际期望的内容
- 如果您还安装了DHCP客户端软件包(
isc-dhcp-client
),您可以使用类似于echo [Match]; echo Name=eth0; echo [Network]; echo DHCP=yes
的命令(只需检查接口名称)
- 您也可以完全按原样保留脚本,在重启后更改用户/密码或网络设置
一旦你通过自动安装程序运行脚本,它将安装Ubuntu Base并重新启动。重新启动后,请使用用户名
ubuntu
和密码
ubuntu
(如果你已更改密码,则使用你自己的密码)登录。
请注意,我
不正在运行
xubuntu-core
的安装,请参阅下面脚本中的注释以及答案下面的注释。
更多详细信息
为了解释这个过程,我还附上了带有所有注释的脚本,供其他人了解更多细节、理解这个过程,并根据自己的喜好进行修改。
#cloud-config
autoinstall:
version: 1
early-commands:
# delete any old partition data, up to 240MB size, if this was reinstall
# erases partition tables and whole boot partition as well, by erasing 30x8MB=240MB (partition is 200MB)
- dd if=/dev/zero of=/dev/sda bs=8M count=30
# create new 200MB boot partition and rest as root partition
- (echo o; echo n; echo p; echo 1; echo ""; echo +200M; echo n; echo p; echo 2; echo ''; echo ''; echo a; echo 1; echo p; echo w) | fdisk /dev/sda
# format boot as FAT 32
- mkfs.fat -F 32 -D 0x80 -M 0xF8 -n BOOT /dev/sda1
# format rest as ext4
- mkfs.ext4 -F /dev/sda2
# create mount points for boot and root
- mkdir /mnt/boot /mnt/root
# don't mount boot yet, as later syslinux requires it unmounted, or mount it here and unmount before syslinux -i
# - mount /dev/sda1 /mnt/boot
- mount /dev/sda2 /mnt/root
# optional commands to see what's mounted, note if you did not mount it, don't ls it because subiquity installer will fail
# - df -h
# - ls -al /mnt
# - ls -al /mnt/boot
# - ls -al /mnt/root
# download ubuntu Base from official repo, we download 20.10 below, this will download to / (root) of ubiquity installer, which means - into memory
# if you want 20.04 or 20.10 or anything else (future releases) just change following these two lines (curl and tar) to reflect that, plus later in script change kernel version
- curl http://cdimage.ubuntu.com/ubuntu-base/releases/20.10/release/ubuntu-base-20.10-base-amd64.tar.gz -o /ubuntu-base-20.10-base-amd64.tar.gz
# extract all files to our sda2, mounted at /mnt/root
- tar -xzvf /ubuntu-base-20.10-base-amd64.tar.gz -C /mnt/root
# create temporary resolv.conf in the new system
- touch /mnt/root/etc/resolv.conf
- echo "nameserver 8.8.8.8" > /mnt/root/etc/resolv.conf
# chroot to /mnt/root and start executing commands one by one
# update apt's package cache
- chroot /mnt/root sh -c "apt-get update"
# install Linux image, which will install kernel and create initrd and all
# you need to install specific version depending on OS, eg 20.04 will use linux-image-5.4.0-42-generic
# we also install: init, dbus, iproute2, sudo, which also pull systemd - to have actually usable system
# additionally install nano to be able to edit confs, you can change that to any other editor
- chroot /mnt/root sh -c "apt-get install -y linux-image-5.8.0-28-generic initramfs-tools init dbus iproute2 sudo nano --no-install-recommends"
# I personally always install openssh-server as well, ping for debugging
# and you may also want to add isc-dhcp-client package to enable networking setup by DHCP server
- chroot /mnt/root sh -c "apt-get install -y openssh-server isc-dhcp-client iputils-ping --no-install-recommends"
# add at least one user, here we add user ubuntu with password ubuntu, change it here or later after first login
- chroot /mnt/root sh -c "useradd -m ubuntu -s '/bin/bash' && echo ubuntu:ubuntu | chpasswd"
# add this new user to correct groups to enable it to be admin and to have sudo access
- chroot /mnt/root sh -c "addgroup ubuntu adm"
- chroot /mnt/root sh -c "addgroup ubuntu sudo"
# this would installs Xubuntu ... or switch to whatever you need... if you install some other package or desktop environment - it will be there after your login
# but it is quite large (2GB) so if ANY package fails or throws ANY error - whole subiquity installer crashes; so I recommend this to be done on first interactive login after reboot
# - chroot /mnt/root sh -c "apt-get install -y xubuntu-core"
# below is syslinux install the easy way, through Ubuntu's official package/repo
# get the syslinux package, note this is not in chroot, this installs just to subiquity memory, so we need to run apt update again
- apt-get update
- apt-get install -y syslinux
# tell syslinux to install itself to your sda1 which is your boot partition
# if you mounted it earlier, unmount boot!! use command below (which is commented out by default)
# - umount /mnt/boot
- syslinux -i /dev/sda1
# now that syslinux is installed, burn it's mbr.bin (or maybe gptmbr.bin if you plan to use GPT + UEFI) to start of your disk; note we target whole device "sda" - NOT sda1
- dd if=/usr/lib/syslinux/mbr/mbr.bin of=/dev/sda bs=440 count=1 conv=notrunc
# now we can safely mount boot partition
- mount /dev/sda1 /mnt/boot
# we create syslinux.cfg, I do touch, as if it doesn't exist it will break subiquity again
- touch /mnt/boot/syslinux.cfg
# echo your config to it; explaining
# PROMPT 0 - don't ask use default / 1 - ask for user input (good for diag); DEFAULT - set which label is default so syslinux can autoboot; LABEL - this is config for our Ubuntu Base OS; KERNEL - vmlinuz or eqivalent kernel name; APPEND - to mount your /root partiton as writeable; INITRD - name of your initrd image
- (echo PROMPT 0; echo DEFAULT base; echo LABEL base; echo KERNEL vmlinuz; echo APPEND root=/dev/sda2 rw; echo INITRD initrd.img) > /mnt/boot/syslinux.cfg
# copy vmlinuz & initrd files that you've installed in your chroot, you can specify exact version, just make sure to change syslinux.cfg echo (above) accordingly
# can also copy * to copy all, but all we need is these ones really
- cp /mnt/root/boot/vmlinuz /mnt/boot
- cp /mnt/root/boot/initrd.img /mnt/boot
# setup EFI boot, you can keep both BIOS and UEFI bootloaders at the same time
# install additional package
- apt-get install -y syslinux-efi
# create directories, will create both BOOT and parent EFI folders
- mkdir -p /mnt/boot/EFI/BOOT/
# copy all files, in order: UEFI bootloader, bootloader's module (required), syslinux config (same as above), kernel and initrd (same as above)
- cp /usr/lib/SYSLINUX.EFI/efi64/syslinux.efi /mnt/boot/EFI/BOOT/BOOTX64.EFI
- cp /usr/lib/syslinux/modules/efi64/ldlinux.e64 /mnt/boot/EFI/BOOT/
- cp /mnt/boot/syslinux.cfg /mnt/boot/EFI/BOOT/syslinux.cfg
- cp /mnt/root/boot/vmlinuz /mnt/boot/EFI/BOOT/
- cp /mnt/root/boot/initrd.img /mnt/boot/EFI/BOOT/
# now we create network config, make sure to change: interface name, IP, gateway
- touch /mnt/root/etc/systemd/network/00-wired.network
- (echo [Match]; echo Name=enp0s10f0; echo [Network]; echo Address=10.10.2.101/24; echo Gateway=10.10.2.99; echo DNS=8.8.8.8) > /mnt/root/etc/systemd/network/00-wired.network
# and enable networkd service so it runs on first boot already
- chroot /mnt/root sh -c "systemctl enable systemd-networkd.service"
# this is optional, but cleans 100+MB from our chroot partition
- chroot /mnt/root sh -c "apt-get clean"
# and finally, I leave this uncommented sometimes, to allow me to do anything in interactive bash shell before final reboot (or to just pause and wait for you if you took coffee and don't want your system to reboot unattended)
# - bash -c "exec bash"
# unmount partitions
- umount /mnt/boot
- umount /mnt/root
# and reboot!
- reboot
# after reboot login with your user (ubuntu/ubuntu in this script) and complete installation and/or configuration
# you can also connect using ssh to this machine, sudo, and install or configure whatever you wish! Congrats!
这个带有注释的脚本也可以直接使用,复制/粘贴即可。与之前一样,请检查您的用户名/密码和网络(接口名称、IP、网关等)。
请注意,它
不会安装Xubuntu,我已经将其注释掉。
这将引导到小于400MB的/(root)分区(还有一个用于引导的小分区,设置为200MB但可以更小,两个BIOS+EFI文件需要大约58MB)。
添加Xubuntu Core大约需要1.8GB(apt说的)。安装了
xubuntu-core
后,分区使用量增加到2.4GB,运行
apt clean
后降低到1.9GB,所以实际上是在Ubuntu Base的基础上额外增加了1.5GB的内容。
如果您遇到任何问题,请告诉我以便为每个人修复说明。
所有这些都经过了Ubuntu
20.04.1的测试(PXE服务器、Live安装ISO映像和Base映像),并且在20.10 "client"上进行了相同或类似的测试,并且对于任何未来的发布版本来说应该都是相同或类似的,只要您根据需要更改文件名(例如在curl和tar命令中以及apt安装Linux镜像的位置)。除非Canonical改变了某些东西,否则其余部分应该是相同的。
测试环境(包括PXE服务器、服务和测试客户端)均在Hyper-V虚拟机上完成。
注意:最初是为了传统BIOS安装,现在已添加UEFI支持。UEFI的更改很小,syslinux安装已扩展。
文档:
Ubuntu Base wiki(已过时)
Ubuntu Base repository
Ubuntu Base Installation Example Guide(严重过时,GUI工具)
编辑-2020-11-22:修改了Ubuntu 20.10的脚本。如果您查看修订历史记录,早期脚本适用于20.04.1,但我同时也进行了一些调整,请进行比较以查看更改。
编辑 - 2020-12-06:修改了脚本,使其更简单直接,具有更好的网络功能,并添加了EFI引导程序支持。我没有为20.04编写新的脚本,只是字面上只需要更改curl
、tar
和apt-get install
中的linux-image-x.x.x-xx-generic
这几行代码,您可以查看此答案的修订版本,最初的版本包含了20.04.1 Ubuntu Base的路径和名称。
sudo apt install tasksel
和sudo tasksel install xubuntu-core
命令添加xfce桌面环境,但这可能会产生意想不到的后果。这里有一个详细的教程:https://linuxconfig.org/install-xfce-xubuntu-desktop-on-ubuntu-20-04-focal-fossa-linux - Mark Kirby