diff --git a/README b/README index 6b1871d..4e68c6f 100644 --- a/README +++ b/README @@ -5,15 +5,15 @@ Scripts to create a lean CentOS Vagrant box. Run: - ./setup + ./box initialize and at the boot prompt press tab to gain access to the boot options. Add the `ks=.*` string you get from the command prompt. The rest of the installation is automated. -Finally, run the last command that `setup` spits out (it's of the -form `./cleanup && ...`). Congratulations! You have just created a -Vagrant box. +Finally, run the last command that the initialize step spits out +(it's of the form `./cleanup && ...`). Congratulations! You have +just created a Vagrant box. Specification diff --git a/box b/box new file mode 100755 index 0000000..2c39342 --- /dev/null +++ b/box @@ -0,0 +1,83 @@ +#!/bin/sh + +vc_printerr () { + printf "$@" 1>&2 +} + +vc_usage () { + vc_printerr "usage: $0" + vc_printerr " action\n" +} + +vc_get_file_dir () { + local src + + src="$1" + while [ -h "${src}" ]; do src="$(readlink "${src}")"; done + src=$(cd -P $(dirname "${src}") && pwd) + + printf "${src}" +} + +vc_check_file_readable () { + if [ ! -r "$1" ]; then + vc_printerr "$0: Cannot read file: \"$1\".\n\n" + vc_usage + exit 2 + fi + printf "$1" +} + +vc_parse_options () { + . ./vars.sh +} + +vc_parse_args () { + local arg + local args + + args=$(getopt -n "$0" "$@") + if [ $? -ne 0 ]; then + vc_usage + exit 1 + fi + + set -- ${args} + for arg in "$@"; do + case "${arg}" in + --) + shift + break + ;; + esac + done + + case "$1" in + initialize|cleanup) + ACTION="$1"; shift + ;; + *) + vc_printerr "$0: Unrecognised action: \"$1\".\n\n" + vc_usage + exit 3 + ;; + esac +} + + +ROOT_DIR=$(vc_get_file_dir $0) + +. "${ROOT_DIR}/src/initialize.sh" +. "${ROOT_DIR}/src/cleanup.sh" + +vc_parse_options +vc_parse_args "$@" + +case "${ACTION}" in + initialize) + vc_action_initialize + ;; + cleanup) + vc_action_cleanup + ;; +esac diff --git a/cleanup b/cleanup deleted file mode 100755 index 7171278..0000000 --- a/cleanup +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/sh - -. ./vars.sh - -set -x - -VBoxManage modifyvm ${NAME} \ - --boot1 disk --boot2 none --boot3 none --boot4 none \ - -VBoxManage storagectl ${NAME} \ - --name SATA --remove - -VBoxManage storagectl ${NAME} \ - --name SATA --add sata --portcount 1 --bootable on - -VBoxManage storageattach ${NAME} \ - --storagectl SATA --port 0 --type hdd \ - --medium "${HDD}" -VBoxManage storageattach ${NAME} \ - --storagectl SATA --port 1 --type hdd \ - --medium "${HDD_SWAP}" diff --git a/setup b/setup deleted file mode 100755 index c2607e6..0000000 --- a/setup +++ /dev/null @@ -1,83 +0,0 @@ -#!/bin/sh -e - -. ./vars.sh - -VBoxManage createvm --name ${NAME} --ostype ${TYPE} --register - -VBoxManage modifyvm ${NAME} \ - --vram 12 \ - --accelerate3d off \ - --memory 613 \ - --usb off \ - --audio none \ - --boot1 disk --boot2 dvd --boot3 none --boot4 none \ - --nictype1 virtio --nic1 nat --natnet1 "${NATNET}" \ - --nictype2 virtio \ - --nictype3 virtio \ - --nictype4 virtio \ - --acpi on --ioapic off \ - --chipset piix3 \ - --rtcuseutc on \ - --hpet on \ - --bioslogofadein off \ - --bioslogofadeout off \ - --bioslogodisplaytime 0 \ - --biosbootmenu disabled - -VBoxManage createhd --filename "${HDD}" --size 8192 -# Swap is recommended to be double the size of RAM. -VBoxManage createhd --filename "${HDD_SWAP}" --size 1226 - -VBoxManage storagectl ${NAME} \ - --name SATA --add sata --portcount 2 --bootable on - -VBoxManage storageattach ${NAME} \ - --storagectl SATA --port 0 --type hdd --medium "${HDD}" -VBoxManage storageattach ${NAME} \ - --storagectl SATA --port 1 --type hdd --medium "${HDD_SWAP}" -VBoxManage storageattach ${NAME} \ - --storagectl SATA --port 2 --type dvddrive --medium "${INSTALLER}" -VBoxManage storageattach ${NAME} \ - --storagectl SATA --port 3 --type dvddrive --medium "${GUESTADDITIONS}" - -VBoxManage startvm ${NAME} --type gui - -setup_ruby() { - # handle errors in here - set +e - - SOURCE=$(which vagrant) - if [ $? -eq 0 ]; then - while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done - EMBEDDED_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )/../embedded" - - unset RUBYOPT - RUBY_EXECUTABLE="${EMBEDDED_DIR}/bin/ruby" - else - RUBY_EXECUTABLE=$(which ruby) - if [ ! $? -eq 0 ]; then - echo "Cannot find Ruby (needed for kickstart file)" - exit 1 - fi - fi - - set -e -} - -# This only really caters for the common case. If you have problems, please -# discover your host's IP address and adjust accordingly. -IP=`echo ${NATNET} | sed -nE 's/^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*/\1/p'` - -setup_ruby - -echo 'At the boot prompt, hit and then type:' -/bin/echo -n " ks=http://${IP}.3:" -${RUBY_EXECUTABLE} httpd.rb ks.cfg - -echo "The box has accepted the kickstart file. It will now go through" -echo "a lengthy install. When it is finished it will shutdown and you" -echo "can run:" -echo -echo " ./cleanup && vagrant package --base ${NAME} --output boxes/${NAME}-`date +%Y%m%d`.box" -echo -echo "to create a Vagrant box." diff --git a/src/cleanup.sh b/src/cleanup.sh new file mode 100644 index 0000000..6ddcc1d --- /dev/null +++ b/src/cleanup.sh @@ -0,0 +1,17 @@ +vc_action_cleanup () { + VBoxManage modifyvm ${NAME} \ + --boot1 disk --boot2 none --boot3 none --boot4 none \ + + VBoxManage storagectl ${NAME} \ + --name SATA --remove + + VBoxManage storagectl ${NAME} \ + --name SATA --add sata --portcount 1 --bootable on + + VBoxManage storageattach ${NAME} \ + --storagectl SATA --port 0 --type hdd \ + --medium "${HDD}" + VBoxManage storageattach ${NAME} \ + --storagectl SATA --port 1 --type hdd \ + --medium "${HDD_SWAP}" +} diff --git a/httpd.rb b/src/httpd.rb similarity index 100% rename from httpd.rb rename to src/httpd.rb diff --git a/src/initialize.sh b/src/initialize.sh new file mode 100644 index 0000000..d37e28a --- /dev/null +++ b/src/initialize.sh @@ -0,0 +1,93 @@ +vc_initialize_ruby_exec () { + local embedded_dir + local ruby_exec + local src + + src=$(which vagrant) + if [ $? -eq 0 ]; then + embedded_dir="$(vc_get_file_dir "${src}")/../embedded" + + unset RUBYOPT + ruby_exec="${embedded_dir}/bin/ruby" + else + ruby_exec=$(which ruby) + if [ ! $? -eq 0 ]; then + vc_printerr "$0: Cannot find Ruby (needed to serve kickstart file)" + exit 4 + fi + fi + + printf "${ruby_exec}" +} + +vc_initialize_setup_box () { + VBoxManage createvm --name ${NAME} --ostype ${TYPE} --register + + VBoxManage modifyvm ${NAME} \ + --vram 12 \ + --accelerate3d off \ + --memory 613 \ + --usb off \ + --audio none \ + --boot1 disk --boot2 dvd --boot3 none --boot4 none \ + --nictype1 virtio --nic1 nat --natnet1 "${NATNET}" \ + --nictype2 virtio \ + --nictype3 virtio \ + --nictype4 virtio \ + --acpi on --ioapic off \ + --chipset piix3 \ + --rtcuseutc on \ + --hpet on \ + --bioslogofadein off \ + --bioslogofadeout off \ + --bioslogodisplaytime 0 \ + --biosbootmenu disabled + + VBoxManage createhd --filename "${HDD}" --size 8192 + VBoxManage createhd --filename "${HDD_SWAP}" --size 1226 + + VBoxManage storagectl ${NAME} \ + --name SATA --add sata --portcount 2 --bootable on + + VBoxManage storageattach ${NAME} \ + --storagectl SATA --port 0 --type hdd --medium "${HDD}" + VBoxManage storageattach ${NAME} \ + --storagectl SATA --port 1 --type hdd --medium "${HDD_SWAP}" + VBoxManage storageattach ${NAME} \ + --storagectl SATA --port 2 --type dvddrive --medium "${INSTALLER}" + VBoxManage storageattach ${NAME} \ + --storagectl SATA --port 3 --type dvddrive --medium "${GUESTADDITIONS}" + + VBoxManage startvm ${NAME} --type gui +} + +vc_initialize_start_httpd () { + # This only really caters for the common case. If you have + # problems, please discover your host's IP address and adjust + # accordingly. + IP=${NATNET%.*/*} + + printf "At the boot prompt, hit and then type:\n\n" + printf " ks=http://${IP}.3:" + + ruby=$(vc_initialize_ruby_exec) + "${ruby}" "${ROOT_DIR}/src/httpd.rb" "${ROOT_DIR}/ks.cfg" +} + +vc_initialize_cleanup_msg () { + printf "\n\n" + printf "The box has accepted the kickstart file. It will now go through\n" + printf "a lengthy install. When it is finished it will shutdown and you\n" + printf "can run:\n\n" + printf " ./box cleanup && vagrant package --base ${NAME} --output boxes/${NAME}-`date +%Y%m%d`.box\n\n" + printf "to create a Vagrant box.\n" +} + +vc_action_initialize () { + local ruby + + vc_initialize_setup_box + vc_initialize_start_httpd + + vc_initialize_cleanup_msg +}