def load_project(self, project_name, project_path, filename, progressCallback = None): """Load the specified filename as project_name and store it in project_path""" tar_filename = filename if not filename.endswith(".mic.tar.bz2"): raise ValueError(_("Specified project restore file: %s, does not end in .mic.tar.bz2")) config_file = os.path.join(self.config_path, "%s.proj" % project_name) if os.path.exists(config_file): raise ValueError(_("A project already exists with that name: %s") % config_file) if project_path.find(' ') != -1: raise ValueError(_("Specified project path contains a space character, not allowed: %s") % project_path) if os.path.exists(project_path): if os.path.isdir(project_path): if len(os.listdir(project_path)): raise ValueError(_("Specified project-path, is a directory, but it is NOT empty: %s") % project_path) else: os.rmdir(project_path) else: raise ValueError(_("Specified project-path, exists, but it is not a directory")) tempdir = tempfile.mkdtemp() cwd = os.getcwd() os.chdir(tempdir) print _("Extracting: %s to temporary directory: %s/") % (filename, tempdir) time.sleep(2) if self.isVerboseProjectTar(): tar_options = "xfjv" else: tar_options = "xfj" pdk_utils.execCommand("tar %s %s --numeric-owner" % (tar_options, filename), callback = progressCallback) os.chdir(cwd) source_config_file = os.path.join(tempdir, "config", "save.proj") if not os.path.isfile(source_config_file): raise ValueError(_("Project config file did not exist in project tarfile. Could not find: %s") % source_config_file) source_project = os.path.join(tempdir, "project") if not os.path.isdir(source_project): raise ValueError(_("Project directory did not exist in project tarfile. Could not find: %s") % source_project) print _("Writing new config file: %s") % config_file self.copyProjectConfigFile(source_config_file, config_file, project_name, project_path) print _("Moving project directory into place at: %s") % project_path cmd_line = "mv -v %s %s" % (source_project, project_path) print cmd_line result = pdk_utils.execCommand(cmd_line) if result: print _("Error doing 'mv' cmd") sys.exit(1) print _("Removing temporary directory: %s") % tempdir pdk_utils.rmtree(tempdir, callback = self.progress_callback) print _("Project: %s restored to: %s") % (project_name, project_path)
def create_usb_image(self, size): print _("Creating USB flash drive image file at: %s") % self.path out_file = open(self.path, 'w') # Make a kibibyte length string of zeroes out_string = chr(0) * 1024 # Write the string out to the file to create file of size * mibibyte in length for count in range(0, size * 1024): if self.progress_callback and count % 1024 == 0: self.progress_callback(None) out_file.write(out_string) out_file.close() cmd_line = "mkfs.vfat %s" % self.path result = pdk_utils.execCommand(cmd_line, callback = self.progress_callback) if result: print >> sys.stderr, _("Error running command: %s") % cmd_line raise EnvironmentError, _("Error running command: %s") % cmd_line # NOTE: Running syslinux on the host development system # means the host and target have compatible architectures. # This runs syslinux inside the jailroot so the correct # version of syslinux is used. jail_path = self.path[len(self.project.path):] self.project.chroot('syslinux %s' % jail_path)
def create_image(self, fs_type='RAMFS'): print _("NFSLiveCDImage: Creating Live CD Image Now...") image_type = _("NFS Live CD Image") self.create_all_initramfs() initrd_stat_result = os.stat('/tmp/.tmp.initrd0') self.tmp_path = tempfile.mkdtemp('','pdk-', '/tmp') self.kernels.insert(0,self.default_kernel) for count, kernel in enumerate(self.kernels): initrd_path = os.path.join(self.tmp_path, "initrd%d.img" % count) try: shutil.move("/tmp/.tmp.initrd%d" % count, initrd_path) except: print _("shutil.move failed. Ignored error") self.kernels.pop(0) # Flashing yellow on a green background self.install_kernels("ae", image_type, 'NFSCDImage') try: pdk_utils.copy(os.path.join(self.project.path, "/usr/lib/syslinux/isolinux.bin"), self.tmp_path) except OSError: print _("Could not copy isolinux.bin. Ignored error") print _("Creating NFS CD image file at: %s") % self.path cmd_line = "genisoimage -quiet -o %s -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -l -R -r %s" % (self.path, self.tmp_path) result = pdk_utils.execCommand(cmd_line, callback = self.progress_callback) if result: print >> sys.stderr, _("Error running command: %s") % cmd_line raise EnvironmentError, _("Error running command: %s") % cmd_line shutil.rmtree(self.tmp_path) self.tmp_path = '' print _("NFSLiveIsoImage: Finished!")
def aptCreateChroot(self, chroot_dir, use_rootstrap, callback = None): """Create chroot in chroot_dir for using APT tools""" if not os.path.exists(chroot_dir): os.makedirs(chroot_dir) target_os = self.target_os var_dir = mic_cfg.config.get('general', 'var_dir') rootstrap_file = os.path.join(var_dir, "rootstraps", "apt", target_os, self.name, "rootstrap.tgz") if not os.path.exists(rootstrap_file): if self.__aptCreateRootstrap(chroot_dir, rootstrap_file, use_rootstrap, callback = callback) == False: return False else: cmd = "tar -jxvf %s -C %s" % (rootstrap_file, chroot_dir) output = [] result = pdk_utils.execCommand(cmd, output = output, callback = callback) if result != 0: print >> sys.stderr, _("ERROR: Unable to rootstrap %s from %s!") % (rootstrap_file, self.name) pdk_utils.rmtree(chroot_dir, callback = callback) # FIXME: Better exception here raise ValueError(" ".join(output)) # Setup copies of some useful files from the host into the chroot for filename in [ 'hosts', 'resolv.conf' ]: source_file = os.path.join("/etc", filename) target_file = os.path.join(chroot_dir, 'etc', filename) pdk_utils.safeTextFileCopy(source_file, target_file, force = True) return True
def mount_container(self): if not self.tmp_path: self.tmp_path = tempfile.mkdtemp('','pdk-', '/tmp') cmd_line = "mount -o loop -t vfat %s %s" % (self.path, self.tmp_path) result = pdk_utils.execCommand(cmd_line, callback = self.progress_callback) if result: print >> sys.stderr, _("Error running command: %s") % cmd_line raise EnvironmentError, _("Error running command: %s") % cmd_line
def create_image(self): print _("InstallCDImage: Creating Install CD Image Now...") image_type = _("Install CD Image. This will DESTROY all content on your hard drive!!") if self.project.platform.config_info['package_manager'] == 'apt': self.create_all_initramfs() self.create_grub_menu() self.apply_hd_kernel_cmdline() if self.project.platform.config_info['package_manager'] == 'yum': self.create_all_initrd() self.create_grub_menu_yum() self.create_rootfs() self.create_bootfs() initrd_stat_result = os.stat('/tmp/.tmp.initrd0') rootfs_stat_result = os.stat(self.rootfs_path) bootfs_stat_result = os.stat(self.bootfs_path) self.tmp_path = tempfile.mkdtemp('','pdk-', '/tmp') self.kernels.insert(0,self.default_kernel) for count, kernel in enumerate(self.kernels): initrd_path = os.path.join(self.tmp_path, "initrd%d.img" % count) try: shutil.move("/tmp/.tmp.initrd%d" % count, initrd_path) except: print _("shutil.move failed. Ignored error") self.kernels.pop(0) # Flashing yellow on a red background self.install_kernels("ce", image_type, 'CDImage') try: pdk_utils.copy(self.rootfs_path, self.tmp_path) except OSError: print _("Could not copy rootfs_path. Ignored error") try: pdk_utils.copy(self.bootfs_path, self.tmp_path) except OSError: print _("Could not copy bootfs_path. Ignored error") try: self.create_install_script(self.tmp_path) except OSError: print _("Could not create install script. Ignored error") try: pdk_utils.copy(os.path.join(self.project.path, "/usr/lib/syslinux/isolinux.bin"), self.tmp_path) except OSError: print _("Could not copy isolinux.bin. Ignored error") print _("Creating CD image file at: %s") % self.path cmd_line = "genisoimage -quiet -o %s -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -l -R -r %s" % (self.path, self.tmp_path) result = pdk_utils.execCommand(cmd_line, callback = self.progress_callback) if result: print >> sys.stderr, _("Error running command: %s") % cmd_line raise EnvironmentError, _("Error running command: %s") % cmd_line shutil.rmtree(self.tmp_path) self.tmp_path = '' self.delete_rootfs() self.delete_bootfs() print _("InstallIsoImage: Finished!") print _("\nYou can now use the image to boot and install the target file-system on the target device's HDD.\n") print _("\nWARNING: Entire contents of the target devices's HDD will be erased prior to installation!") print _(" This includes ALL partitions on the disk!\n") print _("InstallIsoImage: Finished!")
def create_ext3fs_file(self, path, size): """Create a ext3fs file. size is how big to make the file in megabytes""" out_file = open(path, 'w') out_string = chr(0) * 1024 for count in range(0, size * 1024): out_file.write(out_string) out_file.close() cmd_line = "mkfs.ext3 %s -F" % path result = pdk_utils.execCommand(cmd_line, callback = self.progress_callback) if result: print >> sys.stderr, _("Error running command: %s") % cmd_line raise EnvironmentError, _("Error running command: %s") % cmd_line
def create_bootfs(self): self.bootfs = 'bootfs.img' self.bootfs_path = os.path.join(self.target.image_path, self.bootfs) if os.path.isfile(self.bootfs_path): os.remove(self.bootfs_path) print _("Creating bootfs at: %s") % self.bootfs_path # Remove old initrd images for file in os.listdir(os.path.join(self.target.fs_path, 'boot')): if file.find('initrd.img') == 0: os.remove(os.path.join(self.target.fs_path, 'boot', file)) self.kernels.insert(0,self.default_kernel) # copy pre-created initrd img (by create_all_initramfs) for each installed kernel for count, kernel in enumerate(self.kernels): version_str = kernel.split('vmlinuz-').pop().strip() initrd_name = "initrd.img-" + version_str try: shutil.copy("/tmp/.tmp.initrd%d" % count, os.path.join(self.target.fs_path, 'boot', initrd_name)) except OSError: print _("shutil.copy failed. Ignored error") self.kernels.pop(0) if not os.path.exists("%s/boot/boot" % self.target.path): os.symlink(".", "%s/boot/boot" % self.target.path) fs_path = self.target.fs_path[len(self.project.path):] fs_path = os.path.join(fs_path, 'boot') cmd = "du -ks %s" % fs_path cmdOutput = [] print _("Caclulating Boot FS size: %s") % cmd self.project.chroot(cmd, cmdOutput) out_string = chr(0) * 1024 out_file = open(self.bootfs_path, 'w') size = int(string.atoi(cmdOutput[0].split()[0]) * 1.1) + 10240 print _("Bootfs size: %s") % (size) for count in range(0, size): if self.progress_callback and count % 1024 == 0: self.progress_callback(None) out_file.write(out_string) out_file.close() image_path = self.target.image_path[len(self.project.path):] image_path = os.path.join(image_path, 'bootfs.img') cmd = 'mkfs.ext3 -F %s' % image_path self.project.chroot(cmd) temp_dir = os.path.join(self.target.image_path,'temp') if not os.path.exists(temp_dir): os.makedirs(temp_dir) print _("Mounting bootfs.img...") cmd = "mount -o loop %s %s" % (self.bootfs_path, temp_dir) pdk_utils.execCommand(cmd) print _("Copying boot files...") cmd = "cp -a %s/. %s" % (os.path.join(self.target.fs_path, 'boot'), temp_dir) pdk_utils.execCommand(cmd) print _("Unmounting...") cmd = "umount %s" % (temp_dir) pdk_utils.execCommand(cmd)
def __aptCreateRootstrap(self, chroot_dir, rootstrap_file, use_rootstrap, callback = None): codename = self.buildroot_codename components = ",".join(self.buildroot_components) mirror = self.buildroot_mirror chroot_type_string = "Platform" basedir = os.path.dirname(rootstrap_file) if not os.path.exists(basedir): os.makedirs(basedir) if mic_cfg.config.get('general', 'package_manager') == 'apt': cmd = "debootstrap --arch %s --include=apt --components=%s %s %s %s" % (self.architecture, components, codename, chroot_dir, mirror) elif mic_cfg.config.get('general', 'package_manager') == 'yum': cmd = "/usr/sbin/debootstrap --arch %s --include=apt --components=%s %s %s %s" % (self.architecture, components, codename, chroot_dir, mirror) output = [] # XXX Evil hack if not os.path.isfile("/usr/lib/debootstrap/scripts/%s" % codename) and not os.path.isfile("/usr/share/debootstrap/scripts/%s" % codename): cmd += " " + paths.PKGDATADIR + "/debootstrap-scripts/%s" % codename # Sometimes we see network issues that trigger debootstrap to claim the # apt repository is corrupt. This trick will force up to 10 attempts # before bailing out with an error count = 0 while count < 10: count += 1 print _("--------%s rootstrap creation try: %s ----------") % (chroot_type_string, count) print _("Execing command: %s") % cmd result = pdk_utils.execCommand(cmd, output = output, callback = callback) if result == 0: print _("--------%s rootstrap creation completed successfully----------") % chroot_type_string break; if result < 0: print _("Process Aborted") return False print _("--------%s rootstrap creation failed result: %s ----------") % (chroot_type_string, result) sleeptime = 30 print _("--------For try: %s. Sleeping for %s seconds... -----------------") % (count, sleeptime) time.sleep(sleeptime) if result != 0: print >> sys.stderr, _("ERROR: Unable to generate %s rootstrap!") % chroot_type_string raise ValueError(" ".join(output)) self.pkg_manager.cleanPackageCache(chroot_dir) source_dir = os.path.join(self.path, 'sources') for filename in os.listdir(source_dir): source_path = os.path.join(source_dir, filename) dest_path = os.path.join(chroot_dir, 'etc', 'apt', 'sources.list.d', filename) pdk_utils.copySourcesListFile(source_path, dest_path) source_path = os.path.join(self.path, 'preferences') if os.path.exists(source_path): shutil.copy(source_path, os.path.join(chroot_dir, 'etc', 'apt')) if use_rootstrap == True: self.__createRootstrap(chroot_dir, rootstrap_file, callback = None)
def yumCreateChroot(self, chroot_dir, use_rootstrap, callback = None): if not os.path.exists(chroot_dir): os.makedirs(chroot_dir) target_os = self.target_os var_dir = mic_cfg.config.get('general', 'var_dir') rootstrap_file = os.path.join(var_dir, "rootstraps", "yum", target_os, self.name, "rootstrap.tgz") if not os.path.exists(rootstrap_file): self.__yumCreateRootstrap(chroot_dir, rootstrap_file, use_rootstrap, callback = callback) else: cmd = "tar -jxvf %s -C %s" % (rootstrap_file, chroot_dir) output = [] result = pdk_utils.execCommand(cmd, output = output, callback = callback) if result != 0: print >> sys.stderr, _("ERROR: Unable to rootstrap %s from %s!") % (rootstrap_file, name) pdk_utils.rmtree(chroot_dir, callback = callback) # FIXME: Better exception here raise ValueError(" ".join(output))
def __yumCreateRootstrap(self, chroot_dir, rootstrap_file, use_rootstrap, callback = None): basedir = os.path.dirname(rootstrap_file) if not os.path.exists(basedir): os.makedirs(basedir) self.__yumCreateBase(chroot_dir) self.__yumCreateDevices(chroot_dir) self.__yumDoMounts(chroot_dir) # install yum inside the project using the host tools print _("Creating rootstrap directory with yum...") output = [] cmd = 'yum -y --disablerepo=localbase --installroot=%s install yum yum-protectbase' % chroot_dir cmd = 'yum -y --installroot=%s install yum yum-protectbase' % chroot_dir #cmd = 'yum -y --installroot=%s groupinstall buildsys-build' % chroot_dir print _("Exec command: %s") % cmd result = pdk_utils.execCommand(cmd, output = output, callback = callback) if result != 0: raise RuntimeError(_("Failed to create Yum based rootstrap")) # nuke all the yum cache to ensure that we get the latest greatest at project creation shutil.rmtree(os.path.join(chroot_dir, 'var', 'cache', 'yum')) self.__yumDoUmounts(chroot_dir) # Create the rootstrap archive file if use_rootstrap == True: self.__createRootstrap(chroot_dir, rootstrap_file, callback = callback)
def __yumDoMounts(self, chroot_dir): for cmd in ['mount -n -t proc mic_chroot_proc %s/proc' % chroot_dir, 'mount -n -t devpts mic_chroot_devpts %s/dev/pts' % chroot_dir, 'mount -n -t sysfs mic_chroot_sysfs %s/sys' % chroot_dir, ]: pdk_utils.execCommand(cmd)
def __createRootstrap(self, chroot_dir, rootstrap_file, callback = None): cmd = "tar -jcpvf %s -C %s ." % (rootstrap_file, chroot_dir) output = [] result = pdk_utils.execCommand(cmd, output = output, callback = callback) if result != 0: print >> sys.stderr, _("Warning: Unable to archive rootstrap!")