From 8cab3b13ea799f2a545de937633011688f24ef3a Mon Sep 17 00:00:00 2001 From: Heng Lu Date: Wed, 4 Feb 2026 11:57:40 +0800 Subject: [PATCH] =?UTF-8?q?dd=20=E5=BC=95=E5=85=A5cloud-data=20=E6=8F=90?= =?UTF-8?q?=E4=BE=9B=20nocloud=E8=87=AA=E5=8A=A8=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- reinstall.sh | 21 +++++++++++++++++++++ trans.sh | 44 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/reinstall.sh b/reinstall.sh index a0d5518dd..1c77d6aa7 100644 --- a/reinstall.sh +++ b/reinstall.sh @@ -3746,6 +3746,22 @@ This script is outdated, please download reinstall.sh again. cat "$frpc_config" >$initrd_dir/configs/frpc.toml fi + # 收集 cloud-data 打包进 initrd + if [ -n "$cloud_data" ]; then + mkdir -p $initrd_dir/configs/cloud-data + if [ -d "$cloud_data" ]; then + # 本地目录:直接复制 + cp "$cloud_data"/* $initrd_dir/configs/cloud-data/ + else + # URL:在 host 下载 + for f in user-data meta-data network-config; do + curl -fsSL "$cloud_data/$f" -o "$initrd_dir/configs/cloud-data/$f" 2>/dev/null || true + done + fi + # 校验:至少要有 user-data + [ -f $initrd_dir/configs/cloud-data/user-data ] || error_and_exit "--cloud-data must contain user-data" + fi + if is_distro_like_debian $nextos_distro; then mod_initrd_debian_kali else @@ -3912,6 +3928,7 @@ for o in ci installer debug minimal allow-ping force-cn help \ image-name: \ boot-wim: \ img: \ + cloud-data: \ lang: \ passwd: password: \ ssh-port: \ @@ -4147,6 +4164,10 @@ EOF img=$2 shift 2 ;; + --cloud-data) + cloud_data=$2 + shift 2 + ;; --iso) iso=$2 shift 2 diff --git a/trans.sh b/trans.sh index fb8ff2cb8..f289a26b2 100644 --- a/trans.sh +++ b/trans.sh @@ -3793,14 +3793,40 @@ EOF rm -f $os_dir/swapfile } +setup_nocloud() { + os_dir=$1 + info "Setup NoCloud" + + # 1. 配置 NoCloud-only datasource + mkdir -p "$os_dir/etc/cloud/cloud.cfg.d" + cat > "$os_dir/etc/cloud/cloud.cfg.d/99-datasource.cfg" << 'EOF' +datasource_list: [ NoCloud, None ] +datasource: + NoCloud: + seedfrom: /var/lib/cloud/seed/nocloud/ + fs_label: null +EOF + + # 2. 复制 seed 文件(已在 host 上准备好,打包在 initrd 中) + mkdir -p "$os_dir/var/lib/cloud/seed/nocloud" + cp /configs/cloud-data/* "$os_dir/var/lib/cloud/seed/nocloud/" + + # 3. 确保 cloud-init 没有被禁用 + rm -f "$os_dir/etc/cloud/cloud-init.disabled" + + # 4. 清除 cloud-init 旧状态,确保首次启动重新执行 + rm -rf "$os_dir/var/lib/cloud/instance" + rm -rf "$os_dir/var/lib/cloud/instances" +} + modify_os_on_disk() { only_process=$1 info "Modify disk if is $only_process" update_part - # dd linux 的时候不用修改硬盘内容 - if [ "$distro" = "dd" ] && ! lsblk -f /dev/$xda | grep ntfs; then + # dd linux 的时候不用修改硬盘内容(nocloud 模式除外) + if [ "$distro" = "dd" ] && [ "$only_process" != "nocloud" ] && ! lsblk -f /dev/$xda | grep ntfs; then return fi @@ -3810,12 +3836,16 @@ modify_os_on_disk() { # btrfs挂载的是默认子卷,如果没有默认子卷,挂载的是根目录 # fedora 云镜像没有默认子卷,且系统在root子卷中 if mount -o ro /dev/$part /os; then - if [ "$only_process" = linux ]; then + if [ "$only_process" = linux ] || [ "$only_process" = nocloud ]; then if etc_dir=$({ ls -d /os/etc/ || ls -d /os/*/etc/; } 2>/dev/null); then os_dir=$(dirname $etc_dir) # 重新挂载为读写 mount -o remount,rw /os - modify_linux $os_dir + if [ "$only_process" = nocloud ]; then + setup_nocloud $os_dir + else + modify_linux $os_dir + fi return fi elif [ "$only_process" = windows ]; then @@ -7229,7 +7259,11 @@ trans() { # windows 扩容在 windows 下完成 resize_after_install_cloud_image fi - modify_os_on_disk windows + if [ -d /configs/cloud-data ]; then + modify_os_on_disk nocloud + else + modify_os_on_disk windows + fi ;; qemu) # dd qemu 不可能到这里,因为上面已处理 ;;