# Install vagrant-libvirt with kvm and create a box from scratch.
# This is a install example for a kvm running on centos 6 server, and 
creating a scratch centos 6 box for vagrant. 
# Hopefully it will save some users time when trying to figure this out.

yum update libvirt
yum update kvm
yum install libxslt-devel libxml2-devel libvirt-devel libguestfs-tools-c

# https://www.vagrantup.com/downloads.html
rpm -i "https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.2_x86_64.rpm";

vagrant plugin install vagrant-libvirt

# other great options:
vagrant plugin install vagrant-rekey-ssh
vagrant plugin install sahara
vagrant plugin install vagrant-mutate
vagrant plugin install vagrant-triggers


# kvm install example:
# Create your default using virt-install or the GUI tools.
# The example image I used was a CentOS 6.6 install.
# Note we are not using vagrant to create this, but making a base image 
from scratch.
# Not really covering this, use whatever method works for you to create a 
default VM

sudo virt-install \
   --accelerate \
   --hvm \
   --name=vagrant-centos66-base \
   --disk 
path=/var/lib/libvirt/images/vagrant-centos66-base.qcow2,format=qcow2,size=30,sparse=true
 
\
   --vnc \
   --network bridge=br0  \
   --vcpus=2 --ram=1024 \
   --os-type=linux \
   --os-variant=rhel6 \
   --keymap="en-us" \
   --mac="01:01:01:01:01:1F" \
   --location="ftp://<yourserver>/pub/inst" \
   --extra-args="ks=ftp://<yourserver>/pub/kickstart.cfg"




# log into vagrant-centos66-base VM as root and preform the following prep 
for the box:

# Yum rsync and update
yum -y install rsync
yum -y update
yum clean all


# Create the vagrant user
useradd vagrant
mkdir ~vagrant/.ssh
chmod 700 ~vagrant/.ssh
wget --no-check-certificate 
https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub 
-O /home/vagrant/.ssh/authorized_keys
chmod 600 ~vagrant/.ssh/authorized_keys
chown -R vagrant.vagrant ~vagrant/.ssh


# Setup Sudoers
# Comment for rsync to work.
sed -i -r 's/^Defaults .*requiretty/#Defaults requiretty/' /etc/sudoers

# Add vagrant account to sudoers
echo " " >> /etc/sudoers
echo "# User rules for vagrant" >> /etc/sudoers
echo "vagrant ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
echo " " >> /etc/sudoers
visudo -c

#Speed up Grub
sed -i -r 's/^timeout.*/timeout=1/' /boot/grub/grub.conf

# Fix Network Settings
sed -i -r 's/^HOSTNAME.*/HOSTNAME=centos66.vagrantup.com/' 
/etc/sysconfig/network

# Leave only eth0 config - the rest will be managed by vagrant
rm -f /etc/udev/rules.d/70-persistent-net.rules
rm -f /etc/sysconfig/network-scripts/ifcfg-eth{0,1,2,3}
cat > /etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
DEVICE="eth0"
TYPE="Ethernet"
ONBOOT="yes"
NM_CONTROLLED=no
BOOTPROTO="dhcp"
EOF

# password change (not necessary, unless making a public box)
#echo "vagrant" | passwd --stdin vagrant
#echo "vagrant" | passwd --stdin root

#shutdown vagrant-centos66-base
poweroff



# Add VAGRANT_DEFAULT_PROVIDER ENV to your bash shell.
echo "" >> ~/.bashrc 
echo "# User ENV for vagrant" >> ~/.bashrc 
echo "export VAGRANT_DEFAULT_PROVIDER=libvirt" >> ~/.bashrc 
echo "" >> ~/.bashrc 
source ~/.bashrc


#Copy the qcow2 to a new folder under the name box.img

mkdir ~/newbox
cd ~/newbox
cp /var/lib/libvirt/images/vagrant-centos66-base.qcow2 ~/newbox/box.img


#add:

#Create Vagrantfile
cat > ~/newbox/Vagrantfile <<EOF
Vagrant.configure("2") do |config|
  config.vm.box = "vagrant-centos66-base"
end
EOF


#Create metadata.json
cat > ~/newbox/metadata.json <<EOF
{
  "provider": "libvirt",
  "format": "qcow2",
  "virtual_size": 30
}
EOF

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

vagrant box add base-centos66 centos66.box 

# you don't want to use ~/newbox as your rsync directory, make a new one.
mkdir ~/vagrant
cd ~/vagrant
vagrant init base-centos66
vagrant up (optinal now: --provider=libvirt)
vagrant destroy
# always refresh virsh pool after a destroy, clears up a lot of potential 
problems.
virsh pool-refresh default


# Remove a libvirt box (example: testbox)
vagrant box remove testbox
rm /var/lib/libvirt/images/testbox_vagrant_box_image_0.img
virsh pool-refresh default
See: https://github.com/pradels/vagrant-libvirt/issues/85


#Extras:
################


# Useful bootstrap.sh info:
#!/usr/bin/env bash

# If you need to change your box hostname:
#sed -i -r 's/^HOSTNAME.*/HOSTNAME=newhost/' /etc/sysconfig/network
#hostname newhost

#Sync your clock.
ntpdate 1.pool.ntp.org


#Useful Vagrantfile info: Setting up a publicly accessible server using kvm 
interface br0
Vagrant.configure("2") do |config|

# Assumes you are using dhcp, 
# Included an example on how to set a MAC address.
Vagrant.configure("2") do |config|
  config.vm.define :default do |default|
    default.vm.box = "base-centos66"
    default.vm.network :public_network, :mac => " 000101010101", :dev => 
"br0", :mode => 'bridge'
  end
end

-- 
You received this message because you are subscribed to the Google Groups 
"Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to