Skip to content

Commit 09145ef

Browse files
author
Chiradeep Vittal
committed
Bug 8199: inject ssh public key into systemvm iso file
1 parent 2db078a commit 09145ef

3 files changed

Lines changed: 79 additions & 60 deletions

File tree

cloud.spec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ Requires: sudo
156156
Requires: /sbin/service
157157
Requires: /sbin/chkconfig
158158
Requires: /usr/bin/ssh-keygen
159+
Requires: /usr/bin/mkisofs
159160
Requires: MySQL-python
160161
Requires: python-paramiko
161162
Requires: augeas >= 0.7.1

scripts/vm/systemvm/injectkeys.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash
2+
3+
# $1 = new key
4+
5+
#set -x
6+
7+
TMP=/tmp
8+
SYSTEMVM_PATCH_DIR=../../../vms/
9+
MOUNTPATH=/mnt/cloud/systemvm
10+
TMPDIR=${TMP}/cloud/systemvm
11+
12+
13+
inject() {
14+
local isofile=${SYSTEMVM_PATCH_DIR}/$1
15+
local newpubkey=$2
16+
local backup=${isofile}.bak
17+
local tmpiso=${TMP}/$1
18+
rm -rf $TMPDIR
19+
mkdir -p $TMPDIR
20+
[ ! -d $TMPDIR ] && echo "$(basename $0): Could not find/create temporary dir $TMPDIR" && return 1
21+
[ ! -f $isofile ] && echo "$(basename $0): Could not find systemvm iso patch file $isofile" && return 1
22+
cp -b $isofile $backup
23+
[ $? -ne 0 ] && echo "$(basename $0): Failed to backup original iso $isofile" && return 1
24+
mount -o loop $isofile $MOUNTPATH
25+
[ $? -ne 0 ] && echo "$(basename $0): Failed to mount original iso $isofile" && return 1
26+
cp -fr $MOUNTPATH/* $TMPDIR/
27+
[ $? -ne 0 ] && echo "$(basename $0): Failed to copy from original iso $isofile" && return 1
28+
cp $newpubkey $TMPDIR/authorized_keys
29+
[ $? -ne 0 ] && echo "$(basename $0): Failed to copy key $newpubkey from original iso to new iso " && return 1
30+
mkisofs -quiet -r -o $tmpiso $TMPDIR
31+
[ $? -ne 0 ] && echo "$(basename $0): Failed to create new iso $tmpiso from $TMPDIR" && return 1
32+
umount $MOUNTPATH
33+
[ $? -ne 0 ] && echo "$(basename $0): Failed to unmount old iso from $MOUNTPATH" && return 1
34+
cp -f $tmpiso $isofile
35+
[ $? -ne 0 ] && echo "$(basename $0): Failed to overwrite old iso $isofile with $tmpiso" && return 1
36+
rm -rf $TMPDIR
37+
}
38+
39+
mkdir -p $MOUNTPATH
40+
41+
[ $# -ne 1 ] && echo "Usage: $(basename $0) <new keyfile>" && exit 3
42+
newpubkey=$1
43+
[ ! -f $newpubkey ] && echo "$(basename $0): Could not open $newpubkey" && exit 3
44+
[ $EUID -ne 0 ] && echo "$(basename $0): You have to be root to run this script" && exit 3
45+
46+
command -v mkisofs > /dev/null || (echo "$(basename $0): mkisofs not found, please install or ensure PATH is accurate" ; exit 4)
47+
48+
inject systemvm.iso $newpubkey
49+
#inject systemvm-premium.iso $newpubkey
50+
51+
exit $?

server/src/com/cloud/server/ConfigurationServerImpl.java

Lines changed: 27 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.apache.commons.codec.binary.Base64;
4242
import org.apache.log4j.Logger;
4343

44+
import com.cloud.agent.api.routing.NetworkElementCommand;
4445
import com.cloud.configuration.Config;
4546
import com.cloud.configuration.ConfigurationVO;
4647
import com.cloud.configuration.dao.ConfigurationDao;
@@ -84,6 +85,7 @@
8485
import com.cloud.utils.component.ComponentLocator;
8586
import com.cloud.utils.db.DB;
8687
import com.cloud.utils.db.Transaction;
88+
import com.cloud.utils.exception.CloudRuntimeException;
8789
import com.cloud.utils.net.NetUtils;
8890
import com.cloud.utils.script.Script;
8991

@@ -248,54 +250,8 @@ public void persistDefaultValues() throws InvalidParameterValueException, Intern
248250
_configDao.update("init", "true");
249251
}
250252

251-
/*
252-
private String getManagementNetworkCIDR() {
253-
String[] gatewayAndNetmask = getGatewayAndNetmask();
254-
255-
if (gatewayAndNetmask == null) {
256-
return null;
257-
} else {
258-
String gateway = gatewayAndNetmask[0];
259-
String netmask = gatewayAndNetmask[1];
260-
261-
String subnet = NetUtils.getSubNet(gateway, netmask);
262-
long cidrSize = NetUtils.getCidrSize(netmask);
263-
264-
return subnet + "/" + cidrSize;
265-
}
266-
}
267-
*/
268253

269-
private String[] getGatewayAndNetmask() {
270-
String defaultRoute = Script.runSimpleBashScript("/sbin/ip route | grep default");
271-
272-
if (defaultRoute == null) {
273-
return null;
274-
}
275-
276-
String[] defaultRouteList = defaultRoute.split("\\s+");
277-
278-
if (defaultRouteList.length < 5) {
279-
return null;
280-
}
281-
282-
String gateway = defaultRouteList[2];
283-
String ethDevice = defaultRouteList[4];
284-
String netmask = null;
285-
286-
if (ethDevice != null) {
287-
netmask = Script.runSimpleBashScript("/sbin/ifconfig " + ethDevice + " | grep Mask | awk '{print $4}' | cut -d':' -f2");
288-
}
289-
290-
if (gateway == null || netmask == null) {
291-
return null;
292-
} else if (!NetUtils.isValidIp(gateway) || !NetUtils.isValidNetmask(netmask)) {
293-
return null;
294-
} else {
295-
return new String[] {gateway, netmask};
296-
}
297-
}
298-
254+
299255
private String getEthDevice() {
300256
String defaultRoute = Script.runSimpleBashScript("/sbin/route | grep default");
301257

@@ -334,19 +290,7 @@ private String getEnvironmentProperty(String name) {
334290
}
335291
}
336292

337-
private String getDNS() {
338-
String dnsLine = Script.runSimpleBashScript("grep nameserver /etc/resolv.conf");
339-
if (dnsLine == null) {
340-
return null;
341-
} else {
342-
String[] dnsLineArray = dnsLine.split(" ");
343-
if (dnsLineArray.length != 2) {
344-
return null;
345-
} else {
346-
return dnsLineArray[1];
347-
}
348-
}
349-
}
293+
350294

351295
@DB
352296
protected String getHost() {
@@ -511,6 +455,29 @@ protected void updateKeyPairs() {
511455
s_logger.error("SQL of the public key failed",ex);
512456
throw new RuntimeException("SQL of the public key failed");
513457
}
458+
injectSshKeyIntoSystemVmIsoPatch(pubkeyfile.getAbsolutePath());
459+
if (s_logger.isDebugEnabled()) {
460+
s_logger.debug("Public key inserted into systemvm iso");
461+
}
462+
} else {
463+
s_logger.info("Keypairs already in database");
464+
}
465+
}
466+
467+
468+
protected void injectSshKeyIntoSystemVmIsoPatch(String publicKeyPath) {
469+
String injectScript = "scripts/vm/systemvm/injectkeys.sh";
470+
String scriptPath = Script.findScript("" , injectScript);
471+
if ( scriptPath == null ) {
472+
throw new CloudRuntimeException("Unable to find key inject script " + injectScript);
473+
}
474+
final Script command = new Script(scriptPath, s_logger);
475+
command.add(publicKeyPath);
476+
477+
final String result = command.execute();
478+
if (result != null) {
479+
s_logger.warn("Failed to inject generated public key into systemvm iso " + result);
480+
throw new CloudRuntimeException("Failed to inject generated public key into systemvm iso " + result);
514481
}
515482
}
516483

0 commit comments

Comments
 (0)