Example #1
0
 def setup(self, options, block_bytes=512):
   """setup a new geometry by giving options
      return bytes required by geometry or None if geometry is invalid
   """
   if options == None:
     return False
   c = None
   h = None
   s = None
   (c, h, s) = self._parse_chs(options)
   # chs is fully specified
   if c != None and h != None and s != None:
     self.cyls = c
     self.heads = h
     self.secs = s
     return self.get_num_bytes()
   else:
     # we require a size
     if not options.has_key('size'):
       return None
     # parse size
     size = options['size']
     if type(size) != int:
       size = ByteSize.parse_byte_size_str(size)
       if size == None:
         return None
     # select guess algo
     algo = None
     if options.has_key('algo'):
       algo = int(options['algo'])
     # guess size
     return self._guess_for_size(size, approx=True, algo=algo, secs=s, heads=h)  
Example #2
0
  def get_cyl_range(self):
    start = None
    if self.popts.has_key('start'):
      start = int(self.popts['start'])
    # range with start=<n> end=<n>
    if self.popts.has_key('end'):
      end = int(self.popts['end'])
      if start == None or end <= start:
        return None
      else:
        return (start, end)
    # expect a size
    elif self.popts.has_key('size'):
      size = self.popts['size']
      cyls = None
      if type(size) == int:
        cyls = size
      # size in bytes
      elif size[-1] in ('b','B'):
        bytes = ByteSize.parse_byte_size_str(size[:-1])
        if bytes == None:
          return None
        cyls = bytes / self.rdisk.get_cylinder_bytes()
      # size in percent
      elif size[-1] == '%':
        prc = float(size[:-1])
        cyls = int(prc * self.rdisk.get_logical_cylinders() / 100.0)
      # size in cylinders
      else:
        cyls = ByteSize.parse_byte_size_str(size)

      # check cyls
      if cyls == None or cyls < 1:
        return None
      # find a range if no start is given
      if start == None:
        start = self.rdisk.find_free_cyl_range_start(cyls)
        if start == None:
          return None
      return (start, start + cyls - 1)
    # nothing specified -> get next free range
    else:
      ranges = self.rdisk.get_free_cyl_ranges()
      if ranges == None:
        return None
      return ranges[0]
Example #3
0
    def get_cyl_range(self):
        start = None
        if 'start' in self.popts:
            start = int(self.popts['start'])
        # range with start=<n> end=<n>
        if 'end' in self.popts:
            end = int(self.popts['end'])
            if start == None or end <= start:
                return None
            else:
                return (start, end)
        # expect a size
        elif 'size' in self.popts:
            size = self.popts['size']
            cyls = None
            if type(size) == int:
                cyls = size
            # size in bytes
            elif size[-1] in ('b', 'B'):
                num_bytes = ByteSize.parse_byte_size_str(size[:-1])
                if num_bytes == None:
                    return None
                cyls = num_bytes // self.rdisk.get_cylinder_bytes()
            # size in percent
            elif size[-1] == '%':
                prc = float(size[:-1])
                cyls = int(prc * self.rdisk.get_logical_cylinders() / 100.0)
            # size in cylinders
            else:
                cyls = ByteSize.parse_byte_size_str(size)

            # check cyls
            if cyls == None or cyls < 1:
                return None
            # find a range if no start is given
            if start == None:
                start = self.rdisk.find_free_cyl_range_start(cyls)
                if start == None:
                    return None
            return (start, start + cyls - 1)
        # nothing specified -> get next free range
        else:
            ranges = self.rdisk.get_free_cyl_ranges()
            if ranges == None:
                return None
            return ranges[0]
Example #4
0
 def setup(self, options, cyls=None, heads=None, sectors=None):
     """setup a new geometry by giving options
     return bytes required by geometry or None if geometry is invalid
     """
     if options is None:
         return False
     (c, h, s) = self._parse_chs(options)
     if not c:
         c = cyls
     if not h:
         h = heads
     if not s:
         s = sectors
     self._update_block_size(options)
     # chs is fully specified
     if c and h and s:
         self.cyls = c
         self.heads = h
         self.secs = s
         return self.get_num_bytes()
     else:
         # fetch size from another image
         if "from" in options:
             file_name = options["from"]
             size = ImageFile.get_image_size(file_name)
         # we require a size
         elif "size" in options:
             # parse size
             size = options["size"]
             if type(size) != int:
                 size = ByteSize.parse_byte_size_str(size)
                 if size is None:
                     return None
         # no size given
         else:
             return None
         # select guess algo
         algo = None
         if "algo" in options:
             algo = int(options["algo"])
         # guess size
         return self._guess_for_size(size,
                                     approx=True,
                                     algo=algo,
                                     secs=s,
                                     heads=h)
Example #5
0
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
Example #6
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
Example #7
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