Table of Contents

Criando box Ubuntu - libvirt

Instalando a vm

$ sudo virt-install \
--name ubuntu22 \
--description="Ubuntu Server 22.04" \
--ram 2048 \
--vcpus 2 \
--disk size=32,format=qcow2 \
--cdrom /datastore/imagens/isos/ubuntu-22.04.1-live-server-amd64.iso \
--virt-type kvm \
--os-type=Linux \
--os-variant=ubuntu20.04 \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole

Configurando a VM

$ sudo virsh domifaddr ubuntu22
 Nome       Endereço MAC        Protocol     Address
-------------------------------------------------------------------------------
 vnet0      52:54:00:52:65:3f    ipv4         192.168.122.8/24
$ ssh vagrant@192.168.122.8

Configuração do sudo

$ sudo echo 'vagrant ALL=(ALL) NOPASSWD:ALL' >/etc/sudoers.d/99_vagrant
$ sudo chmod 440 /etc/sudoers.d/99_vagrant

Configuração de rede

networking.sh
# $ vim /tmp/networking.sh
#!/bin/sh -eux
 
ubuntu_version="`lsb_release -r | awk '{print $2}'`";
major_version="`echo $ubuntu_version | awk -F. '{print $1}'`";
 
if [ "$major_version" -ge "18" ]; then
echo "Create netplan config for eth0"
cat <<EOF >/etc/netplan/01-netcfg.yaml;
network:
  version: 2
  ethernets:
    eth0:
      dhcp4: true
EOF
else
  # Adding a 2 sec delay to the interface up, to make the dhclient happy
  echo "pre-up sleep 2" >> /etc/network/interfaces;
fi
 
# Disable Predictable Network Interface names and use eth0
[ -e /etc/network/interfaces ] && sed -i 's/en[[:alnum:]]*/eth0/g' /etc/network/interfaces;
sed -i 's/GRUB_CMDLINE_LINUX="\(.*\)"/GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0 \1"/g' /etc/default/grub;
update-grub;
$ sudo bash /tmp/networking.sh

Removendo arquivo temporários e logs

$ sudo find /var/cache -type f -exec rm -rf {} \;
$ sudo find /var/log -type f -exec truncate --size=0 {} \;
$ sudo truncate -s 0 /etc/machine-id
$ sudo rm -rf /tmp/* /var/tmp/*
$ sudo rm -f /var/lib/systemd/random-seed
$ export HISTSIZE=0

Configurando as chaves SSH

$ wget https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant
$ wget https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant.pub
$ ssh-copy-id -i vagrant.pub vagrant@192.168.122.8
$ chmod 0600 vagrant
$ ssh -i vagrant vagrant@192.168.122.8

Criando a box

$ sudo virsh shutdown ubuntu22
$ wget https://raw.githubusercontent.com/vagrant-libvirt/vagrant-libvirt/master/tools/create_box.sh
$ sudo bash create_box.sh /datastore/imagens/ubuntu22.qcow2

Publicando uma box

$ vagrant cloud publish g3anmart1ns/ubuntu22 1.0 libvirt ubuntu22.box \
--description "Ubuntu Server 22.04" \
--version-description "Ubuntu Server 22.04 mínima" \
--release --no-private

Testando a box

Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
 
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'libvirt'
 
Vagrant.configure("2") do |config|
  config.vm.define :ubuntu22 do |ubuntu22|
    ubuntu22.vm.box = "g3anmart1ns/ubuntu22"
    ubuntu22.vm.hostname = "vm-01"
 
    ubuntu22.vm.provider :libvirt do |lv|
      lv.memory = 2048
      lv.cpus = 2
    end
  end
  config.vm.synced_folder ".", "/vagrant", disabled: true
end
$ vagrant up