1
1
#! /bin/sh
2
+ # shellcheck shell=busybox
2
3
3
4
# skip all the setup if running in provisioner mode
4
5
if [ " $SERVICE_TYPE " = " provisioner" ]; then
@@ -16,30 +17,18 @@ run_nas="false"
16
17
mkdir -p /var/log/alicloud/
17
18
mkdir -p /host/etc/kubernetes/volumes/disk/uuid
18
19
19
- HOST_CMD= " /usr/bin/ nsenter --mount=/proc/1/ns/mnt"
20
+ host_cmd () { nsenter --mount=/proc/1/ns/mnt " $@ " ; }
20
21
HOST_ROOT=" /proc/1/root"
21
22
22
- host_os=" centos"
23
-
24
23
OS_RELEASE=/etc/os-release
25
24
if ! [ -f $HOST_ROOT$OS_RELEASE ]; then
26
25
OS_RELEASE=/usr/lib/os-release
27
26
fi
28
- if [ -f $HOST_ROOT$OS_RELEASE ]; then
29
- OS_RELEASE_ID=$( grep -oP ' (?<=^ID=).+' $HOST_ROOT$OS_RELEASE | tr -d ' "' )
30
- if [ -n " $OS_RELEASE_ID " ]; then
31
- host_os=" $OS_RELEASE_ID "
32
-
33
- if [[ $OS_RELEASE_ID = alinux ]]; then
34
- OS_RELEASE_VERSION_ID=$( grep -oP ' (?<=^VERSION_ID=).+' $HOST_ROOT$OS_RELEASE | tr -d ' "' )
35
- host_os=" alinux$( echo $OS_RELEASE_VERSION_ID | cut -d' .' -f1) "
36
- fi
37
- fi
38
- fi
39
- echo " detected host os: $host_os "
40
27
28
+ OS_RELEASE_ID=$( . $HOST_ROOT$OS_RELEASE ; echo " $ID " )
29
+ OS_RELEASE_VERSION_ID=$( . $HOST_ROOT$OS_RELEASE ; echo " $VERSION_ID " )
41
30
ARCH=$( uname -m)
42
- echo " detected host arch: $ARCH "
31
+ echo " detected host OS: $OS_RELEASE_ID , version: $OS_RELEASE_VERSION_ID , arch: $ARCH "
43
32
44
33
cleanup_old_staging_path () (
45
34
echo cleaning up old volume staging path. # kubelet will mount the new path at startup.
@@ -60,7 +49,7 @@ if [ -d "$OLD_STAGING_PATH" ]; then
60
49
fi
61
50
62
51
# # check which plugin is running
63
- for item in $@ ;
52
+ for item in " $@ " ;
64
53
do
65
54
if [ " $item " = " --driver=ossplugin.csi.alibabacloud.com" ]; then
66
55
echo " Running oss plugin...."
77
66
run_nas=" true"
78
67
mkdir -p " $KUBELET_ROOT_DIR /csi-plugins/nasplugin.csi.alibabacloud.com"
79
68
rm -rf " $KUBELET_ROOT_DIR /plugins/nasplugin.csi.alibabacloud.com/csi.sock"
80
- elif [[ $item == * --driver= * ]]; then
81
- tmp=${item}
82
- driver_types=${tmp#* --driver=}
83
- driver_type_array=(${driver_types// ,/ } )
84
- for driver_type in ${driver_type_array[@]} ;
69
+ elif [[ " $item " = * --driver= * ]]; then
70
+ IFS=,
71
+ for driver_type in ${item#* --driver=} ;
85
72
do
86
73
if [ " $driver_type " = " oss" ]; then
87
74
echo " Running oss plugin...."
103
90
run_pov=" true"
104
91
fi
105
92
done
93
+ unset IFS
106
94
fi
107
95
done
108
96
@@ -112,12 +100,12 @@ if [ "$SKIP_UPDATEDB_CONFIG" != "true" ]; then
112
100
if [ -f /host/etc/cron.daily/mlocate ]; then
113
101
if [ -f /host/etc/updatedb.conf ]; then
114
102
sed -i ' s/PRUNE_BIND_MOUNTS.*$/PRUNE_BIND_MOUNTS = \"yes\"/g' /host/etc/updatedb.conf
115
- if ! grep " ^PRUNEFS" /host/etc/updatedb.conf | grep -q --fixed-strings " fuse.ossfs" ; then
103
+ if ! grep " ^PRUNEFS" /host/etc/updatedb.conf | grep -q -F " fuse.ossfs" ; then
116
104
PRUNEFS=" fuse.ossfs"
117
105
echo " add PRUNEFS: $PRUNEFS to /etc/updatedb.conf"
118
106
sed -i " s|PRUNEFS\s*=\s*\" |&${PRUNEFS// |/ \\ |} |" /host/etc/updatedb.conf
119
107
fi
120
- if ! grep " ^PRUNEPATHS" /host/etc/updatedb.conf | grep -q --fixed-strings " $KUBELET_ROOT_DIR " ; then
108
+ if ! grep " ^PRUNEPATHS" /host/etc/updatedb.conf | grep -q -F " $KUBELET_ROOT_DIR " ; then
121
109
PRUNEPATHS=" $KUBELET_ROOT_DIR "
122
110
if [ " $KUBELET_ROOT_DIR " = " /var/lib/kubelet" ]; then
123
111
PRUNEPATHS=" $PRUNEPATHS /var/lib/container/kubelet"
@@ -134,15 +122,15 @@ if [ "$DISABLE_CSIPLUGIN_CONNECTOR" != "true" ] && [ "$run_nas" = "true" ]; then
134
122
# # install/update csi connector
135
123
updateConnector=" true"
136
124
systemdDir=" /host/usr/lib/systemd/system"
137
- if [[ ${host_os} == " lifsea" ] ]; then
125
+ if [ " $OS_RELEASE_ID " = " lifsea" ]; then
138
126
systemdDir=" /host/etc/systemd/system"
139
127
fi
140
128
if [ ! -f " /host/etc/csi-tool/csiplugin-connector" ]; then
141
129
mkdir -p /host/etc/csi-tool/
142
130
echo " mkdir /etc/csi-tool/ directory..."
143
131
else
144
- oldmd5=` md5sum /host/etc/csi-tool/csiplugin-connector | awk ' {print $1}' `
145
- newmd5=` md5sum /csi/csiplugin-connector | awk ' {print $1}' `
132
+ oldmd5=$( md5sum /host/etc/csi-tool/csiplugin-connector | awk ' {print $1}' )
133
+ newmd5=$( md5sum /csi/csiplugin-connector | awk ' {print $1}' )
146
134
if [ " $oldmd5 " = " $newmd5 " ]; then
147
135
updateConnector=" false"
148
136
else
@@ -163,14 +151,14 @@ if [ "$DISABLE_CSIPLUGIN_CONNECTOR" != "true" ] && [ "$run_nas" = "true" ]; then
163
151
164
152
# install/update csiplugin connector service
165
153
updateConnectorService=" true"
166
- if [[ ! -z " ${PLUGINS_SOCKETS} " ] ]; then
154
+ if [ -n " ${PLUGINS_SOCKETS} " ]; then
167
155
sed -i ' s/Restart=always/Restart=on-failure/g' /csi/csiplugin-connector.service
168
156
sed -i ' /^\[Service\]/a Environment=\"WATCHDOG_SOCKETS_PATH=' " ${PLUGINS_SOCKETS} " ' \"' /csi/csiplugin-connector.service
169
157
fi
170
158
if [ -f " $systemdDir /csiplugin-connector.service" ]; then
171
159
echo " Check csiplugin-connector.service...."
172
- oldmd5=` md5sum $systemdDir /csiplugin-connector.service | awk ' {print $1}' `
173
- newmd5=` md5sum /csi/csiplugin-connector.service | awk ' {print $1}' `
160
+ oldmd5=$( md5sum $systemdDir /csiplugin-connector.service | awk ' {print $1}' )
161
+ newmd5=$( md5sum /csi/csiplugin-connector.service | awk ' {print $1}' )
174
162
if [ " $oldmd5 " = " $newmd5 " ]; then
175
163
updateConnectorService=" false"
176
164
else
@@ -182,10 +170,9 @@ if [ "$DISABLE_CSIPLUGIN_CONNECTOR" != "true" ] && [ "$run_nas" = "true" ]; then
182
170
echo " Install csiplugin connector service...."
183
171
cp /csi/csiplugin-connector.service $systemdDir /csiplugin-connector.service
184
172
echo " Starting systemctl daemon-reload."
185
- for(( i = 1 ;i <= 10 ;i ++ ) );
173
+ for i in $( seq 10 ) ;
186
174
do
187
- ${HOST_CMD} systemctl daemon-reload
188
- if [ $? -eq 0 ]; then
175
+ if host_cmd systemctl daemon-reload; then
189
176
break
190
177
else
191
178
echo " Starting retry again systemctl daemon-reload.retry count:$i "
@@ -196,10 +183,9 @@ if [ "$DISABLE_CSIPLUGIN_CONNECTOR" != "true" ] && [ "$run_nas" = "true" ]; then
196
183
197
184
rm -rf /var/log/alicloud/connector.pid
198
185
echo " Starting systemctl enable csiplugin-connector.service."
199
- for(( i = 1 ;i <= 5 ;i ++ ) );
186
+ for i in $( seq 5 ) ;
200
187
do
201
- ${HOST_CMD} systemctl enable csiplugin-connector.service
202
- if [ $? -eq 0 ]; then
188
+ if host_cmd systemctl enable csiplugin-connector.service; then
203
189
break
204
190
else
205
191
echo " Starting retry again systemctl enable csiplugin-connector.service.retry count:$i "
@@ -208,10 +194,9 @@ if [ "$DISABLE_CSIPLUGIN_CONNECTOR" != "true" ] && [ "$run_nas" = "true" ]; then
208
194
done
209
195
210
196
echo " Starting systemctl restart csiplugin-connector.service."
211
- for(( i = 1 ;i <= 5 ;i ++ ) );
197
+ for i in $( seq 5 ) ;
212
198
do
213
- ${HOST_CMD} systemctl restart csiplugin-connector.service
214
- if [ $? -eq 0 ]; then
199
+ if host_cmd systemctl restart csiplugin-connector.service; then
215
200
break
216
201
else
217
202
echo " Starting retry again systemctl restart csiplugin-connector.service.retry count:$i "
222
207
223
208
echo " Start checking if the host system packages needs to be installed"
224
209
225
- if $HOST_CMD dnf --version; then
226
- echo " dnf is available"
227
- PKG_CMD=(dnf install -y)
228
- HAS_DNF=true
229
- HAS_YUM=true
230
- elif $HOST_CMD yum --version; then
231
- echo " yum is available"
210
+ dnf_install () {
211
+ host_cmd dnf install -y " $@ "
212
+ }
213
+
214
+ yum_install () {
232
215
# yum install is not idempotent, so we need to check if the package is already installed.
233
216
# yum also fails if the already installed version is newer, so we need to downgrade.
234
217
# fail if both install and downgrade fail
235
- PKG_CMD=(sh -c ' if rpm --query $(rpm --query --package $1); then
236
- echo $1 already installed;
218
+ # shellcheck disable=SC2016 # the expansion should happen on host, not in container.
219
+ host_cmd sh -c ' if rpm --query $(rpm --query --package "$1"); then
220
+ echo "$1 already installed";
237
221
else
238
- yum install -y $1 || yum downgrade -y $1;
239
- fi' install_pkg)
240
- HAS_YUM=true
241
- elif $HOST_CMD apt-get --version; then
222
+ yum install -y "$1" || yum downgrade -y "$1";
223
+ fi' install_pkg " $1 "
224
+ }
225
+
226
+ apt_install () {
227
+ host_cmd apt-get install -y --allow-downgrades " $@ "
228
+ }
229
+
230
+ if host_cmd dnf --version; then
231
+ echo " dnf is available"
232
+ PKG_MGR=dnf
233
+ SUPPORT_RPM=true
234
+ elif host_cmd yum --version; then
235
+ echo " yum is available"
236
+ PKG_MGR=yum
237
+ SUPPORT_RPM=true
238
+ elif host_cmd apt-get --version; then
242
239
echo " apt-get is available"
243
- PKG_CMD=(apt-get install -y --allow-downgrades)
244
- HAS_APT=true
240
+ PKG_MGR=apt
245
241
fi
246
242
247
243
# idempotent package installation
248
244
# downgrade if the already installed version is newer than the one we want to install
249
- function install_package() {
250
- cp /root/$1 /host/etc/csi-tool/
251
- if [ -z $PKG_CMD ]; then
245
+ install_package () {
246
+ cp " /root/$1 " /host/etc/csi-tool/
247
+ if [ -z " $PKG_MGR " ]; then
252
248
echo " no package manager found"
253
249
return 1
254
250
fi
255
- ${HOST_CMD} " ${PKG_CMD[@]} " /etc/csi-tool/$1 || {
251
+ " ${PKG_MGR} _install " " /etc/csi-tool/$1 " || {
256
252
echo " failed to install $1 "
257
253
return 1
258
254
}
259
- rm -f /host/etc/csi-tool/$1
255
+ rm -f " /host/etc/csi-tool/$1 "
260
256
}
261
257
262
258
# # CPFS-NAS plugin setup
@@ -280,35 +276,35 @@ if [ "$ARCH" = "x86_64" ] && [ "$run_nas" = "true" ]; then
280
276
if [ $install_utils = " true" ]; then
281
277
# cpfs-nas nas-rich-client common rpm
282
278
echo " installing aliyun-alinas-utils"
283
- if [ " $HAS_YUM " = " true" ]; then
279
+ if [ " $SUPPORT_RPM " = " true" ]; then
284
280
PKG=aliyun-alinas-utils-1.1-8.20240527201444.2012cc.al7.noarch.rpm
285
- elif [ " $HAS_APT " = " true " ]; then
281
+ elif [ " $PKG_MGR " = " apt " ]; then
286
282
PKG=aliyun-alinas-utils-1.1-8.deb
287
283
else
288
284
echo " WARN: no supported package manager found, skip install aliyun-alinas-utils"
289
285
install_utils=" false"
290
286
fi
291
287
if [ $install_utils = " true" ]; then
292
- install_package $PKG || exit $?
288
+ install_package " $PKG " || exit $?
293
289
fi
294
290
fi
295
291
296
- if [ $host_os != " alinux2 " ] && [ $install_efc = " true" ]; then
292
+ if { [ " $OS_RELEASE_ID " != " alinux " ] || [[ " $OS_RELEASE_VERSION_ID " != 2. * ]] ; } && [ $install_efc = " true" ]; then
297
293
echo " WARN: skip install efc because host os is not alinux2"
298
294
install_efc=" false"
299
295
fi
300
296
if [ $install_efc = " true" ]; then
301
297
# nas-rich-client rpm
302
298
echo " installing alinas-efc"
303
299
cp /root/alinas-efc-1.2-3.x86_64.rpm /host/etc/csi-tool/
304
- ${HOST_CMD} yum install -y /etc/csi-tool/alinas-efc-1.2-3.x86_64.rpm
300
+ host_cmd yum install -y /etc/csi-tool/alinas-efc-1.2-3.x86_64.rpm
305
301
306
302
echo " checking alinas-efc-1.2-3.x86_64 installed"
307
- ${HOST_CMD} rpm -q alinas-efc-1.2-3.x86_64 || exit 1
303
+ host_cmd rpm -q alinas-efc-1.2-3.x86_64 || exit 1
308
304
echo " starting aliyun-alinas-mount-watchdog"
309
- ${HOST_CMD} systemctl start aliyun-alinas-mount-watchdog || exit 1
305
+ host_cmd systemctl start aliyun-alinas-mount-watchdog || exit 1
310
306
fi
311
307
fi
312
308
313
309
# place it here to remove leftover from previous version
314
- rm -rf /host/etc/csi-tool/* .{ rpm, deb}
310
+ rm -rf /host/etc/csi-tool/* .rpm /host/etc/csi-tool/ * . deb
0 commit comments