Bug 9887 - baremetal: support for image operation (create template from guest disk)

Resolved fixed
This commit is contained in:
frank 2011-07-07 15:34:26 -07:00
parent b885915f1e
commit 3c1a375978
6 changed files with 62 additions and 22 deletions

View File

@ -18,5 +18,8 @@
package com.cloud.vm; package com.cloud.vm;
import com.cloud.exception.ResourceAllocationException;
public interface BareMetalVmService extends UserVmService { public interface BareMetalVmService extends UserVmService {
public Long createTemplate(Long hostId, Integer bits, String displayText, String url, Boolean featured, Boolean publicTemplate, String templateName, Long osTypeId) throws ResourceAllocationException;
} }

View File

@ -27,8 +27,8 @@ BuildRequires: java-1.6.0-openjdk-devel
BuildRequires: tomcat6 BuildRequires: tomcat6
BuildRequires: ws-commons-util BuildRequires: ws-commons-util
#BuildRequires: commons-codec #BuildRequires: commons-codec
BuildRequires: commons-dbcp #BuildRequires: commons-dbcp
BuildRequires: commons-collections #BuildRequires: commons-collections
BuildRequires: commons-httpclient BuildRequires: commons-httpclient
BuildRequires: jpackage-utils BuildRequires: jpackage-utils
BuildRequires: gcc BuildRequires: gcc

View File

@ -26,7 +26,7 @@ from sys import exit
from os import makedirs from os import makedirs
from os.path import exists, join from os.path import exists, join
template = '''DEFAULT default restore_template = '''DEFAULT default
PROMPT 1 PROMPT 1
TIMEOUT 26 TIMEOUT 26
DISPLAY boot.msg DISPLAY boot.msg
@ -35,19 +35,30 @@ KERNEL kernel
APPEND vga=normal devfs=nomount pxe ramdisk_size=66000 load_ramdisk=1 init=/linuxrc prompt_ramdisk=0 initrd=initrd.gz root=/dev/ram0 rw noapic nolapic lba combined_mode=libata ide0=noprobe nomce pci=nomsi irqpoll quiet Server="%s" Share="%s" Directory="%s" Image_To_Restore="%s" After_Completion="Reboot" CIFS_Preferred="Y" Zsplit_Preferred="Y" AUTO="Y" User="%s" Passwd="%s" Extend_Parts_Whenever_Possible="N" Replace_BIOS="N" IP="%s" Netmask="%s" Gateway="%s" APPEND vga=normal devfs=nomount pxe ramdisk_size=66000 load_ramdisk=1 init=/linuxrc prompt_ramdisk=0 initrd=initrd.gz root=/dev/ram0 rw noapic nolapic lba combined_mode=libata ide0=noprobe nomce pci=nomsi irqpoll quiet Server="%s" Share="%s" Directory="%s" Image_To_Restore="%s" After_Completion="Reboot" CIFS_Preferred="Y" Zsplit_Preferred="Y" AUTO="Y" User="%s" Passwd="%s" Extend_Parts_Whenever_Possible="N" Replace_BIOS="N" IP="%s" Netmask="%s" Gateway="%s"
''' '''
backup_template = '''DEFAULT default
PROMPT 1
TIMEOUT 26
DISPLAY boot.msg
LABEL default
KERNEL kernel
APPEND vga=normal devfs=nomount pxe ramdisk_size=66000 load_ramdisk=1 init=/linuxrc prompt_ramdisk=0 initrd=initrd.gz root=/dev/ram0 rw noapic nolapic lba combined_mode=libata ide0=noprobe nomce pci=nomsi irqpoll quiet Server="%s" Share="%s" Directory="%s" Image_To_Restore="Create_New_Image" New_Image_Name="%s" Already_Existing_Image="Replace" Store_MD5="N" Compression_Type="gzip" After_Completion="Reboot" Minimize_Before_Storing="N" Repart="N" CIFS_Preferred="Y" AUTO="Y" User="%s" Passwd="%s" Extend_Parts_Whenever_Possible="N" Replace_BIOS="N" IP="%s" Netmask="%s" Gateway="%s"
'''
cmd = ''
tftp_dir = '' tftp_dir = ''
mac = '' mac = ''
cifs_server = '' cifs_server = ''
share = '' share = ''
directory = '' directory = ''
image_to_restore = '' template_dir = ''
cifs_username = '' cifs_username = ''
cifs_password = '' cifs_password = ''
ip = '' ip = ''
netmask = '' netmask = ''
gateway = '' gateway = ''
def prepare_boot_file(): def prepare(is_restore):
try: try:
pxelinux = join(tftp_dir, "pxelinux.cfg") pxelinux = join(tftp_dir, "pxelinux.cfg")
if exists(pxelinux) == False: if exists(pxelinux) == False:
@ -56,31 +67,32 @@ def prepare_boot_file():
cfg_name = "01-" + mac.replace(':','-').lower() cfg_name = "01-" + mac.replace(':','-').lower()
cfg_path = join(pxelinux, cfg_name) cfg_path = join(pxelinux, cfg_name)
f = open(cfg_path, "w") f = open(cfg_path, "w")
stuff = template % (cifs_server, share, directory, image_to_restore, cifs_username, cifs_password, ip, netmask, gateway) if is_restore:
fmt = restore_template
else:
fmt = backup_template
stuff = fmt % (cifs_server, share, directory, template_dir, cifs_username, cifs_password, ip, netmask, gateway)
f.write(stuff) f.write(stuff)
f.close() f.close()
return 0 return 0
except IOError, e: except Exception, e:
print e print e
return 1 return 1
if __name__ == "__main__": if __name__ == "__main__":
if len(sys.argv) < 12: if len(sys.argv) < 12:
print "Usage: prepare_tftp_bootfile.py tftp_dir mac cifs_server share directory image_to_restor cifs_username cifs_password ip netmask gateway" print "Usage: prepare_tftp_bootfile.py tftp_dir mac cifs_server share directory image_to_restor cifs_username cifs_password ip netmask gateway"
exit(1) exit(1)
tftp_dir = sys.argv[1] (cmd, tftp_dir, mac, cifs_server, share, directory, template_dir, cifs_username, cifs_password, ip, netmask, gateway) = sys.argv[1:]
mac = sys.argv[2]
cifs_server = sys.argv[3]
share = sys.argv[4]
directory = sys.argv[5]
image_to_restore = sys.argv[6]
cifs_username = sys.argv[7]
cifs_password = sys.argv[8]
ip = sys.argv[9]
netmask = sys.argv[10]
gateway = sys.argv[11]
if cmd == "restore":
ret = prepare(True)
elif cmd == "backup":
ret = prepare(False)
else:
print "Unknown cmd: %s"%cmd
ret = 1
ret = prepare_boot_file()
exit(ret) exit(ret)

View File

@ -156,7 +156,26 @@ def power(args):
else: else:
return 0 return 0
call_table = {"ping":ping, "boot_dev":boot_dev, "reboot":reboot, "power":power} def boot_or_reboot(args):
hostname = args.get("hostname")
usrname = args.get("usrname")
password = args.get("password")
o = ipmitool("-H", hostname, "-U", usrname, "-P", password, "chassis", "power", "status")
if o.ret:
print o.stderr
return 1
if "is on" in o.stdout:
args["action"] = "reset"
elif "is off" in o.stdout:
args["action"] = "on"
else:
print "unknown power status:" + o.stdout
return 1
return power(args)
call_table = {"ping":ping, "boot_dev":boot_dev, "reboot":reboot, "power":power, "boot_or_reboot":boot_or_reboot}
def dispatch(args): def dispatch(args):
cmd = args[1] cmd = args[1]
params = args[2:] params = args[2:]

View File

@ -5,6 +5,7 @@ import com.cloud.api.commands.DeleteTemplateCmd;
import com.cloud.api.commands.RegisterIsoCmd; import com.cloud.api.commands.RegisterIsoCmd;
import com.cloud.api.commands.RegisterTemplateCmd; import com.cloud.api.commands.RegisterTemplateCmd;
import com.cloud.exception.ResourceAllocationException; import com.cloud.exception.ResourceAllocationException;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.storage.VMTemplateVO; import com.cloud.storage.VMTemplateVO;
import com.cloud.utils.component.Adapter; import com.cloud.utils.component.Adapter;
@ -35,4 +36,9 @@ public interface TemplateAdapter extends Adapter {
public TemplateProfile prepareDelete(DeleteIsoCmd cmd); public TemplateProfile prepareDelete(DeleteIsoCmd cmd);
public boolean delete(TemplateProfile profile); public boolean delete(TemplateProfile profile);
public TemplateProfile prepare(boolean isIso, Long userId, String name, String displayText, Integer bits,
Boolean passwordEnabled, Boolean requiresHVM, String url, Boolean isPublic, Boolean featured,
Boolean isExtractable, String format, Long guestOSId, Long zoneId, HypervisorType hypervisorType,
String accountName, Long domainId, String chksum, Boolean bootable) throws ResourceAllocationException;
} }

View File

@ -87,7 +87,7 @@ public abstract class TemplateAdapterBase implements TemplateAdapter {
(accountType == Account.ACCOUNT_TYPE_READ_ONLY_ADMIN)); (accountType == Account.ACCOUNT_TYPE_READ_ONLY_ADMIN));
} }
private TemplateProfile prepare(boolean isIso, Long userId, String name, String displayText, Integer bits, public TemplateProfile prepare(boolean isIso, Long userId, String name, String displayText, Integer bits,
Boolean passwordEnabled, Boolean requiresHVM, String url, Boolean isPublic, Boolean featured, Boolean passwordEnabled, Boolean requiresHVM, String url, Boolean isPublic, Boolean featured,
Boolean isExtractable, String format, Long guestOSId, Long zoneId, HypervisorType hypervisorType, Boolean isExtractable, String format, Long guestOSId, Long zoneId, HypervisorType hypervisorType,
String accountName, Long domainId, String chksum, Boolean bootable) throws ResourceAllocationException { String accountName, Long domainId, String chksum, Boolean bootable) throws ResourceAllocationException {