====== Criando box Debian 11 - Libvirt ======
===== Criando Vagrant box do Debian 11 para KVM - Libvirt =====
FIXME Durante a instalação foi criado o usuário **vagrant** com a senha **vagrant** e foi usado a senha para o usuário **root**.
FIXME Instalação miníma. Apenas o SSH foi marcado para ser instalado.
===== Instalação de pacotes =====
Com foi feita uma instalação miníma, vamos instalar o **sudo** e o **python**. O python é opcional. Nesse caso será instalado caso seja necessário o provisionamento com o **ansible**. Já o **sudo** é obrigatório já que precisamos usar o usuário **vagrant** com privilégios de **root** quando formos usar a box.
# apt update && apt install -y sudo python3 qemu-guest-agent
===== Alterando a interface de rede =====
# 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;
# echo "pre-up sleep 2" >> /etc/network/interfaces
===== Elevando os privilégios do usuário vagrant =====
# visudo -f /etc/sudoers.d/vagrant
vagrant ALL=(ALL) NOPASSWD:ALL
===== Zerando o histórico de comandos =====
# export HISTSIZE=0
# exit
$ 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.82
/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.82's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'vagrant@192.168.122.82'"
and check to make sure that only the key(s) you wanted were added.
$ chmod 0600 vagrant
$ ssh -i vagrant vagrant@192.168.122.82
Linux debian 5.10.0-21-amd64 #1 SMP Debian 5.10.162-1 (2023-01-21) x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
$ sudo poweroff
Connection to 192.168.122.82 closed by remote host.
Connection to 192.168.122.82 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/debian11.qcow2
{32}
==> Creating box, tarring and gzipping
./metadata.json
./Vagrantfile
./box.img
Total bytes written: 2153594880 (2,1GiB, 36MiB/s)
==> debian11.box created
==> You can now add the box:
==> 'vagrant box add debian11.box --name debian11'
===== Testando a box =====
$ vagrant box add debian11.box --name debian11
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'debian11' (v0) for provider:
box: Unpacking necessary files from: file:///home/gean/Downloads/vagrant/box/debian11.box
==> box: Successfully added box 'debian11' (v0) for 'libvirt'!
$ vim Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'libvirt'
Vagrant.configure("2") do |config|
config.vm.define :debian11 do |debian11|
debian11.vm.box = "debian11"
debian11.vm.hostname = "vm-01"
debian11.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 'debian11' up with 'libvirt' provider...
==> debian11: No version detected for debian11, using timestamp to watch for modifications. Consider
==> debian11: generating a local metadata for the box with a version to allow better handling.
==> debian11: See https://www.vagrantup.com/docs/boxes/format#box-metadata for further details.
==> debian11: Creating image (snapshot of base box volume).
==> debian11: Creating domain with the following settings...
==> debian11: -- Name: box_debian11
==> debian11: -- Description: Source: /home/gean/Downloads/vagrant/box/Vagrantfile
==> debian11: -- Domain type: kvm
==> debian11: -- Cpus: 2
==> debian11: -- Feature: acpi
==> debian11: -- Feature: apic
==> debian11: -- Feature: pae
==> debian11: -- Clock offset: utc
==> debian11: -- Memory: 2048M
==> debian11: -- Base box: debian11
==> debian11: -- Storage pool: default
==> debian11: -- Image(vda): /home/gean/kvm/images/box_debian11.img, virtio, 32G
==> debian11: -- Disk driver opts: cache='default'
==> debian11: -- Graphics Type: vnc
==> debian11: -- Video Type: cirrus
==> debian11: -- Video VRAM: 16384
==> debian11: -- Video 3D accel: false
==> debian11: -- Keymap: en-us
==> debian11: -- TPM Backend: passthrough
==> debian11: -- INPUT: type=mouse, bus=ps2
==> debian11: Pruning invalid NFS exports. Administrator privileges will be required...
==> debian11: Creating shared folders metadata...
==> debian11: Starting domain.
==> debian11: Domain launching with graphics connection settings...
==> debian11: -- Graphics Port: 5900
==> debian11: -- Graphics IP: 127.0.0.1
==> debian11: -- Graphics Password: Not defined
==> debian11: -- Graphics Websocket: 5700
==> debian11: Waiting for domain to get an IP address...
==> debian11: Waiting for machine to boot. This may take a few minutes...
debian11: SSH address: 192.168.121.206:22
debian11: SSH username: vagrant
debian11: SSH auth method: private key
debian11:
debian11: Vagrant insecure key detected. Vagrant will automatically replace
debian11: this with a newly generated keypair for better security.
debian11:
debian11: Inserting generated public key within guest...
debian11: Removing insecure key from the guest if it's present...
debian11: Key inserted! Disconnecting and reconnecting using new SSH key...
==> debian11: Machine booted and ready!
==> debian11: Setting hostname...
$ vagrant ssh
Linux vm-01 5.10.0-21-amd64 #1 SMP Debian 5.10.162-1 (2023-01-21) x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Tue Feb 28 20:21:20 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/Debian11 1.0 libvirt debian11.box \
--description "Debian 11 Minimal" \
--version-description "Debian 11 instalação mínima" \
--release --no-private
===== Referências =====
- [[https://computingforgeeks.com/create-rocky-linux-8-vagrant-boxes-using-packer/|How To Create Rocky Linux 8 Vagrant Boxes Using Packer]]
- [[https://leyhline.github.io/2019/02/16/creating-a-vagrant-base-box/|Creating a Vagrant Base Box (from Fedora 29 Server, using Libvirt as Provider)]]
- [[https://unix.stackexchange.com/questions/222427/how-to-create-custom-vagrant-box-from-libvirt-kvm-instance|how to create custom vagrant box from libvirt/kvm instance?]]
- [[https://github.com/chef/bento|Bento]]