Skip to main content

KVM 运行并编辑iso镜像文件

· 2 min read

安装 KVM 依赖包

sudo apt -y install bridge-utils cpu-checker libvirt-clients libvirt-daemon qemu qemu-kvm

检查 KVM 可用性

kvm-ok

运行 Ubuntu 用例

准备工作

创建镜像文件

qemu-img create -f qcow2 ubuntu-guest.img 3G

配置默认项

新增配置文件

http/preseed.cfg中为ubuntu示例使用以下配置项

# Locale settings
d-i debian-installer/locale string en_US

# Keyboard settings
d-i console-setup/ask_detect boolean false
d-i console-setup/layoutcode string us
d-i keyboard-configuration/xkb-keymap select us

# Network settings
d-i netcfg/choose_interface select auto
d-i netcfg/get_hostname string LongPark
d-i netcfg/get_domain string unassigned-domain
d-i netcfg/disable_dhcp boolean false
d-i netcfg/dhcp_failed note
d-i netcfg/dhcp_options select Configure network manually

# Mirror settings
d-i mirror/country string manual
d-i mirror/http/hostname string archive.ubuntu.com
d-i mirror/http/directory string /ubuntu
d-i mirror/http/proxy string

# Clock and time zone settings
d-i clock-setup/utc boolean true
d-i time/zone string US/Eastern
d-i clock-setup/ntp boolean true

# Partitioning settings
d-i partman-auto/method string regular
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-md/device_remove_md boolean true
d-i partman-auto/choose_recipe select atomic
d-i partman/confirm_write_new_label boolean true
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true

# User setup
d-i passwd/root-login boolean false
d-i passwd/make-user boolean true
d-i passwd/user-fullname string LongPark User
d-i passwd/username string longpark
d-i passwd/user-password password longpark
d-i passwd/user-password-again password longpark
d-i user-setup/allow-password-weak boolean true
d-i user-setup/encrypt-home boolean false

# Package selection
d-i pkgsel/include string openssh-server
d-i pkgsel/upgrade select none

# Boot loader settings
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean true

# Finishing up the installation
d-i finish-install/reboot_in_progress note
d-i debian-installer/exit/reboot boolean true

# Late command
d-i preseed/late_command string wget -O /target/etc/apt/apt.conf.d/99force-ipv4 http://example.com/99force-ipv4; in-target apt-get update
为配置文件提供http下载服务
sudo python3 -m http.server 8000 --directory http

运行用例

sudo virt-install \
--name ubuntu-guest \
--os-variant ubuntu20.04 \
--vcpus 2 \
--ram 2048 \
--location http://ftp.ubuntu.com/ubuntu/dists/focal/main/installer-amd64/ \
--network bridge=virbr0,model=virtio \
--graphics none \
--disk path=ubuntu-guest.img,format=qcow2 \
--extra-args="console=ttyS0,115200n8 serial auto=true priority=critical url=http://${IPADDR}:8000/preseed.cfg"

删除用例

sudo virsh destroy ubuntu-guest
sudo virsh undefine ubuntu-guest

引用