def compare(self, src_path, dest_stat): '''see if devs are the same''' if not self.exists: return False # dest_stat is a SyncStat object and it's useless here # I need a real, fresh statbuf that includes st_rdev field try: dest_stat = os.lstat(self.name) except OSError as err: error('error checking %s : %s' % (self.name, err.strerror)) return False src_major = os.major(self.src_stat.st_rdev) src_minor = os.minor(self.src_stat.st_rdev) dest_major = os.major(dest_stat.st_rdev) dest_minor = os.minor(dest_stat.st_rdev) if src_major != dest_major or src_minor != dest_minor: stdout('%s should have major,minor %d,%d but has %d,%d' % (self.name, src_major, src_minor, dest_major, dest_minor)) unix_out('# updating major,minor %s' % self.name) terse(synctool.lib.TERSE_SYNC, self.name) return False return True
def valid_vcsa(vcsa): """Check that the vcsa device and associated terminal seem sane""" vcsa_kernel_major = 7 tty_kernel_major = 4 vcsa_range = range(128, 191) tty_range = range(1, 63) tty = PaperTTY.ttydev(vcsa) vs = os.stat(vcsa) ts = os.stat(tty) vcsa_major, vcsa_minor = os.major(vs.st_rdev), os.minor(vs.st_rdev) tty_major, tty_minor = os.major(ts.st_rdev), os.minor(ts.st_rdev) if not (vcsa_major == vcsa_kernel_major and vcsa_minor in vcsa_range): print("Not a valid vcsa device node: {} ({}/{})".format(vcsa, vcsa_major, vcsa_minor)) return False read_vcsa = os.access(vcsa, os.R_OK) write_tty = os.access(tty, os.W_OK) if not read_vcsa: print("No read access to {} - maybe run with sudo?".format(vcsa)) return False if not (tty_major == tty_kernel_major and tty_minor in tty_range): print("Not a valid TTY device node: {}".format(vcsa)) if not write_tty: print("No write access to {} so cannot set terminal size, maybe run with sudo?".format(tty)) return True
def copy_device(source, target_dir, source_dir="", target=None): if not os.path.isdir(target_dir): raise IOError, "'%s' is no directory." % target_dir if not target: target = source s = "%s/%s" % (source_dir, source) s_linkto = None if os.path.islink(s): s_linkto = os.readlink(s) s = "%s/%s" % (source_dir, s_linkto) stats = os.stat(s) if not stats.st_rdev: raise IOError, "'%s' is no device." % s t = "%s/%s" % (target_dir, target) if os.path.exists(t): return try: if s_linkto: create_dir(target_dir, os.path.dirname(s_linkto)) create_device(target_dir, s_linkto, stats.st_mode, os.major(stats.st_rdev), os.minor(stats.st_rdev)) create_dir(target_dir, os.path.dirname(target)) os.symlink(s_linkto, t) else: create_device(target_dir, target, stats.st_mode, os.major(stats.st_rdev), os.minor(stats.st_rdev)) except Exception, msg: raise IOError, "Unable to copy device '%s': %s" % (s, msg)
def unpack_from(self, block, offset=0): (self.magic, dev, self.ino, self.mode, self.uid, self.gid, self.nlink, rdev, mtimehigh, mtimelow, namesize, filesizehigh, filesizelow) = self.coder.unpack_from(block, offset) self.devmajor = os.major(dev) self.devminor = os.minor(dev) self.rdevmajor = os.major(rdev) self.rdevminor = os.minor(rdev) self.mtime = (mtimehigh << 16) | mtimelow self.filesize = (filesizehigh << 16) | filesizelow namestart = offset + self.coder.size datastart = namestart + namesize self.name = block[namestart:datastart - 1] # drop the null if isinstance(self, CpioMemberBin) and (namesize & 1): datastart += 1 # skip a pad byte if necessary self.content = block[datastart:datastart + self.filesize] return self
def compare(self, _src_path, dest_stat): # type: (str, SyncStat) -> bool '''see if devs are the same''' if not self.exists: return False # dest_stat is a SyncStat object and it's useless here # I need a real, fresh statbuf that includes st_rdev field try: dest_stat = os.lstat(self.name) except OSError as err: error('error checking %s : %s' % (self.name, err.strerror)) return False # Note: mypy triggers false errors here # Also, no luck with Union[SyncStat, posix.stat_result] # In any case, for VNodeChrDev and VNodeBlkDev, # the self.src_stat is of type posix.stat_result src_major = os.major(self.src_stat.st_rdev) # type: ignore src_minor = os.minor(self.src_stat.st_rdev) # type: ignore dest_major = os.major(dest_stat.st_rdev) # type: ignore dest_minor = os.minor(dest_stat.st_rdev) # type: ignore if src_major != dest_major or src_minor != dest_minor: stdout('%s should have major,minor %d,%d but has %d,%d' % (self.name, src_major, src_minor, dest_major, dest_minor)) unix_out('# updating major,minor %s' % self.name) terse(synctool.lib.TERSE_SYNC, self.name) return False return True
def verify(self, entry, modlist): """Verify device entry.""" ondisk = self._exists(entry) if not ondisk: return False # attempt to verify device properties as specified in config rv = True dev_type = entry.get('dev_type') if dev_type in ['block', 'char']: major = int(entry.get('major')) minor = int(entry.get('minor')) if major != os.major(ondisk.st_rdev): msg = ("Major number for device %s is incorrect. " "Current major is %s but should be %s" % (entry.get("name"), os.major(ondisk.st_rdev), major)) self.logger.debug('POSIX: ' + msg) entry.set('qtext', entry.get('qtext', '') + "\n" + msg) rv = False if minor != os.minor(ondisk.st_rdev): msg = ("Minor number for device %s is incorrect. " "Current minor is %s but should be %s" % (entry.get("name"), os.minor(ondisk.st_rdev), minor)) self.logger.debug('POSIX: ' + msg) entry.set('qtext', entry.get('qtext', '') + "\n" + msg) rv = False return POSIXTool.verify(self, entry, modlist) and rv
def unpack_from(self, block, offset=0): (self.magic, dev, ino, mode, uid, gid, nlink, rdev, mtime, namesize, filesize) = self.coder.unpack_from(block, offset) _namesize = namesize self.ino = int(ino, 8) self.mode = int(mode, 8) self.uid = int(uid, 8) self.gid = int(gid, 8) self.nlink = int(nlink, 8) dev = int(dev, 8) rdev = int(rdev, 8) self.devmajor = os.major(dev) self.devminor = os.minor(dev) self.rdevmajor = os.major(rdev) self.rdevminor = os.minor(rdev) self.mtime = int(mtime, 8) namesize = int(namesize, 8) self.filesize = int(filesize, 8) namestart = offset + self.coder.size datastart = namestart + namesize self.name = block[namestart:datastart - 1] # drop the null print('+', _namesize, self.name) self.content = block[datastart:datastart + self.filesize] return self
def convert_stat(st): if S_ISDIR(st.st_mode): return { 'kind': 'dir', 'uid': st.st_uid, 'gid': st.st_gid, 'perm': S_IMODE(st.st_mode) } elif S_ISREG(st.st_mode): return { 'kind': 'file', 'uid': st.st_uid, 'gid': st.st_gid, 'mtime': st.st_mtime, 'ctime': st.st_ctime, 'ino': st.st_ino, 'perm': S_IMODE(st.st_mode) } elif S_ISLNK(st.st_mode): return { 'kind': 'lnk' } elif S_ISSOCK: return { 'kind': 'sock', 'uid': st.st_uid, 'gid': st.st_gid, 'perm': S_IMODE(st.st_mode) } elif S_ISFIFO: return { 'kind': 'fifo', 'uid': st.st_uid, 'gid': st.st_gid, 'perm': S_IMODE(st.st_mode) } elif S_ISBLK: return { 'kind': 'blk', 'uid': st.st_uid, 'gid': st.st_gid, 'devmaj': os.major(st.st_rdev), 'devmin': os.minor(st.st_rdev), 'perm': S_IMODE(st.st_mode) } elif S_ISCHR: return { 'kind': 'chr', 'uid': st.st_uid, 'gid': st.st_gid, 'devmaj': os.major(st.st_rdev), 'devmin': os.minor(st.st_rdev), 'perm': S_IMODE(st.st_mode) } else: raise "Unknown file kind"
def create_volume(self, **params): size = params.get('size') snapshot_id = params.get('snapshot') assert size or snapshot_id, 'Not enough params to create volume' if snapshot_id: snapshot = self.describe_snapshot(snapshot_id) if size: if int(size) < int(snapshot['size']): raise StorageError('Size you specified is smaller than snapshot') else: # Size in Gigabytes size = int(size) id = 'vol-%s' % str(uuid.uuid4())[:7] lvm2.lvcreate(vg_name, name=id, size='%sG' % size) lvinfo = lvm2.lvs(lvm2.lvpath(vg_name, id)).values()[0] device = os.path.realpath(lvinfo.lv_path) if snapshot_id: # Apply snapshot system('dd if=%s of=%s' % (self._get_snapshot_path(snapshot_id), device), shell=True) stat = os.stat(device) maj, min = (os.major(stat.st_rdev), os.minor(stat.st_rdev)) self.volumes[id] = dict(id=id, attached_to=None, maj=maj, min=min, host_path=device, size=str(size), source_snapshot=snapshot_id) return self.volumes[id]
def cache_status(self): if not self.is_cached(): raise ValueError('LV is of wrong type') devpath = self.path s = os.stat(devpath) major, minor = os.major(s.st_rdev), os.minor(s.st_rdev) mapper = find_device('/dev/mapper', major, minor) if mapper is None: raise KeyError('failed to find device mapper entry for ' '%s/%s' % (self.vg.name, self.name)) status = dmsetup('status', mapper) status = dict(zip(cache_status_fields, status.strip().split()[:len(cache_status_fields)])) for k in status.keys(): if status[k].isdigit(): status[k] = int(status[k]) elif '/' in status[k]: a, b = [int(x) for x in status[k].split('/')] status['%s_pct' % k] = (a*1.0/b*1.0)*100 return status
def get_file_systems(): ''' Creates a map of mounted filesystems on the machine. Returns: (major, minor) -> FileSystem(device, mount_point) ''' result = {} if not os.access('/proc/mounts', os.R_OK): return result file = open('/proc/mounts') for line in file: try: device, mount_point, fs_type, fs_options, dummy1, dummy2 = line.split() except ValueError: continue if mount_point.startswith('/dev') or mount_point.startswith('/proc') or mount_point.startswith('/sys'): continue if device.startswith('/') and mount_point.startswith('/'): stat = os.stat(mount_point) major = os.major(stat.st_dev) minor = os.minor(stat.st_dev) result[(major, minor)] = _FileSystem(device, mount_point) file.close() return result
def __call__(self, log, *args, **kwargs): context = pyudev.Context() devices = context.list_devices(subsystem = 'drm') log.debug('DRM devices:') for device in devices: if not device.device_node: continue if 'seat' not in device.tags: continue devno = device.device_number log.debug(' %s (%u, %u)' % (device.device_node, os.major(devno), os.minor(devno))) dev = drm.open(device.device_node) version = dev.GetVersion() log.debug(' Driver:', version.name) log.debug(' Description:', version.description) log.debug(' Version: %u.%u.%u (%s)' % (version.major, version.minor, version.patch, version.date))
def _resolveLoopDevice(path): """ Loop devices appear as the loop device under /proc/mount instead of the backing file. As the mount command does the resolution so must we. """ if not path.startswith("/"): return path try: st = os.stat(path) except: return path if not stat.S_ISBLK(st.st_mode): return path minor = os.minor(st.st_rdev) major = os.major(st.st_rdev) backing_file = os.path.join(_SYS_DEV_BLOCK_PATH, '%d:%d' % (major, minor), 'loop', 'backing_file') try: with open(backing_file, "r") as f: # Remove trailing newline return f.read()[:-1] except IOError as e: if e.errno != errno.ENOENT: raise return path
def detailed_str(meta, fields = None): # FIXME: should optional fields be omitted, or empty i.e. "rdev: # 0", "link-target:", etc. if not fields: fields = all_fields result = [] if 'path' in fields: path = meta.path or '' result.append('path: ' + path) if 'mode' in fields: result.append('mode: %s (%s)' % (oct(meta.mode), xstat.mode_str(meta.mode))) if 'link-target' in fields and stat.S_ISLNK(meta.mode): result.append('link-target: ' + meta.symlink_target) if 'rdev' in fields: if meta.rdev: result.append('rdev: %d,%d' % (os.major(meta.rdev), os.minor(meta.rdev))) else: result.append('rdev: 0') if 'size' in fields and meta.size: result.append('size: ' + str(meta.size)) if 'uid' in fields: result.append('uid: ' + str(meta.uid)) if 'gid' in fields: result.append('gid: ' + str(meta.gid)) if 'user' in fields: result.append('user: '******'group' in fields: result.append('group: ' + meta.group) if 'atime' in fields: # If we don't have xstat.lutime, that means we have to use # utime(), and utime() has no way to set the mtime/atime of a # symlink. Thus, the mtime/atime of a symlink is meaningless, # so let's not report it. (That way scripts comparing # before/after won't trigger.) if xstat.lutime or not stat.S_ISLNK(meta.mode): result.append('atime: ' + xstat.fstime_to_sec_str(meta.atime)) else: result.append('atime: 0') if 'mtime' in fields: if xstat.lutime or not stat.S_ISLNK(meta.mode): result.append('mtime: ' + xstat.fstime_to_sec_str(meta.mtime)) else: result.append('mtime: 0') if 'ctime' in fields: result.append('ctime: ' + xstat.fstime_to_sec_str(meta.ctime)) if 'linux-attr' in fields and meta.linux_attr: result.append('linux-attr: ' + hex(meta.linux_attr)) if 'linux-xattr' in fields and meta.linux_xattr: for name, value in meta.linux_xattr: result.append('linux-xattr: %s -> %s' % (name, repr(value))) if 'posix1e-acl' in fields and meta.posix1e_acl: acl = meta.posix1e_acl[0] result.append('posix1e-acl: ' + acl + '\n') if stat.S_ISDIR(meta.mode): def_acl = meta.posix1e_acl[2] result.append('posix1e-acl-default: ' + def_acl + '\n') return '\n'.join(result)
def get_disk_info(device): if isinstance(device, tuple): major, minor = device else: disk_rdev = os.stat(device).st_rdev major, minor = (os.major(disk_rdev), os.minor(disk_rdev)) device_dir = "/sys/dev/block/%d:%d" % (major, minor) if not os.path.isdir("%s/device" % device_dir): return None # Not representing a pysical device def read(file): filename = os.path.join(device_dir, file) if not os.path.isfile(filename): return None return open(filename).read().strip() rst = {"name":device, "identical_name":"/dev/block/%d:%d" % (major, minor),"device_dir":device_dir, "major":major, "minor":minor, "ro":int(read("ro")),"removable":int(read("removable")),"vendor":read("device/vendor") or read("device/oemid"),"model":read("device/model") or read("device/name")} try: device_capacity = get_device_capacity(device) rst["size"] = device_capacity[0] rst["sector_size"] = device_capacity[1] rst["bios_compatible"] = rst["sector_size"] == 512 and rst["size"] <= MAX_BIOS_FRIENDLY_DISK_SIZE except OSError: pass # e.g. unloaded cd-rom drive if "size" in rst: rst["size_str"] = get_capacity_string(rst["size"]) return rst
def get_unused_devices(): devices = [] if os.path.exists("/dev/mapper/planetlab-rawdisk"): devices.append("/dev/mapper/planetlab-rawdisk") # Figure out which partitions are part of the VG in_vg = [] for i in os.listdir("/sys/block"): if not i.startswith("dm-"): continue in_vg.extend(map(lambda x: x.replace("!", "/"), os.listdir("/sys/block/%s/slaves" % i))) # Read the list of partitions partitions = file("/proc/partitions", "r") pat = re.compile("\s+") while True: buf = partitions.readline() if buf == "": break buf = buf.strip() fields = re.split(pat, buf) dev = fields[-1] if (not dev.startswith("dm-") and dev not in in_vg and os.path.exists("/dev/%s" % dev) and (os.minor(os.stat("/dev/%s" % dev).st_rdev) % 2) != 0): devices.append("/dev/%s" % dev) partitions.close() return devices
def _linux_use_stat(f, log=None): devnum_cache = _devicenum_cache(log=log) dev_cache = {} returndict = {} unknown_paths = [] unknown_cache = {} for path in f: try: blk = os.stat(path)[stat.ST_DEV] dev_major = os.major(blk) dev_minor = os.minor(blk) dn = "{}:{}".format(dev_major, dev_minor) if dn in dev_cache: returndict[path] = dev_cache[dn] elif dn in devnum_cache: dev_cache[dn] = devnum_cache[dn] returndict[path] = dev_cache[dn] else: unknown_paths.append(path) except Exception as _: if sillyfacter.config.STRICT: raise if sillyfacter.config.DEBUG: dp = debugprint() dp("Error in stat of '{}' exception is '{}'". format(path, _), "_linux_use_stat") if log: log.error("Stat failed for path: {}".format(path)) returndict[path] = None if len(unknown_paths) > 0: unknown_cache = _run_df(unknown_paths, df_cmd=["/bin/df", "-P"]) returndict = dict(returndict.items() + unknown_cache.items()) return returndict
def build_dmblock_major_minor_tables(): """Returns 1) a table of filenames that are all device mapper block special files 2) a dict mapping each device mapper name to (major,minor) 3) a dict mapping each (major,minor) pair to a table of devce mapper names""" names = [] name2pair = {} pair2name = {} mapper_entries = [] mapper_entries = os.listdir(DMDIR) for n in mapper_entries: s = os.lstat(DMDIR + '/' + n) if stat.S_ISBLK(s[stat.ST_MODE]): names.append(n) maj = str(os.major(s.st_rdev)) min = str(os.minor(s.st_rdev)) name2pair[n] = (maj, min) pair2name[(maj, min)] = n logging.debug('grabbed dmsetup device info') logging.debug('dmsetup devices: ' + str(name2pair)) return (names, name2pair, pair2name)
def getInfoFromDeviceNumber(self, d): retval = None d = parse_device_number(d) # device_cache = self.db("_device_num_to_list") if d not in device_cache and self._offline is False: try: m = [] with open("/proc/mounts", "r") as f: m = f.readlines() for l in m: if re.search(" (nfs|smbfs|cifs|pnfs|afs) ", l): pass else: mount = l.split()[1] devic = l.split()[0] try: stdev = os.stat(mount).st_dev key = "{},{}".format(os.major(stdev), os.minor(stdev)) device_cache[key] = [devic, mount] except: pass except: pass try: retval = device_cache[d] except: pass return retval
def fromDevice(device): if device.startswith(VlmcTapdisk.TAP_DEV): minor = os.minor(os.stat(device).st_rdev) tapdisks = filter(lambda x: x.minor == minor, VlmcTapdisk.list()) if len(tapdisks) == 1: return tapdisks[0] return None
def __init__( self, path, blocking=True, parent=None ): path = Path( '/dev/uio', path ) self.path = path flags = O_RDWR | O_CLOEXEC if not blocking: flags |= O_NONBLOCK # for irq_recv self._fd = os.open( str(path), flags ) # check parent memory region (if any) if parent is not None: if isinstance( parent, Uio ): parent = parent.region() elif isinstance( parent, MemRegion ): raise TypeError # build path to sysfs dir for obtaining metadata dev = os.stat( self._fd ).st_rdev dev = '{0}:{1}'.format( os.major(dev), os.minor(dev) ) self.syspath = Path('/sys/dev/char', dev).resolve() # enumerate memory regions # beware that if there are none, the dir is absent rather than empty self._regions = {} rgninfo = self.syspath/'maps'; if rgninfo.is_dir(): for info in rgninfo.iterdir(): rgn = MemRegion( self, info, parent ) # allow lookup by index or (if available) by name self._regions[ rgn.index ] = rgn if rgn.name: self._regions[ rgn.name ] = rgn
def get_blkdev_major_minor(path, lookup_for_file=True): """Get 'major:minor' number of block device. Get the device's 'major:minor' number of a block device to control I/O ratelimit of the specified path. If lookup_for_file is True and the path is a regular file, lookup a disk device which the file lies on and returns the result for the device. """ st = os.stat(path) if stat.S_ISBLK(st.st_mode): path, st = _get_disk_of_partition(path, st) return "%d:%d" % (os.major(st.st_rdev), os.minor(st.st_rdev)) elif stat.S_ISCHR(st.st_mode): # No I/O ratelimit control is provided for character devices return None elif lookup_for_file: # lookup the mounted disk which the file lies on out, _err = execute("df", path) devpath = out.split("\n")[1].split()[0] if devpath[0] is not "/": # the file is on a network file system return None return get_blkdev_major_minor(devpath, False) else: msg = _("Unable to get a block device for file '%s'") % path raise exception.Error(msg)
def processTree(tarfd, tree): for child in tree.children.values(): # Create file info info = tarfile.TarInfo() info.name = child.filePath info.mode = stat.S_IMODE(child.st.st_mode) info.type = _STAT_TO_TAR_TYPE[stat.S_IFMT(child.st.st_mode)] info.mtime = child.st.st_mtime info.uid = child.st.st_uid info.gid = child.st.st_gid info.devmajor = os.major(child.st.st_dev) info.devminor = os.minor(child.st.st_dev) # Setup content and links content = None if stat.S_IFMT(child.st.st_mode) == stat.S_IFREG: info.size = child.dataSize content = io.BytesIO(child.getData()) elif stat.S_IFMT(child.st.st_mode) == stat.S_IFLNK: info.linkname = child.getData().decode("UTF-8") # Add file and itd content tarfd.addfile(info, content) # Recursion for directories if stat.S_IFMT(child.st.st_mode) == stat.S_IFDIR: processTree(tarfd, child)
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 _initDMinfo(self): """Check files in /dev/mapper to initialize data sctructures for mappings between device-mapper devices, minor device numbers, VGs and LVs. """ self._mapMinorDmName = {} self._mapMinorLV = {} self._mapLVminor = {} self._vgTree = {} if self._dmMajorNum is None: self._initBlockMajorMap() for file in os.listdir(devmapperDir): path = os.path.join(devmapperDir, file) fstat = os.stat(path) major = os.major(fstat.st_rdev) minor = os.minor(fstat.st_rdev) if major == self._dmMajorNum: self._mapMinorDmName[minor] = file mobj = re.match('(.*[^!])-([^!].*)$', file) if mobj: vg = mobj.group(1) lv = mobj.group(2) self._mapMinorLV[minor] = (vg,lv) self._mapLVminor["-".join((vg,lv))] = minor if not self._vgTree.has_key(vg): self._vgTree[vg] = [] self._vgTree[vg].append(lv)
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 mknod(self, path, mode, device): log.debug('function mknod') full_path, minion_id = self._full_path(path) devtype = str(oct(mode)).replace('L', '') if devtype[:2] == '01': ntype = 'p' elif devtype[:2] == '02': ntype = 'c' elif devtype[:2] == '06': ntype = 'b' else: raise OSError major = os.major(device) minor = os.minor(device) res = self._salt_cmd( minion_id, 'file.mknod', kwarg={ 'name': full_path, 'ntype': ntype, 'major': major, 'minor': minor, }, ) return
def setup_class(cls): cls.space = space cls.w_posix = space.appexec([], GET_POSIX) cls.w_path = space.wrap(str(path)) cls.w_path2 = space.wrap(str(path2)) cls.w_pdir = space.wrap(str(pdir)) try: cls.w_unicode_dir = space.wrap( str(unicode_dir).decode(sys.getfilesystemencoding())) except UnicodeDecodeError: # filesystem encoding is not good enough cls.w_unicode_dir = space.w_None if hasattr(os, 'getuid'): cls.w_getuid = space.wrap(os.getuid()) cls.w_geteuid = space.wrap(os.geteuid()) if hasattr(os, 'getgid'): cls.w_getgid = space.wrap(os.getgid()) if hasattr(os, 'getgroups'): cls.w_getgroups = space.newlist([space.wrap(e) for e in os.getgroups()]) if hasattr(os, 'getpgid'): cls.w_getpgid = space.wrap(os.getpgid(os.getpid())) if hasattr(os, 'getsid'): cls.w_getsid0 = space.wrap(os.getsid(0)) if hasattr(os, 'sysconf'): sysconf_name = os.sysconf_names.keys()[0] cls.w_sysconf_name = space.wrap(sysconf_name) cls.w_sysconf_value = space.wrap(os.sysconf_names[sysconf_name]) cls.w_sysconf_result = space.wrap(os.sysconf(sysconf_name)) cls.w_SIGABRT = space.wrap(signal.SIGABRT) cls.w_python = space.wrap(sys.executable) if hasattr(os, 'major'): cls.w_expected_major_12345 = space.wrap(os.major(12345)) cls.w_expected_minor_12345 = space.wrap(os.minor(12345)) cls.w_udir = space.wrap(str(udir))
def attach_volume(self, name, device, mount_device, static=True): try: s = os.stat(device) if not stat.S_ISBLK(s.st_mode): raise exception.InvalidInput(reason='"%s" is not block device'%device) maj, min = os.major(s.st_rdev), os.minor(s.st_rdev) if not static: # ignore mount_device now self._dynamic_attach_or_detach_volume(name, device, maj, min, attach=True) else: conf_path = lxc_device_conf_file(name, device) with open(conf_path, 'w') as f: for i in range(16): f.write('lxc.cgroup.devices.allow = ' 'b %(maj)s:%(min)s rwm\n'%{'maj':maj, 'min':min+i}) LOG.info(_("new config path %(path)s for %(device)s"), {'path': conf_path, 'device': device}) # autodev hook: # add the partitions of this device into the container when it starts with open(lxc_autodev_hook_script(name, device), 'w') as f, \ open('/proc/partitions', 'r') as p: for line in p: fields = line.split() if fields and fields[-1].startswith(os.path.basename(device)): f.write("mknod --mode=0660 $LXC_ROOTFS_MOUNT/dev/%(device)s " "b %(maj)s %(min)s\n" % { "device": fields[-1], "maj":fields[0], "min":fields[1]}) except Exception as ex: with excutils.save_and_reraise_exception(): LOG.error(_('Failed to attach device %(device)s ' ' for %(name)s: %(ex)s'), {'name': name, 'ex': ex.message, 'device': device})
def _resolveLoopDevice(path): """ Loop devices appear as the loop device under /proc/mount instead of the backing file. As the mount command does the resolution so must we. """ if not path.startswith("/"): return path try: st = os.stat(path) except: return path if not stat.S_ISBLK(st.st_mode): return path minor = os.minor(st.st_rdev) major = os.major(st.st_rdev) loopdir = "/sys/dev/block/%d:%d/loop" % (major, minor) if os.path.exists(loopdir): with open(loopdir + "/backing_file", "r") as f: # Remove trailing newline return f.read()[:-1] # Old kernels might not have the sysfs entry, this is a bit slower and does # not work on hosts that do support the above method. for rec in _iterateMtab(): loopOpt = "loop=%s" % path for opt in rec.fs_mntops: if opt != loopOpt: continue return rec.fs_spec return path
def share_device_with_container_docker(container, node): log_sharing_device(node, "docker", container) container_id = subprocess.check_output( ["docker", "inspect", "--format={{.ID}}", container], text=True ).strip() nodeinfo = os.stat(node) major = os.major(nodeinfo.st_rdev) minor = os.minor(nodeinfo.st_rdev) with open( "/sys/fs/cgroup/devices/docker/%s/devices.allow" % container_id, "w" ) as allow: allow.write("a %d:%d rwm\n" % (major, minor)) subprocess.check_call( [ "docker", "exec", container, "sh", "-c", "mkdir -p %s && mknod %s c %d %d || true" % (os.path.dirname(node), node, major, minor), ] )
def get_containing_device(path, devicetree): """ Return the device that a path resides on. """ if not os.path.exists(path): return None st = os.stat(path) major = os.major(st.st_dev) minor = os.minor(st.st_dev) link = "/sys/dev/block/%s:%s" % (major, minor) if not os.path.exists(link): return None try: device_name = os.path.basename(os.readlink(link)) except Exception: # pylint: disable=broad-except log_exception_info(fmt_str="failed to find device name for path %s", fmt_args=[path]) return None if device_name.startswith("dm-"): # have I told you lately that I love you, device-mapper? device_name = blockdev.dm.name_from_node(device_name) return devicetree.get_device_by_name(device_name)
def _resolveLoopDevice(path): """ Loop devices appear as the loop device under /proc/mount instead of the backing file. As the mount command does the resolution so must we. """ if not path.startswith("/"): return path try: st = os.stat(path) except: return path if not stat.S_ISBLK(st.st_mode): return path minor = os.minor(st.st_rdev) major = os.major(st.st_rdev) loopdir = os.path.join(_SYS_DEV_BLOCK_PATH, '%d:%d' % (major, minor), 'loop') try: with open(loopdir + "/backing_file", "r") as f: # Remove trailing newline return f.read()[:-1] except IOError as e: if e.errno != errno.ENOENT: raise # Old kernels might not have the sysfs entry, this is a bit slower and does # not work on hosts that do support the above method. lookup = _getLoopFsSpecs() if path in lookup: return lookup[path] return path
def devs_to_disks(self, devs=set()): """ If PV is a device map, replace by its sysfs name (dm-*) If device map has slaves, replace by its slaves """ disks = set() dm_major = major('device-mapper') try: md_major = major('md') except: md_major = 0 try: lo_major = major('loop') except: lo_major = 0 for dev in devs: try: statinfo = os.stat(dev) except: self.log.debug("can not stat %s" % dev) continue if md_major != 0 and os.major(statinfo.st_rdev) == md_major: md = dev.replace("/dev/", "") syspath = '/sys/block/' + md + '/slaves' disks |= get_blockdev_sd_slaves(syspath) elif os.major(statinfo.st_rdev) == dm_major: dm = 'dm-' + str(os.minor(statinfo.st_rdev)) syspath = '/sys/block/' + dm + '/slaves' disks |= get_blockdev_sd_slaves(syspath) elif lo_major != 0 and os.major(statinfo.st_rdev) == lo_major: self.log.debug("skip loop device %s from disk list"%dev) pass else: parent = get_partition_parent(dev) if parent is not None: disks.add(parent) else: disks.add(dev) _disks = list(disks) for i, disk in enumerate(_disks): _disks[i] = re.sub("^(/dev/[vhs]d[a-z]*)[0-9]*$", r"\1", disk) return disks
def create_volume(self, **params): size = params.get('size') snapshot_id = params.get('snapshot') assert size or snapshot_id, 'Not enough params to create volume' if snapshot_id: snapshot = self.describe_snapshot(snapshot_id) if size: if int(size) < int(snapshot['size']): raise StorageError( 'Size you specified is smaller than snapshot') else: # Size in Gigabytes size = int(size) id = 'vol-%s' % str(uuid.uuid4())[:7] lvm2.lvcreate(vg_name, name=id, size='%sG' % size) lvinfo = lvm2.lvs(lvm2.lvpath(vg_name, id)).values()[0] device = os.path.realpath(lvinfo.lv_path) if snapshot_id: # Apply snapshot system('dd if=%s of=%s' % (self._get_snapshot_path(snapshot_id), device), shell=True) stat = os.stat(device) maj, min = (os.major(stat.st_rdev), os.minor(stat.st_rdev)) self.volumes[id] = dict(id=id, attached_to=None, maj=maj, min=min, host_path=device, size=str(size), source_snapshot=snapshot_id) return self.volumes[id]
def set_from_stat(self): u"""Set the value of self.type, self.mode from self.stat""" if not self.stat: self.type = None st_mode = self.stat.st_mode if stat.S_ISREG(st_mode): self.type = u"reg" elif stat.S_ISDIR(st_mode): self.type = u"dir" elif stat.S_ISLNK(st_mode): self.type = u"sym" elif stat.S_ISFIFO(st_mode): self.type = u"fifo" elif stat.S_ISSOCK(st_mode): raise PathException( util.fsdecode(self.get_relative_path()) + u"is a socket, unsupported by tar") self.type = u"sock" # pylint: disable=unreachable elif stat.S_ISCHR(st_mode): self.type = u"chr" elif stat.S_ISBLK(st_mode): self.type = u"blk" else: raise PathException(u"Unknown type") self.mode = stat.S_IMODE(st_mode) if self.type in (u"chr", u"blk"): try: self.devnums = (os.major(self.stat.st_rdev), os.minor(self.stat.st_rdev)) except: log.Warn( _(u"Warning: %s invalid devnums (0x%X), treating as (0, 0)." ) % (util.fsdecode( self.get_relative_path()), self.stat.st_rdev)) self.devnums = (0, 0)
def attach_volume(self, context, connection_info, instance, mountpoint, disk_bus=None, device_type=None, encryption=None): """Attach block device to a nova instance. Attaching a block device to a container requires a couple of steps. First os_brick connects the cinder volume to the host. Next, the block device is added to the containers profile. Next, the apparmor profile for the container is updated to allow mounting 'ext4' block devices. Finally, the profile is saved. The block device must be formatted as ext4 in order to mount the block device inside the container. See `nova.virt.driver.ComputeDriver.attach_volume' for more information/ """ profile = self.client.profiles.get(instance.name) protocol = connection_info['driver_volume_type'] storage_driver = brick_get_connector(protocol) device_info = storage_driver.connect_volume( connection_info['data']) disk = os.stat(os.path.realpath(device_info['path'])) vol_id = connection_info['data']['volume_id'] disk_device = { vol_id: { 'path': mountpoint, 'major': '%s' % os.major(disk.st_rdev), 'minor': '%s' % os.minor(disk.st_rdev), 'type': 'unix-block' } } profile.devices.update(disk_device) # XXX zulcss (10 Jul 2016) - fused is currently not supported. profile.config.update({'raw.apparmor': 'mount fstype=ext4,'}) profile.save()
def get_disk_info(device): if isinstance(device, tuple): major, minor = device else: disk_rdev = os.stat(device).st_rdev major, minor = (os.major(disk_rdev), os.minor(disk_rdev)) device_dir = "/sys/dev/block/%d:%d" % (major, minor) if not os.path.isdir("%s/device" % device_dir): return None # Not representing a pysical device def read(file): filename = os.path.join(device_dir, file) if not os.path.isfile(filename): return None return open(filename).read().strip() rst = { "name": device, "identical_name": "/dev/block/%d:%d" % (major, minor), "device_dir": device_dir, "major": major, "minor": minor, "ro": int(read("ro")), "removable": int(read("removable")), "vendor": read("device/vendor") or read("device/oemid"), "model": read("device/model") or read("device/name") } try: device_capacity = get_device_capacity(device) rst["size"] = device_capacity[0] rst["sector_size"] = device_capacity[1] rst["bios_compatible"] = rst["sector_size"] == 512 and rst[ "size"] <= MAX_BIOS_FRIENDLY_DISK_SIZE except OSError: pass # e.g. unloaded cd-rom drive if "size" in rst: rst["size_str"] = get_capacity_string(rst["size"]) return rst
def nfs_aware_chown(path, *args, **kwargs): # NFSv4 is fickle. The protocol might either use numeric UIDs/GIDs, or # (canonically, but not by default) user/group names. The latter requires # the NFS server to know about the user/group in advance. This might go # wrong if we only just created them, especially since NetApp caches # nonexistence for a long time. # # We *try* not to do anything that will cause nonexistence to be cached # (we update NIS and wait a good while for the server before we chown) # but actions elsewhere (e.g. someone trying to manually chown to a # not-yet-existent user) might have already triggered the problem. We # can't do anything about that except give the poor sysadmin a hint. try: os.chown(path, *args, **kwargs) except OSError as e: if e.errno == 22: # EINVAL dev = os.stat(path).st_dev dev_str = "%s:%s" % (os.major(dev), os.minor(dev)) with open("/proc/net/nfsfs/volumes", "r") as f: for line in f: fields = line.split() if fields[3] == dev_str: server = fields[1] ver = fields[0] with open("/proc/net/nfsfs/servers", "r") as ff: for lline in ff: ffields = lline.split() if ffields[1] == server: hostname = ffields[4] raise Exception( "Got EINVAL when attempting to chown(%s) on %s via NFS%s. " "That might mean that the user or group is unknown to the NFS server. " "If this seems wrong, it may have cached nonexistence. " "If it's a NetApp, try 'nfs nsdb flush' on %s, or " "just wait an hour or two then retry." % (path, hostname, ver, hostname)) raise
def do_start_char_devices(self): if not self.create_char_devices: return self.lock() for dev in self.devs: raw = self.find_raw(dev) if raw is not None: self.log.info("%s is already mapped to a raw device" % dev) else: raw = self.find_next_raw() cmd = ['raw', "/dev/raw/" + raw, dev] ret, out, err = self.vcall(cmd) if ret != 0: self.unlock() raise ex.Error s = os.stat("/dev/raw/" + raw) self.raws[raw] = { 'rdev': (os.major(s.st_rdev), os.minor(s.st_rdev)), 'devname': dev, 'bdev': self.devs_t[dev] } self.clear_caches() self.unlock()
def __init__( self, path, blocking=True, parent=None ): path = Path( '/dev/uio', path ) self.path = path if path.is_dir(): path = path / 'module' flags = O_RDWR | O_CLOEXEC if not blocking: flags |= O_NONBLOCK # for irq_recv self._fd = os.open( str(path), flags ) # check parent memory region (if any) if parent is not None: if isinstance( parent, Uio ): parent = parent.region() elif not isinstance( parent, MemRegion ): raise TypeError # build path to sysfs dir for obtaining metadata dev = os.stat( self._fd ).st_rdev dev = '{0}:{1}'.format( os.major(dev), os.minor(dev) ) self.syspath = Path('/sys/dev/char', dev).resolve() # enumerate memory regions # beware that if there are none, the dir is absent rather than empty self._regions = {} rgninfo = self.syspath/'maps' if rgninfo.is_dir(): for info in rgninfo.iterdir(): rgn = MemRegion.from_sysfs( self, info, parent ) # allow lookup by index or (if available) by name self._regions[ rgn.index ] = rgn if rgn.name: self._regions[ rgn.name ] = rgn
def disk_size(self, dev): size = 0 if '/dev/mapper/' in dev: try: statinfo = os.stat(dev) except: raise Exception("can not stat %s" % dev) dm = 'dm-' + str(os.minor(statinfo.st_rdev)) path = '/sys/block/' + dm + '/size' if not os.path.exists(path): return 0 else: path = dev.replace('/dev/', '/sys/block/') + '/size' if not os.path.exists(path): cmd = ['blockdev', '--getsize', dev] out, err, ret = justcall(cmd) if ret != 0: return 0 return int(math.ceil(1. * int(out) / 2048)) with open(path, 'r') as f: size = f.read() f.close() return int(math.ceil(1. * int(size) / 2048))
def get_blkdev_major_minor(path, lookup_for_file=True): """Get the device's "major:minor" number of a block device to control I/O ratelimit of the specified path. If lookup_for_file is True and the path is a regular file, lookup a disk device which the file lies on and returns the result for the device. """ st = os.stat(path) if stat.S_ISBLK(st.st_mode): path, st = _get_disk_of_partition(path, st) return '%d:%d' % (os.major(st.st_rdev), os.minor(st.st_rdev)) elif stat.S_ISCHR(st.st_mode): # No I/O ratelimit control is provided for character devices return None elif lookup_for_file: # lookup the mounted disk which the file lies on out, _err = execute('df', path) devpath = out.split("\n")[1].split()[0] if devpath[0] is not '/': # the file is on a network file system return None return get_blkdev_major_minor(devpath, False) else: msg = _("Unable to get a block device for file \'%s\'") % path raise exception.Error(msg)
def drive_is_ssd(orig_dev): """Check if device (whole drive or a partition) is an SSD""" dev = orig_dev while True: dev = os.path.realpath(dev) st = os.lstat(dev) if not stat.S_ISBLK(st.st_mode): raise ValueError("{}: not a block device".format(dev)) M, m = os.major(st.st_rdev), os.minor(st.st_rdev) sysfs_dir = '/sys/dev/block/{M}:{m}'.format(M=M, m=m) if not os.path.exists(sysfs_dir): raise RuntimeError("device {0}: no sysfs entry {1}".format( dev, sysfs_dir)) rotational = '{}/queue/rotational'.format(sysfs_dir) if os.path.isfile(rotational): break else: base_dev = partition_base_device(dev, abspath=True) if base_dev == dev: return False dev = base_dev with open(rotational, 'r') as f: return int(f.read()) == 0
def mounts(self, detectdev=False): mounts = [] with open('/proc/mounts', 'r') as f: for line in f: dev, path, fstype = line.split()[0:3] # simfs: filesystem in OpenVZ if fstype in ('ext2', 'ext3', 'ext4', 'xfs', 'jfs', 'reiserfs', 'btrfs', 'simfs'): if not os.path.isdir(path): continue mounts.append({'dev': dev, 'path': path, 'fstype': fstype}) for mount in mounts: stat = os.statvfs(mount['path']) total = stat.f_blocks * stat.f_bsize free = stat.f_bfree * stat.f_bsize used = (stat.f_blocks - stat.f_bfree) * stat.f_bsize mount['total'] = b2h(total) mount['free'] = b2h(free) mount['used'] = b2h(used) mount['used_rate'] = div_percent(used, total) if detectdev: dev = os.stat(mount['path']).st_dev mount['major'], mount['minor'] = os.major(dev), os.minor(dev) return mounts
def get_file_systems(self): ''' Creates a map of mounted filesystems on the machine. iostat(1): Each sector has size of 512 bytes. Returns: (major, minor) -> FileSystem(device, mount_point) ''' result = {} if not os.access('/proc/mounts', os.R_OK): return result file = open('/proc/mounts') for line in file: try: device, mount_point, fs_type, fs_options, dummy1, dummy2 = line.split( ) except ValueError: continue if mount_point.startswith('/dev') or mount_point.startswith( '/proc') or mount_point.startswith('/sys'): continue if device.startswith('/') and mount_point.startswith('/'): stat = os.stat(mount_point) major = os.major(stat.st_dev) minor = os.minor(stat.st_dev) result[(major, minor)] = { 'device': device, 'mount_point': mount_point } file.close() return result
def cloneDeviceNode(srcpath, devpath): '''Clone a device node into a temporary private location.''' # we don't use os.remove/mknod/chmod/chown because we need sudo command = ['/bin/rm', '-f', devpath] retcode, out, err = utils.execCmd(command, sudo=True, raw=True) if retcode != 0: sys.stderr.write('directlun: error rm -f %s, err = %s\n' % (devpath, err)) sys.exit(2) stat = os.stat(srcpath) major = os.major(stat.st_rdev) minor = os.minor(stat.st_rdev) command = ['/bin/mknod', devpath, 'b', str(major), str(minor)] retcode, out, err = utils.execCmd(command, sudo=True, raw=True) if retcode != 0: sys.stderr.write('directlun: error mknod %s, err = %s\n' % (devpath, err)) sys.exit(2) mode = '660' command = ['/bin/chmod', mode, devpath] retcode, out, err = utils.execCmd(command, sudo=True, raw=True) if retcode != 0: sys.stderr.write('directlun: error chmod %s to %s, err = %s\n' % (devpath, mode, err)) sys.exit(2) group = grp.getgrnam('qemu') gid = group.gr_gid user = pwd.getpwnam('qemu') uid = user.pw_uid owner = str(uid) + ':' + str(gid) command = ['/bin/chown', owner, devpath] retcode, out, err = utils.execCmd(command, sudo=True, raw=True) if retcode != 0: sys.stderr.write('directlun: error chown %s to %s, err = %s\n' % (devpath, owner, err)) sys.exit(2)
def get_file_systems(self): """ Creates a map of mounted filesystems on the machine. iostat(1): Each sector has size of 512 bytes. Returns: (major, minor) -> FileSystem(device, mount_point) """ result = {} if os.access('/proc/mounts', os.R_OK): file = open('/proc/mounts') for line in file: try: mount = line.split() device = mount[0] mount_point = mount[1] fs_type = mount[2] except (IndexError, ValueError): continue # Skip the filesystem if it is not in the list of valid # filesystems if fs_type not in self.filesystems: self.log.debug( "Ignoring %s since it is of type %s which " + " is not in the list of filesystems.", mount_point, fs_type) continue # Process the filters if self.exclude_reg.search(mount_point): self.log.debug( "Ignoring %s since it is in the " + "exclude_filter list.", mount_point) continue if (mount_point.startswith('/dev') or mount_point.startswith('/proc') or mount_point.startswith('/sys')): continue if '/' in device and mount_point.startswith('/'): try: stat = os.stat(mount_point) major = os.major(stat.st_dev) minor = os.minor(stat.st_dev) except OSError: self.log.debug("Path %s is not mounted - skipping.", mount_point) continue if (major, minor) in result: continue result[(major, minor)] = { 'device': os.path.realpath(device), 'mount_point': mount_point, 'fs_type': fs_type } file.close() else: if not psutil: self.log.error('Unable to import psutil') return None partitions = psutil.disk_partitions(False) for partition in partitions: result[(0, len(result))] = { 'device': os.path.realpath(partition.device), 'mount_point': partition.mountpoint, 'fs_type': partition.fstype } pass return result
#!/usr/bin/python import os, sys path = "foo7.txt" info = os.lstat(path) 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
def get_device_id(device_path): dev_stat = os.stat(device_path) return '{},{}'.format(os.major(dev_stat.st_rdev), os.minor(dev_stat.st_rdev))
def _get_md_name_from_kernel_dev(kdev): minor = os.minor(os.stat(kdev).st_rdev) return next(name for name in os.listdir(DEV_MD) if os.minor(os.stat("%s/%s" % (DEV_MD, name)).st_rdev) == minor)
def MajorMinor(path): rdev = stat(path).st_rdev return (major(rdev), minor(rdev))
def decode_dev(field, value): if _marked_as_odev(field): return "%d:%d" % (os.major(value), os.minor(value)) else: return "%d:%d" % (value >> kern_minorbits, value & ((1 << kern_minorbits) - 1))
def __init__(self, device, removable=False): self.device = device if os.access("/dev/.udev", 0): self.type = DEVTYPE_UDEV elif os.access("/dev/udev_network_queue", 0): self.type = DEVTYPE_UDEV elif os.access("/dev/.devfsd", 0): self.type = DEVTYPE_DEVFS else: print "[Harddisk] Unable to determine structure of /dev" self.type = -1 self.card = False self.max_idle_time = 0 self.idle_running = False self.last_access = time.time() self.last_stat = 0 self.timer = None self.is_sleeping = False self.dev_path = '' self.disk_path = '' self.mount_path = None self.mount_device = None self.phys_path = os.path.realpath(self.sysfsPath('device')) self.removable = removable self.internal = "pci" in self.phys_path or "ahci" in self.phys_path or "sata" in self.phys_path try: data = open("/sys/block/%s/queue/rotational" % device, "r").read().strip() self.rotational = int(data) except: self.rotational = True if self.type == DEVTYPE_UDEV: self.dev_path = '/dev/' + self.device self.disk_path = self.dev_path self.card = "sdhci" in self.phys_path or "mmc" in self.device elif self.type == DEVTYPE_DEVFS: tmp = readFile(self.sysfsPath('dev')).split(':') s_major = int(tmp[0]) s_minor = int(tmp[1]) for disc in os.listdir("/dev/discs"): dev_path = os.path.realpath('/dev/discs/' + disc) disk_path = dev_path + '/disc' try: rdev = os.stat(disk_path).st_rdev except OSError: continue if s_major == os.major(rdev) and s_minor == os.minor(rdev): self.dev_path = dev_path self.disk_path = disk_path break self.card = self.device[:2] == "hd" and "host0" not in self.dev_path print "[Harddisk] new device=%s dev_path=%s disk_path=%s removable=%s internal=%s rotational=%s card=%s" % ( self.device, self.dev_path, self.disk_path, removable, self.internal, self.rotational, self.card) if (self.internal or not removable) and not self.card: self.startIdle()
def checkIfDevice(path, diskstats): st_dev = os.stat(path).st_dev return (os.major(st_dev), os.minor(st_dev)) in diskstats
def dev_maj_min(block_dev): """Returns major/minor device numbers for the given block device.""" dev_stat = os.stat(os.path.realpath(block_dev)) return (os.major(dev_stat.st_rdev), os.minor(dev_stat.st_rdev))
## print root user can run this script uid = os.getuid() if uid != 0: print "You need to be a root user to run this script." sys.exit() ## create raw devices with native devices # get the native device details dev_name = raw_input("Enter device name: ") d = os.stat(dev_name) # Get major and minor numbers for the device mj_num = os.major(d.st_rdev) print "Device major number is", mj_num mi_num = os.minor(d.st_rdev) print "Device minor number is", mi_num # Create raw device raw_dev = os.system("raw dev_name mj_num mi_num") print raw_dev # Update details into udev # An example would be: # ACTION=="add", KERNEL=="sda", RUN+="/usr/bin/raw /dev/raw/raw1 %N" # to bind /dev/raw/raw1 to /dev/sda, or # ACTION=="add", ENV{MAJOR}=="8", ENV{MINOR}=="1", RUN+="/usr/bin/raw /dev/raw/raw2 %M %m" # to bind /dev/raw/raw2 to the device with major 8, minor 1.
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 test_mknod(self): self.vol.mknod("dev", stat.S_IFCHR | 0o644, os.makedev(1, 3)) st = self.vol.stat("dev") self.assertTrue(stat.S_ISCHR(st.st_mode)) self.assertEqual(os.major(st.st_rdev), 1) self.assertEqual(os.minor(st.st_rdev), 3)
def get_file_struct(self, fs_path, new_level=False): """Generate file meta data from file abs path. Return the meta data as a dict structure and a binary string :param fs_path: file abs path :param new_level :return: file data structure """ # Get file inode information, whether the file is a regular # file or a symbolic link try: os_stat = os.lstat(fs_path) except (OSError, IOError) as error: raise Exception('[*] Error on file stat: {}'.format(error)) file_mode = os_stat.st_mode # Get file type. If file type is a link it returns also the # file pointed by the link file_type, lname = self.get_file_type(file_mode, fs_path) # If file_type is a socket return False if file_type == 's': return False, False ctime = int(os_stat.st_ctime) mtime = int(os_stat.st_mtime) uname = pwd.getpwuid(os_stat.st_uid)[0] gname = grp.getgrgid(os_stat.st_gid)[0] dev = os_stat.st_dev inumber = os_stat.st_ino nlink = os_stat.st_nlink uid = os_stat.st_uid gid = os_stat.st_gid size = os_stat.st_size devmajor = os.major(dev) devminor = os.minor(dev) level_id = '0000' if new_level: level_id = '1111' # build file meta data as dictionary inode_dict = { 'inode': { 'inumber': inumber, 'nlink': nlink, 'mode': file_mode, 'uid': uid, 'gid': gid, 'size': size, 'devmajor': devmajor, 'devminor': devminor, 'mtime': mtime, 'ctime': ctime, 'uname': uname, 'gname': gname, 'ftype': file_type, 'lname': lname, 'rsync_block_size': RSYNC_BLOCK_SIZE, 'level_id': level_id, 'deleted': '0000' } } # build file meta data as binary string inode_bin_str = (b'{}\00{}\00{}\00{}\00{}' b'\00{}\00{}\00{}\00{}\00{}' b'\00{}\00{}\00{}\00{}\00{}\00{}\00{}\00{}').format( RSYNC_DATA_STRUCT_VERSION, file_mode, uid, gid, size, mtime, ctime, uname, gname, file_type, lname, inumber, nlink, devminor, devmajor, RSYNC_BLOCK_SIZE, level_id, '0000') return inode_dict, inode_bin_str