Skip to content

Commit 6513d36

Browse files
committed
Install CentOS using kickstart on virtualbox VM
0 parents  commit 6513d36

7 files changed

+341
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.7z
2+
*.box

anaconda-ks.cfg

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#version=RHEL7
2+
#Reference for ks.config: http://marclop.svbtle.com/creating-an-automated-centos-7-install-via-kickstart-file
3+
# Text mode or graphical mode?
4+
#graphical
5+
text
6+
7+
# Action
8+
install
9+
10+
# System authorization information
11+
auth --enableshadow --passalgo=sha512
12+
13+
# Use CDROM installation media
14+
cdrom
15+
16+
# installation path for network installation
17+
#url --url=http://192.168.0.3:8000/RPM/
18+
#repo --name="EPEL" --baseurl=http://dl.fedoraproject.org/pub/epel/7/x86_64/
19+
20+
# System language
21+
lang en_US.UTF-8
22+
23+
# Keyboard layouts
24+
keyboard --vckeymap=in-eng --xlayouts='in (eng)'
25+
26+
# Run the Setup Agent on first boot
27+
firstboot --enable
28+
ignoredisk --only-use=sda
29+
30+
# Network information
31+
network --bootproto=dhcp --device=enp0s3 --noipv6 --activate
32+
# Modify the static ip to a relevant value
33+
network --bootproto=static --device=enp0s8 --ip=<static_ip> --netmask=255.255.255.0 --noipv6 --activate
34+
# Specify a hostname
35+
network --hostname=<hostname>
36+
37+
# Root password
38+
rootpw --iscrypted $6$8qxjjbNHuAXaJ2/K$kWQ9QEQAmv/FEKmpv7q4RtVsEuOBccfLQjcdZjwk2DM3HOuXIEXrC2E0nNs9SurFC28gMT4hnH.XgHk/pH3ch0
39+
40+
# System timezone
41+
timezone Asia/Kolkata --isUtc
42+
43+
# Partition clearing information
44+
clearpart --linux --drives=sda
45+
zerombr
46+
47+
# System bootloader configuration
48+
bootloader --location=mbr --boot-drive=sda
49+
autopart --type=lvm
50+
51+
# Reboot after installation?
52+
shutdown
53+
54+
##############################################################################
55+
#
56+
# packages part of the KickStart configuration file
57+
#
58+
##############################################################################
59+
%packages
60+
@core
61+
kexec-tools
62+
make
63+
64+
%end
65+
66+
##############################################################################
67+
#
68+
# post installation part of the KickStart configuration file
69+
#
70+
##############################################################################
71+
%post
72+
#
73+
# This section describes all the post-Anaconda steps to fine-tune the installation
74+
#
75+
76+
# This line allows printing to console, which otherwise is invisible.
77+
exec < /dev/console > /dev/console
78+
79+
# Install rpm repo epel-release
80+
/usr/bin/yum -y install epel-release
81+
82+
# Install net-tools for ifconfig command
83+
echo "Installing net-tools for ifconfig command."
84+
/usr/bin/yum -y install net-tools
85+
86+
# Install other dependencies
87+
echo "Installing other dependencies."
88+
/usr/bin/yum -y install wget
89+
90+
#/usr/bin/rpm -i https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
91+
/usr/bin/yum -y epel-release
92+
93+
# Update the RPMs
94+
echo "Updating the RPMs.."
95+
/usr/bin/yum -y --skip-broken --obsolete update
96+
97+
# Housekeeping
98+
echo "Housekeeping.."
99+
/usr/bin/yum autoremove
100+
/usr/bin/yum clean all
101+
102+
# Guest Additions
103+
echo "Installing other dependencies for Guest Additions."
104+
/usr/bin/yum -y install bzip2
105+
/usr/bin/yum -y install gcc
106+
/usr/bin/yum -y install dkms
107+
/usr/bin/yum -y install kernel-devel
108+
#mkdir -p /mnt/cdrom
109+
#mount -o ro -t iso9660 /dev/sr1 /mnt/cdrom
110+
#/usr/bin/yum -y --skip-broken update
111+
#echo 'yes'|/mnt/cdrom/VBoxLinuxAdditions.run
112+
#umount /dev/sr1
113+
114+
%end

installation using ks.txt

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
1. Create directory for ks host
2+
$ mkdir -p ~/Workspace/ks
3+
4+
2. Rename anaconda-ks.cfg to ks.cfg
5+
6+
3. Copy the ks.cfg file to ~/Workspace/ks
7+
8+
4. Open a bash termial or cmd and launch a python SimpleHTTPServer
9+
Bash:
10+
$ cd ~/Workspace
11+
$ python -m SimpleHTTPServer
12+
13+
cmd:
14+
> cd %USERPROFILE%\Workspace
15+
> python -m SimpleHTTPServer
16+
17+
5. Test the server
18+
a. Check machine IP using ipconfig command (ifconfig on linux)
19+
b. Open a web-browser
20+
c. Open link: http://<machine-ip>:8000/ks
21+
NOTE: You shall see a link to ks.cfg. Clinking the link shall download the file.
22+
23+
6. User the link to point at ks file during Centos installation:
24+
vmlinuz initrd=initrd.img ks=http://<machine-ip>:8000/ks/ks.cfg

installing guestadditions.txt

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Verify CD-ROM:
2+
--------------
3+
$ dmesg|egrep -i --color 'cdrom|dvd|cd/rw|writer'
4+
[ 12.565746] cdrom: Uniform CD-ROM driver Revision: 3.20
5+
6+
$ lsblk
7+
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
8+
sda 8:0 0 8G 0 disk
9+
├─sda1 8:1 0 500M 0 part /boot
10+
└─sda2 8:2 0 7.5G 0 part
11+
├─centos-root 253:0 0 6.7G 0 lvm /
12+
└─centos-swap 253:1 0 820M 0 lvm [SWAP]
13+
sr0 11:0 1 56M 0 rom
14+
15+
Install Dependencies:
16+
---------------------
17+
$ yum -y install epel-release
18+
19+
## Install gcc
20+
$ yum -y install gcc
21+
22+
## Install dkms
23+
$ yum -y install dkms
24+
25+
Mount CD-ROM:
26+
-------------
27+
$ mkdir -p /mnt/cdrom
28+
$ mount -o ro -t iso9660 /dev/cdrom /mnt/cdrom/
29+
30+
Run GuestAdditions:
31+
-------------------
32+
$ cd /mnt/cdrom/
33+
$ sh ./VBoxLinuxAdditions.run
34+
35+
Unmount CD-ROM:
36+
-------------
37+
$ umount -a

notes.txt

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Steps for using Kickstart file:
2+
-------------------------------
3+
4+
1. Create directory for ks host
5+
$ mkdir -p ~/Workspace/ks
6+
7+
2. Rename anaconda-ks.cfg to ks.cfg
8+
9+
3. Copy the ks.cfg file to ~/Workspace/ks
10+
11+
4. Open a bash termial or cmd and launch a python SimpleHTTPServer
12+
Bash:
13+
$ cd ~/Workspace
14+
$ python -m SimpleHTTPServer
15+
16+
cmd:
17+
> cd %USERPROFILE%\Workspace
18+
> python -m SimpleHTTPServer
19+
20+
5. Test the server
21+
a. Check machine IP using ipconfig command (ifconfig on linux)
22+
b. Open a web-browser
23+
c. Open link: http://<machine-ip>:8000/ks
24+
NOTE: You shall see a link to ks.cfg. Clinking the link shall download the file.
25+
26+
6. User the link to point at ks file during Centos installation:
27+
vmlinuz initrd=initrd.img ks=http://<machine-ip>:8000/ks/ks.cfg
28+
29+
NOTE: This file can be used on any VM (online/local Virtual box)
30+
31+
32+
Steps to clone VM from vm-centos7 base box:
33+
-------------------------------------------
34+
35+
1. Copy the base box to "C:\Users\<username>\VirtualBox VMs"
36+
2. Change directory to "C:\Users\<username>\VirtualBox VMs"
37+
3. Run: /c/Program\ Files/Oracle/VirtualBox/VBoxManage.exe registervm "/c/Users/<username>/VirtualBox\ VMs/vm-centos7/vm-centos7.vbox"
38+
4. Copy the downloaded vbox-provisioner.sh to "C:\Users\<gid>\VirtualBox VMs\<name_of_new_vm>\shared"
39+
5. Connect to the VM using the downloaded script: vbox-connect.sh
40+

vbox-clone.sh

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/sh
2+
3+
VM_NAME=$1
4+
if [[ -z ${VM_NAME} ]]; then
5+
read "Specify VM name: " VM_NAME
6+
fi
7+
if [[ -z ${VM_NAME} ]]; then exit 1; fi
8+
9+
"/c/Program Files/Oracle/VirtualBox/VBoxManage.exe" --nologo clonevm vm-centos7 --mode machine --name ${VM_NAME} --basefolder "${HOME}/VirtualBox VMs" --register
10+
11+
# Delete shared drive folder if it exists
12+
echo "Disconnecting shared drive."
13+
"/c/Program Files/Oracle/VirtualBox/VBoxManage.exe" --nologo sharedfolder remove "${VM_NAME}" --name "share"
14+
if [[ -d "${HOME}/VirtualBox VMs/${VM_NAME}/share" ]]; then
15+
rm -rf "${HOME}/VirtualBox VMs/${VM_NAME}/share"
16+
fi
17+
18+
# Create a shared drive folder
19+
if [[ ! -d "${HOME}/VirtualBox VMs/${VM_NAME}/share" ]]; then
20+
echo "Creating shared drive."
21+
mkdir -p "${HOME}/VirtualBox VMs/${VM_NAME}/share"
22+
fi
23+
24+
# Register shared drive
25+
echo "Registering shared drive."
26+
"/c/Program Files/Oracle/VirtualBox/VBoxManage.exe" --nologo sharedfolder add "${VM_NAME}" --name "share" --hostpath "${HOME}/VirtualBox VMs/${VM_NAME}/share" --automount
27+
28+
# Copy files to shared drive
29+
if [[ ! -d "${HOME}/VirtualBox VMs/${VM_NAME}/share/.ssh/" ]]; then
30+
mkdir -p "${HOME}/VirtualBox VMs/${VM_NAME}/share/.ssh/"
31+
fi
32+
cp "${HOME}/.ssh/id_rsa" "${HOME}/VirtualBox VMs/${VM_NAME}/share/.ssh/"
33+
cp "${HOME}/.ssh/id_rsa.pub" "${HOME}/VirtualBox VMs/${VM_NAME}/share/.ssh/"
34+
cat "${HOME}/VirtualBox VMs/vagrant-centos7/share/.ssh/id_rsa.pub">"${HOME}/VirtualBox VMs/${VM_NAME}/share/authorized_keys"
35+
36+
cp "${HOME}/.vimrc" "${HOME}/VirtualBox VMs/${VM_NAME}/share"
37+
cp "${HOME}/Google Drive/vm/vbox-provisioner.sh" "${HOME}/VirtualBox VMs/${VM_NAME}/share"
38+
39+
echo "Starting VM for the first time, it may take long..."
40+
"/c/Program Files/Oracle/VirtualBox/VBoxManage.exe" --nologo startvm ${VM_NAME} --type headless
41+
42+
sleep 15
43+
44+
# Connect to the new VM
45+
# Unable to create directory or copy files to VM at the moment
46+
"${HOME}/$(dirname "$0")/vbox-connect.sh" ${VM_NAME}
47+
48+
"/c/Program Files/Oracle/VirtualBox/VBoxManage.exe" --nologo controlvm ${VM_NAME} poweroff

vbox-connect.sh

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/bin/bash
2+
3+
VBOX=/c/Program\ Files/Oracle/VirtualBox
4+
VM_NAME=$1
5+
export VBOX
6+
export VM_NAME
7+
8+
function check_status()
9+
{
10+
# Keep trying until Guest IP is made available
11+
count=1
12+
while true; do
13+
echo "Acquiring guest IP..."
14+
while true; do
15+
GUEST_IP=$("${VBOX}/VBoxManage.exe" guestproperty enumerate ${VM_NAME}|grep /VirtualBox/GuestInfo/Net/1/V4/IP|cut -d ',' -f 2|cut -d ':' -f 2|tr -d '[[:space:]]')
16+
if [ -n ${GUEST_IP} ]; then
17+
echo "IP of selected Guest: ${GUEST_IP}"
18+
break
19+
fi
20+
sleep 2
21+
done
22+
23+
# Keep pinging until system is up and running
24+
tries=1
25+
while true; do
26+
echo "Attempt ${tries}: Trying to connect. Wait..."
27+
28+
ping -n 1 ${GUEST_IP}>/dev/nul
29+
if [[ $? -eq 0 ]]; then
30+
return 0
31+
fi
32+
33+
((tries++)); sleep 2
34+
if [[ 3 -lt ${tries} ]]; then break; fi
35+
done
36+
37+
echo "Retrying with a fresh IP..."
38+
((count++)); sleep 5
39+
if [[ 3 -lt ${count} ]]; then return 1; fi
40+
41+
unset GUEST_IP
42+
43+
done
44+
}
45+
46+
if [[ -z ${VM_NAME} ]]; then
47+
LIST=$("${VBOX}/VBoxManage.exe" list vms|cut -d' ' -f1|tr -d '"')
48+
LIST=(${LIST})
49+
50+
i=0
51+
for vm in ${LIST[@]}; do
52+
echo $((i++)): $vm
53+
done
54+
55+
read -p "Select VM: " RESPONSE
56+
57+
VM_NAME=${LIST[$RESPONSE]}
58+
fi
59+
60+
# Start if system is not running
61+
if [[ "running" != $("${VBOX}/VBoxManage.exe" showvminfo ${VM_NAME} --details --machinereadable|grep 'VMState='|cut -d'=' -f2|tr -d '"') ]]; then
62+
"${VBOX}/VBoxManage.exe" startvm ${VM_NAME} --type headless
63+
fi
64+
65+
check_status
66+
ret_val=$?
67+
68+
# Connect when system is up
69+
# echo
70+
# read -p "Do you wish to ssh?(Y/n): " RESPONSE
71+
# if [[ ${RESPONSE} = n ]]; then
72+
# exit
73+
# else
74+
echo "Connecting to guest..."
75+
ssh root@${GUEST_IP}
76+
# fi

0 commit comments

Comments
 (0)