def GetPlatformSpecialFiles(self, tmpdir='/tmp'): """Creates any platform specific special files.""" retval = [] console_dev = os.makedev(5, 1) os.mknod(tmpdir + 'console', stat.S_IFCHR | stat.S_IRUSR | stat.S_IWUSR, console_dev) retval.append((tmpdir + 'console', 'dev/console')) null_dev = os.makedev(1, 3) os.mknod(tmpdir + 'null', stat.S_IFCHR | stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH, null_dev) retval.append((tmpdir + 'null', 'dev/null')) tty_dev = os.makedev(5, 0) os.mknod(tmpdir + 'tty', stat.S_IFCHR | stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH, tty_dev) retval.append((tmpdir + 'tty', 'dev/tty')) zero_dev = os.makedev(1, 5) os.mknod(tmpdir + 'zero', stat.S_IFCHR | stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH, zero_dev) retval.append((tmpdir + 'zero', 'dev/zero')) # /selinux is deprecated in favor of /sys/fs/selinux, but preserve it on # those OSes where it's present. if os.path.isdir('/selinux'): os.mkdir(tmpdir + 'selinux', 0755) retval.append((tmpdir + 'selinux', 'selinux')) return retval
def pack_into(self, block, offset=0): dev = os.makedev(self.devmajor, self.devminor) ino = str(self.ino) mode = str(self.mode) uid = str(self.uid) gid = str(self.gid) nlink = str(self.nlink) rdev = os.makedev(self.rdevmajor, self.rdevminor) mtime = str(self.mtime) namesize = str(len(self.name) + 1) # add a null filesize = str(self.filesize) self.coder.pack_into(block, offset, self.magic, dev, ino, mode, uid, gid, nlink, rdev, mtime, namesize, filesize) namesize = len(self.name) + 1 namestart = offset + self.coder.size datastart = namestart + namesize block[namestart:datastart - 2] = self.name block[datastart - 1] = '\x00' block[datastart:datastart + self.filesize] = self.content return self
def test_special_files(tmpdir): # FIFO os.mkfifo(str(tmpdir.join('fifo'))) # Block device try: os.mknod(str(tmpdir.join('blck')), 0o600 | stat.S_IFBLK, os.makedev(10, 20)) except OSError as e: pass # Char device try: os.mknod(str(tmpdir.join('char')), 0o600 | stat.S_IFCHR, os.makedev(30, 40)) except OSError as e: pass # add some control group files tmpdir.join('control1').write('control1', ensure=True) tmpdir.join('control2.txt').write('control2.txt', ensure=True) _jottapath = u'/Jotta/Archive/TEST_SPECIAL' #def compare(localtopdir, jottamountpoint, JFS, followlinks=False, exclude_patterns=None): # dirpath, # byte string, full path # onlylocal, # set(), files that only exist locally, i.e. newly added files that don't exist online, # onlyremote, # set(), files that only exist in the JottaCloud, i.e. deleted locally # bothplaces # set(), files that exist both locally and remotely # onlyremotefolders, tree = list(jottacloud.compare(str(tmpdir), _jottapath, jfs)) _, onlylocal, _, _, _ = tree[0] assert len(onlylocal) == 2 for _basename in [os.path.basename(x.localpath) for x in onlylocal]: assert _basename in ('control1', 'control2.txt')
def create_test_files(self): """Create a minimal test case including all supported file types """ # File self.create_regual_file('empty', size=0) self.create_regual_file('file1', size=1024 * 80) # Directory self.create_regual_file('dir2/file2', size=1024 * 80) # File owner os.chown('input/file1', 100, 200) # File mode os.chmod('input/file1', 0o7755) os.chmod('input/dir2', 0o555) # Block device os.mknod('input/bdev', 0o600 | stat.S_IFBLK, os.makedev(10, 20)) # Char device os.mknod('input/cdev', 0o600 | stat.S_IFCHR, os.makedev(30, 40)) if xattr.is_enabled(): xattr.setxattr(os.path.join(self.input_path, 'file1'), 'user.foo', b'bar') # Hard link os.link(os.path.join(self.input_path, 'file1'), os.path.join(self.input_path, 'hardlink')) # Symlink os.symlink('somewhere', os.path.join(self.input_path, 'link1')) # FIFO node os.mkfifo(os.path.join(self.input_path, 'fifo1'))
def create_test_files(self): """Create a minimal test case including all supported file types """ # File self.create_regular_file('empty', size=0) # 2600-01-01 > 2**64 ns os.utime('input/empty', (19880895600, 19880895600)) self.create_regular_file('file1', size=1024 * 80) self.create_regular_file('flagfile', size=1024) # Directory self.create_regular_file('dir2/file2', size=1024 * 80) # File owner os.chown('input/file1', 100, 200) # File mode os.chmod('input/file1', 0o7755) os.chmod('input/dir2', 0o555) # Block device os.mknod('input/bdev', 0o600 | stat.S_IFBLK, os.makedev(10, 20)) # Char device os.mknod('input/cdev', 0o600 | stat.S_IFCHR, os.makedev(30, 40)) # Hard link os.link(os.path.join(self.input_path, 'file1'), os.path.join(self.input_path, 'hardlink')) # Symlink os.symlink('somewhere', os.path.join(self.input_path, 'link1')) if xattr.is_enabled(): xattr.setxattr(os.path.join(self.input_path, 'file1'), 'user.foo', b'bar') xattr.setxattr(os.path.join(self.input_path, 'link1'), 'user.foo_symlink', b'bar_symlink', follow_symlinks=False) # FIFO node os.mkfifo(os.path.join(self.input_path, 'fifo1')) if has_lchflags: os.lchflags(os.path.join(self.input_path, 'flagfile'), stat.UF_NODUMP)
def pack_into(self, block, offset=0): namesize = len(self.name) dev = os.makedev(self.devmajor, self.devminor) rdev = os.makedev(self.rdevmajor, self.rdevminor) mtimehigh = self.mtime >> 16 mtimelow = self.mtime & 0xffff filesizehigh = self.filesize >> 16 filesizelow = self.filesize & 0xffff self.coder.pack_into(block, offset, self.magic, dev, self.ino, self.mode, self.uid, self.gid, self.nlink, rdev, mtimehigh, mtimelow, namesize, filesizehigh, filesizelow) namestart = offset + self.coder.size datastart = namestart + namesize + 1 block[namestart:datastart - 1] = self.name block[datastart - 1] = '\x00' if isinstance(self, CpioMemberBin) and (namesize & 1): datastart += 1 block[datastart - 1] = '\x00' block[datastart:datastart + self.filesize] = self.content if isinstance(self, CpioMemberBin) and (self.filesize & 1): block[datastart + self.filesize] = '\x00' return self
def explode(self, destDir): linkMap = {} for ent in self: try: target = destDir + '/' + ent.filename parent = os.path.dirname(target) if not os.path.exists(parent): os.makedirs(parent) if stat.S_ISCHR(ent.header.mode): os.mknod(target, stat.S_IFCHR, os.makedev(ent.rdevmajor, ent.rdevminor)) elif stat.S_ISBLK(ent.header.mode): os.mknod(target, stat.S_IFBLK, os.makedev(ent.rdevmajor, ent.rdevminor)) elif stat.S_ISDIR(ent.header.mode): os.mkdir(target) elif stat.S_ISFIFO(ent.header.mode): os.mkfifo(target) elif stat.S_ISLNK(ent.header.mode): os.symlink(ent.payload.read(),target) elif stat.S_ISREG(ent.header.mode): # save hardlinks until after the file content is written if ent.header.nlink > 1 and ent.header.filesize == 0: l = linkMap.get(ent.header.inode, []) l.append(target) linkMap[ent.header.inode] = l continue f = open(target, "w") buf = ent.payload.read(64 * 1024) while buf: f.write(buf) buf = ent.payload.read(64 * 1024) f.close() else: raise Error("unknown file mode 0%o for %s" % (ent.header.mode, ent.filename)) # create hardlinks after the file content is written if ent.header.nlink > 1 and ent.header.filesize: l = linkMap.get(ent.header.inode, []) # the last entry with the same inode should contain the # contents so this list should always have at least one # entry assert(l) for t in l: os.link(target, t) # create hardlinks after the file content is written except OSError, e: if e.errno == errno.EEXIST: pass else: raise if not stat.S_ISLNK(ent.header.mode): os.chmod(target, ent.header.mode & 0777)
def mount_simple_dev(path = '/dev'): ''' Creates mountpoint and mounts cgroup fs in it ''' try_mkdir(path) flags = const.MS_NOEXEC | const.MS_NOSUID | const.MS_NOATIME lowlevel.mount('simple_dev', path, 'tmpfs', flags, 'size=1024k') os.mknod(os.path.join(path, 'null'), 0o777, stat.S_IFCHR | os.makedev(1, 3)) os.mknod(os.path.join(path, 'zero'), 0o777, stat.S_IFCHR | os.makedev(1, 5))
def test_SpecialFile(self): if os.getuid() != 0: print "SKIPPING test that requires root permissions" return os.mknod(self.client.abspath("aDevice"), 0600 | stat.S_IFCHR, os.makedev(123, 45)) self.client.checkin("yuvu") os.unlink(self.client.abspath("aDevice")) self.client.checkout("yuvu") self.assertEquals(self.client.fileCount(), 1) self.assertTrue(stat.S_ISCHR(os.stat(self.client.abspath("aDevice")).st_mode)) self.assertEquals(os.stat(self.client.abspath("aDevice")).st_rdev, os.makedev(123, 45))
def __construct_root(self): for dir in self.__root_dirs: os.mkdir(self.root + dir) os.chmod(self.root + dir, 0777) for ldir in [ "/bin", "/sbin", "/lib", "/lib64" ]: os.symlink(".." + ldir, self.root + "/usr" + ldir) self.__mknod("tty", os.makedev(5, 0)) self.__mknod("null", os.makedev(1, 3)) self.__mknod("net/tun") self.__mknod("rtc")
def testSuccess(self): self.mox.StubOutWithMock(glob, 'glob') self.mox.StubOutWithMock(os, 'mknod') glob.glob('/dev/loop*').AndReturn(['/dev/loop%d' % i for i in xrange(8)]) os.mknod('/dev/loop8', stat.S_IFBLK | 0600, os.makedev(7, 8)).AndRaise( OSError(17, 'File exists')) glob.glob('/dev/loop*').AndReturn(['/dev/loop%d' % i for i in xrange(9)]) os.mknod('/dev/loop9', stat.S_IFBLK | 0600, os.makedev(7, 9)) self.mox.ReplayAll() base._createNewLoopDev()
def _setupDev(self): # files in /dev mock.util.rmtree(self.makeChrootPath("dev"), selinux=self.selinux) mock.util.mkdirIfAbsent(self.makeChrootPath("dev", "pts")) mock.util.mkdirIfAbsent(self.makeChrootPath("dev", "shm")) prevMask = os.umask(0000) devFiles = [ (stat.S_IFCHR | 0666, os.makedev(1, 3), "dev/null"), (stat.S_IFCHR | 0666, os.makedev(1, 7), "dev/full"), (stat.S_IFCHR | 0666, os.makedev(1, 5), "dev/zero"), (stat.S_IFCHR | 0666, os.makedev(1, 8), "dev/random"), (stat.S_IFCHR | 0444, os.makedev(1, 9), "dev/urandom"), (stat.S_IFCHR | 0666, os.makedev(5, 0), "dev/tty"), (stat.S_IFCHR | 0600, os.makedev(5, 1), "dev/console"), ] kver = os.uname()[2] # make the device node for el4 and el5 if mock.util.cmpKernelEVR(kver, '2.6.18') <= 0: devFiles.append((stat.S_IFCHR | 0666, os.makedev(5, 2), "dev/ptmx")) for i in devFiles: # create node os.mknod( self.makeChrootPath(i[2]), i[0], i[1]) # set context. (only necessary if host running selinux enabled.) # fails gracefully if chcon not installed. if self.selinux: mock.util.do( ["chcon", "--reference=/%s"% i[2], self.makeChrootPath(i[2])] , raiseExc=0, shell=False) os.symlink("/proc/self/fd/0", self.makeChrootPath("dev/stdin")) os.symlink("/proc/self/fd/1", self.makeChrootPath("dev/stdout")) os.symlink("/proc/self/fd/2", self.makeChrootPath("dev/stderr")) # don't symlink for RHEL4 systems if mock.util.cmpKernelEVR(kver, '2.6.9') > 0: os.symlink("/proc/self/fd", self.makeChrootPath("dev/fd")) # symlink it for FC and el6 hosts if mock.util.cmpKernelEVR(kver, '2.6.18') > 0: os.symlink("/dev/pts/ptmx", self.makeChrootPath("dev/ptmx")) os.umask(prevMask) # mount/umount for devUnmtCmd in ( 'umount -n %s' % self.makeChrootPath('/dev/pts'), 'umount -n %s' % self.makeChrootPath('/dev/shm') ): if devUnmtCmd not in self.umountCmds: self.umountCmds.append(devUnmtCmd) mountopt = 'gid=%d,mode=0620,ptmxmode=0666' % grp.getgrnam('tty').gr_gid if mock.util.cmpKernelEVR(kver, '2.6.29') >= 0: mountopt += ',newinstance' for devMntCmd in ( 'mount -n -t devpts -o %s mock_chroot_devpts %s' % (mountopt, self.makeChrootPath('/dev/pts')), 'mount -n -t tmpfs mock_chroot_shmfs %s' % self.makeChrootPath('/dev/shm') ): if devMntCmd not in self.mountCmds: self.mountCmds.append(devMntCmd)
def initializeZram(self, size, zramNumber=0): with open("/sys/block/zram%d/disksize" % (zramNumber), "w") as disksize: disksize.write("%d" % (size)) with open("/sys/block/zram%d/disksize" % (zramNumber), "r") as disksize: verify = int(disksize.read()) with open("/sys/block/zram%d/dev" % (zramNumber), "r") as devfile: devnum = devfile.read().strip() deviceName = "/dev/zram%d" % (zramNumber) if not os.path.exists(deviceName): spdevnum = devnum.split(':') major = int(spdevnum[0]) minor = int(spdevnum[1]) os.mknod(deviceName, 0o660 | stat.S_IFBLK,os.makedev(major,minor)) if verify < size: raise Exception("Unexpected %s disksize %d(expected:%d)" % (deviceName, verify, size)) try: verify = self.determineBlockdeviceSize(deviceName) except: raise Exception("Blockdevice size couldn't be determined: %s - %s" % (deviceName, devnum)) if verify < size: raise Exception("Unexpected %s disksize %d(expected:%d)" % (deviceName, verify, size)) return deviceName
def _createNewLoopDev(): """Create a new loop device. On recent kernels (>2.6.22), the kernel actually supports a bunch of loop devices, but only creates nodes for /dev/loop0 through /dev/loop7 by default. We should be able to create more loop devices with just an appropriate mknod call. We need to try multiple times, just in case somebody else is running this at the same time. """ while True: loop_nums = (int(loop[9:]) for loop in glob.glob('/dev/loop*')) last_loop = max(loop_nums) new_loop = last_loop + 1 try: # TODO(ebroder): Figure out how to get udev to generate the /dev # node. We want udev to be doing that so that all of the # permissions get set correctly, etc. os.mknod('/dev/loop%d' % new_loop, stat.S_IFBLK | 0600, os.makedev(7, new_loop)) break except EnvironmentError, e: if e.errno != errno.EEXIST: raise
def from_kernel_device(cls, context, kernel_device): """ Locate a device based on the kernel device. :param `Context` context: the libudev context :param str kernel_device: the kernel device :returns: the device corresponding to ``kernel_device`` :rtype: `Device` """ switch_char = kernel_device[0] rest = kernel_device[1:] if switch_char in ('b', 'c'): number_re = re.compile(r'^(?P<major>\d+):(?P<minor>\d+)$') match = number_re.match(rest) if match: number = os.makedev( int(match.group('major')), int(match.group('minor')) ) return cls.from_device_number(context, switch_char, number) else: raise DeviceNotFoundByKernelDeviceError(kernel_device) elif switch_char == 'n': return cls.from_interface_index(context, rest) elif switch_char == '+': (subsystem, _, kernel_device_name) = rest.partition(':') if kernel_device_name and subsystem: return cls.from_name(context, subsystem, kernel_device_name) else: raise DeviceNotFoundByKernelDeviceError(kernel_device) else: raise DeviceNotFoundByKernelDeviceError(kernel_device)
def create(type, path, mode, uid, gid, age, arg): if type == "d" and \ os.path.isdir(path) and \ (not uid == os.stat(path).st_uid or not gid == os.stat(path).st_gid): type = "D" if type == "L": if not os.path.islink(path): os.symlink(arg, path) return elif type == "D": if os.path.isdir(path): shutil.rmtree(path) elif os.path.islink(path): os.remove(path) elif type == "c": if not os.path.isdir(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) if not os.path.exists(path): dev = [int(x) for x in arg.split(":")] os.mknod(path, mode|stat.S_IFCHR, os.makedev(dev[0], dev[1])) os.chown(path, uid, gid) if type.lower() == "d": if not os.path.isdir(path): os.makedirs(path, mode) os.chown(path, uid, gid) elif type in ["f", "F", "w"]: if not os.path.isfile(path) and type == "w": return if not os.path.isdir(os.path.dirname(path)): os.makedirs(os.path.dirname(path)) os.chown(os.path.dirname(path), uid, gid) write_file(path, arg, mode = "a" if type == "f" else "w") os.chmod(path, mode) os.chown(path, uid, gid)
def __create_selinuxfs(self): # if selinux exists on the host we need to lie to the chroot if os.path.exists("/selinux/enforce"): selinux_dir = self._instroot + "/selinux" # enforce=0 tells the chroot selinux is not enforcing # policyvers=999 tell the chroot to make the highest version of policy it can files = [('/enforce', '0'), ('/policyvers', '999'), ('/commit_pending_bools', ''), ('/mls', str(selinux.is_selinux_mls_enabled()))] for (file, value) in files + self.__getbooleans(): fd = os.open(selinux_dir + file, os.O_WRONLY | os.O_TRUNC | os.O_CREAT) os.write(fd, value) os.close(fd) # we steal mls from the host system for now, might be best to always set it to 1???? # make /load -> /dev/null so chroot policy loads don't hurt anything os.mknod(selinux_dir + "/load", 0666 | stat.S_IFCHR, os.makedev(1, 3)) # selinux is on in the kickstart, so clean up as best we can to start if kickstart.selinux_enabled(self.ks): # label the fs like it is a root before the bind mounting arglist = ["/sbin/setfiles", "-F", "-r", self._instroot, selinux.selinux_file_context_path(), self._instroot] subprocess.call(arglist, close_fds = True) # these dumb things don't get magically fixed, so make the user generic for f in ("/proc", "/sys", "/selinux"): arglist = ["/usr/bin/chcon", "-u", "system_u", self._instroot + f] subprocess.call(arglist, close_fds = True)
def Installdevice(self, entry): '''Install device entries''' try: # check for existing paths and remove them filestat = os.lstat(entry.get('name')) try: os.unlink(entry.get('name')) exists = False except OSError: self.logger.info('Failed to unlink %s' % \ entry.get('name')) return False except OSError: exists = False if not exists: try: dev_type = entry.get('dev_type') mode = calcPerms(device_map[dev_type], entry.get('mode', '0600')) if dev_type in ['block', 'char']: major = int(entry.get('major')) minor = int(entry.get('minor')) device = os.makedev(major, minor) os.mknod(entry.get('name'), mode, device) else: os.mknod(entry.get('name'), mode) os.chown(entry.get('name'), normUid(entry), normGid(entry)) return True except OSError: self.logger.error('Failed to install %s' % entry.get('name')) return False
def create(self, mode = None): device = os.makedev(self.major, self.minor) if(mode != None): os.mknod(self.path, self.type|mode, device) else: os.mknod(self.path, self.type|self.mode, device) return self
def __create_minimal_dev(self): """Create a minimal /dev so that we don't corrupt the host /dev""" origumask = os.umask(0000) devices = ( ("null", 1, 3, 0666), ("urandom", 1, 9, 0666), ("random", 1, 8, 0666), ("full", 1, 7, 0666), ("ptmx", 5, 2, 0666), ("tty", 5, 0, 0666), ("zero", 1, 5, 0666), ) links = ( ("/proc/self/fd", "/dev/fd"), ("/proc/self/fd/0", "/dev/stdin"), ("/proc/self/fd/1", "/dev/stdout"), ("/proc/self/fd/2", "/dev/stderr"), ) for (node, major, minor, perm) in devices: if not os.path.exists(self._instroot + "/dev/" + node): os.mknod(self._instroot + "/dev/" + node, perm | stat.S_IFCHR, os.makedev(major, minor)) for (src, dest) in links: if not os.path.exists(self._instroot + dest): os.symlink(src, self._instroot + dest) os.umask(origumask)
def test_major_minor(self): os = self.posix assert os.major(12345) == self.expected_major_12345 assert os.minor(12345) == self.expected_minor_12345 assert os.makedev(self.expected_major_12345, self.expected_minor_12345) == 12345 raises((ValueError, OverflowError), os.major, -1)
def sshfsmountmap(): """ Return a dictionary mapping mount points to tuples containing the remote login ([user@]hostname) and remote path for all remote hosts mounted with sshfs. """ def unescape(path): "Unescape octal-escaped path." def suboctal(match): "Convert octal sequence regex match to a character." return chr(int(match.group(0)[1:], 8)) return re.sub("\\\\[0-7]{1,3}", suboctal, path) mapping = dict() with open("/proc/self/mountinfo") as iostream: for line in iostream: fields = line.split(' ') fstype = fields[-3] mountpoint = unescape(os.path.abspath(fields[4])) if fstype in ('fuse.sshfs', 'nfs4', 'nfs'): remote, path = fields[-2].split(':', 1) device = os.makedev(*(map(int, fields[2].split(':')))) mapping[mountpoint] = (remote, os.path.abspath(unescape(path))) else: mapping[mountpoint] = None return mapping
def doTestDevices(self, t): if t == stat.S_IFBLK: d = files.BlockDevice(None) else: d = files.CharacterDevice(None) d.inode.perms.set(0604) d.inode.mtime.set(0100) d.inode.owner.set("daemon") d.inode.group.set("uucp") # to make sure that referenced names "exist" files.userCache.nameCache['daemon'] = 2 files.groupCache.nameCache['uucp'] = 14 d.flags.set(0) d.devt.major.set(1) d.devt.minor.set(2) assert(d.sizeString() == " 1, 2") path = tempfile.mkdtemp() try: p = path + "/dev2/foo" self.mimicRoot() d.restore(None, path, p) self.assertEqual(self.mknodLog, [(p, t, os.makedev(1, 2))]) self.assertEqual(self.chownLog, [(p, 2, 14)]) finally: self.realRoot() shutil.rmtree(path) d2 = files.ThawFile(d.freeze(), None) assert(d2 == d)
def init(self, test_bin): print "Construct root for %s" % test_bin subprocess.check_call(["mount", "--make-private", "--bind", ".", self.root]) if not os.access(self.root + "/.constructed", os.F_OK): for dir in ["/bin", "/etc", "/lib", "/lib64", "/dev", "/tmp"]: os.mkdir(self.root + dir) os.chmod(self.root + dir, 0777) os.mknod(self.root + "/dev/tty", stat.S_IFCHR, os.makedev(5, 0)) os.chmod(self.root + "/dev/tty", 0666) os.mknod(self.root + "/.constructed", stat.S_IFREG | 0600) ldd = subprocess.Popen(["ldd", test_bin], stdout = subprocess.PIPE) xl = re.compile('^(linux-gate.so|linux-vdso(64)?.so|not a dynamic)') # This Mayakovsky-style code gets list of libraries a binary # needs minus vdso and gate .so-s libs = map(lambda x: x[1] == '=>' and x[2] or x[0], \ map(lambda x: x.split(), \ filter(lambda x: not xl.match(x), \ map(lambda x: x.strip(), \ filter(lambda x: x.startswith('\t'), ldd.stdout.readlines()))))) ldd.wait() for lib in libs: tlib = self.root + lib if not os.access(tlib, os.F_OK): # Copying should be atomic as tests can be # run in parallel dst = tempfile.mktemp(".tso", "", self.root + os.path.dirname(lib)) shutil.copy2(lib, dst) os.rename(dst, tlib)
def get_fs_info(self, path): fs_type = None uuid = None label = None devpth = None tmpd = None try: st_dev=os.stat(path).st_dev dev=os.makedev(os.major(st_dev),os.minor(st_dev)) tmpd=tempfile.mkdtemp() devpth=("%s/dev" % tmpd) os.mknod(devpth,0o400 | stat.S_IFBLK ,dev) except: raise ret = { } pairs = { 'LABEL' : 'label', 'UUID' : 'uuid' , 'FS_TYPE' : 'fs_type' } for (blkid_n, my_n) in pairs.items(): cmd = [ 'blkid', '-s%s' % blkid_n, '-ovalue', devpth ] print(cmd) try: output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] ret[my_n]=output.rstrip() except Exception as e: os.unlink(devpth) os.rmdir(tmpd) raise UnsupportedException("Unable to determine %s for %s" % (blkid_n, path)) os.unlink(devpth) os.rmdir(tmpd) return(ret)
def add_file(self, fs_dir, f): fn = os.path.join(fs_dir, f.name) if f.ftype == fs.REGULAR: dbf = self.body_db.file_db.files[f.file_id] self.body_db.file_store.extract_file(fn, dbf.offset, dbf.zlength) elif f.ftype == fs.SYMLINK: os.symlink(f.target, fn) elif f.ftype == fs.SOCKET: s = socket.socket(socket.AF_UNIX) s.bind(fn) s.close() elif f.ftype == fs.FIFO: os.mkfifo(fn, f.mode) else: mode = f.mode | stat.S_IFBLK if f.ftype == fs.BLOCKDEV else stat.S_IFCHR device = os.makedev(f.major, f.minor) os.mknod(fn, mode, device) try: os.lchown(fn, f.uid, f.gid) except: pass if not f.ftype == fs.SYMLINK: os.chmod(fn, f.mode) cwrap.set_nsec_mtime(fn, f.mtime_ns)
def from_props(cls, props, *, from_uevent=False): # Needs at least DEVPATH! self = cls() self.properties = dict(props) self.set_syspath(SYS_PATH + props["DEVPATH"]) if "SUBSYSTEM" in props: self.set_subsystem(props["SUBSYSTEM"]) if "IFINDEX" in props: self.set_ifindex(int(props["IFINDEX"])) if "DEVNAME" in props: self.set_devnode(props["DEVNAME"]) if "DEVTYPE" in props: self.set_devtype(props["DEVTYPE"]) if "DEVMODE" in props: self.devnode_mode = int(props["DEVMODE"], 8) major = minor = 0 if "MAJOR" in props: major = int(props["MAJOR"]) if "MINOR" in props: minor = int(props["MINOR"]) self.devnum = os.makedev(major, minor) self.is_uevent_loaded = from_uevent # The device itself doesn't have an action! if "ACTION" in props: del self.properties["ACTION"] cls.registry[self.syspath] = self return self
def apply(self, builder, instr, role, target_base, hierarchy): # Find or create the relevant file cpio_file = cpiofile.File() (clrb, setb) = utils.parse_mode(instr.mode) cpio_file.mode = setb cpio_file.uid = utils.parse_uid(builder, instr.uid) cpio_file.gid = utils.parse_gid(builder, instr.gid) if (instr.type == "char"): cpio_file.mode = cpio_file.mode | cpiofile.File.S_CHAR else: cpio_file.mode = cpio_file.mode | cpiofile.File.S_BLK cpio_file.rdev = os.makedev(int(instr.major), int(instr.minor)) # Zero-length file - it's a device node. cpio_file.name = None cpio_file.data = None #print "target_base = %s for role %s"%(target_base, role) real_path = utils.rel_join(target_base, instr.file_name) cpio_file.key_name = real_path #print "put_target_file %s"%real_path print 'Adding device node %s'%real_path hierarchy.put_target_file(real_path, cpio_file)
def makeDMNode(root="/"): major = minor = None for (fn, devname, val) in (("/proc/devices", "misc", "major"), ("/proc/misc", "device-mapper", "minor")): f = open(fn) lines = f.readlines() f.close() for line in lines: try: (num, dev) = line.strip().split(" ") except: continue if dev == devname: s = "%s = int(num)" % (val,) exec s break # print "major is %s, minor is %s" %(major, minor) if major is None or minor is None: return mkdirChain(root + "/dev/mapper") try: os.mknod(root + "/dev/mapper/control", stat.S_IFCHR | 0600, os.makedev(major, minor)) except: pass
def insert_module(): print '****** Insert Module ******\n' print '1: insmod chdev.ko' subprocess.call(['sudo', 'insmod', 'chdev.ko']) print 'success\n' ps = subprocess.Popen(('dmesg'), stdout=subprocess.PIPE) output = subprocess.check_output(('tail', '-n 5'), stdin=ps.stdout) ps.wait() numbers = [ int(n) for n in output.split() if n.isdigit()] major = numbers[1]; print 'MAJOR NUMBER:', major, '\n' print '2: mknode' filename = "/dev/mychdev" mode = 0777|stat.S_IFCHR dev = os.makedev(major, 0) os.mknod(filename, mode, dev) subprocess.call('sudo chmod 777 /dev/mychdev', shell=True) print 'success'
def test_add_node_dev_null(self): st = os.lstat('/dev/null') dev_maj = os.major(st.st_rdev) dev_min = os.minor(st.st_rdev) self.tf.add_node('/dev/null', st.st_mode, os.makedev(dev_maj, dev_min))
def _copy_node(self, path, st): dev_maj = os.major(st.st_rdev) dev_min = os.minor(st.st_rdev) mode = st.st_mode self.archive.add_node(path, mode, os.makedev(dev_maj, dev_min))
def _setup_devices(self): if self.config['internal_dev_setup']: util.rmtree(self.make_chroot_path("dev"), selinux=self.selinux, exclude=self.mounts.get_mountpoints()) util.mkdirIfAbsent(self.make_chroot_path("dev", "pts")) util.mkdirIfAbsent(self.make_chroot_path("dev", "shm")) prevMask = os.umask(0000) devFiles = [ (stat.S_IFCHR | 0o666, os.makedev(1, 3), "dev/null"), (stat.S_IFCHR | 0o666, os.makedev(1, 7), "dev/full"), (stat.S_IFCHR | 0o666, os.makedev(1, 5), "dev/zero"), (stat.S_IFCHR | 0o666, os.makedev(1, 8), "dev/random"), (stat.S_IFCHR | 0o444, os.makedev(1, 9), "dev/urandom"), (stat.S_IFCHR | 0o666, os.makedev(5, 0), "dev/tty"), (stat.S_IFCHR | 0o600, os.makedev(5, 1), "dev/console"), (stat.S_IFCHR | 0o666, os.makedev(5, 2), "dev/ptmx"), (stat.S_IFCHR | 0o666, os.makedev(10, 237), "dev/loop-control"), (stat.S_IFBLK | 0o666, os.makedev(7, 0), "dev/loop0"), (stat.S_IFBLK | 0o666, os.makedev(7, 1), "dev/loop1"), (stat.S_IFBLK | 0o666, os.makedev(7, 2), "dev/loop2"), (stat.S_IFBLK | 0o666, os.makedev(7, 3), "dev/loop3"), (stat.S_IFBLK | 0o666, os.makedev(7, 4), "dev/loop4"), (stat.S_IFCHR | 0o600, os.makedev(10, 57), "dev/prandom"), (stat.S_IFCHR | 0o600, os.makedev(10, 183), "dev/hwrng"), ] kver = os.uname()[2] self.root_log.debug("kernel version == %s", kver) for i in devFiles: # create node, but only if it exist on host too # except for loop devices, which only show up on the host after they are first used if os.path.exists("/" + i[2]) or "loop" in i[2]: os.mknod(self.make_chroot_path(i[2]), i[0], i[1]) # set context. (only necessary if host running selinux enabled.) # fails gracefully if chcon not installed. if self.selinux: util.do([ "chcon", "--reference=/" + i[2], self.make_chroot_path(i[2]) ], raiseExc=0, shell=False, env=self.env) os.symlink("/proc/self/fd/0", self.make_chroot_path("dev/stdin")) os.symlink("/proc/self/fd/1", self.make_chroot_path("dev/stdout")) os.symlink("/proc/self/fd/2", self.make_chroot_path("dev/stderr")) if os.path.isfile(self.make_chroot_path('etc', 'mtab')) or \ os.path.islink(self.make_chroot_path('etc', 'mtab')): os.remove(self.make_chroot_path('etc', 'mtab')) os.symlink("../proc/self/mounts", self.make_chroot_path('etc', 'mtab')) os.chown(self.make_chroot_path('dev/tty'), pwd.getpwnam('root')[2], grp.getgrnam('tty')[2]) os.chown(self.make_chroot_path('dev/ptmx'), pwd.getpwnam('root')[2], grp.getgrnam('tty')[2]) # symlink /dev/fd in the chroot for everything except RHEL4 if util.cmpKernelVer(kver, '2.6.9') > 0: os.symlink("/proc/self/fd", self.make_chroot_path("dev/fd")) os.umask(prevMask) if util.cmpKernelVer(kver, '2.6.18') >= 0: os.unlink(self.make_chroot_path('/dev/ptmx')) os.symlink("pts/ptmx", self.make_chroot_path('/dev/ptmx'))
os.fstatvfs(fd) # 返回包含文件描述符fd的文件的文件系统的信息,像 statvfs() os.fsync(fd) # 强制将文件描述符为fd的文件写入硬盘。 os.ftruncate(fd, length) # 裁剪文件描述符fd对应的文件, 所以它最大不能超过文件大小。 os.getcwd() # 返回当前工作目录 os.getcwdu() # 返回一个当前工作目录的Unicode对象 os.isatty(fd) # 如果文件描述符fd是打开的,同时与tty(-like)设备相连,则返回true, 否则False。 os.lchflags(path, flags) # 设置路径的标记为数字标记,类似 chflags(),但是没有软链接 os.lchmod(path, mode) # 修改连接文件权限 os.lchown(path, uid, gid) # 更改文件所有者,类似 chown,但是不追踪链接。 os.link(src, dst) # 创建硬链接,名为参数 dst,指向参数 src os.listdir(path) # 返回path指定的文件夹包含的文件或文件夹的名字的列表。 os.lseek(fd, pos, how) # 设置文件描述符 fd当前位置为pos, how方式修改: SEEK_SET 或者 0 设置从文件开始的计算的pos; SEEK_CUR或者 1 则从当前位置计算; os.SEEK_END或者2则从文件尾部开始. 在unix,Windows中有效 os.lstat(path) # 像stat(),但是没有软链接 os.linesep # 当前平台使用的行终止符,win下为"\t\n",Linux下为"\n" os.major(device) # 从原始的设备号中提取设备major号码 (使用stat中的st_dev或者st_rdev field)。 os.makedev(major, minor) # 以major和minor设备号组成一个原始设备号 os.makedirs(path[, mode]) # 递归文件夹创建函数。像mkdir(), 但创建的所有intermediate-level文件夹需要包含子文件夹。 os.minor(device) # 从原始的设备号中提取设备minor号码 (使用stat中的st_dev或者st_rdev field )。 os.mkdir(path[, mode]) # 以数字mode的mode创建一个名为path的文件夹.默认的 mode 是 0777 (八进制)。 os.mkfifo(path[, mode]) # 创建命名管道,mode 为数字,默认为 0666 (八进制) os.mknod(filename[, mode=0600, device]) # 创建一个名为filename文件系统节点(文件,设备特别文件或者命名pipe)。 os.open(file, flags[, mode]) # 打开一个文件,并且设置需要的打开选项,mode参数是可选的 os.openpty() # 打开一个新的伪终端对。返回 pty 和 tty的文件描述符。 os.pathconf(path, name) # 返回相关文件的系统配置信息。 os.pathsep # 用于分割文件路径的字符串 os.pardir # 获取当前目录的父目录字符串名:('..') os.pipe() # 创建一个管道. 返回一对文件描述符(r, w) 分别为读和写 \os.popen(command[, mode[, bufsize]]) # 从一个 command 打开一个管道 os.path.abspath(path) # 返回path规范化的绝对路径 os.path.split(path) # 将path分割成目录和文件名二元组返回 os.path.dirname(path) # 返回path的目录。其实就是os.path.split(path)的第一个元素
# Following is the syntax for makedev() method − # # os.makedev(major, minor) # # Parameters # # major − This is Major device number. # # minor − This is Minor device number. # # Return Value # # This method returns the device number. # Example import os, sys path = "/var/www/html/foo.txt" # Now get the touple info = os.lstat(path) # Get major and minor device number major_dnum = os.major(info.st_dev) minor_dnum = os.minor(info.st_dev) print("Major Device Number :", major_dnum) print("Minor Device Number :", minor_dnum) # Make a device number dev_num = os.makedev(major_dnum, minor_dnum) print("Device Number :", dev_num)
except OSError, e: raise wrap_oserror(space, e) @unwrap_spec(mode=c_int) def mkfifo(space, w_filename, mode=0666): """Create a FIFO (a POSIX named pipe).""" try: dispatch_filename(rposix.mkfifo)(space, w_filename, mode) except OSError, e: raise wrap_oserror2(space, e, w_filename) @unwrap_spec(mode=c_int, device=c_int) def mknod(space, w_filename, mode=0600, device=0): """Create a filesystem node (file, device special file or named pipe) named filename. mode specifies both the permissions to use and the type of node to be created, being combined (bitwise OR) with one of S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK, device defines the newly created device special file (probably using os.makedev()), otherwise it is ignored.""" try: dispatch_filename(rposix.mknod)(space, w_filename, mode, device) except OSError, e: raise wrap_oserror2(space, e, w_filename) @unwrap_spec(mask=c_int) def umask(space, mask): "Set the current numeric umask and return the previous umask." prevmask = os.umask(mask)
len(test_file_contents)) def _has(name): return pytest.mark.skipif(not hasattr(os, name), reason="os.{} does not exist".format(name)) @pytest.mark.parametrize("disabled_call", [ _has("chflags")(lambda: os.chflags("/tmp/stuff", stat.SF_IMMUTABLE)), _has("chroot")(lambda: os.chroot("/tmp/stuff")), _has("lchflags")(lambda: os.lchflags("/tmp/stuff", stat.SF_IMMUTABLE)), _has("mkfifo")(lambda: os.mkfifo("/tmp/stuff")), _has("mkfifo")(lambda: os.mkfifo("/tmp/stuff", 0666)), _has("mknod")(lambda: os.mknod("/tmp/stuff")), _has("mknod")(lambda: os.mknod("/tmp/stuff", 0666, 0)), _has("makedev")(lambda: os.makedev(5, 5)), _has("pathconf")(lambda: os.pathconf("/tmp/stuff", "PC_FILESIZEBITS")), _has("removedirs")(lambda: os.removedirs("/tmp/stuff")), _has("renames")(lambda: os.renames("/tmp/stuff", "/tmp/other-stuff")), _has("utime")(lambda: os.utime("/tmp/stuff", None)), ]) def test_disabled(disabled_call): shield.install_hooks() try: # With this try-except, other errors will be caught # and hooks will still be uninstalled with pytest.raises(shield.common.ShieldError): disabled_call() except Exception: traceback.print_exc() finally:
def _setup_devices(self): if self.config['internal_dev_setup']: util.rmtree(self.make_chroot_path("dev"), selinux=self.selinux, exclude=self.mounts.get_mountpoints()) util.mkdirIfAbsent(self.make_chroot_path("dev", "pts")) util.mkdirIfAbsent(self.make_chroot_path("dev", "shm")) prevMask = os.umask(0000) devFiles = [ (stat.S_IFCHR | 0o666, os.makedev(1, 3), "dev/null"), (stat.S_IFCHR | 0o666, os.makedev(1, 7), "dev/full"), (stat.S_IFCHR | 0o666, os.makedev(1, 5), "dev/zero"), (stat.S_IFCHR | 0o666, os.makedev(1, 8), "dev/random"), (stat.S_IFCHR | 0o444, os.makedev(1, 9), "dev/urandom"), (stat.S_IFCHR | 0o666, os.makedev(5, 0), "dev/tty"), (stat.S_IFCHR | 0o600, os.makedev(5, 1), "dev/console"), (stat.S_IFCHR | 0o666, os.makedev(5, 2), "dev/ptmx"), (stat.S_IFCHR | 0o666, os.makedev(10, 237), "dev/loop-control"), (stat.S_IFCHR | 0o600, os.makedev(10, 57), "dev/prandom"), (stat.S_IFCHR | 0o600, os.makedev(10, 183), "dev/hwrng"), ] for i in range(self.config['dev_loop_count']): devFiles.append( (stat.S_IFBLK | 0o666, os.makedev(7, i), "dev/loop{loop_number}".format(loop_number=i))) kver = os.uname()[2] self.root_log.debug("kernel version == %s", kver) for i in devFiles: src_path = "/" + i[2] chroot_path = self.make_chroot_path(i[2]) if util.cmpKernelVer( kver, '2.6.18') >= 0 and src_path == '/dev/ptmx': continue # create node, but only if it exist on host too # except for loop devices, which only show up on the host after they are first used if os.path.exists(src_path) or "loop" in src_path: try: os.mknod(chroot_path, i[0], i[1]) except OSError as e: # If mknod gives us a permission error, fall back to a different # strategy of using a bind mount from root to host. This won't # work for the loop devices, so just skip them in this case. if e.errno == errno.EPERM: if os.path.exists(src_path): self.mounts.add_device_bindmount(src_path) continue else: raise # Further adjustments if we created a new node instead of bind-mounting # an existing one: # set context. (only necessary if host running selinux enabled.) # fails gracefully if chcon not installed. if self.selinux: util.do( ["chcon", "--reference=" + src_path, chroot_path], raiseExc=0, shell=False, env=self.env) if src_path in ('/dev/tty', '/dev/ptmx'): os.chown(chroot_path, pwd.getpwnam('root')[2], grp.getgrnam('tty')[2]) os.symlink("/proc/self/fd/0", self.make_chroot_path("dev/stdin")) os.symlink("/proc/self/fd/1", self.make_chroot_path("dev/stdout")) os.symlink("/proc/self/fd/2", self.make_chroot_path("dev/stderr")) if os.path.isfile(self.make_chroot_path('etc', 'mtab')) or \ os.path.islink(self.make_chroot_path('etc', 'mtab')): os.remove(self.make_chroot_path('etc', 'mtab')) os.symlink("../proc/self/mounts", self.make_chroot_path('etc', 'mtab')) # symlink /dev/fd in the chroot for everything except RHEL4 if util.cmpKernelVer(kver, '2.6.9') > 0: os.symlink("/proc/self/fd", self.make_chroot_path("dev/fd")) os.umask(prevMask) os.symlink("pts/ptmx", self.make_chroot_path('/dev/ptmx'))
def copy(self, src, dst, link=1, touch=0): """Copy a file, a directory or a link. When link is 1 (default), regular files will be hardlinked, as opposed to being copied. When touch is 1, only the file, but not the contents are copied (useful for logfiles). """ if os.path.islink(src): # if it is a symlink, always copy it (no sense in trying # to hardlink a symlink) os.symlink(os.readlink(src), dst) self.copyown(src, dst) self.counter['syms'] += 1 elif os.path.isdir(src): # directories are also copied always os.mkdir(dst) self.copyown(src, dst) shutil.copystat(src, dst) self.counter['drs'] += 1 elif os.path.isfile(src): # this a file, not a dir or symlink if touch: # means create a new file and copy perms open(dst, 'w') self.copyown(src, dst) shutil.copystat(src, dst) self.counter['touchs'] += 1 elif link: # means we should hardlink if vsutil.is_file_immutable_unlink(src): os.link(src, dst) self.counter['lins'] += 1 else: # since it is not iunlink, copy it anyway print 'Warning: not hardlinking %s because it is not iunlink' % src shutil.copy(src, dst) self.copyown(src, dst) shutil.copystat(src, dst) self.counter['bytes'] += os.path.getsize(dst) self.counter['copys'] += 1 else: # else copy it shutil.copy(src, dst) self.copyown(src, dst) shutil.copystat(src, dst) self.counter['bytes'] += os.path.getsize(dst) self.counter['copys'] += 1 else: # this is a special device? s = os.stat(src) if stat.S_ISBLK(s.st_mode) or stat.S_ISCHR(s.st_mode) \ or stat.S_ISFIFO(s.st_mode): os.mknod(dst, s.st_mode, os.makedev(os.major(s.st_rdev), os.minor(s.st_rdev))) self.copyown(src, dst) shutil.copystat(src, dst) self.counter['devs'] += 1
def makedev(space, major, minor): result = os.makedev(major, minor) return space.newint(result)
def setup(self, errors=None): # # something is mounted already ? mnt=self.isMounted() if len(mnt): rim.runCmd(self.log, "mkdir -p %s" % self.mntpt) hostFields=self.getHost().split(":") host=hostFields[0] if len(hostFields) > 1: port=hostFields[1] else: port="3260" # validate the parameters of the iscsi sesssion self.session=iscsiSession(mnt.split()[0].split("/")[-1]) if self.session.address != host or self.session.port != port or self.session.targetname != self.getDir(): self.log("Found new iscsi parameters - remounting") self.log("addr %s - %s " % (self.session.address, host)) self.log("port %s - %s " % (self.session.port, port)) self.log("dir %s - %s " % (self.session.targetname, self.getDir())) self.unMount() else: # # all good - keep it asis return True # # clear to log in and mount mnt=self.isMounted() if not len(mnt): # start the iscsi deamon (in case it is not already started) ok, code = rim.runCmd(self.log, "/etc/init.d/open-iscsi status", errors=errors) if not ok: ok, code = rim.runCmd(self.log, "/etc/init.d/open-iscsi start", errors=errors) if not ok: self.logErr("Failed to start iscsi deamon") return False # make a list of existing iscsi device so we can find which one we created devPaths=self.getDevices() if len(devPaths) == 0: # # discover can take more then a minute if the packet if lost in route self.logErr("Discovery in progress...", "discovery") rim.runCmd(self.log, "iscsiadm -m discovery -t st -p %s" % self.getHost(), errors=errors) # # log in (no CHAP support for now) cmd = "iscsiadm -m node --target=%s -p %s --login" % (self.getDir(), self.getHost()) ok, code = rim.runCmd(self.log, cmd, errors=errors) if not ok: self.logErr("Failed to login to iscsi session") return False # Get the new device path devPaths=self.getDevices() if len(devPaths) != 1: self.logErr("iScsi session device not found %s"% devPaths) # Try to mount the device devName=devPaths[0] self.log("found device '%s'" % devName) # get a handle on that new session self.session=iscsiSession(devName) if not self.session: self.logErr("Failed to find session for device %s" % devName) self.setdown() return False # make sure deive exists in /dev if not os.path.exists("/dev/%s" % devName): try: os.mknod("/dev/%s" % devName, stat.S_IFBLK+0644, os.makedev(int(self.session.major), int(self.session.minor))) except: self.logErr("Failed to create device /dev/%s" % devName) raise self.setdown() return False # check if we see our volume label here out=[] ok, code = rim.runCmd(self.log, "e2label /dev/%s" % devName, out=out) if not ok or out[0] != self.label: self.logErr("e2label failed - new device - curlabel '%s'" % out[0]) if not self.formatAndLabel(devName): self.setdown() return False cmd="mount /dev/%s %s" % (devName, self.mntpt) ok, code = rim.runCmd(self.log, cmd) if not ok: self.log("warning : mount failed - fsck'ing") cmd="fsck -y -t ext3 %s 2>/dev/null 1>&2 " % (device) # fsck will exit with various non-zero values if not rim.runCmd(self.log, cmd, [ 0, 1, 2 ]): self.logErr("Failed to access partition") self.setdown() return False cmd="mount /dev/%s %s" % (devName, self.mntpt) ok, code = rim.runCmd(self.log, cmd) if not ok: self.log("fatal : mount failed after fsck'ing") return False # all good return True
class stat_result_disk: st_mode = 0o60660 st_rdev = os.makedev(8, 64)
def makedev(space, major, minor): result = os.makedev(major, minor) return space.wrap(result)
class InstallDriver(UninstallDriver): """Installs a driver. Note that only drivers with a signature that validates with client_config.DRIVER_SIGNING_CERT can be loaded. """ @staticmethod def InstallDriver(driver_path): """Loads a driver and starts it.""" cmd = ["/sbin/insmod", driver_path] p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.communicate() exit_status = p.returncode logging.info("Loading driver finished, status: %d.", exit_status) if exit_status != 0: raise OSError("Failed to load driver, may already be installed.") def Run(self, args): """Initializes the driver.""" # This action might crash the box so we need to flush the # transaction log. self.SyncTransactionLog() # This will raise if the signature is bad. args.driver.Verify( config_lib.CONFIG["Client.driver_signing_public_key"]) if args.force_reload: try: self.UninstallDriver(args.driver_name) except OSError: logging.warning("Failed to unload driver.") try: fd = tempfile.NamedTemporaryFile() data = args.driver.data if args.mode >= rdfvalue.DriverInstallTemplate.RewriteMode.ENABLE: force = args.mode == rdfvalue.DriverInstallTemplate.RewriteMode.FORCE data = ko_patcher.KernelObjectPatcher().Patch( data, force_patch=force) fd.write(data) fd.flush() except IOError, e: raise IOError("Failed to write driver file %s" % e) try: # Let exceptions pass through. self.InstallDriver(fd.name) try: line = open("/sys/class/misc/%s/dev" % args.driver_name, "r").read().rstrip() major, minor = line.split(":") os.mknod(args.path, stat.S_IFCHR | 0600, os.makedev(int(major), int(minor))) except (IOError, OSError): pass finally: fd.close()
def stat( path, *, dir_fd: int = None, follow_symlinks: bool = True ) -> typing.Union[os.stat_result, stat_result]: """High-level wrapper around the ``statx(2)`` system call, that delegates to :func:`os.stat` on other platforms, but provides `st_birthtime` on Linux.""" def ts_to_nstime(ts): return ts.tv_sec * 1000_000_000 + ts.tv_nsec if not _error: try: stx_flags = AT_STATX_SYNC_AS_STAT if isinstance(path, int): stx_dirfd = path stx_path = b"" stx_flags |= AT_EMPTY_PATH else: stx_dirfd = dir_fd if dir_fd is not None else AT_FDCWD stx_path = os.fsencode(os.fspath(path)) if not follow_symlinks: stx_flags |= AT_SYMLINK_NOFOLLOW stx_result = statx(stx_dirfd, stx_path, stx_flags, Mask.BASIC_STATS | Mask.BTIME) assert (~stx_result.stx_mask.value & (Mask.BASIC_STATS & ~Mask.BLOCKS)) == 0 st_blocks = None st_birthtime = None st_birthtime_ns = None if stx_result.stx_mask.value & Mask.BLOCKS: st_blocks = stx_result.stx_blocks if stx_result.stx_mask.value & Mask.BTIME: st_birthtime = stx_result.stx_btime.tv_sec st_birthtime_ns = ts_to_nstime(stx_result.stx_btime) return stat_result( # Standard struct data stx_result.stx_mode, stx_result.stx_ino, os.makedev(stx_result.stx_dev_major, stx_result.stx_dev_minor), stx_result.stx_nlink, stx_result.stx_uid, stx_result.stx_gid, stx_result.stx_size, stx_result.stx_atime.tv_sec, stx_result.stx_ctime.tv_sec, stx_result.stx_mtime.tv_sec, # Extended (platform-dependant) attributes stx_result.stx_blksize, os.makedev(stx_result.stx_rdev_major, stx_result.stx_rdev_minor), stx_result.stx_attributes, st_blocks, # High-precision timestamps ts_to_nstime(stx_result.stx_atime), ts_to_nstime(stx_result.stx_ctime), ts_to_nstime(stx_result.stx_mtime), # Non-standard birth time value st_birthtime, st_birthtime_ns, ) except NotImplementedError: pass return os.stat(path, dir_fd=dir_fd, follow_symlinks=follow_symlinks)
def make_stat( mode=DEFAULT_MODE, inode=None, dev=None, nlinks=1, gid=None, uid=None, size=0, atime=None, mtime=None, ctime=None, blocks=1, blksize=None, rdev=stat.S_IFREG, flags=0, ): """helper function to generate os.stat results.""" if inode is None: inode = random.randint(1000, 9999999) if dev is None: dev = os.makedev(64, random.randint(1, 100)) if uid is None: uid = os.getuid() if gid is None: uid2 = os.getuid() gid = pwd.getpwuid(uid2).pw_gid if atime is None: atime = time.time() if mtime is None: mtime = time.time() if ctime is None: ctime = time.time() if os.stat_float_times(): ctime = float(ctime) mtime = float(mtime) atime = float(atime) else: ctime = int(ctime) atime = int(atime) mtime = int(mtime) if blksize is None: blksize = max(size, 2048) s = os.stat_result( ( mode, inode, dev, nlinks, gid, uid, size, atime, mtime, ctime, ), { "st_blocks": blocks, "st_blksize": blksize, "st_rdev": rdev, "st_flags": flags, } ) return s
def make_image(project): global bus print "Preparing install image..." xterm_title("Preparing install image") try: repo = project.get_repo() repo_dir = project.image_repo_dir() reposs = os.path.join(project.work_dir, "repo_cache") image_dir = project.image_dir() initrd_image_dir = project.initrd_image_dir() efi_tmp = project.efi_tmp_path_dir() configdir = os.path.join(project.config_files) #umount all mount dirs run('umount %s/proc' % image_dir, ignore_error=True) run('umount %s/sys' % image_dir, ignore_error=True) run('umount -R %s' % image_dir, ignore_error=True) run('umount %s/proc' % initrd_image_dir, ignore_error=True) run('umount %s/sys' % initrd_image_dir, ignore_error=True) run('umount -R %s' % initrd_image_dir, ignore_error=True) run("umount %s" % efi_tmp, ignore_error=True) run("umount -l %s" % efi_tmp, ignore_error=True) image_dir = project.image_dir(clean=True) run('pisi --yes-all -D"%s" ar pisilinux-install %s --ignore-check' % (image_dir, repo_dir + "/pisi-index.xml.bz2")) print "project type = ", project.type root_image_packages = " ".join(project.all_root_image_packages) run('pisi --yes-all --ignore-comar --ignore-dep --ignore-check --ignore-package-conflicts --ignore-file-conflicts -D"%s" it %s' % (image_dir, root_image_packages)) def chrun(cmd): run('chroot "%s" %s' % (image_dir, cmd)) os.mknod("%s/dev/null" % image_dir, 0666 | stat.S_IFCHR, os.makedev(1, 3)) os.mknod("%s/dev/console" % image_dir, 0666 | stat.S_IFCHR, os.makedev(5, 1)) os.mknod("%s/dev/random" % image_dir, 0666 | stat.S_IFCHR, os.makedev(1, 8)) os.mknod("%s/dev/urandom" % image_dir, 0666 | stat.S_IFCHR, os.makedev(1, 9)) path = "%s/usr/share/baselayout/" % image_dir path2 = "%s/etc" % image_dir for name in os.listdir(path): run('cp -p "%s" "%s"' % (os.path.join(path, name), os.path.join(path2, name))) run('/bin/mount --bind /proc %s/proc' % image_dir) run('/bin/mount --bind /sys %s/sys' % image_dir) chrun("/sbin/ldconfig") chrun("/sbin/update-environment") chroot_comar(image_dir) chrun("/usr/bin/pisi configure-pending baselayout") chrun("/usr/bin/pisi configure-pending") connectToDBus(image_dir) obj = bus.get_object("tr.org.pardus.comar", "/package/baselayout") obj.setUser(0, "", "", "", "live", "", dbus_interface="tr.org.pardus.comar.User.Manager") obj.addUser(1000, "pisilive", "livecd", "/home/pisilive", "/bin/bash", "live", [ "wheel", "users", "lp", "lpadmin", "cdrom", "floppy", "disk", "audio", "video", "power", "dialout" ], [], [], dbus_interface="tr.org.pardus.comar.User.Manager") # Make sure environment is updated regardless of the booting system, by setting comparison # files' atime and mtime to UNIX time 1 os.utime(os.path.join(image_dir, "etc/profile.env"), (1, 1)) ############################################################ run("chroot \"%s\" /bin/service dbus start" % image_dir) run("chroot \"%s\" /usr/bin/pisi cp" % image_dir) #KDE KURULAN AYAR #run("cp -rf %s/default-settings/etc/* %s/etc/" % (configdir,image_dir)) #run("cp -rf %s/default-settings/autostart %s/etc/skel/.config/" % (configdir,image_dir)) run("chroot \"%s\" /bin/service dbus stop" % image_dir) path = os.path.join(image_dir, "etc/calamares/modules") if not os.path.exists(path): os.makedirs(path) run("cp -p %s/calamares/modules/* %s/etc/calamares/modules/" % (configdir, image_dir), ignore_error=True) run("cp -p %s/calamares/* %s/etc/calamares/" % (configdir, image_dir), ignore_error=True) run("cp -p %s/live/sudoers/* %s/etc/sudoers.d/" % (configdir, image_dir), ignore_error=True) #PisiLive Config and chmod path = os.path.join(image_dir, "home/pisilive/.config") if not os.path.exists(path): os.makedirs(path) #KDE LİVE AYAR run("cp -rf %s/default-settings/etc/* %s/etc/" % (configdir, image_dir)) run("cp -rf %s/default-settings/autostart %s/etc/skel/.config/" % (configdir, image_dir)) run("cp -p %s/live/kde/.config/* %s/home/pisilive/.config/" % (configdir, image_dir), ignore_error=True) run("cp -rf %s/live/kde/autostart %s/home/pisilive/.config/" % (configdir, image_dir), ignore_error=True) os.system('/bin/chown 1000:wheel "%s/home/pisilive/.config"' % image_dir) os.chmod(path, 0777) run("chroot \"%s\" /bin/service dbus start" % image_dir) run("chroot \"%s\" /usr/bin/pisi cp" % image_dir) run("chroot \"%s\" /bin/service dbus stop" % image_dir) ################################################################# #chrun('killall comar') run('umount %s/proc' % image_dir) run('umount %s/sys' % image_dir) chrun("rm -rf /run/dbus/*") run("rm -rf %s/var/cache/pisi/*" % image_dir) #setup liveuser chrun("rm -rf /etc/sudoers") path1 = os.path.join(image_dir, "etc/sudoers.orig") path2 = os.path.join(image_dir, "etc/sudoers") run('cp "%s" "%s"' % (path1, path2)) run("/bin/echo 'pisilive ALL=(ALL) NOPASSWD: ALL' >> %s/etc/sudoers" % image_dir) run("/bin/chmod 440 %s/etc/sudoers" % image_dir) make_initrd(project) add_repo(project) setup_live_sddm(project) except KeyboardInterrupt: print "Keyboard Interrupt: make_image() cancelled." sys.exit(1)
def does_stuff(n): a = os.major(n) b = os.minor(n) x = os.makedev(a, b) return '%d,%d,%d' % (a, b, x)
class stat_result_partition: st_mode = 0o60660 st_rdev = os.makedev(8, 65)
raise existent = False if not existent: fp = obj.location else: fp = existent_fp = obj.location + "#new" if fs.isreg(obj): obj.data.transfer_to_path(fp) elif fs.issym(obj): os.symlink(obj.target, fp) elif fs.isfifo(obj): os.mkfifo(fp) elif fs.isdev(obj): dev = os.makedev(obj.major, obj.minor) os.mknod(fp, obj.mode, dev) else: ret = spawn([COPY_BINARY, "-Rp", obj.location, fp]) if ret != 0: raise FailedCopy(obj, "got %i from %s -Rp" % ret) ensure_perms(obj.change_attributes(location=fp)) if existent: os.rename(existent_fp, obj.location) return True def do_link(src, trg): try:
def createDevice(id, chroot=""): dev = "%s/dev/loop%i" % (chroot, id) if not os.path.exists(dev): os.mknod(dev, 0600 | stat.S_IFBLK, os.makedev(7, id))
def _mknod(path, name, mode, major, minor): os.mknod(os.path.join(path, name), mode=(stat.S_IMODE(mode) | stat.S_IFCHR), device=os.makedev(major, minor))
def writeFile(self, dirRec, detFile, all_type): """ Write a file to detFile Return 0 means success otherwise failure. """ global file_out if detFile == "" or dirRec == None: sys.stderr.write("can't write file\n") return E_FAILURE #gen.log "write file (%s)"%(detFile) config.status_text = detFile dirname = os.path.dirname(detFile) if not os.path.exists(dirname): try: os.makedirs(dirname, 0o777) except (OSError): sys.stderr.write("can't makedirs\n") return E_FAILURE if all_type == True: # device file if dirRec.rrip != None and (dirRec.rrip.devH != 0 or dirRec.rrip.devL != 0): #fFlag == 0 high = dirRec.rrip.devH low = dirRec.rrip.devL if high == 0: device = os.makedev(os.major(low), os.minor(low)) else: device = os.makedev(high, os.minor(low)) try: mode = dirRec.rrip.fMode & 0o770000 if mode == S_IFCHR: os.mknod(detFile, 0o777 | stat.S_IFCHR, device) elif mode == S_IFBLK: os.mknod(detFile, 0o777 | stat.S_IFBLK, device) except (OSError): sys.stderr.write("can't mknode, maybe no permission\n") return E_DEVICEFILE return E_SUCCESS loc = dirRec.locExtent length = dirRec.lenData self.isoFile.seek(BLOCK_SIZE * loc) #gen.log "file length(%d)"%(length) r_size = BLOCK_SIZE * 1024 * 50 #100M cache try: f_output = open(detFile, 'wb', r_size) except (IOError): sys.stderr.write("can't open{0} for write\n".format(detFile)) return E_FAILURE while True: if length == 0: break elif length <= r_size: r_size = length length = 0 else: length = length - r_size buf = self.isoFile.read(r_size) f_output.write(buf) f_output.flush() # while True end. f_output.close() return E_SUCCESS
def test_major_minor(self): os = self.posix assert os.major(12345) == self.expected_major_12345 assert os.minor(12345) == self.expected_minor_12345 assert os.makedev(self.expected_major_12345, self.expected_minor_12345) == 12345
def encode_dev(field, value): dev = map(lambda x: int(x), value.split(':')) if _marked_as_odev(field): return os.makedev(dev[0], dev[1]) else: return dev[0] << kern_minorbits | dev[1]
def makedev(major, minor): warnings.warn("isys.makedev is deprecated. Use os.makedev instead.", DeprecationWarning, stacklevel=2) return os.makedev(major, minor)
def make_image(project): global bus print "Preparing install image..." xterm_title("Preparing install image") try: repo = project.get_repo() repo_dir = project.image_repo_dir() # image_file = project.image_file() image_dir = project.image_dir() run('umount %s/proc' % image_dir, ignore_error=True) run('umount %s/sys' % image_dir, ignore_error=True) image_dir = project.image_dir(clean=True) run('pisi --yes-all -D"%s" ar pardus-install %s --ignore-check' % (image_dir, repo_dir + "/pisi-index.xml.bz2")) if project.type == "install": if project.all_install_image_packages: install_image_packages = " ".join( project.all_install_image_packages) else: install_image_packages = " ".join(repo.full_deps("yali")) run('pisi --yes-all --ignore-comar -D"%s" it %s' % (image_dir, install_image_packages)) if project.plugin_package: plugin_package = project.plugin_package run('pisi --yes-all --ignore-comar -D"%s" it %s' % (image_dir, plugin_package)) else: install_packages(project) def chrun(cmd): run('chroot "%s" %s' % (image_dir, cmd)) # FIXME: we write static initramfs.conf for live systems for now, hopefully we will make it dynamic later on # Note that this must be done before configure pending for the mkinitramfs use it f = file(os.path.join(image_dir, "etc/initramfs.conf"), "w") f.write("liveroot=LABEL=PardusLive\n") f.close() os.mknod("%s/dev/null" % image_dir, 0666 | stat.S_IFCHR, os.makedev(1, 3)) os.mknod("%s/dev/console" % image_dir, 0666 | stat.S_IFCHR, os.makedev(5, 1)) os.mknod("%s/dev/random" % image_dir, 0666 | stat.S_IFCHR, os.makedev(1, 8)) os.mknod("%s/dev/urandom" % image_dir, 0666 | stat.S_IFCHR, os.makedev(1, 9)) path = "%s/usr/share/baselayout/" % image_dir path2 = "%s/etc" % image_dir for name in os.listdir(path): run('cp -p "%s" "%s"' % (os.path.join(path, name), os.path.join(path2, name))) run('/bin/mount --bind /proc %s/proc' % image_dir) run('/bin/mount --bind /sys %s/sys' % image_dir) chrun("/sbin/ldconfig") chrun("/sbin/update-environment") chroot_comar(image_dir) chrun("/usr/bin/pisi configure-pending baselayout") chrun("/usr/bin/pisi configure-pending") # Disable Nepomuk in live CDs if project.type == "live": try: os.unlink("%s/usr/share/autostart/nepomukserver.desktop" % image_dir) except OSError: pass if project.type == "install": # FIXME: Do not hard code installer name dm_config = "DISPLAY_MANAGER=yali" # Write default display manager config image_dir = project.image_dir() dest = os.path.join(image_dir, "etc/default/xdm") f = file(dest, "w") f.write(dm_config) f.close() connectToDBus(image_dir) obj = bus.get_object("tr.org.pardus.comar", "/package/baselayout") obj.setUser(0, "", "", "", "pardus", "", dbus_interface="tr.org.pardus.comar.User.Manager") if project.type != "install": obj.addUser( 1000, "pars", "Panter Pardus", "/home/pars", "/bin/bash", "pardus", [ "wheel", "users", "lp", "lpadmin", "cdrom", "floppy", "disk", "audio", "video", "power", "dialout" ], [], [], dbus_interface="tr.org.pardus.comar.User.Manager") path1 = os.path.join(image_dir, "usr/share/baselayout/inittab.live") path2 = os.path.join(image_dir, "etc/inittab") os.unlink(path2) run('mv "%s" "%s"' % (path1, path2)) if project.type != "install" and ("kdebase" in project.all_packages): setup_live_kdm(project) setup_live_policykit_conf(project) if project.type == "install": copyPisiIndex(project) # Make sure environment is updated regardless of the booting system, by setting comparison # files' atime and mtime to UNIX time 1 os.utime(os.path.join(image_dir, "etc/profile.env"), (1, 1)) run('umount %s/proc' % image_dir) run('umount %s/sys' % image_dir) except KeyboardInterrupt: print "Keyboard Interrupt: make_image() cancelled." sys.exit(1)
def create_ptsp_rootfs(output_dir, repository, add_pkgs): try: # Add repository of the packages run('pisi --yes-all --destdir="%s" add-repo pardus %s' % (output_dir, repository)) # Install default components, considiring exclusions install_cmd = "pisi --yes-all --ignore-comar --ignore-file-conflicts -D'%s' install" % output_dir for component in COMPONENTS: install_cmd += " -c %s" % component for pattern in PACKAGE_EXCLUDES: install_cmd += " -x %s" % pattern run(install_cmd) # Install default packages for package in PACKAGES.split(): run('pisi --yes-all --ignore-comar --ignore-file-conflicts -D"%s" it %s' % (output_dir, package)) # Install additional packages for package in add_pkgs: run('pisi --yes-all --ignore-comar --ignore-file-conflicts -D"%s" it %s' % (output_dir, package)) # Create /etc from baselayout path = "%s/usr/share/baselayout/" % output_dir path2 = "%s/etc" % output_dir for name in os.listdir(path): run('cp -p "%s" "%s"' % (os.path.join(path, name), os.path.join(path2, name))) # Create character device os.mknod("%s/dev/null" % output_dir, 0666 | stat.S_IFCHR, os.makedev(1, 3)) os.mknod("%s/dev/console" % output_dir, 0666 | stat.S_IFCHR, os.makedev(5, 1)) # Create urandom character device os.mknod("%s/dev/urandom" % output_dir, 0666 | stat.S_IFCHR, os.makedev(1, 9)) # Use proc and sys of the current system run('/bin/mount --bind /proc %s/proc' % output_dir) run('/bin/mount --bind /sys %s/sys' % output_dir) # run command in chroot def chrun(cmd): run('chroot "%s" %s' % (output_dir, cmd)) chrun("/sbin/ldconfig") chrun("/sbin/update-environment") chroot_comar(output_dir) chrun("/bin/service dbus start") chrun("/usr/bin/pisi cp baselayout") chrun("/usr/bin/pisi cp") chroot_call(output_dir, set_root_password) # If not existing, we must create 'pulse' user to run pulseaudio as system wide daemon group_add(output_dir, "pulse") # Create fuse group to get rid of localdev problems when using ldap users. # Also added a new udev rule (65-fuse.rules) to ptsp-client package to update /dev/fuse permissions regarding this change. group_add(output_dir, "fuse") chroot_call(output_dir, make_initramfs) # Create symbolic link kernel_image = glob.glob1("%s/boot" % output_dir, "kernel-*")[0] initramfs = glob.glob1("%s/boot" % output_dir, "initramfs-*")[0] run("ln -s %s %s/boot/latestkernel" % (kernel_image, output_dir)) run("ln -s %s %s/boot/latestinitramfs" % (initramfs, output_dir)) suffix = kernel_image.split("-", 1)[1] chrun("/sbin/depmod -a %s" % suffix) # Now it is Corporate2 release file(os.path.join(output_dir, "etc/pardus-release"), "w").write("Pardus Corporate 2\n") shrink_rootfs(output_dir) # Devices will be created in postinstall of ptsp-server os.unlink("%s/dev/console" % output_dir) os.unlink("%s/dev/null" % output_dir) os.unlink("%s/dev/urandom" % output_dir) shutil.rmtree("%s/lib/udev/devices" % output_dir) run('umount %s/proc' % output_dir) run('umount %s/sys' % output_dir) except KeyboardInterrupt: run('umount %s/proc' % output_dir, ignore_error=True) run('umount %s/sys' % output_dir, ignore_error=True) sys.exit(1)
def copy(src, dst, link=1, touch=0): """Copy a file, a directory or a link. When link is 1 (default), regular files will be hardlinked, as opposed to being copied. When touch is 1, only the file, but not the contents are copied (useful for logfiles). """ global bytes, lins, drs, syms, touchs, copys, devs if os.path.islink(src): # if it is a symlink, always copy it # (no sense in trying to hardlink a symlink) if DRYRUN: print 'ln -s %s %s' % (os.readlink(src), dst) else: os.symlink(os.readlink(src), dst) copyown(src, dst) syms += 1 elif os.path.isdir(src): # directories are also copied always if DRYRUN: s = os.stat(src) print 'mkdir %s; chmod 4%s %s' % (dst, oct(stat.S_IMODE( s.st_mode)), dst) copyown(src, dst) copytime(src, dst) else: os.mkdir(dst) copyown(src, dst) shutil.copystat(src, dst) drs += 1 elif os.path.isfile(src): # this a file, not a dir or symlink if touch: # means create a new file and copy perms if DRYRUN: print 'touch %s' % dst else: open(dst, 'w') copyown(src, dst) shutil.copystat(src, dst) touchs += 1 elif link: # means we should hardlink if DRYRUN: print 'ln %s %s' % (src, dst) else: if vsutil.is_file_immutable_unlink(src): os.link(src, dst) lins += 1 else: # since it is not iunlink, copy it anyway print 'Warning: not hardlinking %s because it is not iunlink' % src shutil.copy(src, dst) copyown(src, dst) shutil.copystat(src, dst) bytes += os.path.getsize(dst) copys += 1 else: # else copy it if DRYRUN: print 'cp -a %s %s' % (src, dst) else: shutil.copy(src, dst) copyown(src, dst) shutil.copystat(src, dst) bytes += os.path.getsize(dst) copys += 1 else: # this is a special device? s = os.stat(src) if stat.S_ISBLK(s.st_mode) or stat.S_ISCHR(s.st_mode) \ or stat.S_ISFIFO(s.st_mode): if DRYRUN: print "mknod %s %o %02x:%02x" % ( dst, s.st_mode, os.major(s.st_rdev), os.minor(s.st_rdev)) else: os.mknod(dst, s.st_mode, os.makedev(os.major(s.st_rdev), os.minor(s.st_rdev))) copyown(src, dst) shutil.copystat(src, dst) devs += 1
def encode_dev(field, value): dev = [int(x) for x in value.split(':')] if _marked_as_odev(field): return os.makedev(dev[0], dev[1]) else: return dev[0] << kern_minorbits | dev[1]