Skip to content

Commit 87f1104

Browse files
committed
Merge pull request #1379 from jayapalu/configdriveScripts
CLOUDSTACK-8324: config drive data set/get scripts for the guest vmAdded the guest vm scripts for set/get the vm data, password and ssh keys * pr/1379: CLOUDSTACK-8324: updated the mount directory name and kvm virt device CLOUDSTACK-8324: config drive data set/get scripts for the guest vm Signed-off-by: Rajani Karuturi <rajani.karuturi@accelerite.com>
2 parents 9efd905 + 6518607 commit 87f1104

File tree

4 files changed

+509
-0
lines changed

4 files changed

+509
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#!/bin/bash
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
19+
20+
mountdir=$(mktemp -d)
21+
filepath=$mountdir/cloudstack
22+
23+
user_data=$filepath/userdata/user_data.txt
24+
availability_zone=$filepath/metadata/availability_zone.txt
25+
cloud_identifier=$filepath/metadata/cloud_identifier.txt
26+
instance_id=$filepath/metadata/instance_id.txt
27+
local_hostname=$filepath/metadata/local_hostname.txt
28+
service_offering=$filepath/metadata/service_offering.txt
29+
vm_id=$filepath/metadata/vm_id.txt
30+
public_key=$filepath/metadata/public_keys.txt
31+
vm_password=$filepath/password/vm_password.txt
32+
33+
# If lable name is other than config, please change the below line as required
34+
DefaultDisk=/dev/disk/by-label/config
35+
36+
function usage
37+
{
38+
echo -e "USAGE: cloud-get-vm-data -options"
39+
echo -e " where options include:"
40+
echo -e "\\t-m | --metadata [availability-zone | cloud-identifier | instance-id | local-hostname | service-offering | vm-id | public-key] \\n\\t\\tprint vm metadata"
41+
echo -e "\\t-p | --password \\n\\t\\tprint vm password"
42+
echo -e "\\t-u | --userdata \\n\\t\\tprint vm userdata"
43+
}
44+
45+
function prepare_mount
46+
{
47+
if [ ! -e $mountdir ]; then
48+
mkdir $mountdir
49+
chmod 700 $mountdir
50+
fi
51+
52+
if [ -e $DefaultDisk ]; then
53+
Disk=$DefaultDisk
54+
else
55+
BLOCK_DEVICE=$(blkid -t LABEL='config' /dev/hd? /dev/sd? /dev/xvd? -o device)
56+
if [ -n $BLOCK_DEVICE ]; then
57+
Disk=$BLOCK_DEVICE
58+
else
59+
logger -t "cloud" "Unable to get the VM data: Config drive device not found"
60+
exit 1
61+
fi
62+
fi
63+
64+
mount -r $Disk $mountdir
65+
if [ $? -ne 0 ]; then
66+
echo "Failed mounting $Disk to /mnt/configdrive"
67+
exit 1
68+
fi
69+
}
70+
71+
function remove_mount
72+
{
73+
umount $mountdir
74+
}
75+
76+
prepare_mount
77+
78+
case $1 in
79+
-u | --userdata ) echo -n "USERDATA: "
80+
filename=$user_data
81+
;;
82+
-m | --metadata ) shift
83+
if [ "$1" != "" ]; then
84+
case $1 in
85+
availability-zone ) echo -n "availability zone: "; filename=$availability_zone
86+
;;
87+
cloud-identifier ) echo -n "cloud identifier: "; filename=$cloud_identifier
88+
;;
89+
instance-id ) echo -n "instance-id: "; filename=$instance_id
90+
;;
91+
local-hostname ) echo -n "local-hostname: "; filename=$local_hostname
92+
;;
93+
service-offering ) echo -n "service-offering: "; filename=$service_offering
94+
;;
95+
vm-id ) echo -n "vm-id: "; filename=$vm_id
96+
;;
97+
public-key ) echo -n "public-key: "; filename=$public_key
98+
;;
99+
* ) usage
100+
remove_mount
101+
exit 1
102+
esac
103+
else
104+
echo -e "METADATA\\n"
105+
[ -f $availability_zone ] && echo -e "availability zone:\t" "$(cat $availability_zone)"
106+
[ -f $cloud_identifier ] && echo -e "cloud identifier:\t" "$(cat $cloud_identifier)"
107+
[ -f $instance_id ] && echo -e "instance-id:\t\t" "$(cat $instance_id)"
108+
[ -f $local_hostname ] && echo -e "local-hostname:\t\t" "$(cat $local_hostname)"
109+
[ -f $service_offering ] && echo -e "service-offering:\t" "$(cat $service_offering)"
110+
[ -f $vm_id ] && echo -e "vm-id:\t\t\t" "$(cat $vm_id)"
111+
[ -f $public_key ] && echo -e "public-key:\t\t" "$(cat $public_key)"
112+
fi
113+
;;
114+
-p | --password ) echo -n "PASSWORD: "
115+
filename=$vm_password
116+
;;
117+
-h | --help ) usage
118+
remove_mount
119+
exit 0
120+
;;
121+
* ) usage
122+
remove_mount
123+
exit 1
124+
esac
125+
126+
if [ "$filename" != "" ] && [ -e $filename ]
127+
then
128+
cat $filename
129+
fi
130+
131+
remove_mount
132+
exit 0
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/bin/bash
2+
#
3+
# Init file for Password Download Client
4+
#
5+
# chkconfig: 345 98 02
6+
# description: Password Download Client
7+
8+
# Licensed to the Apache Software Foundation (ASF) under one
9+
# or more contributor license agreements. See the NOTICE file
10+
# distributed with this work for additional information
11+
# regarding copyright ownership. The ASF licenses this file
12+
# to you under the Apache License, Version 2.0 (the
13+
# "License"); you may not use this file except in compliance
14+
# with the License. You may obtain a copy of the License at
15+
#
16+
# http://www.apache.org/licenses/LICENSE-2.0
17+
#
18+
# Unless required by applicable law or agreed to in writing,
19+
# software distributed under the License is distributed on an
20+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21+
# KIND, either express or implied. See the License for the
22+
# specific language governing permissions and limitations
23+
# under the License.
24+
25+
26+
# Modify this line to specify the user (default is root)
27+
user=root
28+
29+
mountdir=$(mktemp -d)
30+
31+
# If lable name is other than config, please change the below line as required
32+
DefaultDisk=/dev/disk/by-label/config
33+
34+
35+
Password_File=$mountdir/cloudstack/password/vm_password.txt
36+
password_received=0
37+
38+
function prepare_mount
39+
{
40+
if [ ! -e $mountdir ]; then
41+
mkdir $mountdir
42+
chmod 700 $mountdir
43+
fi
44+
45+
if [ -e $DefaultDisk ]; then
46+
Disk=$DefaultDisk
47+
else
48+
BLOCK_DEVICE=$(blkid -t LABEL='config' /dev/hd? /dev/sd? /dev/xvd? /dev/vd? -o device)
49+
if [ -n $BLOCK_DEVICE ]; then
50+
Disk=$BLOCK_DEVICE
51+
else
52+
logger -t "cloud" "Unable to set password to VM: Config drive not found"
53+
exit 1
54+
fi
55+
fi
56+
57+
mount -r $Disk $mountdir
58+
if [ $? -ne 0 ]; then
59+
echo "Failed mounting $Disk to /mnt/configdrive"
60+
exit 1
61+
fi
62+
}
63+
64+
function remove_mount
65+
{
66+
umount $mountdir
67+
}
68+
69+
prepare_mount
70+
71+
72+
if [ -f $Password_File ]
73+
then
74+
password=$(cat $Password_File)
75+
password=$(echo $password | tr -d '\r')
76+
remove_mount
77+
78+
case $password in
79+
80+
"") logger -t "cloud" "Config drive did not have any password for the VM"
81+
exit 0
82+
;;
83+
84+
"saved_password") logger -t "cloud" "VM has already saved a password"
85+
exit 0
86+
;;
87+
88+
*) logger -t "cloud" "VM got a valid password"
89+
password_received=1
90+
;;
91+
esac
92+
else
93+
remove_mount
94+
logger -t "cloud" "Password set failed on VM: password file not found in config drive"
95+
exit 0
96+
fi
97+
98+
if [ "$password_received" = "1" ]; then
99+
100+
newmd5sum=$(echo $password | md5sum | awk '{print $1}')
101+
if [ -f /var/vmpasswordmd5sum ]
102+
then
103+
oldsum=$(cat /var/vmpasswordmd5sum)
104+
105+
if [ "$newmd5sum" == "$oldsum" ]
106+
then
107+
logger -t "cloud" "There is no update of VM password from cloudstack, returning true"
108+
exit 0
109+
fi
110+
fi
111+
112+
logger -t "cloud" "Changing password ..."
113+
echo $user:$password | chpasswd
114+
115+
if [ $? -gt 0 ]
116+
then
117+
usermod -p `mkpasswd -m SHA-512 $password` $user
118+
119+
if [ $? -gt 0 ]
120+
then
121+
logger -t "cloud" "Failed to change password for user $user"
122+
exit 1
123+
else
124+
logger -t "cloud" "Successfully changed password for user $user"
125+
fi
126+
fi
127+
fi
128+
129+
echo $newmd5sum > /var/vmpasswordmd5sum
130+
exit 0
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/bin/bash
2+
#
3+
# Init file for SSH Public Keys Download Client
4+
#
5+
# chkconfig: 345 98 02
6+
# description: SSH Public Keys Download Client
7+
8+
# Licensed to the Apache Software Foundation (ASF) under one
9+
# or more contributor license agreements. See the NOTICE file
10+
# distributed with this work for additional information
11+
# regarding copyright ownership. The ASF licenses this file
12+
# to you under the Apache License, Version 2.0 (the
13+
# "License"); you may not use this file except in compliance
14+
# with the License. You may obtain a copy of the License at
15+
#
16+
# http://www.apache.org/licenses/LICENSE-2.0
17+
#
18+
# Unless required by applicable law or agreed to in writing,
19+
# software distributed under the License is distributed on an
20+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21+
# KIND, either express or implied. See the License for the
22+
# specific language governing permissions and limitations
23+
# under the License.
24+
25+
26+
# Modify this line to specify the user (default is root)
27+
user=root
28+
29+
mountdir=$(mktemp -d)
30+
31+
# If lable name is other than config, please change the below line as required
32+
DefaultDisk=/dev/disk/by-label/config
33+
34+
SSHKey_File=$mountdir/cloudstack/metadata/public_keys.txt
35+
keys_received=0
36+
37+
function prepare_mount
38+
{
39+
if [ ! -e $mountdir ]; then
40+
mkdir $mountdir
41+
chmod 700 $mountdir
42+
fi
43+
44+
if [ -e $DefaultDisk ]; then
45+
Disk=$DefaultDisk
46+
else
47+
BLOCK_DEVICE=$(blkid -t LABEL='config' /dev/hd? /dev/sd? /dev/xvd? /dev/vd? -o device)
48+
if [ -n $BLOCK_DEVICE ]; then
49+
Disk=$BLOCK_DEVICE
50+
else
51+
logger -t "cloud" "Unable to get SSH public key: Config drive not found"
52+
exit 1
53+
fi
54+
fi
55+
56+
mount -r $Disk $mountdir
57+
if [ $? -ne 0 ]; then
58+
echo "Failed mounting $Disk to /mnt/configdrive"
59+
exit 1
60+
fi
61+
}
62+
63+
function remove_mount
64+
{
65+
umount $mountdir
66+
}
67+
68+
prepare_mount
69+
70+
71+
if [ -f $SSHKey_File ]
72+
then
73+
publickey=$(cat $SSHKey_File)
74+
publickey=$(echo $publickey | tr -d '\r')
75+
remove_mount
76+
77+
if [ -z "$publickey" ]; then
78+
logger -t "cloud" "Did not receive any keys"
79+
exit 1
80+
fi
81+
else
82+
remove_mount
83+
logger -t "cloud" "Did not receive any keys"
84+
exit 1
85+
fi
86+
87+
homedir=$(grep ^$user /etc/passwd|awk -F ":" '{print $6}')
88+
sshdir=$homedir/.ssh
89+
authorized=$sshdir/authorized_keys
90+
91+
if [ ! -e $sshdir ]; then
92+
mkdir $sshdir
93+
chmod 700 $sshdir
94+
fi
95+
96+
if [ ! -e $authorized ]; then
97+
touch $authorized
98+
chmod 600 $authorized
99+
fi
100+
101+
102+
cat $authorized|grep -v "$publickey"|tee $authorized > /dev/null
103+
echo "$publickey" >> $authorized
104+
105+
which restorecon && restorecon -R -v $sshdir
106+
107+
exit 0

0 commit comments

Comments
 (0)