def _mount_instroot(self, base_on=None): if base_on and os.path.isfile(base_on): self._imgdir = os.path.dirname(base_on) imgname = os.path.basename(base_on) self._base_on(base_on) self._set_image_size(misc.get_file_size(self._image)) # here, self._instloops must be [] self._instloops.append({ "mountpoint": "/", "label": self.name, "name": imgname, "size": self.__image_size or 4096L, "fstype": self.__fstype or "ext3", "extopts": None, "loop": None, "uuid": None, "kspart": None }) self._check_imgdir() for loop in self._instloops: fstype = loop['fstype'] mp = os.path.join(self._instroot, loop['mountpoint'].lstrip('/')) size = loop['size'] * 1024L * 1024L imgname = loop['name'] if fstype in ("ext2", "ext3", "ext4"): MyDiskMount = fs.ExtDiskMount elif fstype == "btrfs": MyDiskMount = fs.BtrfsDiskMount elif fstype in ("vfat", "msdos"): MyDiskMount = fs.VfatDiskMount else: raise MountError('Cannot support fstype: %s' % fstype) loop['loop'] = MyDiskMount(fs.SparseLoopbackDisk( os.path.join(self._imgdir, imgname), size), mp, fstype, self._blocksize, loop['label'], fsuuid=loop['uuid']) if fstype in ("ext2", "ext3", "ext4"): loop['loop'].extopts = loop['extopts'] try: msger.verbose('Mounting image "%s" on "%s"' % (imgname, mp)) fs.makedirs(mp) loop['loop'].mount() # Make an autogenerated uuid avaialble in _get_post_scripts_env() if loop['kspart'] and loop['kspart'].uuid is None and \ loop['loop'].uuid: loop['kspart'].uuid = loop['loop'].uuid except MountError, e: raise
def savefs_before_chroot(chrootdir, saveto = None): """ backup chrootdir to another directory before chrooting in """ if configmgr.chroot['saveto']: savefs = True saveto = configmgr.chroot['saveto'] wrnmsg = "Can't save chroot fs for dir %s exists" % saveto if saveto == chrootdir: savefs = False wrnmsg = "Dir %s is being used to chroot" % saveto elif os.path.exists(saveto): if msger.ask("Dir %s already exists, cleanup and continue?" % saveto): shutil.rmtree(saveto, ignore_errors = True) savefs = True else: savefs = False if savefs: msger.info("Saving image to directory %s" % saveto) fs_related.makedirs(os.path.dirname(os.path.abspath(saveto))) runner.quiet("cp -af %s %s" % (chrootdir, saveto)) devs = ['dev/fd', 'dev/stdin', 'dev/stdout', 'dev/stderr', 'etc/mtab'] ignlst = [os.path.join(saveto, x) for x in devs] map(os.unlink, filter(os.path.exists, ignlst)) else: msger.warning(wrnmsg)
def _configure_efi_bootloader(self, isodir): """Set up the configuration for an EFI bootloader""" fs_related.makedirs(isodir + "/EFI/boot") if not self.__copy_efi_files(isodir): shutil.rmtree(isodir + "/EFI") return for f in os.listdir(isodir + "/isolinux"): os.link("%s/isolinux/%s" %(isodir, f), "%s/EFI/boot/%s" %(isodir, f)) cfg = self.__get_basic_efi_config(name = self.name, timeout = self._timeout) cfg += self.__get_efi_image_stanzas(isodir, self.name) cfgf = open(isodir + "/EFI/boot/grub.conf", "w") cfgf.write(cfg) cfgf.close() # first gen mactel machines get the bootloader name wrong apparently if rpmmisc.getBaseArch() == "i386": os.link(isodir + "/EFI/boot/grub.efi", isodir + "/EFI/boot/boot.efi") os.link(isodir + "/EFI/boot/grub.conf", isodir + "/EFI/boot/boot.conf") # for most things, we want them named boot$efiarch efiarch = {"i386": "ia32", "x86_64": "x64"} efiname = efiarch[rpmmisc.getBaseArch()] os.rename(isodir + "/EFI/boot/grub.efi", isodir + "/EFI/boot/boot%s.efi" %(efiname,)) os.link(isodir + "/EFI/boot/grub.conf", isodir + "/EFI/boot/boot%s.conf" %(efiname,))
def __write_initrd_conf(self, path): content = "" if not os.path.exists(os.path.dirname(path)): fs_related.makedirs(os.path.dirname(path)) f = open(path, "w") content += 'LIVEOS="yes"\n' content += 'PROBE="no"\n' content += 'MODULES+="squashfs ext3 ext2 vfat msdos "\n' content += 'MODULES+="sr_mod sd_mod ide-cd cdrom "\n' for module in self.__modules: if module == "=usb": content += 'MODULES+="ehci_hcd uhci_hcd ohci_hcd "\n' content += 'MODULES+="usb_storage usbhid "\n' elif module == "=firewire": content += 'MODULES+="firewire-sbp2 firewire-ohci "\n' content += 'MODULES+="sbp2 ohci1394 ieee1394 "\n' elif module == "=mmc": content += 'MODULES+="mmc_block sdhci sdhci-pci "\n' elif module == "=pcmcia": content += 'MODULES+="pata_pcmcia "\n' else: content += 'MODULES+="' + module + ' "\n' f.write(content) f.close()
def _stage_final_image(self): try: fs_related.makedirs(self.__ensure_isodir() + "/LiveOS") minimal_size = self._resparse() if not self.skip_minimize: fs_related.create_image_minimizer(self.__isodir + \ "/LiveOS/osmin.img", self._image, minimal_size) if self.skip_compression: shutil.move(self._image, self.__isodir + "/LiveOS/ext3fs.img") else: fs_related.makedirs(os.path.join( os.path.dirname(self._image), "LiveOS")) shutil.move(self._image, os.path.join(os.path.dirname(self._image), "LiveOS", "ext3fs.img")) fs_related.mksquashfs(os.path.dirname(self._image), self.__isodir + "/LiveOS/squashfs.img") self.__create_iso(self.__isodir) if self.pack_to: isoimg = os.path.join(self._outdir, self.name + ".iso") packimg = os.path.join(self._outdir, self.pack_to) misc.packing(packimg, isoimg) os.unlink(isoimg) finally: shutil.rmtree(self.__isodir, ignore_errors = True) self.__isodir = None
def savefs_before_chroot(chrootdir, saveto=None): """ backup chrootdir to another directory before chrooting in """ if configmgr.chroot['saveto']: savefs = True saveto = configmgr.chroot['saveto'] wrnmsg = "Can't save chroot fs for dir %s exists" % saveto if saveto == chrootdir: savefs = False wrnmsg = "Dir %s is being used to chroot" % saveto elif os.path.exists(saveto): if msger.ask("Dir %s already exists, cleanup and continue?" % saveto): shutil.rmtree(saveto, ignore_errors=True) savefs = True else: savefs = False if savefs: msger.info("Saving image to directory %s" % saveto) fs_related.makedirs(os.path.dirname(os.path.abspath(saveto))) runner.quiet("cp -af %s %s" % (chrootdir, saveto)) devs = [ 'dev/fd', 'dev/stdin', 'dev/stdout', 'dev/stderr', 'etc/mtab' ] ignlst = [os.path.join(saveto, x) for x in devs] map(os.unlink, filter(os.path.exists, ignlst)) else: msger.warning(wrnmsg)
def _configure_syslinux_bootloader(self, isodir): """configure the boot loader""" fs_related.makedirs(isodir + "/isolinux") menu = self.__find_syslinux_menu() self.__copy_syslinux_files(isodir, menu, self.__find_syslinux_mboot()) background = "" if self.__copy_syslinux_background(isodir + "/isolinux/splash.jpg"): background = "menu background splash.jpg" cfg = self.__get_basic_syslinux_config(menu = menu, background = background, name = self.name, timeout = self._timeout * 10, distroname = self.distro_name) cfg += self.__get_image_stanzas(isodir) cfg += self.__get_memtest_stanza(isodir) cfg += self.__get_local_stanza(isodir) cfg += self._get_isolinux_stanzas(isodir) cfgf = open(isodir + "/isolinux/isolinux.cfg", "w") cfgf.write(cfg) cfgf.close()
def _configure_efi_bootloader(self, isodir): """Set up the configuration for an EFI bootloader""" fs_related.makedirs(isodir + "/EFI/boot") if not self.__copy_efi_files(isodir): shutil.rmtree(isodir + "/EFI") return for f in os.listdir(isodir + "/isolinux"): os.link("%s/isolinux/%s" % (isodir, f), "%s/EFI/boot/%s" % (isodir, f)) cfg = self.__get_basic_efi_config(name=self.name, timeout=self._timeout) cfg += self.__get_efi_image_stanzas(isodir, self.name) cfgf = open(isodir + "/EFI/boot/grub.conf", "w") cfgf.write(cfg) cfgf.close() # first gen mactel machines get the bootloader name wrong apparently if rpmmisc.getBaseArch() == "i386": os.link(isodir + "/EFI/boot/grub.efi", isodir + "/EFI/boot/boot.efi") os.link(isodir + "/EFI/boot/grub.conf", isodir + "/EFI/boot/boot.conf") # for most things, we want them named boot$efiarch efiarch = {"i386": "ia32", "x86_64": "x64"} efiname = efiarch[rpmmisc.getBaseArch()] os.rename(isodir + "/EFI/boot/grub.efi", isodir + "/EFI/boot/boot%s.efi" % (efiname, )) os.link(isodir + "/EFI/boot/grub.conf", isodir + "/EFI/boot/boot%s.conf" % (efiname, ))
def package(self, destdir="."): """Prepares the created image for final delivery. In its simplest form, this method merely copies the install root to the supplied destination directory; other subclasses may choose to package the image by e.g. creating a bootable ISO containing the image and bootloader configuration. destdir -- the directory into which the final image should be moved; this defaults to the current directory. """ self._stage_final_image() if not os.path.exists(destdir): fs.makedirs(destdir) if self._recording_pkgs: self._save_recording_pkgs(destdir) # For image formats with two or multiple image files, it will be # better to put them under a directory if self.image_format in ("raw", "vmdk", "vdi", "nand", "mrstnand"): destdir = os.path.join(destdir, "%s-%s" % (self.name, self.image_format)) msger.debug("creating destination dir: %s" % destdir) fs.makedirs(destdir) # Ensure all data is flushed to _outdir runner.quiet("sync") misc.check_space_pre_cp(self._outdir, destdir) for f in os.listdir(self._outdir): shutil.move(os.path.join(self._outdir, f), os.path.join(destdir, f)) self.outimage.append(os.path.join(destdir, f)) self.do_genchecksum(os.path.join(destdir, f))
def _stage_final_image(self): try: fs_related.makedirs(self.__ensure_isodir() + "/LiveOS") minimal_size = self._resparse() if not self.skip_minimize: fs_related.create_image_minimizer(self.__isodir + \ "/LiveOS/osmin.img", self._image, minimal_size) if self.skip_compression: shutil.move(self._image, self.__isodir + "/LiveOS/ext3fs.img") else: fs_related.makedirs(os.path.join( os.path.dirname(self._image), "LiveOS")) shutil.move(self._image, os.path.join(os.path.dirname(self._image), "LiveOS", "ext3fs.img")) fs_related.mksquashfs(os.path.dirname(self._image), self.__isodir + "/LiveOS/squashfs.img") self.__create_iso(self.__isodir) finally: shutil.rmtree(self.__isodir, ignore_errors = True) self.__isodir = None
def _configure_syslinux_bootloader(self, isodir): """configure the boot loader""" fs_related.makedirs(isodir + "/isolinux") menu = self.__find_syslinux_menu() self.__copy_syslinux_files(isodir, menu, self.__find_syslinux_mboot()) background = "" if self.__copy_syslinux_background(isodir + "/isolinux/splash.jpg"): background = "menu background splash.jpg" cfg = self.__get_basic_syslinux_config(menu=menu, background=background, name=self.name, timeout=self._timeout * 10, distroname=self.distro_name) cfg += self.__get_image_stanzas(isodir) cfg += self.__get_memtest_stanza(isodir) cfg += self.__get_local_stanza(isodir) cfg += self._get_isolinux_stanzas(isodir) cfgf = open(isodir + "/isolinux/isolinux.cfg", "w") cfgf.write(cfg) cfgf.close()
def apply(self, kstimezone): self._check_sysconfig() tz = kstimezone.timezone or "America/New_York" utc = str(kstimezone.isUtc) f = open(self.path("/etc/sysconfig/clock"), "w+") f.write("ZONE=\"" + tz + "\"\n") f.write("UTC=" + utc + "\n") f.close() if not os.path.exists("/opt/etc"): fs.makedirs("/opt/etc") tz_source = "/usr/share/zoneinfo/%s" % (tz) tz_midst = "/opt/etc/localtime" tz_dest = "/etc/localtime" try: lncmd = fs.find_binary_inchroot('ln', self.instroot) if lncmd: self.call([lncmd, "-s", tz_source, tz_midst]) self.call([lncmd, "-s", tz_midst, tz_dest]) else: lncmd = fs.find_binary_path('ln') subprocess.call( [lncmd, "-s", self.path(tz_source), self.path(tz_midst)]) subprocess.call( [lncmd, "-s", self.path(tz_midst), self.path(tz_dest)]) except (IOError, OSError), (errno, msg): raise errors.KsError("Timezone setting error: %s" % msg)
def package(self, destdir = "."): """Prepares the created image for final delivery. In its simplest form, this method merely copies the install root to the supplied destination directory; other subclasses may choose to package the image by e.g. creating a bootable ISO containing the image and bootloader configuration. destdir -- the directory into which the final image should be moved; this defaults to the current directory. """ self._stage_final_image() if not os.path.exists(destdir): fs.makedirs(destdir) if self._img_compression_method: if not self._img_name: raise CreatorError("Image name not set.") rc = None img_location = os.path.join(self._outdir,self._img_name) if self._img_compression_method == "bz2": bzip2 = fs.find_binary_path('bzip2') msger.info("Compressing %s with bzip2. Please wait..." \ % img_location) rc = runner.show([bzip2, "-f", img_location]) if rc: raise CreatorError("Failed to compress image %s with %s." \ % (img_location, self._img_compression_method)) for bootimg in glob.glob(os.path.dirname(img_location) + \ "/*-boot.bin"): msger.info("Compressing %s with bzip2. Please wait..." \ % bootimg) rc = runner.show([bzip2, "-f", bootimg]) if rc: raise CreatorError("Failed to compress image %s with " "%s." \ % (bootimg, self._img_compression_method)) if self._recording_pkgs: self._save_recording_pkgs(destdir) # For image formats with two or multiple image files, it will be # better to put them under a directory if self.image_format in ("raw", "vmdk", "vdi", "nand", "mrstnand"): destdir = os.path.join(destdir, "%s-%s" \ % (self.name, self.image_format)) msger.debug("creating destination dir: %s" % destdir) fs.makedirs(destdir) # Ensure all data is flushed to _outdir runner.quiet('sync') for f in os.listdir(self._outdir): shutil.move(os.path.join(self._outdir, f), os.path.join(destdir, f)) self.outimage.append(os.path.join(destdir, f)) self.do_genchecksum(os.path.join(destdir, f))
def _stage_final_image(self): try: isodir = self._get_isodir() fs_related.makedirs(isodir + "/LiveOS") minimal_size = self._resparse() if not self.skip_minimize: fs_related.create_image_minimizer(isodir + "/LiveOS/osmin.img", self._image, minimal_size) if self.skip_compression: shutil.move(self._image, isodir + "/LiveOS/ext3fs.img") else: fs_related.makedirs( os.path.join(os.path.dirname(self._image), "LiveOS")) shutil.move( self._image, os.path.join(os.path.dirname(self._image), "LiveOS", "ext3fs.img")) fs_related.mksquashfs(os.path.dirname(self._image), isodir + "/LiveOS/squashfs.img") self._create_usbimg(isodir) if self.pack_to: usbimg = os.path.join(self._outdir, self.name + ".usbimg") packimg = os.path.join(self._outdir, self.pack_to) misc.packing(packimg, usbimg) os.unlink(usbimg) finally: shutil.rmtree(isodir, ignore_errors=True) self._set_isodir(None)
def _do_chroot_tar(cls, target, cmd=[]): mountfp_xml = os.path.splitext(target)[0] + '.xml' if not os.path.exists(mountfp_xml): raise errors.CreatorError("No mount point file found for this tar " "image, please check %s" % mountfp_xml) import tarfile tar = tarfile.open(target, 'r') tmpdir = misc.mkdtemp() tar.extractall(path=tmpdir) tar.close() mntdir = misc.mkdtemp() loops = [] for (mp, label, name, size, fstype) in load_mountpoints(mountfp_xml): if fstype in ("ext2", "ext3", "ext4"): myDiskMount = fs_related.ExtDiskMount elif fstype == "btrfs": myDiskMount = fs_related.BtrfsDiskMount elif fstype in ("vfat", "msdos"): myDiskMount = fs_related.VfatDiskMount else: raise errors.CreatorError("Cannot support fstype: %s" % fstype) name = os.path.join(tmpdir, name) size = size * 1024L * 1024L loop = myDiskMount(fs_related.SparseLoopbackDisk(name, size), os.path.join(mntdir, mp.lstrip('/')), fstype, size, label) try: msger.verbose("Mount %s to %s" % (mp, mntdir + mp)) fs_related.makedirs(os.path.join(mntdir, mp.lstrip('/'))) loop.mount() except: loop.cleanup() for lp in reversed(loops): chroot.cleanup_after_chroot("img", lp, None, mntdir) shutil.rmtree(tmpdir, ignore_errors=True) raise loops.append(loop) try: if len(cmd) != 0: cmdline = "/usr/bin/env HOME=/root " + ' '.join(cmd) else: cmdline = "/usr/bin/env HOME=/root /bin/bash" chroot.chroot(mntdir, None, cmdline) except: raise errors.CreatorError("Failed to chroot to %s." % target) finally: for loop in reversed(loops): chroot.cleanup_after_chroot("img", loop, None, mntdir) shutil.rmtree(tmpdir, ignore_errors=True)
def setup_qemu_emulator(rootdir, arch): # mount binfmt_misc if it doesn't exist if not os.path.exists("/proc/sys/fs/binfmt_misc"): modprobecmd = find_binary_path("modprobe") runner.show([modprobecmd, "binfmt_misc"]) if not os.path.exists("/proc/sys/fs/binfmt_misc/register"): mountcmd = find_binary_path("mount") runner.show([mountcmd, "-t", "binfmt_misc", "none", "/proc/sys/fs/binfmt_misc"]) # qemu_emulator is a special case, we can't use find_binary_path # qemu emulator should be a statically-linked executable file if arch == "aarch64": node = "/proc/sys/fs/binfmt_misc/aarch64" if os.path.exists("/usr/bin/qemu-arm64") and is_statically_linked("/usr/bin/qemu-arm64"): arm_binary = "qemu-arm64" elif os.path.exists("/usr/bin/qemu-aarch64") and is_statically_linked("/usr/bin/qemu-aarch64"): arm_binary = "qemu-aarch64" elif os.path.exists("/usr/bin/qemu-arm64-static"): arm_binary = "qemu-arm64-static" elif os.path.exists("/usr/bin/qemu-aarch64-static"): arm_binary = "qemu-aarch64-static" else: raise CreatorError("Please install a statically-linked %s" % arm_binary) else: node = "/proc/sys/fs/binfmt_misc/arm" arm_binary = "qemu-arm" if not os.path.exists("/usr/bin/qemu-arm") or not is_statically_linked("/usr/bin/qemu-arm"): arm_binary = "qemu-arm-static" if not os.path.exists("/usr/bin/%s" % arm_binary): raise CreatorError("Please install a statically-linked %s" % arm_binary) qemu_emulator = "/usr/bin/%s" % arm_binary if not os.path.exists(rootdir + "/usr/bin"): makedirs(rootdir + "/usr/bin") shutil.copy(qemu_emulator, rootdir + qemu_emulator) # disable selinux, selinux will block qemu emulator to run if os.path.exists("/usr/sbin/setenforce"): msger.info('Try to disable selinux') runner.show(["/usr/sbin/setenforce", "0"]) # unregister it if it has been registered and is a dynamically-linked executable if os.path.exists(node): qemu_unregister_string = "-1\n" with open(node, "w") as fd: fd.write(qemu_unregister_string) # register qemu emulator for interpreting other arch executable file if not os.path.exists(node): if arch == "aarch64": qemu_arm_string = ":aarch64:M::\\x7fELF\\x02\\x01\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\xb7:\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\x00\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\xff:%s:\n" % qemu_emulator else: qemu_arm_string = ":arm:M::\\x7fELF\\x01\\x01\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\x28\\x00:\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\x00\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfa\\xff\\xff\\xff:%s:\n" % qemu_emulator with open("/proc/sys/fs/binfmt_misc/register", "w") as fd: fd.write(qemu_arm_string) return qemu_emulator
def __create_repo_file(self, repo, repodir): fs.makedirs(self.path(repodir)) f = open(self.path(repodir + "/" + repo.name + ".repo"), "w") self.__create_repo_section(repo, "base", f) if repo.debuginfo: self.__create_repo_section(repo, "debuginfo", f) if repo.source: self.__create_repo_section(repo, "source", f) f.close()
def __save_repo_keys(self, repodata): if not repodata: return None gpgkeydir = "/etc/pki/rpm-gpg" fs.makedirs(self._instroot + gpgkeydir) for repo in repodata: if repo["repokey"]: repokey = gpgkeydir + "/RPM-GPG-KEY-%s" % repo["name"] shutil.copy(repo["repokey"], self._instroot + repokey)
def setup_qemu_emulator(rootdir, arch): # mount binfmt_misc if it doesn't exist if not os.path.exists("/proc/sys/fs/binfmt_misc"): modprobecmd = find_binary_path("modprobe") runner.show([modprobecmd, "binfmt_misc"]) if not os.path.exists("/proc/sys/fs/binfmt_misc/register"): mountcmd = find_binary_path("mount") runner.show([mountcmd, "-t", "binfmt_misc", "none", "/proc/sys/fs/binfmt_misc"]) # qemu_emulator is a special case, we can't use find_binary_path # qemu emulator should be a statically-linked executable file qemu_emulator = "/usr/bin/qemu-arm" if not os.path.exists(qemu_emulator) or not is_statically_linked(qemu_emulator): qemu_emulator = "/usr/bin/qemu-arm-static" if not os.path.exists(qemu_emulator): raise CreatorError("Please install a statically-linked qemu-arm") # qemu emulator version check armv7_list = [arch for arch in rpmmisc.archPolicies.keys() if arch.startswith('armv7')] if arch in armv7_list: # need qemu (>=0.13.0) qemuout = runner.outs([qemu_emulator, "-h"]) m = re.search("version\s*([.\d]+)", qemuout) if m: qemu_version = m.group(1) if qemu_version < "0.13": raise CreatorError("Requires %s version >=0.13 for %s" % (qemu_emulator, arch)) else: msger.warning("Can't get version info of %s, please make sure it's higher than 0.13.0" % qemu_emulator) if not os.path.exists(rootdir + "/usr/bin"): makedirs(rootdir + "/usr/bin") shutil.copy(qemu_emulator, rootdir + "/usr/bin/qemu-arm-static") qemu_emulator = "/usr/bin/qemu-arm-static" # disable selinux, selinux will block qemu emulator to run if os.path.exists("/usr/sbin/setenforce"): msger.info('Try to disable selinux') runner.show(["/usr/sbin/setenforce", "0"]) # unregister it if it has been registered and is a dynamically-linked executable node = "/proc/sys/fs/binfmt_misc/arm" if os.path.exists(node): qemu_unregister_string = "-1\n" fd = open("/proc/sys/fs/binfmt_misc/arm", "w") fd.write(qemu_unregister_string) fd.close() # register qemu emulator for interpreting other arch executable file if not os.path.exists(node): qemu_arm_string = ":arm:M::\\x7fELF\\x01\\x01\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\x28\\x00:\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\x00\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfa\\xff\\xff\\xff:%s:\n" % qemu_emulator fd = open("/proc/sys/fs/binfmt_misc/register", "w") fd.write(qemu_arm_string) fd.close() return qemu_emulator
def _mount_instroot(self, base_on=None): if base_on and os.path.isfile(base_on): self.__imgdir = os.path.dirname(base_on) imgname = os.path.basename(base_on) self._base_on(base_on) self._set_image_size(misc.get_file_size(self._image)) # here, self._instloops must be [] self._instloops.append({ "mountpoint": "/", "label": self.name, "name": imgname, "size": self.__image_size or 4096, "fstype": self.__fstype or "ext3", "loop": None }) self._check_imgdir() for loop in self._instloops: fstype = loop['fstype'] mp = os.path.join(self._instroot, loop['mountpoint'].lstrip('/')) size = loop['size'] * 1024 * 1024 imgname = loop['name'] fsopts = loop['fsopts'] dargs = [ fs.SparseLoopbackDisk(os.path.join(self._imgdir, imgname), size), mp, fstype, self._blocksize, loop['label'] ] dkwargs = {"fsopts": fsopts} if fstype in ("ext2", "ext3", "ext4"): MyDiskMount = fs.ExtDiskMount elif fstype == "btrfs": MyDiskMount = fs.BtrfsDiskMount dkwargs["subvolumes"] = loop["subvolumes"] dkwargs["snapshots"] = loop["snapshots"] elif fstype in ("vfat", "msdos"): MyDiskMount = fs.VfatDiskMount else: msger.error('Cannot support fstype: %s' % fstype) loop['loop'] = MyDiskMount(*dargs, **dkwargs) loop['uuid'] = loop['loop'].uuid try: msger.verbose('Mounting image "%s" on "%s"' % (imgname, mp)) fs.makedirs(mp) loop['loop'].mount() except MountError as e: raise
def get_cachedir(self, cachedir=None): if self.cachedir: return self.cachedir self.__ensure_builddir() if cachedir: self.cachedir = cachedir else: self.cachedir = self.__builddir + "/mic-cache" fs.makedirs(self.cachedir) return self.cachedir
def _mount_instroot(self, base_on=None): if base_on and os.path.isfile(base_on): self.__imgdir = os.path.dirname(base_on) imgname = os.path.basename(base_on) self._base_on(base_on) self._set_image_size(misc.get_file_size(self._image)) # here, self._instloops must be [] self._instloops.append({ "mountpoint": "/", "label": self.name, "name": imgname, "size": self.__image_size or 4096L, "fstype": self.__fstype or "ext3", "extopts": None, "loop": None }) self._check_imgdir() for loop in self._instloops: fstype = loop['fstype'] mp = os.path.join(self._instroot, loop['mountpoint'].lstrip('/')) size = loop['size'] * 1024L * 1024L imgname = loop['name'] if fstype in ("ext2", "ext3", "ext4"): MyDiskMount = fs.ExtDiskMount elif fstype == "btrfs": MyDiskMount = fs.BtrfsDiskMount elif fstype in ("vfat", "msdos"): MyDiskMount = fs.VfatDiskMount else: msger.error('Cannot support fstype: %s' % fstype) loop['loop'] = MyDiskMount(fs.SparseLoopbackDisk( os.path.join(self.__imgdir, imgname), size), mp, fstype, self._blocksize, loop['label']) if fstype in ("ext2", "ext3", "ext4"): loop['loop'].extopts = loop['extopts'] try: msger.verbose('Mounting image "%s" on "%s"' % (imgname, mp)) fs.makedirs(mp) loop['loop'].mount() except MountError, e: raise
def package(self, destdir = "."): """Prepares the created image for final delivery. In its simplest form, this method merely copies the install root to the supplied destination directory; other subclasses may choose to package the image by e.g. creating a bootable ISO containing the image and bootloader configuration. destdir -- the directory into which the final image should be moved; this defaults to the current directory. """ self._stage_final_image() if not os.path.exists(destdir): fs.makedirs(destdir) if self._img_compression_method: if not self._img_name: raise CreatorError("Image name not set.") rc = None img_location = os.path.join(self._outdir,self._img_name) if self._img_compression_method == "bz2": bzip2 = fs.find_binary_path('bzip2') msger.info("Compressing %s with bzip2. Please wait..." % img_location) rc = runner.show([bzip2, "-f", img_location]) if rc: raise CreatorError("Failed to compress image %s with %s." % (img_location, self._img_compression_method)) for bootimg in glob.glob(os.path.dirname(img_location) + "/*-boot.bin"): msger.info("Compressing %s with bzip2. Please wait..." % bootimg) rc = runner.show([bzip2, "-f", bootimg]) if rc: raise CreatorError("Failed to compress image %s with %s." % (bootimg, self._img_compression_method)) if self._recording_pkgs: self._save_recording_pkgs(destdir) """ For image formats with two or multiple image files, it will be better to put them under a directory """ if self.image_format in ("raw", "vmdk", "vdi", "nand", "mrstnand"): destdir = os.path.join(destdir, "%s-%s" % (self.name, self.image_format)) msger.debug("creating destination dir: %s" % destdir) fs.makedirs(destdir) # Ensure all data is flushed to _outdir runner.quiet('sync') for f in os.listdir(self._outdir): shutil.move(os.path.join(self._outdir, f), os.path.join(destdir, f)) self.outimage.append(os.path.join(destdir, f)) self.do_genchecksum(os.path.join(destdir, f))
def apply(self, kslang): self._check_sysconfig() if kslang.lang: f = open(self.path("/etc/sysconfig/i18n"), "w+") f.write("LANG=\"" + kslang.lang + "\"\n") f.close() f = open(self.path("/etc/locale.conf"), "w+") f.write("LANG=\"" + kslang.lang + "\"\n") f.close() #cp ks lang setting to other file, then can access the file in %post section fs.makedirs(self.path("/etc/config")) f = open(self.path("/etc/config/mic_language"), "w+") f.write("LANG=\"" + kslang.lang + "\"\n") f.close()
def apply(self, ksnet): fs.makedirs(self.path("/etc/sysconfig/network-scripts")) useipv6 = False nodns = False hostname = None gateway = None nameservers = None for network in ksnet.network: if not network.device: raise errors.KsError("No --device specified with " "network kickstart command") if (network.onboot and network.bootProto.lower() != "dhcp" and not (network.ip and network.netmask)): raise errors.KsError("No IP address and/or netmask " "specified with static " "configuration for '%s'" % network.device) self.write_ifcfg(network) self.write_wepkey(network) if network.ipv6: useipv6 = True if network.nodns: nodns = True if network.hostname: hostname = network.hostname if network.gateway: gateway = network.gateway if network.nameserver: nameservers = network.nameserver.split(",") self.write_sysconfig(useipv6, hostname, gateway) self.write_hosts(hostname) self.write_resolv(nodns, nameservers)
def mount(self, base_on = None, cachedir = None): """Setup the target filesystem in preparation for an install. This function sets up the filesystem which the ImageCreator will install into and configure. The ImageCreator class merely creates an install root directory, bind mounts some system directories (e.g. /dev) and writes out /etc/fstab. Other subclasses may also e.g. create a sparse file, format it and loopback mount it to the install root. base_on -- a previous install on which to base this install; defaults to None, causing a new image to be created cachedir -- a directory in which to store the Yum cache; defaults to None, causing a new cache to be created; by setting this to another directory, the same cache can be reused across multiple installs. """ self.__ensure_builddir() fs.makedirs(self._instroot) fs.makedirs(self._outdir) self._mount_instroot(base_on) for d in ("/dev/pts", "/etc", "/boot", "/var/log", "/sys", "/proc", "/usr/bin"): fs.makedirs(self._instroot + d) if self.target_arch and self.target_arch.startswith("arm"): self.qemu_emulator = misc.setup_qemu_emulator(self._instroot, self.target_arch) self.get_cachedir(cachedir) # bind mount system directories into _instroot for (f, dest) in [("/sys", None), ("/proc", None), ("/proc/sys/fs/binfmt_misc", None), ("/dev/pts", None)]: self.__bindmounts.append(fs.BindChrootMount(f, self._instroot, dest)) self._do_bindmounts() self.__create_minimal_dev() if os.path.exists(self._instroot + "/etc/mtab"): os.unlink(self._instroot + "/etc/mtab") os.symlink("../proc/mounts", self._instroot + "/etc/mtab") self.__write_fstab() # get size of available space in 'instroot' fs self._root_fs_avail = misc.get_filesystem_avail(self._instroot)
def _stage_final_image(self): try: isodir = self._get_isodir() fs_related.makedirs(isodir + "/LiveOS") minimal_size = self._resparse() if not self.skip_minimize: fs_related.create_image_minimizer(isodir + "/LiveOS/osmin.img", self._image, minimal_size) if self.skip_compression: shutil.move(self._image, isodir + "/LiveOS/ext3fs.img") else: fs_related.makedirs(os.path.join( os.path.dirname(self._image), "LiveOS")) shutil.move(self._image, os.path.join(os.path.dirname(self._image), "LiveOS", "ext3fs.img")) fs_related.mksquashfs(os.path.dirname(self._image), isodir + "/LiveOS/squashfs.img") self._create_usbimg(isodir) if self.pack_to: self.image_files.update({'image_files': self.pack_to}) usbimg = os.path.join(self._outdir, self.name + ".usbimg") packimg = os.path.join(self._outdir, self.pack_to) packing(packimg, usbimg) os.unlink(usbimg) else: self.image_files.update({'image_files': self.name + ".usbimg"}) finally: shutil.rmtree(isodir, ignore_errors = True) self._set_isodir(None)
def _stage_final_image(self): try: isodir = self._get_isodir() fs_related.makedirs(isodir + "/LiveOS") minimal_size = self._resparse() if not self.skip_minimize: fs_related.create_image_minimizer(isodir + "/LiveOS/osmin.img", self._image, minimal_size) if self.skip_compression: shutil.move(self._image, isodir + "/LiveOS/ext3fs.img") else: fs_related.makedirs(os.path.join(os.path.dirname(self._image), "LiveOS")) shutil.move(self._image, os.path.join(os.path.dirname(self._image), "LiveOS", "ext3fs.img")) fs_related.mksquashfs(os.path.dirname(self._image), isodir + "/LiveOS/squashfs.img") self._create_usbimg(isodir) finally: shutil.rmtree(isodir, ignore_errors=True) self._set_isodir(None)
def mount(self, base_on=None, cachedir=None): """Setup the target filesystem in preparation for an install. This function sets up the filesystem which the ImageCreator will install into and configure. The ImageCreator class merely creates an install root directory, bind mounts some system directories (e.g. /dev) and writes out /etc/fstab. Other subclasses may also e.g. create a sparse file, format it and loopback mount it to the install root. base_on -- a previous install on which to base this install; defaults to None, causing a new image to be created cachedir -- a directory in which to store the Yum cache; defaults to None, causing a new cache to be created; by setting this to another directory, the same cache can be reused across multiple installs. """ self.__setup_tmpdir() self.__ensure_builddir() # prevent popup dialog in Ubuntu(s) misc.hide_loopdev_presentation() fs.makedirs(self._instroot) fs.makedirs(self._outdir) self._mount_instroot(base_on) for d in ("/dev/pts", "/etc", "/boot", "/var/log", "/sys", "/proc", "/usr/bin"): fs.makedirs(self._instroot + d) if self.target_arch and self.target_arch.startswith("arm"): self.qemu_emulator = misc.setup_qemu_emulator( self._instroot, self.target_arch) self.get_cachedir(cachedir) # bind mount system directories into _instroot for (f, dest) in [("/sys", None), ("/proc", None), ("/proc/sys/fs/binfmt_misc", None), ("/dev/pts", None)]: self.__bindmounts.append( fs.BindChrootMount(f, self._instroot, dest)) self._do_bindmounts() self.__create_minimal_dev() if os.path.exists(self._instroot + "/etc/mtab"): os.unlink(self._instroot + "/etc/mtab") os.symlink("../proc/mounts", self._instroot + "/etc/mtab") self.__write_fstab() # get size of available space in 'instroot' fs self._root_fs_avail = misc.get_filesystem_avail(self._instroot)
def _stage_final_image(self): try: fs_related.makedirs(self.__ensure_isodir() + "/LiveOS") minimal_size = self._resparse() if not self.skip_minimize: fs_related.create_image_minimizer(self.__isodir + \ "/LiveOS/osmin.img", self._image, minimal_size) if self.skip_compression: shutil.move(self._image, self.__isodir + "/LiveOS/ext3fs.img") else: fs_related.makedirs( os.path.join(os.path.dirname(self._image), "LiveOS")) shutil.move( self._image, os.path.join(os.path.dirname(self._image), "LiveOS", "ext3fs.img")) fs_related.mksquashfs(os.path.dirname(self._image), self.__isodir + "/LiveOS/squashfs.img") self.__create_iso(self.__isodir) if self.pack_to: isoimg = os.path.join(self._outdir, self.name + ".iso") packimg = os.path.join(self._outdir, self.pack_to) packing(packimg, isoimg) os.unlink(isoimg) self.image_files.update({'image_files': [self.pack_to]}) else: self.image_files.update({'image_files': [self.name + ".iso"]}) finally: shutil.rmtree(self.__isodir, ignore_errors=True) self.__isodir = None
def safecopy(src, dst, symlinks=False, ignore_ptns=()): if os.path.isdir(src): if os.path.isdir(dst): dst = os.path.join(dst, os.path.basename(src)) if os.path.exists(dst): shutil.rmtree(dst, ignore_errors=True) src = src.rstrip('/') # check common prefix to ignore copying itself if dst.startswith(src + '/'): ignore_ptns = list(ignore_ptns) + [os.path.basename(src)] ignores = shutil.ignore_patterns(*ignore_ptns) try: shutil.copytree(src, dst, symlinks, ignores) except (OSError, IOError): shutil.rmtree(dst, ignore_errors=True) raise else: if not os.path.isdir(dst): makedirs(os.path.dirname(dst)) shutil.copy2(src, dst)
def safecopy(src, dst, symlinks=False, ignore_ptns=()): if os.path.isdir(src): if os.path.isdir(dst): dst = os.path.join(dst, os.path.basename(src)) if os.path.exists(dst): shutil.rmtree(dst, ignore_errors=True) src = src.rstrip('/') # check common prefix to ignore copying itself if dst.startswith(src + '/'): ignore_ptns = list(ignore_ptns) + [ os.path.basename(src) ] ignores = shutil.ignore_patterns(*ignore_ptns) try: shutil.copytree(src, dst, symlinks, ignores) except (OSError, IOError): shutil.rmtree(dst, ignore_errors=True) raise else: if not os.path.isdir(dst): makedirs(os.path.dirname(dst)) shutil.copy2(src, dst)
def package(self, destdir="."): """Prepares the created image for final delivery. In its simplest form, this method merely copies the install root to the supplied destination directory; other subclasses may choose to package the image by e.g. creating a bootable ISO containing the image and bootloader configuration. destdir -- the directory into which the final image should be moved; this defaults to the current directory. """ self._stage_final_image() if not os.path.exists(destdir): fs.makedirs(destdir) if self._recording_pkgs: self._save_recording_pkgs(destdir) # For image formats with two or multiple image files, it will be # better to put them under a directory if self.image_format in ("raw", "vmdk", "vdi", "nand", "mrstnand"): destdir = os.path.join(destdir, "%s-%s" \ % (self.name, self.image_format)) msger.debug("creating destination dir: %s" % destdir) fs.makedirs(destdir) # Ensure all data is flushed to _outdir runner.quiet('sync') misc.check_space_pre_cp(self._outdir, destdir) for f in os.listdir(self._outdir): shutil.move(os.path.join(self._outdir, f), os.path.join(destdir, f)) self.outimage.append(os.path.join(destdir, f)) self.do_genchecksum(os.path.join(destdir, f))
def __write_dracut_conf(self, path): if not os.path.exists(os.path.dirname(path)): fs_related.makedirs(os.path.dirname(path)) f = open(path, "a") f.write('add_dracutmodules+=" dmsquash-live pollcdrom "') f.close()
def _create_usbimg(self, isodir): overlaysizemb = 64 #default #skipcompress = self.skip_compression? fstype = "vfat" homesizemb=0 swapsizemb=0 homefile="home.img" plussize=128 kernelargs=None if fstype == 'vfat': if overlaysizemb > 2047: raise CreatorError("Can't have an overlay of 2048MB or " "greater on VFAT") if homesizemb > 2047: raise CreatorError("Can't have an home overlay of 2048MB or " "greater on VFAT") if swapsizemb > 2047: raise CreatorError("Can't have an swap overlay of 2048MB or " "greater on VFAT") livesize = misc.get_file_size(isodir + "/LiveOS") usbimgsize = (overlaysizemb + \ homesizemb + \ swapsizemb + \ livesize + \ plussize) * 1024L * 1024L disk = fs_related.SparseLoopbackDisk("%s/%s.usbimg" \ % (self._outdir, self.name), usbimgsize) usbmnt = self._mkdtemp("usb-mnt") usbloop = PartitionedMount(usbmnt) usbloop.add_disk('/dev/sdb', disk) usbloop.add_partition(usbimgsize/1024/1024, "/dev/sdb", "/", fstype, boot=True) usbloop.mount() try: fs_related.makedirs(usbmnt + "/LiveOS") if os.path.exists(isodir + "/LiveOS/squashfs.img"): shutil.copyfile(isodir + "/LiveOS/squashfs.img", usbmnt + "/LiveOS/squashfs.img") else: fs_related.mksquashfs(os.path.dirname(self._image), usbmnt + "/LiveOS/squashfs.img") if os.path.exists(isodir + "/LiveOS/osmin.img"): shutil.copyfile(isodir + "/LiveOS/osmin.img", usbmnt + "/LiveOS/osmin.img") if fstype == "vfat" or fstype == "msdos": uuid = usbloop.partitions[0]['mount'].uuid label = usbloop.partitions[0]['mount'].fslabel usblabel = "UUID=%s-%s" % (uuid[0:4], uuid[4:8]) overlaysuffix = "-%s-%s-%s" % (label, uuid[0:4], uuid[4:8]) else: diskmount = usbloop.partitions[0]['mount'] usblabel = "UUID=%s" % diskmount.uuid overlaysuffix = "-%s-%s" % (diskmount.fslabel, diskmount.uuid) args = ['cp', "-Rf", isodir + "/isolinux", usbmnt + "/syslinux"] rc = runner.show(args) if rc: raise CreatorError("Can't copy isolinux directory %s" \ % (isodir + "/isolinux/*")) if os.path.isfile("/usr/share/syslinux/isolinux.bin"): syslinux_path = "/usr/share/syslinux" elif os.path.isfile("/usr/lib/syslinux/isolinux.bin"): syslinux_path = "/usr/lib/syslinux" else: raise CreatorError("syslinux not installed : " "cannot find syslinux installation path") for f in ("isolinux.bin", "vesamenu.c32"): path = os.path.join(syslinux_path, f) if os.path.isfile(path): args = ['cp', path, usbmnt + "/syslinux/"] rc = runner.show(args) if rc: raise CreatorError("Can't copy syslinux file " + path) else: raise CreatorError("syslinux not installed: " "syslinux file %s not found" % path) fd = open(isodir + "/isolinux/isolinux.cfg", "r") text = fd.read() fd.close() pattern = re.compile('CDLABEL=[^ ]*') text = pattern.sub(usblabel, text) pattern = re.compile('rootfstype=[^ ]*') text = pattern.sub("rootfstype=" + fstype, text) if kernelargs: text = text.replace("rd.live.image", "rd.live.image " + kernelargs) if overlaysizemb > 0: msger.info("Initializing persistent overlay file") overfile = "overlay" + overlaysuffix if fstype == "vfat": args = ['dd', "if=/dev/zero", "of=" + usbmnt + "/LiveOS/" + overfile, "count=%d" % overlaysizemb, "bs=1M"] else: args = ['dd', "if=/dev/null", "of=" + usbmnt + "/LiveOS/" + overfile, "count=1", "bs=1M", "seek=%d" % overlaysizemb] rc = runner.show(args) if rc: raise CreatorError("Can't create overlay file") text = text.replace("rd.live.image", "rd.live.image rd.live.overlay=" + usblabel) text = text.replace(" ro ", " rw ") if swapsizemb > 0: msger.info("Initializing swap file") swapfile = usbmnt + "/LiveOS/" + "swap.img" args = ['dd', "if=/dev/zero", "of=" + swapfile, "count=%d" % swapsizemb, "bs=1M"] rc = runner.show(args) if rc: raise CreatorError("Can't create swap file") args = ["mkswap", "-f", swapfile] rc = runner.show(args) if rc: raise CreatorError("Can't mkswap on swap file") if homesizemb > 0: msger.info("Initializing persistent /home") homefile = usbmnt + "/LiveOS/" + homefile if fstype == "vfat": args = ['dd', "if=/dev/zero", "of=" + homefile, "count=%d" % homesizemb, "bs=1M"] else: args = ['dd', "if=/dev/null", "of=" + homefile, "count=1", "bs=1M", "seek=%d" % homesizemb] rc = runner.show(args) if rc: raise CreatorError("Can't create home file") mkfscmd = fs_related.find_binary_path("/sbin/mkfs." + fstype) if fstype == "ext2" or fstype == "ext3": args = [mkfscmd, "-F", "-j", homefile] else: args = [mkfscmd, homefile] rc = runner.show(args) if rc: raise CreatorError("Can't mke2fs home file") if fstype == "ext2" or fstype == "ext3": tune2fs = fs_related.find_binary_path("tune2fs") args = [tune2fs, "-c0", "-i0", "-ouser_xattr,acl", homefile] rc = runner.show(args) if rc: raise CreatorError("Can't tune2fs home file") if fstype == "vfat" or fstype == "msdos": syslinuxcmd = fs_related.find_binary_path("syslinux") syslinuxcfg = usbmnt + "/syslinux/syslinux.cfg" args = [syslinuxcmd, "-d", "syslinux", usbloop.partitions[0]["device"]] elif fstype == "ext2" or fstype == "ext3": extlinuxcmd = fs_related.find_binary_path("extlinux") syslinuxcfg = usbmnt + "/syslinux/extlinux.conf" args = [extlinuxcmd, "-i", usbmnt + "/syslinux"] else: raise CreatorError("Invalid file system type: %s" % (fstype)) os.unlink(usbmnt + "/syslinux/isolinux.cfg") fd = open(syslinuxcfg, "w") fd.write(text) fd.close() rc = runner.show(args) if rc: raise CreatorError("Can't install boot loader.") finally: usbloop.unmount() usbloop.cleanup() # Need to do this after image is unmounted and device mapper is closed msger.info("set MBR") mbrfile = "/usr/lib/syslinux/mbr.bin" if not os.path.exists(mbrfile): mbrfile = "/usr/share/syslinux/mbr.bin" if not os.path.exists(mbrfile): raise CreatorError("mbr.bin file didn't exist.") mbrsize = os.path.getsize(mbrfile) outimg = "%s/%s.usbimg" % (self._outdir, self.name) args = ['dd', "if=" + mbrfile, "of=" + outimg, "seek=0", "conv=notrunc", "bs=1", "count=%d" % (mbrsize)] rc = runner.show(args) if rc: raise CreatorError("Can't set MBR.")
def _create_usbimg(self, isodir): overlaysizemb = 64 #default #skipcompress = self.skip_compression? fstype = "vfat" homesizemb=0 swapsizemb=0 homefile="home.img" plussize=128 kernelargs=None if fstype == 'vfat': if overlaysizemb > 2047: raise CreatorError("Can't have an overlay of 2048MB or " "greater on VFAT") if homesizemb > 2047: raise CreatorError("Can't have an home overlay of 2048MB or " "greater on VFAT") if swapsizemb > 2047: raise CreatorError("Can't have an swap overlay of 2048MB or " "greater on VFAT") livesize = misc.get_file_size(isodir + "/LiveOS") usbimgsize = (overlaysizemb + \ homesizemb + \ swapsizemb + \ livesize + \ plussize) * 1024 * 1024 disk = fs_related.SparseLoopbackDisk("%s/%s.usbimg" \ % (self._outdir, self.name), usbimgsize) usbmnt = self._mkdtemp("usb-mnt") usbloop = PartitionedMount({'/dev/sdb':disk}, usbmnt) usbloop.add_partition(usbimgsize/1024/1024, "/dev/sdb", "/", fstype, boot=True) usbloop.mount() try: fs_related.makedirs(usbmnt + "/LiveOS") if os.path.exists(isodir + "/LiveOS/squashfs.img"): shutil.copyfile(isodir + "/LiveOS/squashfs.img", usbmnt + "/LiveOS/squashfs.img") else: fs_related.mksquashfs(os.path.dirname(self._image), usbmnt + "/LiveOS/squashfs.img") if os.path.exists(isodir + "/LiveOS/osmin.img"): shutil.copyfile(isodir + "/LiveOS/osmin.img", usbmnt + "/LiveOS/osmin.img") uuid = usbloop.partitions[0]['mount'].uuid label = usbloop.partitions[0]['mount'].fslabel usblabel = "UUID=%s" % (uuid) overlaysuffix = "-%s-%s" % (label, uuid) args = ['cp', "-Rf", isodir + "/isolinux", usbmnt + "/syslinux"] rc = runner.show(args) if rc: raise CreatorError("Can't copy isolinux directory %s" \ % (isodir + "/isolinux/*")) if os.path.isfile("/usr/share/syslinux/isolinux.bin"): syslinux_path = "/usr/share/syslinux" elif os.path.isfile("/usr/lib/syslinux/isolinux.bin"): syslinux_path = "/usr/lib/syslinux" else: raise CreatorError("syslinux not installed : " "cannot find syslinux installation path") for f in ("isolinux.bin", "vesamenu.c32"): path = os.path.join(syslinux_path, f) if os.path.isfile(path): args = ['cp', path, usbmnt + "/syslinux/"] rc = runner.show(args) if rc: raise CreatorError("Can't copy syslinux file " + path) else: raise CreatorError("syslinux not installed: " "syslinux file %s not found" % path) fd = open(isodir + "/isolinux/isolinux.cfg", "r") text = fd.read() fd.close() pattern = re.compile('CDLABEL=[^ ]*') text = pattern.sub(usblabel, text) pattern = re.compile('rootfstype=[^ ]*') text = pattern.sub("rootfstype=" + fstype, text) if kernelargs: text = text.replace("liveimg", "liveimg " + kernelargs) if overlaysizemb > 0: msger.info("Initializing persistent overlay file") overfile = "overlay" + overlaysuffix if fstype == "vfat": args = ['dd', "if=/dev/zero", "of=" + usbmnt + "/LiveOS/" + overfile, "count=%d" % overlaysizemb, "bs=1M"] else: args = ['dd', "if=/dev/null", "of=" + usbmnt + "/LiveOS/" + overfile, "count=1", "bs=1M", "seek=%d" % overlaysizemb] rc = runner.show(args) if rc: raise CreatorError("Can't create overlay file") text = text.replace("liveimg", "liveimg overlay=" + usblabel) text = text.replace(" ro ", " rw ") if swapsizemb > 0: msger.info("Initializing swap file") swapfile = usbmnt + "/LiveOS/" + "swap.img" args = ['dd', "if=/dev/zero", "of=" + swapfile, "count=%d" % swapsizemb, "bs=1M"] rc = runner.show(args) if rc: raise CreatorError("Can't create swap file") args = ["mkswap", "-f", swapfile] rc = runner.show(args) if rc: raise CreatorError("Can't mkswap on swap file") if homesizemb > 0: msger.info("Initializing persistent /home") homefile = usbmnt + "/LiveOS/" + homefile if fstype == "vfat": args = ['dd', "if=/dev/zero", "of=" + homefile, "count=%d" % homesizemb, "bs=1M"] else: args = ['dd', "if=/dev/null", "of=" + homefile, "count=1", "bs=1M", "seek=%d" % homesizemb] rc = runner.show(args) if rc: raise CreatorError("Can't create home file") mkfscmd = fs_related.find_binary_path("/sbin/mkfs." + fstype) if fstype == "ext2" or fstype == "ext3": args = [mkfscmd, "-F", "-j", homefile] else: args = [mkfscmd, homefile] rc = runner.show(args) if rc: raise CreatorError("Can't mke2fs home file") if fstype == "ext2" or fstype == "ext3": tune2fs = fs_related.find_binary_path("tune2fs") args = [tune2fs, "-c0", "-i0", "-ouser_xattr,acl", homefile] rc = runner.show(args) if rc: raise CreatorError("Can't tune2fs home file") if fstype == "vfat" or fstype == "msdos": syslinuxcmd = fs_related.find_binary_path("syslinux") syslinuxcfg = usbmnt + "/syslinux/syslinux.cfg" args = [syslinuxcmd, "-d", "syslinux", usbloop.partitions[0]["device"]] elif fstype == "ext2" or fstype == "ext3": extlinuxcmd = fs_related.find_binary_path("extlinux") syslinuxcfg = usbmnt + "/syslinux/extlinux.conf" args = [extlinuxcmd, "-i", usbmnt + "/syslinux"] else: raise CreatorError("Invalid file system type: %s" % (fstype)) os.unlink(usbmnt + "/syslinux/isolinux.cfg") fd = open(syslinuxcfg, "w") fd.write(text) fd.close() rc = runner.show(args) if rc: raise CreatorError("Can't install boot loader.") finally: usbloop.unmount() usbloop.cleanup() # Need to do this after image is unmounted and device mapper is closed msger.info("set MBR") mbrfile = "/usr/lib/syslinux/mbr.bin" if not os.path.exists(mbrfile): mbrfile = "/usr/share/syslinux/mbr.bin" if not os.path.exists(mbrfile): raise CreatorError("mbr.bin file didn't exist.") mbrsize = os.path.getsize(mbrfile) outimg = "%s/%s.usbimg" % (self._outdir, self.name) args = ['dd', "if=" + mbrfile, "of=" + outimg, "seek=0", "conv=notrunc", "bs=1", "count=%d" % (mbrsize)] rc = runner.show(args) if rc: raise CreatorError("Can't set MBR.")
def _check_sysconfig(self): if not os.path.exists(self.path("/etc/sysconfig")): fs.makedirs(self.path("/etc/sysconfig"))
def get_metadata_from_repos(repos, cachedir): my_repo_metadata = [] for repo in repos: reponame = repo['name'] baseurl = repo['baseurl'] if 'proxy' in repo: proxy = repo['proxy'] else: proxy = get_proxy_for(baseurl) proxies = None if proxy: proxies = {str(baseurl.split(":")[0]):str(proxy)} makedirs(os.path.join(cachedir, reponame)) url = os.path.join(baseurl, "repodata/repomd.xml") filename = os.path.join(cachedir, reponame, 'repomd.xml') repomd = myurlgrab(url, filename, proxies) try: root = xmlparse(repomd) except SyntaxError: raise CreatorError("repomd.xml syntax error.") ns = root.getroot().tag ns = ns[0:ns.rindex("}")+1] filepaths = {} checksums = {} sumtypes = {} for elm in root.getiterator("%sdata" % ns): if elm.attrib["type"] == "patterns": filepaths['patterns'] = elm.find("%slocation" % ns).attrib['href'] checksums['patterns'] = elm.find("%sopen-checksum" % ns).text sumtypes['patterns'] = elm.find("%sopen-checksum" % ns).attrib['type'] break for elm in root.getiterator("%sdata" % ns): if elm.attrib["type"] in ("group_gz", "group"): filepaths['comps'] = elm.find("%slocation" % ns).attrib['href'] checksums['comps'] = elm.find("%sopen-checksum" % ns).text sumtypes['comps'] = elm.find("%sopen-checksum" % ns).attrib['type'] break primary_type = None for elm in root.getiterator("%sdata" % ns): if elm.attrib["type"] in ("primary_db", "primary"): primary_type = elm.attrib["type"] filepaths['primary'] = elm.find("%slocation" % ns).attrib['href'] checksums['primary'] = elm.find("%sopen-checksum" % ns).text sumtypes['primary'] = elm.find("%sopen-checksum" % ns).attrib['type'] break if not primary_type: continue for item in ("primary", "patterns", "comps"): if item not in filepaths: filepaths[item] = None continue if not filepaths[item]: continue filepaths[item] = _get_metadata_from_repo(baseurl, proxies, cachedir, reponame, filepaths[item], sumtypes[item], checksums[item]) """ Get repo key """ try: repokey = _get_metadata_from_repo(baseurl, proxies, cachedir, reponame, "repodata/repomd.xml.key") except CreatorError: repokey = None msger.debug("\ncan't get %s/%s" % (baseurl, "repodata/repomd.xml.key")) my_repo_metadata.append({"name":reponame, "baseurl":baseurl, "repomd":repomd, "primary":filepaths['primary'], "cachedir":cachedir, "proxies":proxies, "patterns":filepaths['patterns'], "comps":filepaths['comps'], "repokey":repokey}) return my_repo_metadata
with open(_path(binpth), 'w') as wf: wf.write(mic_cont) def safecopy(src, dst, symlinks=False, ignore_ptns=[]): if os.path.isdir(src): if os.path.isdir(dst): dst = os.path.join(dst, os.path.basename(src)) if os.path.exists(dst): shutil.rmtree(dst, ignore_errors=True) src = src.rstrip('/') # check common prefix to ignore copying itself if dst.startswith(src + '/'): ignore_ptns += os.path.basename(src) try: ignores = shutil.ignore_patterns(*ignore_ptns) shutil.copytree(src, dst, symlinks, ignores) except OSError, IOError: shutil.rmtree(dst, ignore_errors=True) raise else: try: if not os.path.isdir(dst): makedirs(os.path.dirname(dst)) shutil.copy2(src, dst) except: raise
def chroot(chrootdir, bindmounts=None, execute="/bin/bash"): def mychroot(): os.chroot(chrootdir) os.chdir("/") if configmgr.chroot['saveto']: savefs = True saveto = configmgr.chroot['saveto'] wrnmsg = "Can't save chroot fs for dir %s exists" % saveto if saveto == chrootdir: savefs = False wrnmsg = "Dir %s is being used to chroot" % saveto elif os.path.exists(saveto): if msger.ask("Dir %s already exists, cleanup and continue?" % saveto): shutil.rmtree(saveto, ignore_errors=True) savefs = True else: savefs = False if savefs: msger.info("Saving image to directory %s" % saveto) fs_related.makedirs(os.path.dirname(os.path.abspath(saveto))) runner.quiet("cp -af %s %s" % (chrootdir, saveto)) devs = ['dev/fd', 'dev/stdin', 'dev/stdout', 'dev/stderr'] ignlst = [os.path.join(saveto, x) for x in devs] list(map(os.unlink, list(filter(os.path.exists, ignlst)))) else: msger.warning(wrnmsg) dev_null = os.open("/dev/null", os.O_WRONLY) files_to_check = ["/bin/bash", "/sbin/init"] architecture_found = False """ Register statically-linked qemu-arm if it is an ARM fs """ qemu_emulator = None for ftc in files_to_check: ftc = "%s/%s" % (chrootdir, ftc) # Return code of 'file' is "almost always" 0 based on some man pages # so we need to check the file existance first. if not os.path.exists(ftc): continue for line in runner.outs(['file', ftc]).splitlines(): if 'ARM' in line: qemu_emulator = misc.setup_qemu_emulator(chrootdir, "arm") architecture_found = True break if 'MIPS' in line: qemu_emulator = misc.setup_qemu_emulator(chrootdir, "mipsel") architecture_found = True break if 'Intel' in line: architecture_found = True break if architecture_found: break os.close(dev_null) if not architecture_found: raise errors.CreatorError("Failed to get architecture from any of the " "following files %s from chroot." \ % files_to_check) try: msger.info("Launching shell. Exit to continue.\n" "----------------------------------") globalmounts = setup_chrootenv(chrootdir, bindmounts) subprocess.call(execute, preexec_fn=mychroot, shell=True) except OSError as err: raise errors.CreatorError("chroot err: %s" % str(err)) finally: cleanup_chrootenv(chrootdir, bindmounts, globalmounts) if qemu_emulator: os.unlink(chrootdir + qemu_emulator)
def setup_qemu_emulator(rootdir, arch): # mount binfmt_misc if it doesn't exist if not os.path.exists("/proc/sys/fs/binfmt_misc"): modprobecmd = find_binary_path("modprobe") runner.show([modprobecmd, "binfmt_misc"]) if not os.path.exists("/proc/sys/fs/binfmt_misc/register"): mountcmd = find_binary_path("mount") runner.show([ mountcmd, "-t", "binfmt_misc", "none", "/proc/sys/fs/binfmt_misc" ]) # qemu_emulator is a special case, we can't use find_binary_path # qemu emulator should be a statically-linked executable file if arch == "aarch64": node = "/proc/sys/fs/binfmt_misc/aarch64" if os.path.exists("/usr/bin/qemu-arm64") and is_statically_linked( "/usr/bin/qemu-arm64"): arm_binary = "qemu-arm64" elif os.path.exists("/usr/bin/qemu-aarch64") and is_statically_linked( "/usr/bin/qemu-aarch64"): arm_binary = "qemu-aarch64" elif os.path.exists("/usr/bin/qemu-arm64-static"): arm_binary = "qemu-arm64-static" elif os.path.exists("/usr/bin/qemu-aarch64-static"): arm_binary = "qemu-aarch64-static" else: raise CreatorError("Please install a statically-linked %s" % arm_binary) elif arch == "mipsel": node = "/proc/sys/fs/binfmt_misc/mipsel" arm_binary = "qemu-mipsel" if not os.path.exists( "/usr/bin/%s" % arm_binary) or not is_statically_linked("/usr/bin/%s"): arm_binary = "qemu-mipsel-static" if not os.path.exists("/usr/bin/%s" % arm_binary): raise CreatorError("Please install a statically-linked %s" % arm_binary) else: node = "/proc/sys/fs/binfmt_misc/arm" arm_binary = "qemu-arm" if not os.path.exists("/usr/bin/qemu-arm") or not is_statically_linked( "/usr/bin/qemu-arm"): arm_binary = "qemu-arm-static" if not os.path.exists("/usr/bin/%s" % arm_binary): raise CreatorError("Please install a statically-linked %s" % arm_binary) qemu_emulator = "/usr/bin/%s" % arm_binary if not os.path.exists(rootdir + "/usr/bin"): makedirs(rootdir + "/usr/bin") shutil.copy(qemu_emulator, rootdir + qemu_emulator) # disable selinux, selinux will block qemu emulator to run if os.path.exists("/usr/sbin/setenforce"): msger.info('Try to disable selinux') runner.show(["/usr/sbin/setenforce", "0"]) # unregister it if it has been registered and is a dynamically-linked executable if os.path.exists(node): qemu_unregister_string = "-1\n" with open(node, "w") as fd: fd.write(qemu_unregister_string) # register qemu emulator for interpreting other arch executable file if not os.path.exists(node): if arch == "aarch64": qemu_arm_string = ":aarch64:M::\\x7fELF\\x02\\x01\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\xb7:\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\x00\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\xff:%s:\n" % qemu_emulator elif arch == "mipsel": qemu_arm_string = ":mipsel:M::\\x7fELF\\x01\\x01\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\x08\\x00:\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\x00\\xfe\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfe\\xff\\xff\\xff:%s:\n" % qemu_emulator else: qemu_arm_string = ":arm:M::\\x7fELF\\x01\\x01\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\x28\\x00:\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\x00\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xfa\\xff\\xff\\xff:%s:\n" % qemu_emulator with open("/proc/sys/fs/binfmt_misc/register", "w") as fd: fd.write(qemu_arm_string) return qemu_emulator
def get_package(pkg, repometadata, arch = None): ver = "" target_repo = None if not arch: arches = [] elif arch not in rpmmisc.archPolicies: arches = [arch] else: arches = rpmmisc.archPolicies[arch].split(':') arches.append('noarch') for repo in repometadata: if repo["primary"].endswith(".xml"): root = xmlparse(repo["primary"]) ns = root.getroot().tag ns = ns[0:ns.rindex("}")+1] for elm in root.getiterator("%spackage" % ns): if elm.find("%sname" % ns).text == pkg: if elm.find("%sarch" % ns).text in arches: version = elm.find("%sversion" % ns) tmpver = "%s-%s" % (version.attrib['ver'], version.attrib['rel']) if tmpver > ver: ver = tmpver location = elm.find("%slocation" % ns) pkgpath = "%s" % location.attrib['href'] target_repo = repo break if repo["primary"].endswith(".sqlite"): con = sqlite.connect(repo["primary"]) if arch: sql = 'select version, release, location_href from packages ' \ 'where name = "%s" and arch IN ("%s")' % \ (pkg, '","'.join(arches)) for row in con.execute(sql): tmpver = "%s-%s" % (row[0], row[1]) if tmpver > ver: ver = tmpver pkgpath = "%s" % row[2] target_repo = repo break else: sql = 'select version, release, location_href from packages ' \ 'where name = "%s"' % pkg for row in con.execute(sql): tmpver = "%s-%s" % (row[0], row[1]) if tmpver > ver: ver = tmpver pkgpath = "%s" % row[2] target_repo = repo break con.close() if target_repo: makedirs("%s/packages/%s" % (target_repo["cachedir"], target_repo["name"])) url = os.path.join(target_repo["baseurl"], pkgpath) filename = str("%s/packages/%s/%s" % (target_repo["cachedir"], target_repo["name"], os.path.basename(pkgpath))) if os.path.exists(filename): ret = rpmmisc.checkRpmIntegrity('rpm', filename) if ret == 0: return filename msger.warning("package %s is damaged: %s" % (os.path.basename(filename), filename)) os.unlink(filename) pkg = myurlgrab(str(url), filename, target_repo["proxies"]) return pkg else: return None
def mkdtemp(dir = "/var/tmp", prefix = "wic-tmp-"): """ FIXME: use the dir in wic.conf instead """ makedirs(dir) return tempfile.mkdtemp(dir = dir, prefix = prefix)
def chroot(chrootdir, bindmounts = None, execute = "/bin/bash"): def mychroot(): os.chroot(chrootdir) os.chdir("/") if configmgr.chroot['saveto']: savefs = True saveto = configmgr.chroot['saveto'] wrnmsg = "Can't save chroot fs for dir %s exists" % saveto if saveto == chrootdir: savefs = False wrnmsg = "Dir %s is being used to chroot" % saveto elif os.path.exists(saveto): if msger.ask("Dir %s already exists, cleanup and continue?" % saveto): shutil.rmtree(saveto, ignore_errors = True) savefs = True else: savefs = False if savefs: msger.info("Saving image to directory %s" % saveto) fs_related.makedirs(os.path.dirname(os.path.abspath(saveto))) runner.quiet("cp -af %s %s" % (chrootdir, saveto)) devs = ['dev/fd', 'dev/stdin', 'dev/stdout', 'dev/stderr', 'etc/mtab'] ignlst = [os.path.join(saveto, x) for x in devs] map(os.unlink, filter(os.path.exists, ignlst)) else: msger.warning(wrnmsg) dev_null = os.open("/dev/null", os.O_WRONLY) files_to_check = ["/bin/bash", "/sbin/init"] architecture_found = False """ Register statically-linked qemu-arm if it is an ARM fs """ qemu_emulator = None for ftc in files_to_check: ftc = "%s/%s" % (chrootdir,ftc) # Return code of 'file' is "almost always" 0 based on some man pages # so we need to check the file existance first. if not os.path.exists(ftc): continue for line in runner.outs(['file', ftc]).splitlines(): if 'ARM' in line: qemu_emulator = misc.setup_qemu_emulator(chrootdir, "arm") architecture_found = True break if 'Intel' in line: architecture_found = True break if architecture_found: break os.close(dev_null) if not architecture_found: raise errors.CreatorError("Failed to get architecture from any of the " "following files %s from chroot." \ % files_to_check) try: msger.info("Launching shell. Exit to continue.\n" "----------------------------------") globalmounts = setup_chrootenv(chrootdir, bindmounts) subprocess.call(execute, preexec_fn = mychroot, shell=True) except OSError, err: raise errors.CreatorError("chroot err: %s" % str(err))