惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
V
V2EX
博客园_首页
Engineering at Meta
Engineering at Meta
博客园 - 聂微东
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
H
Help Net Security
A
About on SuperTechFans
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Blog — PlanetScale
Blog — PlanetScale
W
WeLiveSecurity
云风的 BLOG
云风的 BLOG
D
Docker
Security Archives - TechRepublic
Security Archives - TechRepublic
Help Net Security
Help Net Security
N
News and Events Feed by Topic
Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
A
Arctic Wolf
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
博客园 - 三生石上(FineUI控件)
aimingoo的专栏
aimingoo的专栏
Hacker News: Ask HN
Hacker News: Ask HN
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 司徒正美
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy International News Feed
T
Troy Hunt's Blog
T
Tenable Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Recorded Future
Recorded Future
F
Fortinet All Blogs
D
DataBreaches.Net
B
Blog
T
Threat Research - Cisco Blogs
MyScale Blog
MyScale Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
The GitHub Blog
The GitHub Blog
Security Latest
Security Latest
M
MIT News - Artificial intelligence

Posts on Noah Bailey

How to turn anything into a router Deploy to Cloudfront from GitHub using OpenID Connect Backup Postgres databases with Kubernetes CronJobs The spelling error made 200 billion times a day Restarting Kubernetes pods using a CronJob You've just bought a new domain. Now what? Who Sawed My Motherboard??? Linux on the P8 Aliexpress Mini Laptop Recovering Mysql/Mariadb after a nasty crash Using EXIF data to pick my next lens Converting and developing RAW photos on Linux automatically Thank you, 2016 iPhone Don't Make It Work Self-hosted Surveillance with ZoneMinder Backups, Monitoring, and Security for small Mastodon servers Block web scanners with ipset & iptables Executing commands over SSH with GitHub Actions Debian Sid on encrypted ZFS Protect your dangerously insecure redis server Debian: the luxurious boring lifestyle Monitor radiation with a Raspberry Pi Simple Linux server alerts: Know your performance, errors, security, syslog, and security NUC crashes on debian 11 - How I fixed it Basic Linux server security with fail2ban, ossec, and firewall Windows 11 will create heaps of needless trash Domesticated Kubernetes Networking The Cursed Certificate Our mostly disposable and entirely stupid world Trying out OpenBSD (as a Linux geek) Making VoIP Calls with Antique Rotary Phones Monitoring WAN speed with speedtest-cli and ElasticSearch Monitoring WAN latency with InfluxDB The Zeroshell botnet returns Installing Gentoo on a vintage Thinkpad T60 Malware emails 2: Russian boogaloo TP-Link Device Weirdness ElasticSearch broke all my nice things (a story of cascading failure) A New Botnet is Targeting Network Infrastructure Malware on the Wire: Monitoring Network Traffic with Suricata and ClamAV Cloud Threat Protection with OSSEC and Suricata Malware Emails From Jerks Surviving the Apocalypse with an Offline Wikipedia Server Being Attacked by Bots Linux Router, Firewall and IDS Appliance You Probably Don't Need a VPN Fix an Oversharded Elasticsearch Cluster Update all your linux servers as fast as possible Cleanup Systemd Journald Storage Stop Putting Your SSH Keys on Github! Clustering KVM with Ceph Storage Stealing Windows Sessions FreeRadius Active Directory Integration Retrieving WPA2 Keys on Windows Deploy MDT Litetouch on Linux with TFTPD and Syslinux Generating MSI transform files with Orca The Inflatable Dinghy Generating Cisco IOS config files with Python Homebrew SAN Getting Cloudy
Automating KVM Virtualization
2019-09-28 · via Posts on Noah Bailey

Think of it like, “OpenStack for cheapskates.”

There are plenty of ways to automate the provisioning of virtual machines, and while this isn’t the best way it certainly works great for me. I am fortunate enough to have a very heterogeneous environment at home; aside from a few appliances nearly all my virtual machines are running Ubuntu 18.04. This approach certainly won’t work for those who have a mixed environment with different versions Linux, Windows, and BSD derivatives.

I’ve packaged this up as an Ansible role, go see it on GitHub!

System Components

For this to work, you essentially need these pieces:

  • A dedicated computer to run KVM, with Linux already installed
  • A network switch configured with tagged VLANs for the virtual networks on KVM
  • A router or multilayer switch to handle the routing and network access for the VLANs
  • A desktop/laptop computer with Ansible and Virt-Manager (optional) for managing the servers

I built this system for managing my own servers, so it’s entirely possible that there were oversights or assumptions.

VM Images

As I said earlier, I use Ubuntu 18.04 for my servers. This is the #1 assumption I’ve made by hardcoding this. However, this is where the real beauty of this system comes in.

Instead of using a system like Hashicorp Packer to build images, I simply download the exact same images used by OpenStack private cloud, as well as Docker and LXC containers. They’re a very minimal GNU/Linux install, but they’re more than enough for spinning up basic VMs.

When making new virtual machines, the scripts will first check if the server has an image saved, and download one if needed. Then, it will convert the image to .qcow format and clone it to make the new VM. Then the customization begins. Depending on the user-defined data, it will also expand the disk image.

Cloud-Config

The next nifty technology this integrates is the cloud-init system. Usually this is used to upload scripts to ec2 (Or other cloud providers) instances at launch time to automate the configuration. I use it for a similar purpose:

  • Set the hostname and domain
  • Configure users
  • Add authorized SSH keys
  • Either set DHCP or static IP

The script will generate cloud-config and network metadata files, then create an ISO image to attach to the VM. This is required since there is not metadata API in my ‘private cloud’, instead we must use local files that are read at boot.

Launch

To fire up the VM, we use the virt-install script with a set of default parameters. Initially, I used the libvirt_xml module in Ansible, but found it to be less flexible than just running a command in the playbook.

Examples

The following are examples of user-defined config that would live in the inventories/group_vars directory.

Users

This is a list of users that should be added to the server during provisioning.

The passwd and pub_key parameters take your hashed password and your SSH public key so that you are able to access the server after provisioning.

users: 
- name: ongo
  full_name: Ongo Gablogian 
  passwd: $6$rounds=2048$aaaaaaaa
  pub_key: ssh-rsa AAAAB3N [email protected]

VMs

This is a list of virtual machines that will be built. The only required parameters are the name, all others will fall back to default settings.

virtual_machines: 
- name: u18-svr-001
  cpu: 1
  mem: 1024
  disk: 10G
  bridge: br10 

A VM can also be created with a static IP address defined:

- name: u18-svr-002
  cpu: 1
  mem: 512 
  disk: 5G
  bridge: br10 
  net: 
    ip: 10.11.12.13/24
    gateway: 10.11.12.1 
    domain: gablogianartcollection.org
    dns: 
      - 1.1.1.1
      - 9.9.9.9

Default Settings

If these settings are not defined, the defaults will be applied when creating the virtual machine:

If no value is supplied, the default settings will be used:

Parameter Default Value
CPU 1 core
Memory 512 MB
Disk 5GB
Bridge Default libvirt NAT network
Network DHCP

Adding the role to your Ansible project

The best way to add this role is by using a Git submodule:

git submodule add https://github.com/noahbailey/ansible-qemu-kvm.git roles/kvm

Then, the role can be added to your main.yml or other top-level playbook:

---
- name: Provision Virtual Machines
  hosts: hypervisors
  become: true 
  roles: 
    - kvm

I keep this towards the top so that the VMs I provision will be configured later on in the same run.

Hopefully this was a good introduction to working with and automating Linux virtualization. Since I’ve moved away from ESXi I’ve really taken to liking KVM/QEMU for its simplicity and ease of automation.

As always, thanks for reading!