Phase 5: KVM Virtual Machines
Bài 5.1: Tạo VM cơ bản — qcow2, virtio, cloud-init
Lý thuyết cốt lõi
Disk format:
| Format | Thin | Snapshot | Performance | Use case |
|---|---|---|---|---|
| raw | ✅ (LVM-thin) | Storage-level | Nhanh nhất | Production high-perf |
| qcow2 | ✅ | Built-in | Tốt | Default — flexible |
| vmdk | - | - | OK | Migrate từ VMware |
virtio drivers: luôn dùng (performance cao hơn IDE/SATA 2–5×):
virtio-blk/virtio-scsi: diskvirtio-net: NICvirtio-balloon: memory ballooningvirtio-rng: entropy source
Cloud-init: inject config (IP, user, SSH key) vào VM khi boot lần đầu.
Bài tập thực hành
Tạo VM Ubuntu 24.04 từ cloud image:
# 1. Download cloud image
cd /var/lib/vz/template/iso
wget https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-amd64.img
# 2. Tạo VM
qm create 9000 --name ubuntu-2404-tmpl --memory 2048 --cores 2 \
--net0 virtio,bridge=vmbr1,tag=10 \
--scsihw virtio-scsi-single \
--ostype l26 \
--agent enabled=1
# 3. Import disk
qm importdisk 9000 ubuntu-24.04-server-cloudimg-amd64.img local-zfs
qm set 9000 --scsi0 local-zfs:vm-9000-disk-0,discard=on,ssd=1
# 4. Cloud-init drive
qm set 9000 --ide2 local-zfs:cloudinit
qm set 9000 --boot order=scsi0
# 5. Serial console (cloud image không có VNC)
qm set 9000 --serial0 socket --vga serial0
# 6. Cloud-init config
qm set 9000 --ciuser ubuntu --cipassword 'SecureP@ss123'
qm set 9000 --ipconfig0 ip=10.0.10.50/24,gw=10.0.10.1
qm set 9000 --sshkeys ~/.ssh/authorized_keys
qm set 9000 --nameserver 8.8.8.8
# 7. Convert thành template
qm template 9000
# 8. Clone ra VM production
qm clone 9000 100 --name web01 --full --storage ceph-rbd
qm start 100
Kết quả đầu ra
$ qm list
VMID NAME STATUS MEM(MB) BOOTDISK(GB) PID
100 web01 running 2048 10.0 12345
9000 ubuntu-2404-tmpl stopped 2048 10.0 -
$ qm config 100
agent: 1
boot: order=scsi0
cores: 2
memory: 2048
name: web01
net0: virtio=XX:XX:XX:XX,bridge=vmbr1,tag=10
scsi0: ceph-rbd:vm-100-disk-0,discard=on,ssd=1,size=10G
ostype: l26
Troubleshooting
- Không SSH được sau khi tạo từ cloud-init: check
qm cloudinit dump 100 user, verify SSH key - IP không gán đúng:
qm cloudinit update 100rồi reboot VM - Performance disk chậm: thêm
discard=on,ssd=1, chọnaio=io_uring(mặc định VE 9)
Ứng dụng thực tế
Tạo template Ubuntu cho đội dev → mỗi developer tự qm clone → có VM riêng trong 30 giây.
Bài 5.2: Windows 11 VM với vTPM + Secure Boot (VE 9.1)
Lý thuyết cốt lõi
Windows 11 yêu cầu:
- UEFI (không legacy BIOS)
- TPM 2.0
- Secure Boot
- Minimum 4 GB RAM, 2 vCPU
VE 9.1 hỗ trợ vTPM trên qcow2 + snapshot đầy đủ (trước phải dùng raw disk).
Bài tập thực hành
# 1. Tạo VM Windows 11
qm create 101 --name win11-01 \
--memory 8192 --cores 4 --cpu host \
--machine q35 --bios ovmf \
--scsihw virtio-scsi-single \
--net0 virtio,bridge=vmbr1,tag=10 \
--agent enabled=1 \
--ostype win11
# 2. Add EFI disk + vTPM
qm set 101 --efidisk0 ceph-rbd:1,efitype=4m,pre-enrolled-keys=1
qm set 101 --tpmstate0 ceph-rbd:1,version=v2.0
# 3. Add main disk + ISO
qm set 101 --scsi0 ceph-rbd:60,discard=on,ssd=1
qm set 101 --ide0 local:iso/Win11_24H2_x64.iso,media=cdrom
qm set 101 --ide1 local:iso/virtio-win-0.1.262.iso,media=cdrom
# 4. Boot order
qm set 101 --boot order=scsi0;ide0;net0
# 5. Start
qm start 101
Trong quá trình cài Win11, load virtio-scsi driver từ ISO virtio-win để nhận disk.
Sau khi cài: cài QEMU Guest Agent từ D:\virtio-win\guest-agent\qemu-ga-x86_64.msi.
Kết quả đầu ra
Trong VM Windows 11, mở tpm.msc:
Specification Version: 2.0
Manufacturer Name: SWTPM
Status: Ready for use
Snapshot VM lần đầu (trước VE 9.1 sẽ fail với vTPM + qcow2):
qm snapshot 101 before-update
# → success on VE 9.1
Troubleshooting
- Không nhận disk khi cài Win11: load driver virtio-scsi từ ISO secondary
- Secure Boot fail: verify
pre-enrolled-keys=1trong efidisk - Guest Agent không hoạt động: cài dịch vụ + restart VM, check
qm agent 101 ping
Ứng dụng thực tế
Tình huống: Văn phòng 50 người cần chạy Windows 11 ảo hóa.
Giải pháp: 1 template Win11 + clone linked → mỗi user 40 GB thay vì 60 GB full clone. Dùng RDP Gateway publish ra Internet, MFA qua Duo.
Bài 5.3: Migrate VM từ VMware ESXi sang Proxmox
Lý thuyết cốt lõi
3 cách migrate:
- Import Wizard (VE 8.2+, VE 9): Web GUI → Datacenter → Storage → Add ESXi → import
- OVF export +
qm importovf: export OVF từ ESXi, upload, import - Cold clone: shutdown VM, copy VMDK qua SCP,
qm importdisk
Cách 1 nhanh nhất VE 9:
- Add ESXi host làm storage type
esxi - Browse VM → Import → chọn target storage
- Tự động convert VMDK → qcow2 + đổi driver
Bài tập thực hành
Cách 1 (Import Wizard):
# 1. Add ESXi storage
pvesm add esxi esxi-host1 --server esxi01.lab.local \
--username root --password 'xxx' --skip-cert-verification 1
# 2. List VM từ ESXi
pvesm list esxi-host1
# 3. Import VM (Web GUI dễ hơn: click VM → Import)
# Hoặc CLI (chỉ có trong VE 9):
qm importovf 500 /mnt/pve/esxi-host1/ha-datacenter/vm/legacy-web01/legacy-web01.vmx local-zfs
Post-import checklist:
- Uninstall VMware Tools (nếu còn)
- Install QEMU Guest Agent
- Đổi NIC driver e1000 → virtio-net (tăng 3× throughput)
- Đổi disk controller IDE → virtio-scsi
Kết quả đầu ra
Importing disk 'legacy-web01.vmdk' to local-zfs ...
(100/100) [████████████████] 20.0 GB / 20.0 GB
Import complete, VM 500 created.
Troubleshooting
- VM không boot sau migrate (Windows BSOD):
inaccessible_boot_device→ boot vào recovery, chạysc config storvsc start= autotrước khi migrate hoặc dùng IDE controller tạm - Linux không thấy NIC: đổi tên interface trong
/etc/netplan/*.yamlhoặc udev rule
Ứng dụng thực tế
Dự án thực tế: migrate 30 VM (20 Linux + 10 Windows) từ VMware 7 sang VE 9 trong 1 tuần:
- Day 1–2: build cluster Proxmox
- Day 3–5: migrate Linux (10 VM/ngày, mỗi VM ~30 phút)
- Day 6–7: migrate Windows + test