Skip to content

Build Vagrant box for libvirt

Install debian (or other) linux in a 10G qcow2

adduser vagrant
passwd vagrant vagrant
passwd root vagrant

apt update
apt upgrade

apt install sudo openssh-server  

apt install mc telnet links lynx curl sudo netcat nmap tcpdump strace

echo "vagrant ALL=(ALL) NOPASSWD:ALL" >  /etc/sudoers.d/vagrant

mkdir -p /home/vagrant/.ssh
chmod 0700 /home/vagrant/.ssh

wget --no-check-certificate \
https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub \
-O /home/vagrant/.ssh/authorized_keys

chmod 0600 /home/vagrant/.ssh/authorized_keys
chown -R vagrant /home/vagrant/.ssh


vi /etc/ssh/sshd_config and change

PubKeyAuthentication yes
AuthorizedKeysFile %h/.ssh/authorized_keys
PermitEmptyPasswords no
PasswordAuthentication no

service ssh restart

cat << EOF >> /etc/systemd/network/80-dhcp.network
[Match]
Name=en*

[Network]
DHCP=yes

EOF


systemctl enable systemd-networkd.service
systemctl start systemd-networkd.service

On vagrant host

mkdir box
cd box

cat << EOF >> metadata.json
{
  "provider"     : "libvirt",
  "format"       : "qcow2",
  "virtual_size" : 10
}
EOF


cat << EOF >> Vagrantfile
Vagrant.configure("2") do |config|
         config.vm.provider :libvirt do |libvirt|
         libvirt.driver = "kvm"
         libvirt.host = 'localhost'
         libvirt.uri = 'qemu:///system'
         end
config.vm.define "new" do |custombox|
         custombox.vm.box = "custombox"       
         custombox.vm.provider :libvirt do |test|
         test.memory = 1024
         test.cpus = 1
         end
         end
end

EOF

cp /path/linux.qcow2 ./box.img



tar cvzf linux_custom_box.box ./metadata.json ./Vagrantfile ./box.img 

vagrant box add --name linux_custom linux_custom_box.box

vagrant init linux_custom

vagrant up --provider=libvirt