Skip to content

A powerful and user-friendly tool for automating the creation of Virtual Machines (VMs) using KVM/QEMU with Terraform and cloud-init. This tool supports the deployment of single VMs or multiple VMs at once with easy-to-understand YAML configurations.

License

Notifications You must be signed in to change notification settings

Pepryan/libvirt-terraform-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

VM Generator untuk KVM dengan Terraform

Sebuah tool powerful dan user-friendly untuk mengotomatisasi pembuatan Virtual Machine (VM) menggunakan KVM/QEMU dengan Terraform dan cloud-init. Tool ini mendukung deployment single VM maupun multiple VM sekaligus dengan konfigurasi YAML yang mudah dipahami.

πŸš€ Fitur Utama

  • βœ… Multiple VM Deployment: Deploy beberapa VM sekaligus dengan satu konfigurasi
  • βœ… YAML Configuration: Konfigurasi yang mudah dibaca dan dipahami
  • βœ… Cloud-init Integration: Otomatis setup user, SSH keys, packages, dan custom commands
  • βœ… Smart Networking: Static IP tanpa DHCP waiting, DHCP dengan proper lease handling
  • βœ… Base Volume Architecture: Efisiensi storage dengan Copy-on-Write, base image aman
  • βœ… Storage Pool Support: Terintegrasi dengan virsh storage pools
  • βœ… Interactive Mode: Mode interaktif untuk VM tunggal
  • βœ… Auto SSH Key Detection: Otomatis deteksi SSH public key
  • βœ… Makefile Integration: Command shortcuts untuk workflow Terraform
  • βœ… Deployment Isolation: Setiap deployment dibuat dalam folder terpisah
  • βœ… Error Handling: Validasi konfigurasi dan error handling yang robust
  • βœ… Overwrite Protection: Perlindungan terhadap deployment folder yang sudah ada
  • βœ… Example Template Generation: Generate template konfigurasi YAML dengan mudah

πŸ“‹ Prerequisites

Sistem Requirements

  • Ubuntu/Debian Linux
  • KVM/QEMU terinstall dan berjalan
  • libvirt terinstall dan dikonfigurasi
  • Terraform >= 0.13
  • Akses sudo untuk mengelola VM

Dependencies

# Install KVM/QEMU dan libvirt
sudo apt update
sudo apt install -y qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils

# Install Terraform
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt update && sudo apt install terraform

# Install yq (optional, untuk parsing YAML yang lebih akurat)
sudo apt install yq

# Tambahkan user ke grup libvirt
sudo usermod -a -G libvirt $USER

Setup Storage Pools

# Buat storage pools jika belum ada
virsh pool-define-as --name images --type dir --target /var/lib/libvirt/images
virsh pool-define-as --name vms --type dir --target /var/lib/libvirt/vms
virsh pool-start images
virsh pool-start vms
virsh pool-autostart images
virsh pool-autostart vms

Download Cloud Images

# Download Ubuntu cloud image
cd /var/lib/libvirt/images
sudo wget https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img
sudo wget https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img

πŸ› οΈ Instalasi

# Clone atau download project
git clone <repository-url>
cd vm-generator

# Atau download manual
chmod +x vm-generator.sh

πŸ“– Penggunaan

1. Mode YAML (Multiple VMs)

Buat Konfigurasi YAML

# Generate example config
./vm-generator.sh --example

# Edit sesuai kebutuhan
nano vms-config.yaml

Generate dan Deploy

# Generate Terraform files (akan dibuat di deployment-vms-config/)
./vm-generator.sh vms-config.yaml

# Masuk ke deployment directory dan deploy VMs
cd deployment-vms-config
make init
make plan
make apply

2. Mode Interactive (Single VM)

# Jalankan mode interactive
./vm-generator.sh

# Ikuti prompt untuk konfigurasi
# Deploy VM (masuk ke deployment folder yang dibuat)
cd deployment-<vm-name>
make init && make apply

πŸ“ Konfigurasi YAML

Struktur Konfigurasi

# Global settings
global:
  image_pool: "images"           # Pool untuk cloud images
  vm_pool: "vms"                # Pool untuk VM disks
  cloud_image: "jammy-server-cloudimg-amd64.img"
  network_name: "default"       # Network name
  domain: "local"
  
  # User configuration
  ssh_user: "ubuntu"
  ssh_password: "ubuntu"
  ssh_password_auth: false
  timezone: "Asia/Jakarta"
  ssh_public_key: ""            # Auto-detect jika kosong
  
  # Global packages
  packages:
    - qemu-guest-agent
    - curl
    - vim

# VM definitions
vms:
  - name: "web-server"
    cpu: 2
    ram_gb: 4
    disk_gb: 20
    network_type: "static"       # static atau dhcp
    ip: "192.168.122.10"
    gateway: "192.168.122.1"
    netmask: "24"
    dns: ["8.8.8.8", "8.8.4.4"]
    packages:                    # Additional packages
      - nginx
      - mysql-server
    commands:                    # Custom commands
      - "systemctl enable nginx"
      - "ufw allow 80,443/tcp"

Contoh Konfigurasi Complete

global:
  image_pool: "images"
  vm_pool: "vms" 
  cloud_image: "jammy-server-cloudimg-amd64.img"
  network_name: "default"
  domain: "local"
  ssh_user: "ubuntu"
  ssh_password: "ubuntu"
  ssh_password_auth: false
  timezone: "Asia/Jakarta"
  packages:
    - qemu-guest-agent
    - curl
    - wget
    - vim
    - htop

vms:
  # Web Server dengan static IP
  - name: "web-01"
    cpu: 2
    ram_gb: 4
    disk_gb: 20
    network_type: "static"
    ip: "192.168.122.10"
    gateway: "192.168.122.1"
    netmask: "24"
    dns: ["8.8.8.8", "8.8.4.4"]
    packages:
      - nginx
    commands:
      - "systemctl enable nginx"
      
  # Database Server
  - name: "db-01"
    cpu: 4
    ram_gb: 8
    disk_gb: 50
    network_type: "static"
    ip: "192.168.122.20"
    gateway: "192.168.122.1"
    netmask: "24"
    dns: ["8.8.8.8"]
    packages:
      - postgresql
      - postgresql-contrib
    commands:
      - "systemctl enable postgresql"
      
  # App Server dengan DHCP
  - name: "app-01"
    cpu: 2
    ram_gb: 6
    disk_gb: 30
    network_type: "dhcp"
    packages:
      - docker.io
      - docker-compose
    commands:
      - "systemctl enable docker"
      - "usermod -aG docker ubuntu"

🎯 Makefile Commands

Setelah generate files dengan ./vm-generator.sh, masuk ke deployment directory dan gunakan commands berikut:

# Masuk ke deployment directory
cd deployment-<nama-config>
# atau untuk interactive mode:
cd deployment-<vm-name>

# Terraform workflow
make init          # Initialize Terraform
make plan          # Show deployment plan
make apply         # Deploy VMs
make destroy       # Destroy all VMs

# Utility commands
make validate      # Validate Terraform files
make fmt           # Format Terraform files
make output        # Show all outputs
make ips           # Show VM IP addresses only
make clean         # Clean temporary files

# SSH commands (auto-generated)
make ssh-web-01    # SSH to web-01 VM
make ssh-db-01     # SSH to db-01 VM
make ssh-app-01    # SSH to app-01 VM

# Complete workflow
make all           # init + plan + apply

# Help
make help          # Show available commands

πŸ”§ Advanced Configuration

Custom Networks

# Buat custom network
virsh net-define-as mynet 192.168.100.0/24
virsh net-start mynet
virsh net-autostart mynet

# Update YAML config
network_name: "mynet"

Multiple Storage Pools

# Setup custom storage pools
virsh pool-define-as ssd-pool dir /mnt/ssd/vms
virsh pool-start ssd-pool

# Update YAML
vm_pool: "ssd-pool"

SSH Key Management

# Generate SSH key jika belum ada
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

# Script akan auto-detect key dari ~/.ssh/id_rsa.pub
# Atau specify manual di YAML:
ssh_public_key: "ssh-rsa AAAAB3NzaC1yc2E..."

πŸ”„ Base Volume Architecture

Konsep Base Volume

Script ini menggunakan base volume architecture yang memberikan keuntungan signifikan:

  • βœ… Efisiensi Storage: VM disk dibuat sebagai copy-on-write dari base image
  • βœ… Keamanan Data: Base image tidak pernah dimodifikasi atau terhapus
  • βœ… Fast Deployment: Tidak perlu copy full image, hanya membuat reference
  • βœ… Safe Destroy: terraform destroy hanya menghapus VM disk, bukan base image

Terraform Volume Configuration

# Generated volume configuration
resource "libvirt_volume" "vm_disk" {
  name               = "vm-name-disk.qcow2"
  pool               = "vms"                    # Target pool untuk VM disk
  base_volume_name   = "noble-server-cloudimg-amd64.img"
  base_volume_pool   = "isos"                  # Source pool untuk base image
  size               = 20000000000             # 20GB
  format             = "qcow2"
}

Network Configuration

Script secara otomatis mengatur network interface berdasarkan tipe yang dipilih:

Static IP Configuration

network_interface {
  network_name   = "net-192.168.100"
  wait_for_lease = false                # Tidak menunggu DHCP lease
  addresses      = ["192.168.100.88"]  # Set IP address langsung
}

Keuntungan:

  • Deployment lebih cepat (tidak ada DHCP waiting time)
  • IP address predictable dan konsisten
  • Cocok untuk production dan services

DHCP Configuration

network_interface {
  network_name   = "default"
  wait_for_lease = true    # Menunggu DHCP server assign IP
}

Keuntungan:

  • Setup mudah, tidak perlu planning IP
  • Automatic network configuration
  • Cocok untuk development dan testing

Keuntungan Architecture Ini

  1. Base Image Preservation: File asli cloud images tetap aman
  2. Fast Provisioning: VM baru menggunakan CoW (Copy-on-Write)
  3. Storage Efficiency: Multiple VM share base image
  4. Safe Cleanup: Destroy VM tidak affect base image

πŸ“ Deployment Folder Management

Konsep Deployment Isolation

Script ini menggunakan konsep deployment isolation dimana setiap deployment dibuat dalam folder terpisah. Ini memberikan beberapa keuntungan:

  • βœ… Isolasi Deployment: Setiap konfigurasi VM terpisah satu sama lain
  • βœ… Parallel Development: Bisa mengembangkan multiple environment bersamaan
  • βœ… Version Control: Mudah untuk track dan backup deployment
  • βœ… Clean Management: Mudah untuk menghapus seluruh deployment

Naming Convention

# YAML mode: deployment-{config-filename}
./vm-generator.sh web-servers.yaml
# Creates: deployment-web-servers/

# Interactive mode: deployment-{vm-name}
# VM Name: "my-app"
# Creates: deployment-my-app/

Overwrite Protection

Script memberikan perlindungan jika deployment folder sudah ada:

[WARNING] Deployment directory 'deployment-web-servers' already exists!
Do you want to overwrite? (y/N): 
  • N (default): Membatalkan deployment untuk mencegah overwrite
  • Y: Menghapus folder lama dan membuat yang baru

Multiple Deployment Management

# Buat multiple deployment
./vm-generator.sh web-servers.yaml    # -> deployment-web-servers/
./vm-generator.sh db-cluster.yaml     # -> deployment-db-cluster/
./vm-generator.sh monitoring.yaml     # -> deployment-monitoring/

# Manage setiap deployment independen
cd deployment-web-servers && make apply
cd deployment-db-cluster && make apply
cd deployment-monitoring && make apply

# Cleanup specific deployment
cd deployment-web-servers && make destroy
rm -rf deployment-web-servers/

Best Practices

  1. Naming: Gunakan nama deskriptif untuk config file (e.g., production-web.yaml)
  2. Documentation: Simpan notes di deployment folder untuk referensi
  3. Backup: Backup deployment folder sebelum major changes
  4. Cleanup: Hapus deployment folder yang tidak digunakan
# Backup deployment
tar -czf deployment-web-servers-backup.tar.gz deployment-web-servers/

# List semua deployment
ls -d deployment-*/

# Cleanup unused deployment
cd deployment-old-test && make destroy
cd .. && rm -rf deployment-old-test/

🚨 Troubleshooting

Common Issues

1. Permission Denied

# Pastikan user ada di grup libvirt
sudo usermod -a -G libvirt $USER
# Logout dan login kembali

2. Cloud Image Not Found

# Check available images in pool
virsh vol-list images

# Download missing images
cd /var/lib/libvirt/images
sudo wget https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img

3. Network Issues

# Check available networks
virsh net-list --all

# Start default network if stopped
virsh net-start default
virsh net-autostart default

4. Terraform Provider Issues

# Clean dan reinitialize
make clean
make init

5. VM tidak dapat SSH

  • Pastikan cloud-init selesai (tunggu 2-3 menit)
  • Check console: virsh console vm-name
  • Verify SSH service: systemctl status ssh

Debug Mode

# Enable verbose output
export TF_LOG=DEBUG
make apply

# Check cloud-init logs di VM
sudo tail -f /var/log/cloud-init-output.log

πŸ“ Struktur File

vm-generator/
β”œβ”€β”€ vm-generator.sh              # Main script
β”œβ”€β”€ vms-config.yaml             # VM configuration template
β”œβ”€β”€ README.md                   # Documentation
β”œβ”€β”€ LICENSE                     # License file
└── deployment-<config-name>/   # Generated deployment folder
    β”œβ”€β”€ vms-config.yaml         # Copy of config file
    β”œβ”€β”€ main.tf                 # Generated Terraform file
    β”œβ”€β”€ Makefile                # Build commands
    β”œβ”€β”€ cloudinit_*.cfg        # Cloud-init configs per VM
    └── network_*.cfg          # Network configs per VM

πŸ”„ Workflow Example

# 1. Setup
./vm-generator.sh --example
nano vms-config.yaml  # Edit configuration

# 2. Generate (creates deployment-vms-config/)
./vm-generator.sh vms-config.yaml

# 3. Deploy
cd deployment-vms-config
terraform init
terraform plan     # Review plan
terraform apply    # Deploy

# 4. Verify
make ips      # Get VM IPs
make ssh-n8n-server  # SSH to VM

# 5. Manage
virsh list    # List running VMs
make destroy  # Clean up when done

πŸ›‘οΈ Security Notes

  • Script menggunakan SSH key authentication secara default
  • Password authentication dapat diaktifkan tapi tidak direkomendasikan untuk production
  • VM menggunakan user dengan sudo access
  • Firewall dikonfigurasi sesuai commands di YAML
  • Cloud-init dijalankan dengan privilege root

πŸ“Š Resource Management

Estimasi Resource Usage

Per VM Base:
- RAM: Minimum 1GB, Recommended 2GB+
- Disk: Minimum 10GB, Recommended 20GB+
- CPU: Minimum 1 vCPU, Recommended 2+

Host Requirements:
- RAM: (Total VM RAM) + 2GB untuk host
- Disk: (Total VM Disk) + space untuk images
- CPU: Total vCPU tidak boleh > Physical CPU cores

πŸ”„ Updates dan Maintenance

Update Cloud Images

# Download latest Ubuntu images
cd /var/lib/libvirt/images
sudo wget https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img
sudo wget https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img

# Update YAML config dengan image name yang baru

Backup VMs

# Backup VM disk
virsh dumpxml vm-name > vm-name.xml
cp /var/lib/libvirt/vms/vm-name-disk.qcow2 /backup/

# Restore
virsh define vm-name.xml

🀝 Contributing

  1. Fork repository
  2. Buat feature branch (git checkout -b feature/amazing-feature)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push ke branch (git push origin feature/amazing-feature)
  5. Buat Pull Request

πŸ“ž Support

Jika mengalami issues atau membutuhkan bantuan:

  1. Check troubleshooting section di atas
  2. Buat issue di GitHub repository
  3. Sertakan output error dan konfigurasi YAML

πŸ“ Changelog

v1.2.0 (Latest)

  • βœ… NEW: Base volume architecture untuk efisiensi dan keamanan storage
  • βœ… NEW: Smart static IP handling (tidak menunggu DHCP lease)
  • βœ… IMPROVED: Volume configuration menggunakan base_volume_name dan base_volume_pool
  • βœ… IMPROVED: Network interface conditional generation berdasarkan tipe network
  • βœ… FIXED: Safe destroy - base images tidak terhapus saat terraform destroy
  • βœ… FIXED: Duplicate disk blocks di interactive mode
  • βœ… ENHANCED: Deployment lebih cepat untuk static IP VMs

v1.1.0

  • βœ… NEW: Deployment folder isolation
  • βœ… NEW: Overwrite protection untuk deployment
  • βœ… NEW: Auto-copy config ke deployment folder
  • βœ… IMPROVED: Better path management
  • βœ… IMPROVED: Enhanced error handling

v1.0.0

  • βœ… Initial release
  • βœ… Multiple VM support
  • βœ… YAML configuration
  • βœ… Cloud-init integration
  • βœ… Interactive mode
  • βœ… Makefile automation
  • βœ… Storage pool support
  • βœ… Auto SSH key detection
  • βœ… Error handling dan validation

πŸ“„ License

Project ini dilisensikan under MIT License - lihat file LICENSE untuk detail lengkap.

πŸ‘¨β€πŸ’» Author

Febryan Ramadhan


⭐ Jika project ini berguna, jangan lupa berikan star di GitHub! ⭐

About

A powerful and user-friendly tool for automating the creation of Virtual Machines (VMs) using KVM/QEMU with Terraform and cloud-init. This tool supports the deployment of single VMs or multiple VMs at once with easy-to-understand YAML configurations.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages