def init_blkdev(self, file_name): # do not overwrite an existing image file if not os.path.exists(file_name): raise IOError("Image File does not exist: '%s'" % file_name) # make sure size is given if len(self.opts) < 1: print( "Usage: resize ( size=<n> | chs=<c,h,s> | from=<img> ) [bs=<n>]" ) return None # determine disk geometry opts = KeyValue.parse_key_value_strings(self.opts) geo = DiskGeometry() if not geo.setup(opts): raise IOError("Can't set geometry of disk: '%s'" % file_name) # grow or shrink image file cur_size = ImageFile.get_image_size(file_name) new_size = geo.get_num_bytes() if cur_size == new_size: print("Image size unchanged") elif cur_size < new_size: print("Growing image") else: print("Shrinking image") new_blocks = new_size // geo.block_bytes blkdev = RawBlockDevice(file_name, block_bytes=geo.block_bytes) blkdev.resize(new_blocks) blkdev.geo = geo return blkdev
def handle_rdisk(self, rdisk): # arguments if len(self.opts) < 1: print("Usage: remap secs=<secs> heads=<heads>") return None opts = KeyValue.parse_key_value_strings(self.opts) geo = DiskGeometry() c, h, s = rdisk.get_cyls_heads_secs() if not geo.setup(opts, cyls=c, heads=h, sectors=s): raise ValueError("Can't set new geometry!") if geo.cyls != c: raise ValueError("Do not change cylinders!") print("Remap to", geo.heads, "heads and", geo.secs, "sectors") rdisk.remap(geo.heads, geo.secs) return 0
def init_blkdev(self, file_name): # make sure image file exists if not os.path.exists(file_name): raise IOError("Image File not found: '%s'" % file_name) # open existing raw block device blkdev = RawBlockDevice(file_name, self.args.read_only) blkdev.open() # try to guess geometry geo = DiskGeometry() num_blocks = blkdev.num_blocks block_bytes = blkdev.block_bytes opts = KeyValue.parse_key_value_strings(self.opts) if geo.detect(num_blocks * block_bytes, opts) == None: raise IOError("Can't detect geometry of disk: '%s'" % file_name) blkdev.geo = geo return blkdev
def init_blkdev(self, file_name): # do not overwrite an existing image file if os.path.exists(file_name) and not self.args.force: raise IOError("Image File already exists: '%s'" % file_name) # make sure size is given if len(self.opts) < 1: print("Usage: create ( size=<n> | chs=<c,h,s> ) [bs=<n>]") return None # determine disk geometry opts = KeyValue.parse_key_value_strings(self.opts) geo = DiskGeometry() if not geo.setup(opts): raise IOError("Can't set geometry of disk: '%s'" % file_name) # create new empty image file for geometry blkdev = RawBlockDevice(file_name, block_bytes=geo.block_bytes) blkdev.create(geo.get_num_blocks()) blkdev.geo = geo return blkdev
def init_blkdev(self, file_name): # make sure image file exists if not os.path.exists(file_name): raise IOError("Image File not found: '%s'" % file_name) # parse opts opts = KeyValue.parse_key_value_strings(self.opts) # is a block size given in options? if yes then enforce it bs = 512 opts_bs = self._get_opts_block_size(opts) if opts_bs: bs = opts_bs # setup initial raw block dev with default block size blkdev = RawBlockDevice(file_name, self.args.read_only, block_bytes=bs) blkdev.open() # if no bs was given in options then try to find out block size # from an existing rdb if not opts_bs: rd = RDisk(blkdev) peek_bs = rd.peek_block_size() # real block size differs: re-open dev with correct size if peek_bs and peek_bs != blkdev.block_bytes: blkdev.close() blkdev = RawBlockDevice(file_name, self.args.read_only, block_bytes=peek_bs) blkdev.open() bs = peek_bs # try to guess geometry file_size = blkdev.num_blocks * blkdev.block_bytes geo = DiskGeometry(block_bytes=bs) if not geo.detect(file_size, opts): raise IOError("Can't detect geometry of disk: '%s'" % file_name) # make sure block size is still the same if geo.block_bytes != bs: raise IOError("Invalid geo block size chosen: %d" % geo.block_bytes) # keep geo blkdev.geo = geo return blkdev
def main(args=None): if not args: a = sys.argv else: a = args n = len(a) if n < 3: print( "Usage: (detect <size|file> [options] | setup <options> | open <file> [options] | create <file> <options>)" ) print("""Options: size=<size> chs=<n>,<n>,<n> c=<n> h=<n> s=<n> algo=1|2 """) return 1 else: cmd = a[1] # detect disk geometry from given image file if cmd == "detect": if os.path.exists(a[2]): # its a file size = os.path.getsize(a[2]) else: # parse size string size = ByteSize.parse_byte_size_str(a[2]) if size == None: print("Invalid size!") else: d = DiskGeometry() opts = None if n > 3: opts = KeyValue.parse_key_value_strings(a[3:]) print("size: ", size) print("opts: ", opts) size = d.detect(size, opts) if size != None: print("geo: ", d) else: print("FAILED") # setup a new disk geometry from options elif cmd == "setup": d = DiskGeometry() opts = KeyValue.parse_key_value_strings(a[2:]) print("opts: ", opts) size = d.setup(opts) if size != None: print("setup: ", size, ByteSize.to_byte_size_str(size)) print("geo: ", d) else: print("FAILED") # open a blkdev and detect geometry elif cmd == "open": opts = None if n > 3: opts = KeyValue.parse_key_value_strings(a[3:]) print("opts: ", opts) f = BlkDevFactory() blkdev = f.open(a[2], options=opts) if blkdev != None: print("blkdev: ", blkdev.__class__.__name__) print("geo: ", blkdev.get_geometry()) blkdev.close() else: print("FAILED") # create a new blkdev with setup geometry elif cmd == "create": opts = KeyValue.parse_key_value_strings(a[3:]) print("opts: ", opts) f = BlkDevFactory() blkdev = f.create(a[2], options=opts) if blkdev != None: print("blkdev: ", blkdev.__class__.__name__) print("geo: ", blkdev.get_geometry()) blkdev.close() else: print("FAILED") return 0
def main(): a = sys.argv n = len(a) if n < 3: print "Usage: (detect <size|file> [options] | setup <options> | open <file> [options] | create <file> <options>)" print """Options: size=<size> chs=<n>,<n>,<n> c=<n> h=<n> s=<n> algo=1|2 """ return 1 else: cmd = a[1] # detect disk geometry from given image file if cmd == 'detect': if os.path.exists(a[2]): # its a file size = os.path.getsize(a[2]) else: # parse size string size = ByteSize.parse_byte_size_str(a[2]) if size == None: print "Invalid size!" else: d = DiskGeometry() opts = None if n > 3: opts = KeyValue.parse_key_value_strings(a[3:]) print "size: ", size print "opts: ", opts size = d.detect(size, opts) if size != None: print "geo: ", d else: print "FAILED" # setup a new disk geometry from options elif cmd == 'setup': d = DiskGeometry() opts = KeyValue.parse_key_value_strings(a[2:]) print "opts: ", opts size = d.setup(opts) if size != None: print "setup: ", size, ByteSize.to_byte_size_str(size) print "geo: ", d else: print "FAILED" # open a blkdev and detect geometry elif cmd == 'open': opts = None if n > 3: opts = KeyValue.parse_key_value_strings(a[3:]) print "opts: ", opts f = BlkDevFactory() blkdev = f.open(a[2], options=opts) if blkdev != None: print "blkdev: ", blkdev.__class__.__name__ print "geo: ", blkdev.get_geometry() blkdev.close() else: print "FAILED" # create a new blkdev with setup geometry elif cmd == 'create': opts = KeyValue.parse_key_value_strings(a[3:]) print "opts: ", opts f = BlkDevFactory() blkdev = f.create(a[2], options=opts) if blkdev != None: print "blkdev: ", blkdev.__class__.__name__ print "geo: ", blkdev.get_geometry() blkdev.close() else: print "FAILED" return 0
def main(): a = sys.argv n = len(a) if n < 3: print "Usage: (detect <size|file> [options] | setup <options> | open <file> [options] | create <file> <options>)" print """Options: size=<size> chs=<n>,<n>,<n> c=<n> h=<n> s=<n> algo=1|2 """ return 1 else: cmd = a[1] # detect disk geometry from given image file if cmd == 'detect': if os.path.exists(a[2]): # its a file size = os.path.getsize(a[2]) else: # parse size string size = ByteSize.parse_byte_size_str(a[2]) if size == None: print "Invalid size!" else: d = DiskGeometry() opts = None if n > 3: opts = KeyValue.parse_key_value_strings(a[3:]) print "size: ",size print "opts: ",opts size = d.detect(size, opts) if size != None: print "geo: ",d else: print "FAILED" # setup a new disk geometry from options elif cmd == 'setup' : d = DiskGeometry() opts = KeyValue.parse_key_value_strings(a[2:]) print "opts: ",opts size = d.setup(opts) if size != None: print "setup: ",size,ByteSize.to_byte_size_str(size) print "geo: ",d else: print "FAILED" # open a blkdev and detect geometry elif cmd == 'open': opts = None if n > 3: opts = KeyValue.parse_key_value_strings(a[3:]) print "opts: ",opts f = BlkDevFactory() blkdev = f.open(a[2], options=opts) if blkdev != None: print "blkdev: ",blkdev.__class__.__name__ print "geo: ",blkdev.get_geometry() blkdev.close() else: print "FAILED" # create a new blkdev with setup geometry elif cmd == 'create': opts = KeyValue.parse_key_value_strings(a[3:]) print "opts: ",opts f = BlkDevFactory() blkdev = f.create(a[2], options=opts) if blkdev != None: print "blkdev: ",blkdev.__class__.__name__ print "geo: ",blkdev.get_geometry() blkdev.close() else: print "FAILED" return 0