-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkvm
executable file
·186 lines (164 loc) · 6.27 KB
/
mkvm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env bash
# # simple script to generate new vm using specified iso to install from and
# # specified kickstart menu to boot from
ISO_BASE=${ISO_BASE-/Users/Shared/software/oracle/OEL}
VBOX_APPL_RES="/Applications/VirtualBox.app/Contents/MacOS/"
VBOX_VM_BASE=${VBOX_VM_BASE-"$HOME/VirtualBox VMs"}
VBOX_INFO=${VBOX_INFO-"$HOME/vbox"}
KICK_START_KEYS=thuis.ronr.nl:/Library/Server/Web/Data/Sites/Default/
TFTP_DIR=${TFTP_DIR-"${HOME}/Library/VirtualBox/TFTP"}
PXEMENU=${TFTP_DIR}/pxelinux.cfg/default
if [ $# -ne 3 ]
then
{
echo "$0 vmname install.iso kickstart_label#"
echo " to be created"
echo " where install.iso is one of `ls ${ISO_BASE}`"
echo " and kickstart_option one of"
if [ -f $PXEMENU ]
then
echo "Choose the label # needed for this vm"
echo "pxe menu in:$PXEMENU:"
cat $PXEMENU
else
echo "$PXEMENU does not exist"
echo " create this first"
fi
} >&2
exit 1
fi
vmname=$1
isoname=$2
pxelabel=$3
VM_INFO="$VBOX_INFO/${vmname}"
if [ ! -d "$VBOX_VM_BASE" ]
then
echo "$0 directory $VBOX_VM_BASE nonexistent" >&2
echo bijna goed
exit 1
fi
if [ ! -f ${ISO_BASE}/${isoname} ]
then
echo "$0 iso $isoname not in $ISO_BASE" >&2
exit 2
fi
if [ -d "$VBOX_VM_BASE/${vmname}" ]
then
echo "$0 ${vmname} already in $VBOX_VM_BASE" >&2
exit 3
fi
if [ -f "$VBOX_VM_BASE/${vmname}/${vmname}.vbox" ]
then
echo "$0 ${vmname} already in $VBOX_VM_BASE" >&2
exit 3
fi
grep -i "^label" $PXEMENU|awk '{print $2}'|grep $pxelabel >/dev/null
if [ $? -ne 0 ]
then
echo "pxe label specified $pxelabel should be one of" >&2
grep -i "^label" $PXEMENU|awk '{print $2}' >&2
exit 4
fi
# remove possible current default choice
sed '/menu default/d' $PXEMENU >.mkvm.default.$$
# and make this label the current default
sed '/^label '${pxelabel}$'/a\
menu default
' .mkvm.default.$$ >$PXEMENU
rm .mkvm.default.$$
if [ ! -d "$VM_INFO" ]
then
mkdir -p "$VM_INFO"
fi
if [ -f "${VM_INFO}/${vmname}.info" ]
then
echo "$0 Re using settings from previous $vmname"
if [ -f "${VM_INFO}/${vmname}_host_keys.tar.gz" ]
then
scp -p "${VM_INFO}/${vmname}_host_keys.tar.gz" "$KICK_START_KEYS"
fi
fi
bridge_nic=`ifconfig | pcregrep -M -o '^[^\t:]+:([^\n]|\n\t)*status: active'| \
grep "^[a-z].*:"|cut -f1 -d":"|sort|grep -v "awdl" |head -1`
case $bridge_nic in
en0) type=Ethernet;;
en1) type="Wi-Fi (AirPort)";;
*) type=geenID;;
esac
echo "$0 for Bridged using $bridge_nic $type"
VBoxManage createvm --name "${vmname}" --register
if [ $? -ne 0 ]
then
echo "$0 createvm for ${vmname} failed" >&2
exit 10
fi
VBoxManage modifyvm "${vmname}" --memory 1024
VBoxManage modifyvm "${vmname}" --cpus 2
VBoxManage modifyvm "${vmname}" --ioapic on
VBoxManage modifyvm "${vmname}" --acpi on
VBoxManage modifyvm "${vmname}" --boot1 disk --boot2 net --boot3 DVD --boot4 none
#
# # kickstart from tftp only supported using NAT
#
VBoxManage modifyvm "${vmname}" --nic1 NAT
VBoxManage modifyvm "${vmname}" --nattftpfile1 /pxelinux.0
VBoxManage modifyvm "${vmname}" --nic2 bridged --bridgeadapter2 "${bridge_nic}: ${type}"
# default is Am79C973
# VBoxManage modifyvm "${vmname}" --nictype1 "82540EM"
# VBoxManage modifyvm "${vmname}" --nictype2 "82540EM"
if [ -f "$VM_INFO/${vmname}.info" ]
then
echo "$0 Re using saved macadresses"
grep "^NIC" $VM_INFO/${vmname}.info|grep " MAC:"|cut -f1 -d","|sed "s/:/ /g"|
while read x NICn x MACa
do
echo "$0 Re using $MACa for NIC${NICn}"
VBoxManage modifyvm "${vmname}" --macaddress${NICn} "${MACa}"
done
fi
VBoxManage modifyvm "${vmname}" --ostype "Oracle_64"
#
VBoxManage storagectl "${vmname}" --name "SATA Controller" --add sata
VBoxManage storagectl "${vmname}" --name "IDE Controller" --add ide
# # create disks
VBoxManage createvdi --filename "$VBOX_VM_BASE/${vmname}/${vmname}-disk01.vdi" --size 64000 # 64g
VBoxManage createvdi --filename "$VBOX_VM_BASE/${vmname}/${vmname}-disk02.vdi" --size 64000 # 64g
VBoxManage createvdi --filename "$VBOX_VM_BASE/${vmname}/${vmname}-disk03.vdi" --size 64000 # 64g
VBoxManage storageattach "${vmname}" --storagectl "SATA Controller" \
--port 0 --device 0 --type hdd \
--medium "$VBOX_VM_BASE/${vmname}/${vmname}-disk01.vdi"
VBoxManage storageattach "${vmname}" --storagectl "SATA Controller" \
--port 1 --device 0 --type hdd \
--medium "$VBOX_VM_BASE/${vmname}/${vmname}-disk02.vdi"
VBoxManage storageattach "${vmname}" --storagectl "SATA Controller" \
--port 2 --device 0 --type hdd \
--medium "$VBOX_VM_BASE/${vmname}/${vmname}-disk03.vdi"
VBoxManage storageattach "${vmname}" --storagectl "IDE Controller" \
--port 0 --device 1 --type dvddrive \
--medium "${ISO_BASE}/${isoname}"
## --medium emptydrive
## /dev/sr0
## boot devices .... no cd no cdrom
echo "$0 Installing ${vmname} using kickstart form TFTP"
VBoxHeadless --startvm "${vmname}"
echo not $0 VBoxManage startvm "${vmname}"
##
## the kickstart script will shutdown the guest
##
echo "$0 Basic OS install of ${vmname} is complete replace NAT interface by hostonly"
## convert nic1 back to hostonly
VBoxManage modifyvm "${vmname}" --nic1 hostonly --hostonlyadapter1 vboxnet0
VBoxManage showvminfo ${vmname} >$VM_INFO/${vmname}.info
#
# # replace installer disc by guest host additions disc
#
VBoxManage storageattach "${vmname}" --storagectl "IDE Controller" \
--port 0 --device 1 --type dvddrive \
--medium "$VBOX_APPL_RES/VBoxGuestAdditions.iso"
## --medium emptydrive
## device0 /dev/sr0
## device1 /dev/sr1
VBoxManage startvm "${vmname}"
#
# # use ansible to finish the setup, including guest additions
#