Skip to content

Instantly share code, notes, and snippets.

@TomasKulhanek
Last active September 19, 2022 08:36
Show Gist options
  • Save TomasKulhanek/83540398747d6c593a6da69394984b65 to your computer and use it in GitHub Desktop.
Save TomasKulhanek/83540398747d6c593a6da69394984b65 to your computer and use it in GitHub Desktop.
Script to prepare Centos9stream vagrant box with minimal gui

Preparing vagrant VM template

The following section describes how to prepare VM template from scratch, e.g. to be used as base vagrant box or as a base VM template. It's not needed to perform these steps when installing any products, however, might be usefull to maintain version of base OS.

Centos 9 stream

Download ISO

As a base for most VM templates, download Centos 9 stream iso

Install minimal system

In installation packages - select Minimal system. Create disk partition /boot and / without swap. Create swap manually

Define root and vagrant user

Set root password (vagrant) and create new user (vagrant:vagrant)

Post-installation script

In Virtualbox (6.1.38 +) - Insert VBoxGuest Additions Devices -> Insert Guest Additions CD image .... note that in older Virtualbox < 6.1.36 the kernel modules won't build as scripts do not support kernel 5.14 or later.

Log-in as root, and execute one of the following script:

  1. for non-GUI environment: bash <(curl -L https://bit.ly/2xDpLwR)
  2. for GUI environment: bash <(curl -L http://bit.ly/2GfrE7z)

Reset, check if everything works, if new kernel was installed - then manually uninstall old kernel

uname -a
# outputs which kernel is loaded
rpm -q kernel
# outputs which kernel is installed
yum remove kernel-...
# uninstalls unused kernel
bash <(curl -L ....)
#repeat post-install script 1. for non-GUI or 2. GUI

Create box

Stop virtualbox, remove unused IDE, sound card, change video memory etc.

Launch vagrant script to package box. Expecting the virtual machine name is my-sl7-virtualmachine

vagrant package --output centos9mini.box --base my-centos9-virtualmachine

Explanation:

  • package instruct vagrant to get virtual machine from virtual box and package it into separate file
  • --output centos9mini.box writes the result to file named as centos9mini.box
  • --base my-centos9-virtualmachine takes VirtualBox virtual machine named my-centos9-virtualmachine
#enable sudo
echo 'vagrant ALL=(ALL) NOPASSWD:ALL' | sudo EDITOR='tee -a' visudo
# update system
yum update -y
# expected that SL7 is installed as minimal system, vagrant account as administrator, run with sudo privileges
yum -y install epel-release
yum -y groupinstall "Xfce"
yum -y install xfce4-netload-plugin xfce4-systemload-plugin
yum -y remove openbox orage pavucontrol postfix tigervnc*
# start gdm
systemctl set-default graphical.target
# automatic login vagrant
sed -i '/^\[daemon\]/,/^\[security\]/{//!d}' /etc/gdm/custom.conf
sed -i '/\[daemon\]/ aAutomaticLoginEnable=True\n AutomaticLogin=vagrant' /etc/gdm/custom.conf
# systemctl set-default graphical.target
# enable ssh
systemctl enable sshd
yum install -y wget
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
#install vboxguest
yum install -y bzip2 gcc kernel-devel kernel-headers tar perl dkms
mount /dev/cdrom /mnt
cd /mnt
./VBoxLinuxAdditions.run
# clean
yum remove -y gcc kernel-devel
yum autoremove -y
yum remove -y perl-*
cd /home/vagrant
wget https://gist.github.com/TomasKulhanek/21e4544823dfcd181b3d0787a5b525a1/raw/1973aad8ebb9754ee1c598c0cb568b55fd625c5a/sshd_config -O /etc/ssh/sshd_config
function print_green {
echo -e "\e[32m${1}\e[0m"
}
print_green 'Clean Yum'
yum -y remove linux-firmware
yum -y remove iwl*
yum -y autoremove
yum clean all
rm -rf /var/cache/yum
print_green 'Cleanup bash history'
unset HISTFILE
[ -f /root/.bash_history ] && rm /root/.bash_history
[ -f /home/vagrant/.bash_history ] && rm /home/vagrant/.bash_history
rm -rf /home/vagrant/.cache /home/vagrant/.local /root/.cache /root/.local
print_green 'Cleanup log files'
find /var/log -type f | while read f; do echo -ne '' > $f; done
print_green 'Whiteout root'
count=`df --sync -kP / | tail -n1 | awk -F ' ' '{print $4}'`
let count--
dd if=/dev/zero of=/tmp/whitespace bs=1024 count=$count
rm /tmp/whitespace
print_green 'Whiteout swap'
swapoff -v /swapfile
sudo dd if=/dev/zero of=/swapfile bs=1024 count=2097152
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
if grep -q '/swapfile' /etc/fstab; then
echo "swapfile already in fstab"
else
echo "/swapfile swap swap defaults 0 0\n" >>/etc/fstab
echo "adding /swapfile to fstab"
fi
print_green 'Vagrant cleanup complete'
yum install -y wget
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
function print_green {
echo -e "\e[32m${1}\e[0m"
}
print_green 'Clean Yum'
yum -y remove linux-firmware
yum -y remove iwl*
yum -y autoremove
yum clean all
rm -rf /var/cache/yum
print_green 'Cleanup bash history'
unset HISTFILE
[ -f /root/.bash_history ] && rm /root/.bash_history
[ -f /home/vagrant/.bash_history ] && rm /home/vagrant/.bash_history
rm -rf /home/vagrant/.cache /home/vagrant/.local /root/.cache /root/.local
print_green 'Cleanup log files'
find /var/log -type f | while read f; do echo -ne '' > $f; done
print_green 'Whiteout root'
count=`df --sync -kP / | tail -n1 | awk -F ' ' '{print $4}'`
let count--
dd if=/dev/zero of=/tmp/whitespace bs=1024 count=$count
rm /tmp/whitespace
print_green 'Whiteout swap'
swapoff -v /swapfile
sudo dd if=/dev/zero of=/swapfile bs=1024 count=2097152
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
if grep -q '/swapfile' /etc/fstab; then
echo "swapfile already in fstab"
else
echo "/swapfile swap swap defaults 0 0\n" >>/etc/fstab
echo "adding /swapfile to fstab"
fi
print_green 'Vagrant cleanup complete'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment