User Tools

Site Tools


wikiv2:vagrant_box_ubuntu22

Criando box Ubuntu 22.04 - Libvirt

Criando Vagrant box do Ubuntu 22.04 LTS para KVM - Libvirt

FIXME Durante a instalação foi criado o usuário vagrant com a senha vagrant.

FIXME Instalação miníma. Apenas o SSH foi marcado para ser instalado.

Acessando a VM

$ virsh list
 Id   Name       State
--------------------------
 5    ubuntu22   running
$ virsh domifaddr ubuntu22
 Name       MAC address          Protocol     Address
-------------------------------------------------------------------------------
 vnet4      52:54:00:ba:53:92    ipv4         192.168.122.231/24
$ ssh vagrant@192.168.122.231
The authenticity of host '192.168.122.231 (192.168.122.231)' can't be established.
ED25519 key fingerprint is SHA256:mppHft83QEP+bKkdAVrNfGX5GnOEwzaK07U3bz+EZss.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.122.231' (ED25519) to the list of known hosts.
vagrant@192.168.122.231's password: 
Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.15.0-60-generic x86_64)
 
 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage
 
This system has been minimized by removing packages and content that are
not required on a system that users do not log into.
 
To restore this content, you can run the 'unminimize' command.
 
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
 
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
 
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
 
vagrant@ubuntu:~$ 

Instalação de pacotes

Instalando o agent qemu.

$ sudo apt update && sudo apt install -y qemu-guest-agent

Alterando a interface de rede

cat <<EOF | sudo tee /etc/netplan/01-netcfg.yaml
network:
  version: 2
  ethernets:
    eth0:
      dhcp4: true
EOF
$ sudo sed -i 's/GRUB_CMDLINE_LINUX="\(.*\)"/GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0 \1"/g' /etc/default/grub
$ sudo update-grub
Sourcing file `/etc/default/grub'
Sourcing file `/etc/default/grub.d/init-select.cfg'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.15.0-60-generic
Found initrd image: /boot/initrd.img-5.15.0-60-generic
Warning: os-prober will not be executed to detect other bootable partitions.
Systems on them will not be added to the GRUB boot configuration.
Check GRUB_DISABLE_OS_PROBER documentation entry.
done

Configuração do sudo

$ echo 'vagrant ALL=(ALL) NOPASSWD:ALL' | sudo tee /etc/sudoers.d/vagrant
vagrant ALL=(ALL) NOPASSWD:ALL
$ sudo chmod 440 /etc/sudoers.d/vagrant

Removendo arquivo temporários, cache e logs

$ sudo apt-get -y autoremove
$ sudo apt-get -y clean
$ sudo rm -rf /usr/share/doc/*
$ sudo find /var/cache -type f -exec rm -rf {} \;
$ sudo find /var/log -type f -exec truncate --size=0 {} \;
$ sudo truncate -s 0 /var/lib/dbus/machine-id 
$ sudo rm -rf /tmp/* /var/tmp/*
$ sudo rm -f /var/lib/systemd/random-seed
$ exit

Chave SSH

$ curl -O https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant
$ curl -O https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant.pub
$ ssh-copy-id -i vagrant.pub vagrant@192.168.122.231
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "vagrant.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
vagrant@192.168.122.231's password: 
 
Number of key(s) added: 1
 
Now try logging into the machine, with:   "ssh 'vagrant@192.168.122.231'"
and check to make sure that only the key(s) you wanted were added.
$ chmod 0600 vagrant
$ ssh -i vagrant vagrant@192.168.122.231
Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.15.0-60-generic x86_64)
 
 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage
 
This system has been minimized by removing packages and content that are
not required on a system that users do not log into.
 
To restore this content, you can run the 'unminimize' command.
Last login: Wed Mar  1 19:17:19 2023 from 192.168.122.1
$ export HISTSIZE=0 && sudo poweroff
Connection to 192.168.122.231 closed by remote host.
Connection to 192.168.122.231 closed.

Criando a box

$ curl -O  https://raw.githubusercontent.com/vagrant-libvirt/vagrant-libvirt/master/tools/create_box.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3263  100  3263    0     0   5869      0 --:--:-- --:--:-- --:--:--  5879
$ sudo bash create_box.sh /home/gean/kvm/images/ubuntu22.qcow2
{32}
==> Creating box, tarring and gzipping
./metadata.json
./Vagrantfile
./box.img
Total bytes written: 4390133760 (4,1GiB, 36MiB/s)
==> ubuntu22.box created
==> You can now add the box:
==>   'vagrant box add ubuntu22.box --name ubuntu22'

Testando a box

$ vagrant box add ubuntu22.box --name ubuntu22
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'ubuntu22' (v0) for provider: 
    box: Unpacking necessary files from: file:///home/gean/Downloads/vagrant/box/ubuntu22.box
==> box: Successfully added box 'ubuntu22' (v0) for 'libvirt'!
Vagrantfile
$ vim 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 = "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
Bringing machine 'ubuntu22' up with 'libvirt' provider...
==> ubuntu22: No version detected for ubuntu22, using timestamp to watch for modifications. Consider
==> ubuntu22: generating a local metadata for the box with a version to allow better handling.
==> ubuntu22: See https://www.vagrantup.com/docs/boxes/format#box-metadata for further details.
==> ubuntu22: Uploading base box image as volume into Libvirt storage...
==> ubuntu22: Creating image (snapshot of base box volume).
==> ubuntu22: Creating domain with the following settings...
==> ubuntu22:  -- Name:              box_ubuntu22
==> ubuntu22:  -- Description:       Source: /home/gean/Downloads/vagrant/box/Vagrantfile
==> ubuntu22:  -- Domain type:       kvm
==> ubuntu22:  -- Cpus:              2
==> ubuntu22:  -- Feature:           acpi
==> ubuntu22:  -- Feature:           apic
==> ubuntu22:  -- Feature:           pae
==> ubuntu22:  -- Clock offset:      utc
==> ubuntu22:  -- Memory:            2048M
==> ubuntu22:  -- Base box:          ubuntu22
==> ubuntu22:  -- Storage pool:      default
==> ubuntu22:  -- Image(vda):        /home/gean/kvm/images/box_ubuntu22.img, virtio, 32G
==> ubuntu22:  -- Disk driver opts:  cache='default'
==> ubuntu22:  -- Graphics Type:     vnc
==> ubuntu22:  -- Video Type:        cirrus
==> ubuntu22:  -- Video VRAM:        16384
==> ubuntu22:  -- Video 3D accel:    false
==> ubuntu22:  -- Keymap:            en-us
==> ubuntu22:  -- TPM Backend:       passthrough
==> ubuntu22:  -- INPUT:             type=mouse, bus=ps2
==> ubuntu22: Creating shared folders metadata...
==> ubuntu22: Starting domain.
==> ubuntu22: Domain launching with graphics connection settings...
==> ubuntu22:  -- Graphics Port:      5900
==> ubuntu22:  -- Graphics IP:        127.0.0.1
==> ubuntu22:  -- Graphics Password:  Not defined
==> ubuntu22:  -- Graphics Websocket: 5700
==> ubuntu22: Waiting for domain to get an IP address...
==> ubuntu22: Waiting for machine to boot. This may take a few minutes...
    ubuntu22: SSH address: 192.168.121.125:22
    ubuntu22: SSH username: vagrant
    ubuntu22: SSH auth method: private key
    ubuntu22: 
    ubuntu22: Vagrant insecure key detected. Vagrant will automatically replace
    ubuntu22: this with a newly generated keypair for better security.
    ubuntu22: 
    ubuntu22: Inserting generated public key within guest...
    ubuntu22: Removing insecure key from the guest if it's present...
    ubuntu22: Key inserted! Disconnecting and reconnecting using new SSH key...
==> ubuntu22: Machine booted and ready!
==> ubuntu22: Setting hostname...
$ vagrant ssh
Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.15.0-60-generic x86_64)
 
 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage
 
This system has been minimized by removing packages and content that are
not required on a system that users do not log into.
 
To restore this content, you can run the 'unminimize' command.
Last login: Wed Mar  1 19:44:09 2023 from 192.168.122.1
vagrant@vm-01:~$
vagrant@vm-01:~$ exit
$ vagrant halt
$ vagrant destroy -f

Publicando a box

$ vagrant cloud publish g3anmart1ns/ubuntu22 1.0 libvirt ubuntu22.box \
--description "Ubuntu Server 22.04 TLS" \
--version-description "Ubuntu 22.04 - instalação miníma" \
--release --no-private

Referências

wikiv2/vagrant_box_ubuntu22.txt · Last modified: by 127.0.0.1