使用autoinstall无人值守安装Ubuntu
先贴配置文件 user-data
#cloud-config
autoinstall:
version: 1
locale: en_US.UTF-8
keyboard:
layout: us
timezone: Asia/Shanghai
source:
id: ubuntu-server-minimal
network:
version: 2
ethernets:
eth0:
addresses:
- 192.168.44.55/24
nameservers:
addresses:
- 61.139.2.69
- 233.5.5.5
routes:
- to: default
via: 192.168.44.2
apt:
mirror-selection:
primary:
- uri: "https://mirrors.tuna.tsinghua.edu.cn/ubuntu"
packages:
- vim
- wget
- lrzsz
- bash-completion
- tree
- lsof
- rsync
- unzip
- net-tools
- iputils-ping
- cron
- dnsutils
- file
- less
- time
- fontconfig
identity:
username: ops
hostname: localhost
# openssl passwd -6
password: '$y$j9T$TCSwfiAMof47jXw52cXqx1$S7GU2tyWCKdfuTR91ntxF/sswnIk1lYA2b0Ugg3tKiC'
ssh:
install-server: true
allow-pw: true
authorized-keys: []
shutdown: reboot
# Install additional packages on the target system and run some custom scripts.
late-commands:
- curtin in-target -- apt remove --autoremove -y snapd
- curtin in-target -- sed -i '/^RemainAfterExit=yes$/a TimeoutStartSec=3s' /lib/systemd/system/systemd-networkd-wait-online.service
- curtin in-target -- sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config
- curtin in-target -- sed -i '/^#*DNS=/c\DNS=223.6.6.6' /etc/systemd/resolved.conf
- curtin in-target -- sed -i '/^#*DNSStubListener=/c\DNSStubListener=no' /etc/systemd/resolved.conf
- curtin in-target -- ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
- curtin in-target -- rm -f /etc/update-motd.d/*
- curtin in-target -- sed -i 's/GRUB_CMDLINE_LINUX=""/GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"/' /etc/default/grub
- curtin in-target -- update-grub
storage:
swap:
size: 0
layout:
name: direct
config:
- path: /dev/sda
ptable: gpt
wipe: superblock-recursive
type: disk
id: disk-sda
- device: disk-sda
size: 100%FREE
type: partition
id: partition-0
- fstype: ext4
volume: partition-0
type: format
id: format-0
- path: /
device: format-0
type: mount
id: mount-0
安装准备:
创建一个http服务: 里面文件包含3个。
meta-data user-data vendor-data
meta-data 和 vendor-data 都为空即可。
user-data 内容为上面内容
安装
如果我们想执行完全自动化的安装,或者在安装 Ubuntu 服务器时使用 autoinstall 配置,我们必须使用 cloud-init,并将配置用作 cloud-config 数据文件:这就是 autoinstall 文件最开头的 “#cloud-config” 注释的用途。
为了让 cloud-init 在启动时读取文件,我们需要将其重命名为 user-data,并在同一目录中创建一个名为 meta-data 的空文件。我们按 “e” 修改实时 ISO GRUB 菜单的第一个条目:
然后,我们修改以 “linux” 开头的行,在三个连字符之前添加以下字符串(转义分号很重要!
net.ifnames=0 autoinstall ds=nocloud-net\;s=http://192.168.44.77:8000
然后,我们修改以 “linux” 开头的行,在三个连字符之前添加以下字符串(转义分号很重要!
此时要开始安装,我们只需按 Ctrl-x。
参考:
https://cn.linux-console.net/?p=33717
https://canonical-subiquity.readthedocs-hosted.com/en/latest/reference/autoinstall-reference.html#username