Skip to content

Instantly share code, notes, and snippets.

@craig-m-unsw
Last active December 18, 2022 12:28
Show Gist options
  • Save craig-m-unsw/e329f964d7321af670123e98250cae70 to your computer and use it in GitHub Desktop.
Save craig-m-unsw/e329f964d7321af670123e98250cae70 to your computer and use it in GitHub Desktop.
vagrant based CentOS 7 Development environment example

disposable CentOS 7 VMs

An example of a fast, low effort, cross platform CentOS 7 development environment.

Write code, automate, and then rinse and repeat.

requirements

Your BIOS needs to have support for a Hypervisor, but most do. However this can often be disabled by default, and software is often not good at checking for this. Likely you need to enable VT-x or AMD-V if things seem to fail for no good reason :-)

https://en.wikipedia.org/wiki/X86_virtualization#Hardware-assisted_virtualization

Note for Apple M1 users: these instructions will not work for you. x86 hardware is still fine, but not Apple silicon.

using

Open a terminal and create the file vagrant_centos7 and then proceed:

export VAGRANT_VAGRANTFILE=vagrant_centos7
vagrant validate vagrant_centos7
vagrant status
vagrant up
vagrant ssh-config
vagrant ssh

You should now be logged into your virtual machine.

If you want to login from another client on your Desktop you can paste the output from ssh-config into your ~/.ssh/config file.

With VS-Code you can remote into the VM and work from there: https://code.visualstudio.com/docs/remote/ssh

When finished:

exit
vagrant destroy

Docs

#!/bin/bash
# bash only (not work on MacOS zsh)
BOX_BASE_URL="https://cloud.centos.org/centos/7/vagrant/x86_64/images/"
BOX_VER="CentOS-7-x86_64-Vagrant-2004_01"
declare -A boxes
boxes=(
["VirtualBox"]="7e83943defcb5c4e9bebbe4184cce4585c82805a15e936b01b1e893b63dee2c5"
["HyperV"]="51bb0495a2c01f25ed511ab02608d05c868285d17372be4efedd798f9ac1c81f"
["LibVirt"]="48e14597e88a0663b1748ac2239de6fea5e9976687f5c57b1ba2480612a0b154"
["VMwareFusion"]="7a23242c7b42be2b2ada25b09973601cc31aff6ef7deea5499ad7737ff3f3481"
)
echo "* starting CentOS 7 box downloads";
for boxkey in "${!boxes[@]}"; do
if [ ! -f "${BOX_VER}.${boxkey}.box" ]; then
echo "* downloading";
sleep 3;
curl "${BOX_BASE_URL}${BOX_VER}.${boxkey}.box" -O "${BOX_VER}.${boxkey}.box";
else
echo "* already have file";
ls -lah -- ${BOX_VER}.${boxkey}.box || { echo "error: missing file"; exit 1; }
fi
if [ $? -eq 0 ]; then
echo "* got ${BOX_VER}.${boxkey}.box";
else
echo "ERROR downloading ${BOX_VER}.${boxkey}.box";
exit 1;
fi
shasum_known=${boxes[$boxkey]}
shasum_new=$(sha256sum ${BOX_VER}.${boxkey}.box | awk '{print $1}')
if [[ "$shasum_known" == "$shasum_new" ]]; then
echo "* shasum of ${BOX_VER}.${boxkey}.box is OK";
else
echo "BAD shasum";
exit 1;
fi
done
echo "* finished box download";
# Vagrant use example with CentOS 7
BOX_BASE_URL = "https://cloud.centos.org/centos/7/vagrant/x86_64/images/"
BOX_VER = "CentOS-7-x86_64-Vagrant-2004_01"
$script_prov_root = <<-SCRIPT
yum update
yum upgrade --exclude=kernel\* -y
yum install tmux vim git gdb -y -q
yum group install "Development Tools"
# setup epel
yum install epel-release.noarch -y
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
yum install clang inotify-tools sshfs -y
SCRIPT
$script_prov_user = <<-SCRIPT
mkdir -pv ~/{.local,code,temp}
SCRIPT
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.box_check_update = false
config.vm.synced_folder ".", "/vagrant", disabled: true
config.ssh.keep_alive = true
config.ssh.compression = false
config.ssh.forward_agent = false
config.ssh.insert_key = true
# VirtualBox
config.vm.provider :virtualbox do |virtualbox, override|
override.vm.box_url = BOX_BASE_URL + BOX_VER + ".VirtualBox.box"
override.vm.box_download_checksum_type = "sha256"
override.vm.box_download_checksum = "7e83943defcb5c4e9bebbe4184cce4585c82805a15e936b01b1e893b63dee2c5"
end
# HyperV
config.vm.provider :hyperv do |hpv, override|
override.vm.box_url = BOX_BASE_URL + BOX_VER + ".HyperV.box"
override.vm.box_download_checksum_type = "sha256"
override.vm.box_download_checksum = "51bb0495a2c01f25ed511ab02608d05c868285d17372be4efedd798f9ac1c81f"
end
# LibVirt
config.vm.provider :libvirt do |libv, override|
override.vm.box_url = BOX_BASE_URL + BOX_VER + ".LibVirt.box"
override.vm.box_download_checksum_type = "sha256"
override.vm.box_download_checksum = "48e14597e88a0663b1748ac2239de6fea5e9976687f5c57b1ba2480612a0b154"
end
# VMwareFusion
config.vm.provider :vmware_fusion do |vmf, override|
override.vm.box_url = BOX_BASE_URL + BOX_VER + ".VMwareFusion.box"
override.vm.box_download_checksum_type = "sha256"
override.vm.box_download_checksum = "7a23242c7b42be2b2ada25b09973601cc31aff6ef7deea5499ad7737ff3f3481"
end
config.vm.provision :shell,
name: "root setup script",
upload_path: "/home/vagrant/.setup_root.sh",
inline: $script_prov_root,
:privileged => true
config.vm.provision :shell,
name: "user setup script",
upload_path: "/home/vagrant/.setup_user.sh",
inline: $script_prov_user,
:privileged => false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment