Skip to content

Instantly share code, notes, and snippets.

@mochidamochiko
Last active June 15, 2016 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mochidamochiko/973895e2eca648339cb1126cd39dd409 to your computer and use it in GitHub Desktop.
Save mochidamochiko/973895e2eca648339cb1126cd39dd409 to your computer and use it in GitHub Desktop.
Vagrant用のBoxを自動作成するスクリプト。Macで動作確認済み。
#!/bin/bash
########################################
### user configuration
########################################
VM_NAME=centos67-minimal
VM_OS_TYPE=RedHat_64
OS_ISO_IMAGE_PATH=~/Downloads/CentOS-6.7-x86_64-minimal.iso
VBOX_GUEST_ADDITION_ISO_IMAGE_PATH=/Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso
########################################
### main process
########################################
#
# create VM
#
VBoxManage createvm --name ${VM_NAME} --ostype ${VM_OS_TYPE} --register
VBoxManage modifyvm ${VM_NAME} \
--memory 512 \
--vram 12 \
--rtcuseutc on \
--boot1 dvd --boot2 disk --boot3 none
vm_path_dir=$(VBoxManage showvminfo ${VM_NAME} | grep vbox | sed "s/\/${VM_NAME}\.vbox//" | sed "s/Config file:\s*//")
vm_path_dir=`echo ${vm_path_dir}`
VBoxManage createhd \
--filename "${vm_path_dir}/${VM_NAME}.vdi" \
--size 8192
VBoxManage storagectl ${VM_NAME} \
--name IDE \
--add ide
VBoxManage storagectl ${VM_NAME} \
--name SATA \
--add sata \
--portcount 1
VBoxManage storageattach ${VM_NAME} \
--storagectl IDE \
--port 1 \
--device 0 \
--type dvddrive \
--medium emptydrive
VBoxManage storageattach ${VM_NAME} \
--storagectl SATA \
--port 0 \
--type hdd \
--medium "${vm_path_dir}/${VM_NAME}.vdi"
#
# start VM and OS install
#
VBoxManage storageattach ${VM_NAME} \
--storagectl IDE \
--port 1 \
--device 0 \
--type dvddrive \
--medium "${OS_ISO_IMAGE_PATH}"
VBoxManage startvm ${VM_NAME}
echo ""
echo "########################################"
echo "#"
echo "# OS Install & Network Configuration"
echo "#"
echo "# 1. OS Installing. Root password is \"vagrant\"."
echo "# 2. After OS Installed, then reboot and root login."
echo "# 3. sed -i -e \"s/ONBOOT=no/ONBOOT=yes/\" /etc/sysconfig/network-scripts/ifcfg-eth0"
echo "# 4. service network restart"
echo "# 5. Then, press Enter Key"
echo "#"
echo "########################################"
read pause
while :
do
echo -n "Is Done? If you done, type y: "
read key
case "${key}" in
"y" ) break ;;
"*" ) ;;
esac
done
#
# install VirtualBox Guest Additions
#
VBoxManage storageattach ${VM_NAME} \
--storagectl IDE \
--port 1 \
--device 0 \
--type dvddrive \
--medium "${VBOX_GUEST_ADDITION_ISO_IMAGE_PATH}"
VBoxManage controlvm ${VM_NAME} natpf1 ssh,tcp,,2222,,22
cmdset1="
yum update -y
yum install -y kernel-devel kernel-headers gcc perl
reboot
"
expect << EOF1
set timeout -1
spawn ssh -p 2222 root@localhost -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null
expect "password:"
send "vagrant\r"
expect "root@localhost ~]#"
send "${cmdset1}\r"
expect eof
exit
EOF1
echo ""
echo "########################################"
echo "#"
echo "# Now VM rebooting... After that, press Enter Key"
echo "#"
echo "########################################"
read pause
while :
do
echo -n "Is Done? If you done, type y: "
read key
case "${key}" in
"y" ) break ;;
"*" ) ;;
esac
done
cmdset2="
mkdir /media/cdrom
mount -r /dev/cdrom /media/cdrom
sh /media/cdrom/VBoxLinuxAdditions.run
umount /media/cdrom
exit
"
expect << EOF2
set timeout -1
spawn ssh -p 2222 root@localhost -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null
expect "password:"
send "vagrant\r"
expect "root@localhost ~]#"
send "${cmdset2}\r"
expect eof
exit
EOF2
#
# OS configuration for vagrant
#
cmdset3="
# useradd
useradd -m vagrant
echo 'vagrant' | passwd --stdin vagrant
# ssh-key
mkdir /home/vagrant/.ssh
chmod 700 /home/vagrant/.ssh
curl -k -L -o /home/vagrant/.ssh/authorized_keys 'https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub'
chmod 600 /home/vagrant/.ssh/authorized_keys
chown -R vagrant:vagrant /home/vagrant/.ssh
# sudo
sed -i -e 's/Defaults requiretty/#Defaults requiretty/' /etc/sudoers
sed -i -e '/NOPASSWD/a vagrant\\\tALL=(ALL)\\\tNOPASSWD: ALL' /etc/sudoers
# disable iptables
service iptables stop
service ip6tables stop
chkconfig iptables off
chkconfig ip6tables off
# disable selinux
sed -i -e 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux
# standby box
yum clean all
dd if=/dev/zero of=/EMPTY bs=1M
rm -f /EMPTY
shutdown -h now
"
expect << EOF3
set timeout -1
spawn ssh -p 2222 root@localhost -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null
expect "password:"
send "vagrant\r"
expect "root@localhost ~]#"
send "${cmdset3}\r"
expect eof
exit
EOF3
# post OS configuration
VBoxManage storageattach ${VM_NAME} \
--storagectl IDE \
--port 1 \
--device 0 \
--type dvddrive \
--medium emptydrive
VBoxManage controlvm ${VM_NAME} natpf1 delete ssh
#
# create box
#
vagrant package --base ${VM_NAME} --output ${VM_NAME}.box
#
# delete VM
#
VBoxManage unregistervm ${VM_NAME} --delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment