def vm_create_node(vm_name): logger.info('%s(): caller: %s()', log_utils.get_fname(1), log_utils.get_fname(2)) try: vm_config = conf.vm[vm_name] except Exception: logger.error("Failed to import VM configuration config.vm_%s.", vm_name) raise vm.vm_create(vm_config) vm.vm_mem(vm_config) vm.vm_cpus(vm_config) configure_node_netifs(vm_name) if conf.vm[vm_name].ssh_port: vm.vm_port(vm_name, "ssh", conf.vm[vm_name].ssh_port, 22) if conf.vm[vm_name].http_port: vm.vm_port(vm_name, "http", conf.vm[vm_name].http_port, 80) if conf.wbatch: vm.vm_add_share(vm_name, conf.share_dir, conf.share_name) for index, disk in enumerate(conf.vm[vm_name].disks): # Turn number into letter (0->a, 1->b, etc.) disk_letter = chr(index + ord('a')) port = index if disk is None: continue elif disk == "base": base_disk_path = cvb.get_base_disk_path() if conf.wbatch or not vm.vm_disk_is_multiattach(base_disk_path): # Skip the following workaround if the base disk is already # registered as a multiattach image to avoid creating orphaned # COW images. Windows batch scripts use the workaround # unconditionally and therefore create COW orphan images # (visible in the VirtualBox Media Manager). # Attach and detach base disk first to have it registered with # the VirtualBox Media Manager. Required by VirtualBox 6+ for # configuring a multiattach volume. vm.vm_attach_disk(vm_name, base_disk_path) vm.vm_detach_disk(vm_name) vm.vm_attach_disk_multi(vm_name, base_disk_path) else: size = disk disk_name = "{}-sd{}.vdi".format(vm_name, disk_letter) disk_path = os.path.join(conf.img_dir, disk_name) # print("Adding additional disk to {}:\n\t{}".format(vm_name, # disk_path)) vm.create_vdi(disk_path, size) vm.vm_attach_disk(vm_name, disk_path, port)
def vm_create_node(vm_name): try: vm_config = conf.vm[vm_name] except Exception: logger.error("Failed to import VM configuration config.vm_%s.", vm_name) raise vm.vm_create(vm_config) vm.vm_mem(vm_config) vm.vm_cpus(vm_config) configure_node_netifs(vm_name) if conf.vm[vm_name].ssh_port: vm.vm_port(vm_name, "ssh", conf.vm[vm_name].ssh_port, 22) if conf.vm[vm_name].http_port: vm.vm_port(vm_name, "http", conf.vm[vm_name].http_port, 80) if conf.wbatch: vm.vm_add_share(vm_name, conf.share_dir, conf.share_name) for index, disk in enumerate(conf.vm[vm_name].disks): # Turn number into letter (0->a, 1->b, etc.) disk_letter = chr(index + ord('a')) port = index if disk is None: continue elif disk == "base": vm.vm_attach_disk_multi(vm_name, cvb.get_base_disk_path()) else: size = disk disk_name = "{}-sd{}.vdi".format(vm_name, disk_letter) disk_path = os.path.join(conf.img_dir, disk_name) # print("Adding additional disk to {}:\n\t{}".format(vm_name, # disk_path)) vm.create_vdi(disk_path, size) vm.vm_attach_disk(vm_name, disk_path, port)
def vm_install_base(): vm_name = "base" conf.vm[vm_name] = conf.VMconfig(vm_name) base_disk_path = cvb.get_base_disk_path() base_build_disk = os.path.join(conf.img_dir, "tmp-disk.vdi") logger.info("Creating\n\t%s.", base_disk_path) if conf.wbatch: wbatch.wbatch_begin_base() wbatch.wbatch_delete_disk(base_build_disk) if conf.do_build: if base_disk_exists(): logger.info("Deleting existing basedisk.") base_disk_delete() try: os.remove(base_build_disk) except OSError as err: if err.errno != errno.ENOENT: raise # File doesn't exist, that's fine. vm_config = conf.vm[vm_name] if conf.do_build: install_iso = iso_image.find_install_iso() else: install_iso = os.path.join(conf.img_dir, conf.iso_image.name) logger.info("Install ISO:\n\t%s", install_iso) vm.vm_create(vm_config) vm.vm_mem(vm_config) vm.vm_attach_dvd(vm_name, install_iso) if conf.wbatch: vm.vm_attach_guestadd_iso(vm_name) vm.create_vdi(base_build_disk, conf.base_disk_size) vm.vm_attach_disk(vm_name, base_build_disk) if conf.wbatch: # Automounted on /media/sf_bootstrap for first boot vm.vm_add_share_automount(vm_name, conf.share_dir, "bootstrap") # Mounted on /conf.share_name after first boot vm.vm_add_share(vm_name, conf.share_dir, conf.share_name) else: vm.vm_port(vm_name, "ssh", conf.vm[vm_name].ssh_port, 22) vm.vbm("modifyvm", vm_name, "--boot1", "dvd") vm.vbm("modifyvm", vm_name, "--boot2", "disk") autostart.autostart_reset() if conf.wbatch: autostart.autostart_queue("osbash/activate_autostart.sh") autostart.autostart_queue("osbash/base_fixups.sh") autostart.autostart_from_config(conf.base_install_scripts) autostart.autostart_queue("zero_empty.sh", "shutdown.sh") logger.info("Booting VM %s.", vm_name) vm.vm_boot(vm_name) # Note: It takes about 5 seconds for the installer in the VM to be ready # on a fairly typical laptop. If we don't wait long enough, the # installation will fail. Ideally, we would have a different method # of making sure the installer is ready. For now, we just have to # try and err on the side of caution. delay = 10 logger.info("Waiting %d seconds for VM %s to come up.", delay, vm_name) cs.conditional_sleep(delay) logger.info("Booting into distribution installer.") distro_boot.distro_start_installer(vm_config) autostart.autostart_and_wait(vm_name) vm.vm_wait_for_shutdown(vm_name) # Detach disk from VM now or it will be deleted by vm_unregister_del vm.vm_detach_disk(vm_name) vm.vm_unregister_del(vm_name) del conf.vm[vm_name] logger.info("Compacting %s.", base_build_disk) vm.vbm("modifyhd", base_build_disk, "--compact") # This disk will be moved to a new name, and this name will be used for # a new disk next time the script runs. vm.disk_unregister(base_build_disk) logger.info("Base disk created.") logger.info("Moving base disk to:\n\t%s", base_disk_path) if conf.do_build: import shutil shutil.move(base_build_disk, base_disk_path) if conf.wbatch: wbatch.wbatch_rename_disk(os.path.basename(base_build_disk), os.path.basename(base_disk_path)) wbatch.wbatch_end_file() logger.info("Base disk build ends.")