iventor无人值守模式自动安装ubuntu22.04 live server lts

一、脚本内容

自动安装脚本如下(脚本需要放在同一路径):

脚本meta-data:

1
2
3
# iVentoy meta-data for Ubuntu autoinstall
# 此文件与 user-data 配对,iVentoy 会自动提供
instance-id: iventoy-ubuntu-22.04

脚本user-data_v2版(解决换源问题-成功自动安装):

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#cloud-config
# =============================================================================
# Ubuntu 22.04 Live Server 无人值守安装应答文件 (Subiquity autoinstall)
# 适用于 iVentoy 网络启动环境
# =============================================================================
# 放置路径: user/scripts/user-data
# iVentoy 会在客户端 PXE 引导时自动注入此文件
# =============================================================================

autoinstall:
version: 1

# ----- 交互模式:完全关闭,实现无人值守 -----
interactive-sections: []

# =========================================================================
# 1. 语言 / 区域 / 键盘
# =========================================================================
locale: en_US.UTF-8
keyboard:
layout: us
toggle: null
variant: ""

# =========================================================================
# 2. 时区
# =========================================================================
timezone: Asia/Shanghai

# =========================================================================
# 3. 网络 (使用 DHCP,iVentoy 已分配 IP)
# =========================================================================
network:
version: 2
ethernets:
all-eth:
match:
name: "e*"
dhcp4: true
dhcp6: false

# =========================================================================
# 4. APT 镜像源 (已修改为 阿里云镜像源)
# 注意:Autoinstall 里的 apt 需要按照 "primary" 和 "security" 结构写
# =========================================================================
apt:
preserve_sources_list: false
primary:
- arches: [amd64]
uri: https://mirrors.aliyun.com/ubuntu/
security:
- arches: [amd64]
uri: https://mirrors.aliyun.com/ubuntu/
# 只有 Ubuntu 的 main, universe, restricted, multiverse 默认组件。
# backports, updates 等通常会在安装后自动生成,如果一定要强制安装时添加,可加在下面:
sources:
aliyun-jammy-updates:
source: "deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse"
aliyun-jammy-backports:
source: "deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse"

# =========================================================================
# 5. 用户与身份
# =========================================================================
identity:
hostname: ubuntu-server
# 用户名
username: admin
# 密码哈希: "admin123" 的 SHA-512 crypt 值
password: "$6$gtU9oDdf0eJjCXCc$3tvK5AFbXnA4NduVMOqXBWFXm3g/9gO/zviSDhNOyrR8UWmqVevJgtrCwUT.QvYLyBpQrY9ipEEhytKAIa505."

# =========================================================================
# 6. SSH 服务 (安装后可通过 SSH 登录)
# =========================================================================
ssh:
install-server: true
allow-pw: true
authorized-keys: []

# =========================================================================
# 7. 存储分区 (自动使用整盘,兼容 BIOS + UEFI)
# direct layout: Subiquity 自动创建合理分区
# EFI 系统: ESP 分区 (UEFI) 或 bios_grub (BIOS)
# 根分区: / (ext4, 剩余全部空间)
# =========================================================================

storage:
layout:
name: direct
# match: # 如需指定磁盘,取消注释:
# path: /dev/sda # 固定磁盘路径
# size: largest # 或选择最大磁盘

# =========================================================================
# 8. 软件包安装
# =========================================================================
packages:
- openssh-server
- vim
- curl
- wget
- net-tools
- htop
- git
- bash-completion
- ca-certificates
- cloud-init

# =========================================================================
# 9. 安装后执行脚本 (late-commands)
# 在目标系统 chroot 环境中运行
# =========================================================================
late-commands:
# 禁用自动更新 (可选)
- sed -i 's/^Prompt=.*/Prompt=never/' /target/etc/update-manager/release-upgrades
- sed -i 's/^\(APT::Periodic::Update-Package-Lists\).*/\1 "1";/' /target/etc/apt/apt.conf.d/20auto-upgrades || true
- sed -i 's/^\(APT::Periodic::Unattended-Upgrade\).*/\1 "0";/' /target/etc/apt/apt.conf.d/20auto-upgrades || true

# 配置 bash 历史记录显示时间戳
- echo 'export HISTTIMEFORMAT="%F %T "' >> /target/etc/skel/.bashrc

# 安装完成后自动重启
- 'sh -c "sleep 2 && /sbin/reboot" &'

# =========================================================================
# 10. 错误处理与报告
# =========================================================================
error-commands:
- 'echo "AUTOINSTALL FAILED at $(date)" >> /var/log/installer/autoinstall-error.log'
- 'cat /var/log/installer/subiquity-debug.log 2>/dev/null >> /var/log/installer/autoinstall-error.log || true'

脚本user-data_v1版(换源有点问题):

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#cloud-config
# =============================================================================
# Ubuntu 22.04 Live Server 无人值守安装应答文件 (Subiquity autoinstall)
# 适用于 iVentoy 网络启动环境
# =============================================================================
# 放置路径: user/scripts/user-data
# iVentoy 会在客户端 PXE 引导时自动注入此文件
# =============================================================================

autoinstall:
version: 1

# ----- 交互模式:完全关闭,实现无人值守 -----
interactive-sections: []

# =========================================================================
# 1. 语言 / 区域 / 键盘
# =========================================================================
locale: en_US.UTF-8
keyboard:
layout: us
toggle: null
variant: ""

# =========================================================================
# 2. 时区
# =========================================================================
timezone: Asia/Shanghai

# =========================================================================
# 3. 网络 (使用 DHCP,iVentoy 已分配 IP)
# =========================================================================
network:
version: 2
ethernets:
all-eth:
match:
name: "e*"
dhcp4: true
dhcp6: false

# =========================================================================
# 4. APT 镜像源 (指向 iVentoy 内置 HTTP 镜像加速)
# 变量 $$VT_SERVER_IP$$ / $$VT_HTTP_PORT$$ / $$VT_IMG_PMD5$$
# 由 iVentoy 在启动时自动替换为实际值
# =========================================================================
apt:
preserve_sources_list: false
primary:
- arches: [amd64, i386]
uri: http://$$VT_SERVER_IP$$:$$VT_HTTP_PORT$$/eiso/pmd5/$$VT_IMG_PMD5$$
security:
- arches: [amd64, i386]
uri: http://security.ubuntu.com/ubuntu
fallback: http://archive.ubuntu.com/ubuntu

# =========================================================================
# 5. 用户与身份
# =========================================================================
identity:
hostname: ubuntu-server
# 用户名
username: admin
# 密码哈希: "admin123" 的 SHA-512 crypt 值
# ---------- 自行生成新密码哈希 ----------
# Linux: mkpasswd -m sha-512
# Python: python3 -c "import crypt; print(crypt.crypt('YOUR_PASS', crypt.mksalt(crypt.METHOD_SHA512)))"
# 在线: openssl passwd -6
# ----------------------------------------
password: "$6$gtU9oDdf0eJjCXCc$3tvK5AFbXnA4NduVMOqXBWFXm3g/9gO/zviSDhNOyrR8UWmqVevJgtrCwUT.QvYLyBpQrY9ipEEhytKAIa505."

# =========================================================================
# 6. SSH 服务 (安装后可通过 SSH 登录)
# =========================================================================
ssh:
install-server: true
allow-pw: true
authorized-keys: []

# =========================================================================
# 7. 存储分区 (自动使用整盘,兼容 BIOS + UEFI)
# direct layout: Subiquity 自动创建合理分区
# EFI 系统: ESP 分区 (UEFI) 或 bios_grub (BIOS)
# 根分区: / (ext4, 剩余全部空间)
# =========================================================================
storage:
layout:
name: direct
# match: # 如需指定磁盘,取消注释:
# path: /dev/sda # 固定磁盘路径
# size: largest # 或选择最大磁盘

# =========================================================================
# 8. 软件包安装
# =========================================================================
packages:
- openssh-server
- vim
- curl
- wget
- net-tools
- htop
- git
- bash-completion
- ca-certificates
- cloud-init

# =========================================================================
# 9. 安装后执行脚本 (late-commands)
# 在目标系统 chroot 环境中运行
# =========================================================================
late-commands:
# 禁用自动更新 (可选)
- sed -i 's/^Prompt=.*/Prompt=never/' /target/etc/update-manager/release-upgrades
- sed -i 's/^\(APT::Periodic::Update-Package-Lists\).*/\1 "1";/' /target/etc/apt/apt.conf.d/20auto-upgrades || true
- sed -i 's/^\(APT::Periodic::Unattended-Upgrade\).*/\1 "0";/' /target/etc/apt/apt.conf.d/20auto-upgrades || true

# 配置 bash 历史记录显示时间戳
- echo 'export HISTTIMEFORMAT="%F %T "' >> /target/etc/skel/.bashrc

# 允许 root 通过 SSH 密码登录 (按需启用)
# - sed -i 's/^#PermitRootLogin.*/PermitRootLogin yes/' /target/etc/ssh/sshd_config

# 安装完成后自动重启
- 'sh -c "sleep 2 && /sbin/reboot" &'

# =========================================================================
# 10. 错误处理与报告
# =========================================================================
error-commands:
- 'echo "AUTOINSTALL FAILED at $(date)" >> /var/log/installer/autoinstall-error.log'
- 'cat /var/log/installer/subiquity-debug.log 2>/dev/null >> /var/log/installer/autoinstall-error.log || true'

二、使用(iventoy版本-iventoy-1.0.36)

按转完成后记得修改bios启动项把启动方式更换为本地磁盘启动

三、Ubuntu 22.04 无人值守安装应答文件说明文档

概述

本套应答文件用于 *iVentoy 网络启动环境下,实现 Ubuntu 22.04 Live Server*全自动无人值守安装。客户端通过 PXE 网络引导后,无需任何人工干预即可完成整个操作系统安装。

文件清单

文件 路径 作用
user-data user/scripts/user-data 核心安装应答文件,定义安装全过程的所有参数
meta-data user/scripts/meta-data 云实例标识文件,Subiquity 安装器要求存在,内容极简

文件系统中的绝对路径(供 iVentoy 界面引用):

1
E:/iventoy-1.0.38-win64-free/iventoy-1.0.38/user/scripts/user-data

工作原理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
┌──────────────┐     PXE/DHCP      ┌──────────────┐     HTTP(16000)     ┌──────────────┐
│ 客户端虚拟机 │ ◄──────────────► │ iVentoy │ ◄─────────────────► │ user-data │
│ (空磁盘) │ TFTP/iPXE │ 服务器 │ 读取 + 注入 │ meta-data │
└──────────────┘ └──────────────┘ └──────────────┘
│ │
│ 1. DHCP 获取 IP │
│ 2. TFTP 下载 iPXE 引导程序 │
│ 3. HTTP 下载 GRUB2 + 内核 │
│ │
│ 4. 内核启动,cmdline 携带: │
│ autoinstall ds=nocloud-net │
│ s=http://192.168.253.1: │
│ 16000/scripts/ │
│ │
▼ ▼
Subiquity 安装器 ────────────► 拉取 user-data + meta-data


解析 YAML 配置 → 执行无人值守安装 → 自动重启

关键机制:iVentoy 解析 ISO 中的 GRUB 配置后,自动在 Linux 内核启动参数中注入 autoinstall ds=nocloud-net;s=...,将 Subiquity 安装器的配置源指向 iVentoy 的内置 HTTP 服务器,服务器再把 user-data 提供给安装器。


user-data 各字段详解

1. 基础声明

1
2
3
4
#cloud-config
autoinstall:
version: 1
interactive-sections: []
  • #cloud-config:声明文件类型为 cloud-init 配置(Subiquity 兼容格式)
  • version: 1:autoinstall 规范版本
  • interactive-sections: []空列表 = 所有环节均不交互,这是实现真正无人值守的关键

2. 语言与键盘

1
2
3
locale: en_US.UTF-8
keyboard:
layout: us

跳过语言选择界面,预设美式英语 + US 键盘布局。

3. 时区

1
timezone: Asia/Shanghai

设定系统时区为东八区(中国标准时间 UTC+8),跳过时区选择界面。

4. 网络

1
2
3
4
5
6
7
network:
version: 2
ethernets:
all-eth:
match:
name: "e*"
dhcp4: true

自动匹配所有以太网卡并启用 DHCP(iVentoy 内置 DHCP 服务器已为客户端分配 IP),跳过网络配置界面。

5. APT 软件源

1
2
3
4
5
apt:
primary:
- arches: [amd64, i386]
uri: http://$$VT_SERVER_IP$$:$$VT_HTTP_PORT$$/eiso/pmd5/$$VT_IMG_PMD5$$
fallback: http://archive.ubuntu.com/ubuntu
  • $$VT_*$$iVentoy 动态变量,启动时自动替换为实际值:
    • $$VT_SERVER_IP$$ → iVentoy 服务器 IP(如 192.168.253.1
    • $$VT_HTTP_PORT$$ → HTTP 端口(如 16000
    • $$VT_IMG_PMD5$$ → ISO 镜像的哈希标识
  • 优先从 iVentoy 内置镜像缓存获取软件包(速度快)
  • 回退到 Ubuntu 官方源

6. 用户与密码

1
2
3
4
identity:
hostname: ubuntu-server
username: admin
password: "$6$rounds=4096$..."
参数 说明
hostname ubuntu-server 安装后的主机名
username admin 登录用户名(跳过用户创建向导)
password SHA-512 哈希 加密后的密码,不存明文(由 openssl passwd -6生成)

默认登录凭据:

项目
用户名 admin
密码 admin123

**⚠️ 生产环境请在首次登录后立即用 **passwd 命令更换密码。

如需自定义密码,用以下方法生成哈希后替换 password: 字段:

1
2
3
4
5
6
7
8
# OpenSSL(Windows / Linux 通用,推荐)
openssl passwd -6 "你的密码"

# Linux / WSL
mkpasswd -m sha-512 "你的密码"

# Python(跨平台)
python3 -c "import crypt; print(crypt.crypt('你的密码', crypt.mksalt(crypt.METHOD_SHA512)))"

7. SSH 服务

1
2
3
ssh:
install-server: true
allow-pw: true

安装完成后自动启用 SSH,允许密码登录,方便远程管理。

8. 磁盘分区

1
2
3
storage:
layout:
name: direct

direct 模式告诉 Subiquity 自动使用整块磁盘进行分区:

固件类型 自动创建的分区
UEFI ESP 分区(FAT32, 512MB) +/(ext4, 剩余全部)
BIOS bios_grub(1MB) +/(ext4, 剩余全部)

⚠️ *警告direct 布局会*清空目标磁盘全部数据,不可恢复。

如需指定特定磁盘(多盘环境),可改为:

1
2
3
4
5
6
storage:
layout:
name: direct
match:
size: largest # 选最大磁盘
# path: /dev/sda # 或者直接指定设备

9. 额外软件包

1
2
3
4
5
6
7
8
9
10
packages:
- openssh-server # SSH 远程登录
- vim # 文本编辑器
- curl wget # 网络工具
- net-tools # ifconfig 等传统网络命令
- htop # 系统监控
- git # 版本控制
- bash-completion # Bash 命令补全
- ca-certificates # CA 证书
- cloud-init # 云初始化工具

安装阶段自动安装这些包,无需事后手动安装。

10. 安装后脚本(late-commands)

1
2
3
4
5
6
7
8
9
late-commands:
# 关闭自动更新提示
- sed -i 's/^Prompt=.*/Prompt=never/' /target/etc/update-manager/release-upgrades
# 禁用无人值守安全更新
- sed -i 's/^\(APT::Periodic::Unattended-Upgrade\).*/\1 "0";/' /target/etc/apt/apt.conf.d/20auto-upgrades
# Bash 历史记录显示时间戳
- echo 'export HISTTIMEFORMAT="%F %T "' >> /target/etc/skel/.bashrc
# 安装完成后 2 秒自动重启
- 'sh -c "sleep 2 && /sbin/reboot" &'

注意late-commands 运行在目标系统的 chroot 环境中(路径前缀为 /target),而非安装器自身环境。

11. 错误处理

1
2
3
error-commands:
- 'echo "AUTOINSTALL FAILED at $(date)" >> /var/log/installer/autoinstall-error.log'
- 'cat /var/log/installer/subiquity-debug.log >> ...'

**如果安装过程出现错误,自动收集调试日志写入 **autoinstall-error.log,方便事后排查。


定制修改指南

改密码

  1. 生成新密码哈希(见上方第 6 节)
  2. **替换 **user-data 文件中 identity.password 的值

改时区

**查找 **timezone 字段,替换为你的时区,例如:

  • America/New_York — 美东
  • Europe/London — 伦敦
  • Asia/Tokyo — 东京

改磁盘分区方案

**将 **storage.layout.name: direct 换为 lvm 使用 LVM:

1
2
3
storage:
layout:
name: lvm

添加更多软件包

**在 **packages: 列表下追加,例如:

1
2
3
4
5
packages:
- ...
- nginx
- docker.io
- python3-pip

验证安装结果

安装完成重启后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 1. 检查主机名
hostnamectl

# 2. 检查时区
timedatectl

# 3. 检查磁盘分区
lsblk

# 4. 检查用户
id admin

# 5. 检查 SSH 状态
systemctl status ssh

# 6. 检查已安装的软件包
dpkg -l | grep -E "vim|curl|htop|git"

故障排查

症状 可能原因 解决方法
安装器仍然弹出交互界面 user-data语法错误 yamllint user-data检查 YAML 语法
APT 源无法访问 iVentoy HTTP 服务未启动 检查 iVentoy 服务状态,确认端口 16000
磁盘分区失败 目标磁盘太小 (< 5GB) 分配 ≥ 20GB 虚拟磁盘
密码登录无效 哈希生成错误 重新用 mkpasswd -m sha-512生成
安装日志为空 内核 cmdline 未注入 确认 iVentoy 将该 ISO 识别为 Linux 类型
UEFI 下无网络引导选项 网卡类型不兼容 .vmx中改为 e1000e并添加 uefi.forcePxe = "TRUE"