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 + 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"]) node = "/proc/sys/fs/binfmt_misc/arm" if is_statically_linked(qemu_emulator) and os.path.exists(node): return qemu_emulator # unregister it if it has been registered and is a dynamically-linked executable if not is_statically_linked(qemu_emulator) and 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 get_image_type(path): def _get_extension_name(path): match = re.search("(?<=\.)\w+$", path) if match: return match.group(0) else: return None if os.path.isdir(path): _check_meego_chroot(path) return "fs" maptab = { "tar": "loop", "raw":"raw", "vmdk":"vmdk", "vdi":"vdi", "iso":"livecd", "usbimg":"liveusb", } extension = _get_extension_name(path) if extension in maptab: return maptab[extension] fd = open(path, "rb") file_header = fd.read(1024) fd.close() vdi_flag = "<<< Sun VirtualBox Disk Image >>>" if file_header[0:len(vdi_flag)] == vdi_flag: return maptab["vdi"] output = runner.outs(['file', path]) isoptn = re.compile(r".*ISO 9660 CD-ROM filesystem.*(bootable).*") usbimgptn = re.compile(r".*x86 boot sector.*active.*") rawptn = re.compile(r".*x86 boot sector.*") vmdkptn = re.compile(r".*VMware. disk image.*") ext3fsimgptn = re.compile(r".*Linux.*ext3 filesystem data.*") ext4fsimgptn = re.compile(r".*Linux.*ext4 filesystem data.*") btrfsimgptn = re.compile(r".*BTRFS.*") if isoptn.match(output): return maptab["iso"] elif usbimgptn.match(output): return maptab["usbimg"] elif rawptn.match(output): return maptab["raw"] elif vmdkptn.match(output): return maptab["vmdk"] elif ext3fsimgptn.match(output): return "ext3fsimg" elif ext4fsimgptn.match(output): return "ext4fsimg" elif btrfsimgptn.match(output): return "btrfsimg" else: raise CreatorError("Cannot detect the type of image: %s" % path)
def get_image_type(path): def _get_extension_name(path): match = re.search("(?<=\.)\w+$", path) if match: return match.group(0) else: return None if os.path.isdir(path): _check_meego_chroot(path) return "fs" maptab = { "tar": "loop", "raw": "raw", "vmdk": "vmdk", "vdi": "vdi", "iso": "livecd", "usbimg": "liveusb", } extension = _get_extension_name(path) if extension in maptab: return maptab[extension] fd = open(path, "rb") file_header = fd.read(1024) fd.close() vdi_flag = "<<< Sun VirtualBox Disk Image >>>" if file_header[0:len(vdi_flag)] == vdi_flag: return maptab["vdi"] output = runner.outs(['file', path]) isoptn = re.compile(r".*ISO 9660 CD-ROM filesystem.*(bootable).*") usbimgptn = re.compile(r".*x86 boot sector.*active.*") rawptn = re.compile(r".*x86 boot sector.*") vmdkptn = re.compile(r".*VMware. disk image.*") ext3fsimgptn = re.compile(r".*Linux.*ext3 filesystem data.*") ext4fsimgptn = re.compile(r".*Linux.*ext4 filesystem data.*") btrfsimgptn = re.compile(r".*BTRFS.*") if isoptn.match(output): return maptab["iso"] elif usbimgptn.match(output): return maptab["usbimg"] elif rawptn.match(output): return maptab["raw"] elif vmdkptn.match(output): return maptab["vmdk"] elif ext3fsimgptn.match(output): return "ext3fsimg" elif ext4fsimgptn.match(output): return "ext4fsimg" elif btrfsimgptn.match(output): return "btrfsimg" else: raise CreatorError("Cannot detect the type of image: %s" % path)
def __format_filesystem(self): if self.skipformat: msger.debug("Skip filesystem format.") return msger.verbose("Formating %s filesystem on %s" % (self.fstype, self.disk.device)) rc = runner.show([self.mkfscmd, "-L", self.fslabel, self.disk.device]) if rc != 0: raise MountError("Error creating %s filesystem on disk %s" % (self.fstype,self.disk.device)) self.uuid = self.__parse_field(runner.outs([self.blkidcmd, self.disk.device]), "UUID")
def check_armv7_qemu_version(arch, qemu_emulator): 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) return None
def _get_metadata_from_repo(baseurl, proxies, cachedir, reponame, filename, sumtype=None, checksum=None): url = os.path.join(baseurl, filename) filename_tmp = str("%s/%s/%s" % (cachedir, reponame, os.path.basename(filename))) if os.path.splitext(filename_tmp)[1] in (".gz", ".bz2"): filename = os.path.splitext(filename_tmp)[0] else: filename = filename_tmp if sumtype and checksum and os.path.exists(filename): sumcmd = "%ssum" % sumtype file_checksum = runner.outs([sumcmd, filename]).split()[0] if file_checksum == checksum: return filename return _get_uncompressed_data_from_url(url,filename_tmp,proxies)
def selinux_check(arch, fstypes): try: getenforce = find_binary_path('getenforce') except CreatorError: return selinux_status = runner.outs([getenforce]) if arch and arch.startswith("arm") and selinux_status == "Enforcing": raise CreatorError("Can't create arm image if selinux is enabled, " "please run 'setenforce 0' to disable selinux") use_btrfs = filter(lambda typ: typ == 'btrfs', fstypes) if use_btrfs and selinux_status == "Enforcing": raise CreatorError("Can't create btrfs image if selinux is enabled," " please run 'setenforce 0' to disable selinux")
def my_fuser(fp): fuser = find_binary_path("fuser") if not os.path.exists(fp): return False rc = runner.quiet([fuser, "-s", fp]) if rc == 0: for pid in runner.outs([fuser, fp]).split(): fd = open("/proc/%s/cmdline" % pid, "r") cmdline = fd.read() fd.close() if cmdline[:-1] == "/bin/bash": return True # not found return False
def get_cow_used(self): if not self.__created: return 0 # # dmsetup status on a snapshot returns e.g. # "0 8388608 snapshot 416/1048576" # or, more generally: # "A B snapshot C/D" # where C is the number of 512 byte sectors in use # out = runner.outs([self.dmsetupcmd, "status", self.__name]) try: return int((out.split()[3]).split('/')[0]) * 512 except ValueError: raise SnapshotError("Failed to parse dmsetup status: " + out)
def __format_filesystem(self): if self.skipformat: msger.debug("Skip filesystem format.") return msger.verbose("Formating %s filesystem on %s" % (self.fstype, self.disk.device)) rc = runner.show( [self.mkfscmd, "-F", "-L", self.fslabel, "-m", "1", "-b", str(self.blocksize), self.disk.device] ) # str(self.disk.size / self.blocksize)]) if rc != 0: raise MountError("Error creating %s filesystem on disk %s" % (self.fstype, self.disk.device)) out = runner.outs([self.dumpe2fs, "-h", self.disk.device]) self.uuid = self.__parse_field(out, "Filesystem UUID") msger.debug("Tuning filesystem on %s" % self.disk.device) runner.show([self.tune2fs, "-c0", "-i0", "-Odir_index", "-ouser_xattr,acl", self.disk.device])
def __format_filesystem(self): if self.skipformat: msger.debug("Skip filesystem format.") return msger.verbose("Formating %s filesystem on %s" % (self.fstype, self.disk.device)) rc = runner.show([self.mkfscmd, "-F", "-L", self.fslabel, "-m", "1", "-b", str(self.blocksize), self.disk.device]) # str(self.disk.size / self.blocksize)]) if rc != 0: raise MountError("Error creating %s filesystem on disk %s" % (self.fstype, self.disk.device)) out = runner.outs([self.dumpe2fs, '-h', self.disk.device]) self.uuid = self.__parse_field(out, "Filesystem UUID") msger.debug("Tuning filesystem on %s" % self.disk.device) runner.show([self.tune2fs, "-c0", "-i0", "-Odir_index", "-ouser_xattr,acl", self.disk.device])
def _get_metadata_from_repo(baseurl, proxies, cachedir, reponame, filename, sumtype=None, checksum=None): url = os.path.join(baseurl, filename) filename_tmp = str("%s/%s/%s" % (cachedir, reponame, os.path.basename(filename))) if os.path.splitext(filename_tmp)[1] in (".gz", ".bz2"): filename = os.path.splitext(filename_tmp)[0] else: filename = filename_tmp if sumtype and checksum and os.path.exists(filename): sumcmd = "%ssum" % sumtype file_checksum = runner.outs([sumcmd, filename]).split()[0] if file_checksum == checksum: return filename return _get_uncompressed_data_from_url(url, filename_tmp, proxies)
def __get_size_from_filesystem(self): return int(self.__parse_field(runner.outs([self.dumpe2fs, '-h', self.disk.lofile]), "Block count")) * self.blocksize
def __get_size_from_filesystem(self): return int( self.__parse_field( runner.outs([self.dumpe2fs, '-h', self.disk.lofile]), "Block count")) * self.blocksize
def is_statically_linked(binary): return ", statically linked, " in runner.outs(['file', binary])