Ubuntu 中使用 KVM 快速创建虚拟机

介绍 ubuntu + KVM 搭配使用来创建虚拟机。


开始

# 自定义 Ubuntu iso
echoxu@echoxu-Standard-PC-Q35-ICH9-2009:~/files$ ./ubuntu-autoinstall-generator.sh -a -u user-data -m meta-data -r ubuntu-20.04.4-live-server-amd64.iso -d ubuntu-autoinstall-example.iso
[2022-05-23 09:50:01] 👶 Starting up...
[2022-05-23 09:50:01] 🔎 Checking for current release...
[2022-05-23 09:50:03] 💿 Current release is 20.04.4
[2022-05-23 09:50:03] 📁 Created temporary working directory /tmp/tmp.3nQKZh2WvC
[2022-05-23 09:50:03] 🔎 Checking for required utilities...
[2022-05-23 09:50:03] 👍 All required utilities are installed.
[2022-05-23 09:50:03] ☑️ Using existing /home/echoxu/files/ubuntu-20.04.4-live-server-amd64.iso file.
[2022-05-23 09:50:03] 🌎 Downloading SHA256SUMS & SHA256SUMS.gpg files...
[2022-05-23 09:50:05] 🌎 Downloading and saving Ubuntu signing key...
[2022-05-23 09:50:07] 👍 Downloaded and saved to /home/echoxu/files/843938DF228D22F7B3742BC0D94AA3F0EFE21092.keyring
[2022-05-23 09:50:07] 🔐 Verifying /home/echoxu/files/ubuntu-20.04.4-live-server-amd64.iso integrity and authenticity...
[2022-05-23 09:50:16] 👍 Verification succeeded.
[2022-05-23 09:50:16] 🔧 Extracting ISO image...
[2022-05-23 09:50:21] 👍 Extracted to /tmp/tmp.3nQKZh2WvC
[2022-05-23 09:50:21] 🧩 Adding autoinstall parameter to kernel command line...
[2022-05-23 09:50:21] 👍 Added parameter to UEFI and BIOS kernel command lines.
[2022-05-23 09:50:21] 🧩 Adding user-data and meta-data files...
[2022-05-23 09:50:21] 👍 Added data and configured kernel command line.
[2022-05-23 09:50:21] 👷 Updating /tmp/tmp.3nQKZh2WvC/md5sum.txt with hashes of modified files...
[2022-05-23 09:50:21] 👍 Updated hashes.
[2022-05-23 09:50:21] 📦 Repackaging extracted files into an ISO image...
[2022-05-23 09:50:32] 👍 Repackaged into /home/echoxu/files/ubuntu-autoinstall-2022-05-23.iso
[2022-05-23 09:50:32] ✅ Completed.
[2022-05-23 09:50:32] 🚽 Deleted temporary working directory /tmp/tmp.3nQKZh2WvC


# 创建 KVM 虚拟机并自动安装,前提是要准备好 user-data 文件

sudo mount -r ~/files/ubuntu-20.04.4-live-server-amd64.iso /mnt


cd ~/www
python3 -m http.server 3003

truncate -s 10G image.img


kvm -no-reboot -m 1024 \
    -drive file=image.img,format=raw,cache=none,if=virtio \
    -cdrom ~/files/ubuntu-20.04.4-live-server-amd64.iso \
    -kernel /mnt/casper/vmlinuz \
    -initrd /mnt/casper/initrd \
    -append 'autoinstall ds=nocloud-net;s=http://_gateway:3003/'


kvm -no-reboot -m 1024 \
    -drive file=image.img,format=raw,cache=none,if=virtio

上次更新:
贡献者: iEchoxu