-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmdata-fetch
executable file
·228 lines (197 loc) · 6.95 KB
/
mdata-fetch
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
#!/usr/bin/bash
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License"). You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at http://smartos.org/CDDL
#
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file.
#
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Copyright (c) 2013, Joyent, Inc. All rights reserved.
#
export PS4='[\D{%FT%TZ}] ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
set -o xtrace
# Use platform script instead, if it exists
[ -f /lib/svc/method/mdata-fetch ] && exec /lib/svc/method/mdata-fetch $@
#
# The rest is legacy for platforms that do not come with its own
#
. /lib/svc/share/smf_include.sh
smf_is_globalzone && exit ${SMF_EXIT_OK}
if [ ! -x /usr/sbin/mdata-get ]; then
echo "Metadata mdata-get tool not found."
exit ${SMF_EXIT_ERR_FATAL}
fi
# For old zones that were created prior to OS-2253 and bumping the mdata:fetch
# start timeout, we need to fix this otherwise we could timeout waiting for the
# socket.
cur_timeout=$(svcprop -p start/timeout_seconds svc:/smartdc/mdata:fetch)
if [[ -z ${cur_timeout} || ${cur_timeout} -lt 1800 ]]; then
# The current manifest has an old timeout value, fix in case we timeout
# here. XXX we can still hit OS-2296 here where smf will forget that we
# set this.
svccfg -s svc:/smartdc/mdata:fetch 'setprop start/timeout_seconds = 1800'
svcadm refresh svc:/smartdc/mdata:fetch
fi
if [[ -x /usr/vm/sbin/filewait ]]; then
# This waits until /.zonecontrol/metadata.sock exists then exits 0
/usr/vm/sbin/filewait /.zonecontrol/metadata.sock
else
timer=300
while [ $((timer--)) -gt 0 ]; do
if [[ -e /.zonecontrol/metadata.sock ]] ||\
[[ -e /var/run/smartdc/metadata.sock ]]; then
/usr/sbin/mdata-get sdc:uuid
result=$?
if [[ ${result} -lt 10 ]]; then
# exit 10 means problem talking to socket. We assume errors 10 or
# higher (I've seen 141 from something in here getting SIGPIPE too)
# is something we'll just retry on for now. <10 we assume is a
# problem retrying won't fix (eg. not found) or is success (0)
break;
fi
fi
# This is an alternate signal to metadata which we enable if the socket isn't
# here yet in case something else in smf is messed up. (for debugging)
[[ -e /tmp/.ready_for_metadata ]] || touch /tmp/.ready_for_metadata
sleep 1
done
fi
if [ ! -e /.zonecontrol/metadata.sock ] && \
[ ! -e /var/run/smartdc/metadata.sock ]; then
echo "No metadata socket file, skipping metadata loading."
exit ${SMF_EXIT_OK}
fi
/usr/sbin/mdata-get sdc:uuid
result=$?
if [[ ${result} -gt 0 ]]; then
# Assume this is SDC6
legacy_platform=yes
fi
# Update sysinfo to ensure values that come from metadata are populated.
[ -z ${legacy_platform} ] && \
/usr/bin/sysinfo -fu
echo "Retrieving metadata user-data"
/usr/sbin/mdata-get user-data >/var/db/mdata-user-data.new
case $? in
0)
echo "Metadata user-data successfuly retrieved."
mv /var/db/mdata-user-data{.new,}
;;
1)
echo "Metadata user-data not defined."
rm -f /var/db/mdata-user-data{,.new}
;;
*)
echo "Metadata couldn't be retrieved."
exit ${SMF_EXIT_ERR_FATAL}
;;
esac
echo "Retrieving metadata user-script..."
/usr/sbin/mdata-get user-script >/var/svc/mdata-user-script.new
case $? in
0)
echo "Metadata user-script successfuly retrieved."
mv /var/svc/mdata-user-script{.new,}
chmod +x /var/svc/mdata-user-script
;;
1)
echo "Metadata user-script not defined."
rm -f /var/svc/mdata-user-script{,.new}
;;
*)
echo "Metadata couldn't be retrieved."
exit ${SMF_EXIT_ERR_FATAL}
;;
esac
[ ${legacy_platform} ] || {
echo "Retrieving metadata operator-script..."
/usr/sbin/mdata-get sdc:operator-script >/var/svc/mdata-operator-script.new
case $? in
0)
echo "Metadata operator-script successfuly retrieved."
mv /var/svc/mdata-operator-script{.new,}
chmod +x /var/svc/mdata-operator-script
;;
1)
echo "Metadata operator-script not defined."
rm -f /var/svc/mdata-operator-script{,.new}
;;
*)
echo "Metadata couldn't be retrieved."
exit ${SMF_EXIT_ERR_FATAL}
;;
esac
echo "Retrieving tmpfs value..."
tmpfs=$(/usr/sbin/mdata-get sdc:tmpfs)
if [[ $? == 0 && -n ${tmpfs} && -f /etc/vfstab ]]; then
check="swap - /tmp tmpfs";
new="swap - /tmp tmpfs - yes size=${tmpfs}m";
if ! /usr/bin/grep "^${new}" /etc/vfstab; then
if ! /usr/bin/grep "^${check}" /etc/vfstab; then
# no tmpfs line. add it.
echo "${new}" >> /etc/vfstab
else
# existing tmpfs line, but wrong value. fix it.
/usr/bin/sed -i "" -e "s|^swap.*/tmp.*tmpfs.*$|${new}|" /etc/vfstab
echo $?
fi
# also fix current size, since /etc/vfstab didn't have our correct line
/usr/sbin/mount -F tmpfs -o remount,size=${tmpfs}m /tmp
fi
fi
# We use the special sdc:nics value here though this is not an interface for
# use elsewhere. If this is changed please also update agent.js in the metadata
# agent.
#
# We run this every startup in case nics have changed since last boot. As
# network/physical has an optional_all dependency on this service, we'll have
# had our chance to write these files out before networking comes up. This
# might eventually be replaced by network/physical grabbing data directly.
echo "Retrieving nics data..."
while IFS="|" read -r iface ip netmask primary gateway; do
# so it shows up in the logs
echo "iface[${iface}] ip[${ip}] netmask[${netmask}]" \
"primary[${primary}] gateway[${gateway}]"
# if we don't have interface name, we don't know what files to write out.
if [[ -z ${iface} ]]; then
continue;
fi
if [[ ${ip} != "dhcp" && -n ${ip} && -n ${netmask} ]]; then
echo "${ip} netmask ${netmask} up" > /etc/hostname.${iface}
else
rm -f /etc/hostname.${iface}
fi
# remove any existing dhcp.${iface} file, we'll create one if it belongs later
rm -f /etc/dhcp.${iface}
if [[ -n ${primary} && ${primary} == "true" ]]; then
if [[ -n ${gateway} ]]; then
echo "${gateway}" > /etc/defaultrouter
fi
if [[ ${ip} == "dhcp" ]]; then
touch /etc/dhcp.${iface}
fi
fi
# XXX we leave old hostname.netX files around and just replace when we have
# one next.
done < <(/usr/sbin/mdata-get sdc:nics \
| /usr/bin/json -d '|' -a interface ip netmask primary gateway)
}
# Unconditionally enable mdata:execute, so that the last provisioning step
# is always taken (regardless of whether user-script exists or not)
svcadm enable smartdc/mdata:execute
exit ${SMF_EXIT_OK}