-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
minikube-automount
executable file
·220 lines (182 loc) · 8.34 KB
/
minikube-automount
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
#!/bin/bash
set -x
echo "automount ...";
LABEL=boot2docker-data
MAGIC="boot2docker, please format-me"
# If there is a partition with `boot2docker-data` as its label, use it and be
# very happy. Thus, you can come along if you feel like a room without a roof.
BOOT2DOCKER_DATA=`blkid -o device -l -t LABEL=$LABEL`
DISKS="$(lsblk | grep disk | cut -f1 -d' ')"
echo $BOOT2DOCKER_DATA
for DISK in $DISKS; do
if [ -n "$BOOT2DOCKER_DATA" ]; then
# The primary minikube disk has been found or provisioned; stop provisioning other disks
break
fi
UNPARTITIONED_HD="/dev/$DISK"
echo "Is the disk unpartitioned?, test for the 'boot2docker format-me' string"
# Is the disk unpartitioned?, test for the 'boot2docker format-me' string
parted --script "$UNPARTITIONED_HD" print | grep "Partition Table: unknown"
if [ $? -eq 0 ]; then
# Test for our magic string (it means that the disk was made by ./boot2docker init)
HEADER=`dd if=$UNPARTITIONED_HD bs=1 count=${#MAGIC} 2>/dev/null`
if [ "$HEADER" = "$MAGIC" ]; then
# save the preload userdata.tar file
dd if=$UNPARTITIONED_HD of=/userdata.tar bs=1 count=4096 2>/dev/null
# Create the partition, format it and then mount it
echo "NEW boot2docker managed disk image ($UNPARTITIONED_HD): formatting it for use"
# Let kernel re-read partition table
partprobe
# Add the data partition
(echo g; echo n; echo 1; echo ; echo ; echo w) | fdisk $UNPARTITIONED_HD
# Let kernel re-read partition table
partprobe
# wait for the partition to actually exist, timeout after about 5 seconds
timer=0
while [ "$timer" -lt 10 ]; do
if [ -b "${UNPARTITIONED_HD}1" ]; then
BOOT2DOCKER_DATA=`echo "${UNPARTITIONED_HD}1"`
mkfs.ext4 -i 2048 -L $LABEL $BOOT2DOCKER_DATA && break
fi
echo "Waiting for ${UNPARTITIONED_HD}1 to exist."
timer=$((timer + 1))
sleep 0.5
done
fi
DISK_VENDOR=$(cat /sys/class/block/$(basename $UNPARTITIONED_HD /dev/)/device/vendor /sys/class/block/$(basename $UNPARTITIONED_HD /dev/)/device/model | tr -d "\n")
# Test if disk is "VMware, VMware Virtual S" and empty.
if [ "$DISK_VENDOR" = "VMware, VMware Virtual S" ] || [ "$DISK_VENDOR" = "VMware Virtual disk " ]; then
# Check whether the disk has any known partitions on it
blkid -o device $UNPARTITIONED_HD
if [ $? == 2 ]; then
# As there are no partitions, let's make sure the disk is empty for real
dd if=$UNPARTITIONED_HD of=device_test_file bs=1k count=256 > /dev/null 2>&1
NON_NUL=$(<device_test_file tr -d '\0\n' | wc -c)
if [ $NON_NUL == 0 ]; then
# Create the partition, format it and then mount it
echo "NEW VMware boot2docker managed disk image ($UNPARTITIONED_HD): formatting it for use"
(echo g; echo n; echo 1; echo ; echo ; echo w) | fdisk $UNPARTITIONED_HD
BOOT2DOCKER_DATA=`echo "${UNPARTITIONED_HD}1"`
mkfs.ext4 -i 2048 -L $LABEL $BOOT2DOCKER_DATA
else
echo "Disk ($UNPARTITIONED_HD) unpartitioned but something is there... not doing anything"
fi
else
echo "Partition table found on disk ($UNPARTITIONED_HD), not doing anything"
fi
fi
else
# Pick the first ext4 as a fallback
# TODO: mount all Linux partitions and look for a /var/lib/docker...
BOOT2DOCKER_DATA=`blkid | grep -e 'TYPE="btrfs"' -e 'TYPE="ext4"' | head -n 1 | sed 's/:.*//'`
fi
done
echo $BOOT2DOCKER_DATA
if [ -n "$BOOT2DOCKER_DATA" ]; then
PARTNAME=`echo "$BOOT2DOCKER_DATA" | sed 's/.*\///'`
echo "mount p:$PARTNAME ..."
mkdir -p /mnt/$PARTNAME
if ! mount $BOOT2DOCKER_DATA /mnt/$PARTNAME 2>/dev/null; then
# for some reason, mount doesn't like to modprobe btrfs
BOOT2DOCKER_FSTYPE=`blkid -o export $BOOT2DOCKER_DATA | grep TYPE= | cut -d= -f2`
modprobe $BOOT2DOCKER_FSTYPE || true
umount -f /mnt/$PARTNAME || true
mount $BOOT2DOCKER_DATA /mnt/$PARTNAME
fi
# Just in case, the links will fail if not
umount -f /var/lib/docker || true
rm -rf /var/lib/docker /var/lib/boot2docker
mkdir -p /var/lib
mkdir -p /mnt/$PARTNAME/var/lib/boot2docker
mkdir /var/lib/boot2docker
mount --bind /mnt/$PARTNAME/var/lib/boot2docker /var/lib/boot2docker
mkdir -p /mnt/$PARTNAME/var/lib/docker
mkdir -p /var/lib/docker
mount --bind /mnt/$PARTNAME/var/lib/docker /var/lib/docker
mkdir -p /mnt/$PARTNAME/var/lib/containerd
mkdir -p /var/lib/containerd
mount --bind /mnt/$PARTNAME/var/lib/containerd /var/lib/containerd
mkdir -p /mnt/$PARTNAME/var/lib/buildkit
mkdir -p /var/lib/buildkit
mount --bind /mnt/$PARTNAME/var/lib/buildkit /var/lib/buildkit
mkdir -p /mnt/$PARTNAME/var/lib/containers
mkdir -p /var/lib/containers
mount --bind /mnt/$PARTNAME/var/lib/containers /var/lib/containers
mkdir -p /mnt/$PARTNAME/var/log
mkdir /var/log
mount --bind /mnt/$PARTNAME/var/log /var/log
mkdir -p /mnt/$PARTNAME/var/tmp
mkdir /var/tmp
mount --bind /mnt/$PARTNAME/var/tmp /var/tmp
mkdir -p /mnt/$PARTNAME/var/lib/kubelet
mkdir /var/lib/kubelet
mount --bind /mnt/$PARTNAME/var/lib/kubelet /var/lib/kubelet
mkdir -p /mnt/$PARTNAME/var/lib/cni
mkdir /var/lib/cni
mount --bind /mnt/$PARTNAME/var/lib/cni /var/lib/cni
mkdir -p /mnt/$PARTNAME/data
mkdir /data
mount --bind /mnt/$PARTNAME/data /data
mkdir -p /mnt/$PARTNAME/hostpath_pv
mkdir /tmp/hostpath_pv
mount --bind /mnt/$PARTNAME/hostpath_pv /tmp/hostpath_pv
mkdir -p /mnt/$PARTNAME/hostpath-provisioner
mkdir /tmp/hostpath-provisioner
mount --bind /mnt/$PARTNAME/hostpath-provisioner /tmp/hostpath-provisioner
if [ -e "/userdata.tar" ]; then
mv /userdata.tar /var/lib/boot2docker/
fi
tar xf /var/lib/boot2docker/userdata.tar -C /home/docker/
chown -R docker:docker /home/docker/.ssh
rm -f '/home/docker/boot2docker, please format-me'
mkdir -p /mnt/$PARTNAME/var/lib/minikube
mkdir /var/lib/minikube
mount --bind /mnt/$PARTNAME/var/lib/minikube /var/lib/minikube
mkdir -p /mnt/$PARTNAME/var/lib/toolbox
mkdir /var/lib/toolbox
mount --bind /mnt/$PARTNAME/var/lib/toolbox /var/lib/toolbox
mkdir -p /mnt/$PARTNAME/var/lib/minishift
mkdir /var/lib/minishift
mount --bind /mnt/$PARTNAME/var/lib/minishift /var/lib/minishift
## make an env file for other services to discover this PARTNAME easier
mkdir -p /var/run/minikube
echo "PERSISTENT_DIR=\"/mnt/$PARTNAME\"" > /var/run/minikube/env
fi
# /etc dirs are initialised from /usr/local, to allow the user/admin to customise
mkdir -p /var/lib/boot2docker/etc/
# Below code is taken from: https://github.com/boot2docker/boot2docker/blob/master/rootfs/rootfs/etc/rc.d/vbox
# VirtualBox Host Mounting
# - this will bail quickly and gracefully if we're not in VBox
if modprobe vboxguest &> /dev/null && modprobe vboxsf &> /dev/null; then
mountOptions='defaults'
if grep -q '^docker:' /etc/passwd; then
mountOptions="${mountOptions},uid=$(id -u docker),gid=$(id -g docker)"
fi
# try mounting "$name" (which defaults to "$dir") at "$dir",
# but quietly clean up empty directories if it fails
try_mount_share() {
dir="$1"
name="${2:-$dir}"
# normalize "dir" to be definitively root-relative
# ie, "/Users" and "Users" will both translate to "/Users" explicitly
dir="/${dir#/}"
mkdir -p "$dir" 2>/dev/null
if ! mount -t vboxsf -o "$mountOptions" "$name" "$dir" 2>/dev/null; then
rmdir "$dir" 2>/dev/null || true
while [ "$(dirname "$dir")" != "$dir" ]; do
dir="$(dirname "$dir")"
rmdir "$dir" 2>/dev/null || break
done
return 1
fi
return 0
}
for line in $(VBoxControl --nologo sharedfolder list -automount | tail -n+3 | cut -d ' ' -f 3); do
try_mount_share "$line"
done
fi
# This script will get called when the machine is started up
# Feel free to add any initialization you need to be ran here
if [ -e /var/lib/boot2docker/bootlocal.sh ]; then
/bin/sh /var/lib/boot2docker/bootlocal.sh
fi