Tạo VM template
🎯 Mục tiêu: Tạo một VM template chuẩn hóa trên Proxmox VE (hoặc Hyper-V) để dùng lại cho mọi lần provisioning mà không cài đặt lại OS.
🧰 Công cụ / nền tảng: Proxmox VE 8 (SSH/CLI), hoặc Windows Server 2022 với Hyper-V role + PowerShell 5.1+.
📦 Chuẩn bị: Có host Proxmox hoặc Hyper-V; file ISO Ubuntu 22.04 Server hoặc Windows Server 2022 eval; quyền admin trên hypervisor.
▶️ Các bước — Proxmox VE (CLI qua SSH):
# 1. Tải cloud image Ubuntu 22.04 (cloud-init ready)
ssh root@<proxmox-host>
cd /var/lib/vz/template/iso
wget https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img
# 2. Tạo VM với ID 9000 (dùng làm template)
qm create 9000 --name tpl-ubuntu2204-base-202601 \
--memory 2048 --cores 2 --net0 virtio,bridge=vmbr0 \
--serial0 socket --vga serial0
# 3. Import disk từ cloud image vào storage "local-lvm"
qm importdisk 9000 jammy-server-cloudimg-amd64.img local-lvm
# 4. Gắn disk vừa import và thiết lập boot
qm set 9000 --scsihw virtio-scsi-pci --scsi0 local-lvm:vm-9000-disk-0
qm set 9000 --ide2 local-lvm:cloudinit --boot c --bootdisk scsi0
# 5. Cấu hình cloud-init (SSH key, user)
qm set 9000 --ciuser ubuntu --sshkeys ~/.ssh/id_rsa.pub
qm set 9000 --ipconfig0 ip=dhcp
# 6. Chuyển VM thành template (KHÔNG THỂ hoàn tác)
qm template 9000
# Xác nhận
qm list | grep 9000
▶️ Các bước — Hyper-V (PowerShell):
# Chạy PowerShell với quyền Administrator
# 1. Tạo VHDX từ ISO (sau khi cài OS thủ công, sysprep)
# Giả sử đã có file VHDX đã sysprep tại C:\Templates\ws2022-base.vhdx
# 2. Tạo VM "template" (không start)
New-VM -Name "tpl-ws2022-base-202601" `
-MemoryStartupBytes 2GB `
-Generation 2 `
-VHDPath "C:\Templates\ws2022-base.vhdx" `
-SwitchName "InternalSwitch"
# 3. Tắt auto-start và đặt note
Set-VM -Name "tpl-ws2022-base-202601" -AutomaticStartAction Nothing
Set-VM -Name "tpl-ws2022-base-202601" -Notes "Template: Windows Server 2022 Eval, sysprep 2026-01"
# 4. Export template ra thư mục dùng chung
Export-VM -Name "tpl-ws2022-base-202601" -Path "C:\VM-Templates\"
Get-VM -Name "tpl-ws2022-base-202601" | Select Name, State, Notes
✅ Kết quả mong đợi: Proxmox: qm list hiển thị VM 9000 với dấu hiệu template (T); Hyper-V: Get-VM trả về State=Off, Notes đúng. Clone từ template tạo VM mới trong <30 giây.
🧹 Cleanup: Template giữ lại để dùng cho LAB-027. Nếu muốn xóa: Proxmox qm destroy 9000; Hyper-V Remove-VM -Name "tpl-ws2022-base-202601" -Force.