Ubuntu 22.04离线自动安装流程优化及ubuntu-desktop包安装提速咨询
Hey there! I get that waiting 30+ minutes for an offline autoinstall is super frustrating—let’s walk through how to optimize this process based on your setup and configuration files.
First, Let’s Diagnose the Slowdown
Your current approach copies the APT cache from your minimal media to the target system, but there are a few likely bottlenecks here:
- The cached packages might not include all dependencies for
ubuntu-desktop, forcing APT to waste time retrying failed network requests (even offline, it’ll keep checking for missing packages). - Using default
apt-get installflags without tuning for offline environments or hardware resources leads to slow unpacking and configuration. - Your VM’s resource allocation might be limiting parallel processing during installation.
Step-by-Step Optimizations
1. Fix Offline Package Preparation (Replace Cache Copy with Local Repository)
Instead of relying on a copied APT cache, create a complete offline package repository with all ubuntu-desktop dependencies upfront. This ensures APT has every file it needs locally, no network checks required.
Update your chroot_script.sh (Run on a network-connected Ubuntu 22.04 machine):
#!/bin/bash # Run this on a networked Ubuntu 22.04 amd64 system to download all required packages mkdir -p /tmp/offline-packages # Mount your Ubuntu 22.04 minimal ISO as a base source mount -o loop ubuntu-22.04-minimal.iso /mnt # Configure APT to use the ISO + network to fetch all dependencies cat > /tmp/sources.list << EOF deb [trusted=yes] file:///mnt jammy main restricted deb http://archive.ubuntu.com/ubuntu jammy universe multiverse EOF # Download ubuntu-desktop and ALL dependencies to your offline directory apt-get -o Dir::Etc::SourcesList=/tmp/sources.list update apt-get -y install --download-only -o Dir::Cache::Archives=/tmp/offline-packages ubuntu-desktop # Cleanup umount /mnt
After running this, copy the /tmp/offline-packages folder to your offline media (the same drive with your autoinstall files).
2. Optimize Your user-data Late Commands
Update your autoinstall config to use the local offline repository instead of copying cache. This eliminates APT’s network retry delays and ensures all packages are available locally:
#cloud-config autoinstall: version: 1 locale: en_US.UTF-8 keyboard: layout: us identity: hostname: user username: user password: crypted_password late-commands: # Create mount point for offline packages - mkdir -p /target/mnt/offline # Mount your offline media's packages directory (adjust path to match your setup) - mount /media/minimal/offline-packages /target/mnt/offline # Add local repository to APT sources - echo 'deb [trusted=yes] file:///mnt/offline ./' > /target/etc/apt/sources.list.d/local-offline.list # Refresh APT to recognize the local repo - curtin in-target --target /target -- apt-get update # Install ubuntu-desktop with offline-friendly flags - curtin in-target --target /target -- apt-get -yy --no-install-recommends --no-upgrade install ubuntu-desktop # Cleanup - curtin in-target --target /target -- umount /mnt/offline - rm /target/etc/apt/sources.list.d/local-offline.list
--no-install-recommends: Skips non-essential recommended packages (cuts down on installation time; remove this if you need the full desktop suite).--no-upgrade: Prevents APT from trying to update packages (useless offline, saves time).
3. Tune Your VirtualBox VM for Speed
Given your host hardware (8GB RAM, 256GB SSD) and VM specs, these tweaks will boost installation speed:
- Increase CPU Cores: In VirtualBox, go to VM Settings > System > Processor, and allocate 2–4 cores (your host can handle this easily). APT and dpkg will use parallel processing to unpack/configure packages faster.
- Enable Host I/O Cache: Go to VM Settings > Storage > Controller: SATA, check "Use Host I/O Cache" to leverage your host’s SSD speed.
- Move VM Disk to SSD: Ensure your VirtualBox virtual disk file is stored on your host’s SSD (not a slow HDD).
- Optimize dpkg Parallelism: Add this line to your
late-commandsbefore installing packages to let dpkg use multiple cores:
Adjust- curtin in-target --target /target -- echo 'jobs=2' > /etc/dpkg/dpkg.cfg.d/02paralleljobsto match the number of CPU cores you allocated (e.g.,jobs=4for 4 cores).
Final Checks
- Double-check that all
.debpackages in youroffline-packagesfolder are intact (no corrupted files). - Verify your offline media is mounted correctly during autoinstall (the path
/media/minimalshould match your setup—adjust if needed).
Give these changes a shot, and you should see your installation time drop significantly. Let me know if you run into any hiccups!
备注:内容来源于stack exchange,提问作者rain89

