forked from boot2docker/boot2docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot2docker
executable file
·280 lines (241 loc) · 7.36 KB
/
boot2docker
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env sh
VM_NAME=boot2docker-vm
VBM=VBoxManage
DOCKER_PORT=4243
SSH_HOST_PORT=2022
TC_PORT=8080
VM_DISK=./boot2docker.vmdk
VM_DISK_SIZE=40000
VM_MEM=1024
BOOT2DOCKER_ISO=./boot2docker.iso
test -f $HOME/.boot2docker/profile && . $HOME/.boot2docker/profile
get_latest_release_name() {
local LRN
LRN=$(curl 'https://api.github.com/repos/steeve/boot2docker/releases' 2>/dev/null|grep -o -m 1 -e "\"tag_name\":[[:space:]]*\"[a-z0-9.]*\""|head -1|cut -d: -f2|tr -d ' "')
if [ -z "$LRN" ]; then
echo "ERROR"
else
echo "$LRN"
fi
}
download_latest() {
LATEST_RELEASE=$(get_latest_release_name)
if [ ! "$LATEST_RELEASE" = "ERROR" ]; then
log "Latest version is $LATEST_RELEASE, downloading..."
mkdir -p "${BOOT2DOCKER_ISO%/*}"
curl -L -o "$BOOT2DOCKER_ISO" "https://github.com/steeve/boot2docker/releases/download/$LATEST_RELEASE/boot2docker.iso"
log "Done"
else
log "Could not get lastest release name! Cannot download boot2docker.iso."
fi
}
log() {
echo "[`date +"%Y-%m-%d %H:%M:%S"`] ${*}"
}
init() {
if `$VBM showvminfo $VM_NAME > /dev/null 2>&1`; then
echo "$VM_NAME Virtual Box vm already exists"
exit 1
fi
VM_OSTYPE=Linux26_64
VM_NIC=virtio
unamestr=`uname`
case $unamestr in
Linux) VM_CPUS=`nproc`;;
Darwin) VM_CPUS=`sysctl -n hw.physicalcpu`;;
*) echo "$unamestr not yet supported - please raise an issue" ; exit 1
esac
if `nc -z -w2 localhost $DOCKER_PORT > /dev/null` ; then
log "DOCKER_PORT=$DOCKER_PORT on localhost is used by an other process! Change the port to a free port."
exit 1
fi
if `nc -z -w2 localhost $TC_PORT > /dev/null` ; then
log "TC_PORT=$TC_PORT on localhost is used by an other process! Change the port to a free port."
exit 1
fi
if `nc -z -w2 localhost $SSH_HOST_PORT > /dev/null` ; then
log "SSH_HOST_PORT=$SSH_HOST_PORT on localhost is used by an other process! Change the port to a free port."
exit 1
fi
log "Creating VM $VM_NAME"
$VBM createvm --name $VM_NAME --register
log "Setting VM settings"
TXUX_SUPPORT=""
if $($VBM modifyvm|grep -q "\-\-vtxux"); then
TXUX_SUPPORT="--vtxux on"
fi
if ! $VBM modifyvm $VM_NAME \
--ostype $VM_OSTYPE \
--cpus $VM_CPUS \
--memory $VM_MEM \
--rtcuseutc on \
--acpi on \
--ioapic on \
--hpet on \
--hwvirtex on \
--vtxvpid on \
$TXUX_SUPPORT \
--largepages on \
--nestedpaging on \
--firmware bios \
--bioslogofadein off --bioslogofadeout off --bioslogodisplaytime 0 --biosbootmenu disabled \
--boot1 dvd; then
echo "An error occured, upgrade VirtualBox or try to disable some options"
delete
exit 1
fi
log "Setting VM networking"
$VBM modifyvm $VM_NAME \
--nic1 nat \
--nictype1 $VM_NIC \
--cableconnected1 on
if `$VBM showvminfo $VM_NAME | grep Rule | grep ssh > /dev/null`; then
$VBM modifyvm $VM_NAME \
--natpf1 delete "ssh"
fi
if `$VBM showvminfo $VM_NAME | grep Rule | grep docker > /dev/null`; then
$VBM modifyvm $VM_NAME \
--natpf1 delete "docker"
fi
$VBM modifyvm $VM_NAME \
--natpf1 "ssh,tcp,127.0.0.1,$SSH_HOST_PORT,,22" \
--natpf1 "docker,tcp,127.0.0.1,$DOCKER_PORT,,4243" \
--natpf1 "tc,tcp,127.0.0.1,$TC_PORT,,8080"
if [ ! -e $BOOT2DOCKER_ISO ]; then
log "boot2docker.iso not found."
download_latest
fi
log "Setting VM disks"
if `$VBM showvminfo $VM_NAME | grep SATA > /dev/null`; then
$VBM storagectl $VM_NAME --name "SATA" --remove
fi
if [ ! -e $VM_DISK ]; then
log "Creating $VM_DISK_SIZE Meg hard drive..."
# closemedium may complain when not needed
$VBM closemedium disk $VM_DISK > /dev/null 2>&1
## make a simple text file, and use that as the flag to say 'format me'
$VBM createhd --format VMDK --filename $VM_DISK --size $VM_DISK_SIZE
echo "boot2docker, please format-me" | cat - /dev/zero | head -c 5242880 > format-flag.txt # 5M
$VBM convertfromraw format-flag.txt format-flag.vmdk --format VMDK
$VBM clonehd format-flag.vmdk $VM_DISK --existing
$VBM closemedium disk format-flag.vmdk > /dev/null 2>&1 && rm format-flag.txt format-flag.vmdk
fi
$VBM storagectl $VM_NAME --name "SATA" --add sata --hostiocache on
$VBM storageattach $VM_NAME --storagectl "SATA" --port 0 --device 0 --type dvddrive --medium $BOOT2DOCKER_ISO
$VBM storageattach $VM_NAME --storagectl "SATA" --port 1 --device 0 --type hdd --medium $VM_DISK
log "Done."
log "You can now type boot2docker up and wait for the VM to start."
}
do_ssh() {
is_installed || status
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p $SSH_HOST_PORT docker@localhost $*
}
start() {
is_installed || status
if ! is_running; then
if is_paused; then
log "Resuming $VM_NAME"
$VBM controlvm $VM_NAME resume > /dev/null
wait_vm
log "Resumed."
else
log "Starting $VM_NAME..."
$VBM startvm $VM_NAME --type headless > /dev/null &
wait_vm
log "Started."
fi
else
log "$VM_NAME is already running."
fi
}
wait_vm() {
while ! echo "ping" | nc localhost $SSH_HOST_PORT > /dev/null 2>&1; do
sleep 1
done
}
pause() {
is_installed || status
if is_running; then
log "Pausing $VM_NAME..."
$VBM controlvm $VM_NAME pause > /dev/null
else
log "$VM_NAME is not running."
fi
}
stop() {
is_installed || status
if is_running; then
log "Shutting down $VM_NAME..."
$VBM controlvm $VM_NAME acpipowerbutton > /dev/null
while is_running; do
sleep 1
done
else
log "$VM_NAME is not running."
fi
}
restart() {
is_installed || status
if is_running; then
stop && sleep 1 && start
else
start
fi
}
info() {
if is_installed; then
$VBM showvminfo $VM_NAME
else
echo "$VM_NAME does not exist."
fi
}
is_installed() {
$VBM list vms | grep "$VM_NAME" > /dev/null
}
is_running() {
info | grep "State:\s\+running" > /dev/null
}
is_paused() {
info | grep "State:\s\+paused" > /dev/null
}
is_stopped() {
info | grep "State:\s\+powered off" > /dev/null
}
is_aborted() {
info | grep "State:\s\+aborted" > /dev/null
}
status() {
if is_running; then
log "$VM_NAME is running."
exit 0
elif is_paused; then
log "$VM_NAME is suspended."
exit 1
elif is_stopped; then
log "$VM_NAME is stopped."
exit 1
else
log "$VM_NAME does not exist."
exit 1
fi
}
delete() {
if [ ! is_stopped ] || [ ! is_aborted ]; then
log "$VM_NAME needs to be stopped to delete it."
exit 1
fi
$VBM unregistervm --delete $VM_NAME
}
case $1 in
init | setup) init;;
start | up) start;;
pause | suspend) pause;;
stop | halt | down) stop;;
restart) restart;;
status) status;;
info) info;;
delete) delete;;
ssh) shift; do_ssh "$@";;
download) download_latest;;
*) echo "Usage $0 {init|start|up|pause|stop|restart|status|info|delete|ssh|download}"; exit 1
esac