-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboot2docker
executable file
·306 lines (265 loc) · 8.17 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env sh
#Load the user's profile and then default any remaining values
: ${BOOT2DOCKER_CFG_DIR:=${HOME}/.boot2docker}
: ${BOOT2DOCKER_PROFILE:=${BOOT2DOCKER_CFG_DIR}/profile}
test -f "$BOOT2DOCKER_PROFILE" && . "$BOOT2DOCKER_PROFILE"
: ${VM_NAME:=boot2docker-vm}
: ${VBM:=VBoxManage}
: ${DOCKER_PORT:=4243}
: ${SSH_HOST_PORT:=2022}
: ${VM_DISK_SIZE:=40000}
: ${VM_MEM:=1024}
: ${VM_DISK:=${BOOT2DOCKER_CFG_DIR}/${VM_NAME}.vmdk}
: ${BOOT2DOCKER_ISO:=${BOOT2DOCKER_CFG_DIR}/boot2docker.iso}
#Check if all required commands exist
cmd_exists() {
while [ -n "$1" ]
do
command -v $1 >/dev/null 2>&1 || { echo >&2 "command '$1' is required but not installed. Aborting."; notOK=1; [[ "$1" == "$VBM" ]] && echo "You need to install VirtualBox https://www.virtualbox.org!"; }
shift
done
[ -n "$notOK" ] && exit 1
}
cmd_exists bc nc curl grep head tr $VBM
mkdir -p "${BOOT2DOCKER_CFG_DIR}"
get_latest_release_name() {
local LRN
LRN=$(curl 'https://api.github.com/repos/boot2docker/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/boot2docker/boot2docker/releases/download/$LATEST_RELEASE/boot2docker.iso"
log "Done"
else
log "Could not get latest 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! Set the DOCKER_PORT in $BOOT2DOCKER_PROFILE 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! Set the SSH_HOST_PORT in $BOOT2DOCKER_PROFILE free port."
exit 1
fi
log "Creating VM $VM_NAME"
$VBM createvm --name $VM_NAME --register
log "Apply interim patch to VM $VM_NAME (https://www.virtualbox.org/ticket/12748)"
$VBM setextradata $VM_NAME VBoxInternal/CPUM/EnableHVP 1
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"
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..."
echo "boot2docker, please format-me" | $VBM convertfromraw stdin "$VM_DISK" $(echo "$VM_DISK_SIZE * 1024 * 1024" | bc) --format 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 LogLevel=quiet -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
if [ "$DOCKER_HOST" != "tcp://localhost:${DOCKER_PORT}" ]; then
echo
echo "To connect the docker client to the Docker daemon, please set:"
echo "export DOCKER_HOST=tcp://localhost:${DOCKER_PORT}"
echo
fi
}
wait_vm() {
while ! echo "ping" | nc localhost $SSH_HOST_PORT > /dev/null 2>&1; do
sleep 1
done
}
suspend() {
is_installed || status
if is_running; then
log "Suspending $VM_NAME..."
$VBM controlvm $VM_NAME savestate > /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 -E "State:[ ]+running" > /dev/null
}
is_paused() {
info | grep -E "State:[ ]+paused" > /dev/null
}
is_saved() {
info | grep -E "State:[ ]+saved" > /dev/null
}
is_suspended() {
info | grep -E "State:[ ]+suspended" > /dev/null
}
is_stopped() {
info | grep -E "State:[ ]+powered off" > /dev/null
}
is_aborted() {
info | grep -E "State:[ ]+aborted" > /dev/null
}
status() {
if is_running; then
log "$VM_NAME is running."
exit 0
elif is_paused; then
log "$VM_NAME is paused."
exit 1
elif is_saved; then
log "$VM_NAME is saved."
exit 1
elif is_suspended; then
log "$VM_NAME is suspended."
exit 1
elif is_stopped; then
log "$VM_NAME is stopped."
exit 1
elif is_aborted; then
log "$VM_NAME is aborted."
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;;
save | pause | suspend) suspend;;
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|save|pause|stop|restart|status|info|delete|ssh|download}"; exit 1
esac