Пример #1
0
 def do_mount(self):
     if self.has_mounted: return True
     if not self.blk_path: Log.e('MiDevice.do_mount block path is "%s" ' % self.blk_path); return False
     # Priority to treat iso virtual device
     if self.fstype == 'iso9660':
         if not cdrom_available(self.blk_path):
             Log.w('MiDevice.do_mount %s type device "%s" cannot be used\n' % (self.fstype, self.blk_path))
             return False
         else:
             isopath = self.blk_path
             mountdir = self.mntdir_fixed and self.mntdir or self.loopmntdir
             ret, errmsg = mount_dev('iso9660', isopath, mountdir, flags='loop')
             if not ret:
                 Log.e("LoMount %s on %s as %s failed: %s" %  (isopath, mountdir, 'iso9660', str(errmsg)))
                 return False
             else:
                 if not self.mntdir_fixed: self.mntdir = self.loopmntdir
                 self.has_mounted = True
                 return True
                 
     # Then treat Harddisk Device
     if not self.mntdir_fixed:
         ret, mntdir = mount_dev(self.fstype, self.blk_path)
     else:
         ret, mntdir = mount_dev(self.fstype, self.blk_path, self.mntdir)
     if not ret:
         Log.e("MiDevice.do_mount Mount %s on %s as %s failed: %s" % \
                       (self.blk_path, mntdir, self.fstype, str(mntdir)))
         return False
     else:
         if not self.mntdir_fixed: self.mntdir = mntdir
         self.has_mounted = True
         return True
Пример #2
0
def logger_copy_logfiles(mia, operid, param):
    def start_operate(title):
        time.sleep(0.2)
        print title

    usb_dev_path, usb_fs_type, logfiles = param
    usb_mount_dir = ""
    steps = 4
    step = 0
    mia.set_step(operid, step, steps)
    # mount the usb device.
    step = step + 1
    mia.set_step(operid, step, steps)
    start_operate('Mount...')
    ret, msg = mount_dev(usb_fs_type, usb_dev_path)
    if not ret:
        return str(msg)
    else:
        usb_mount_dir = msg

    # Copy logfiles to usb.
    step = step + 1
    mia.set_step(operid, step, steps)
    start_operate('Copy logfiles...')
    ret, msg = copy_logfiles(logfiles, os.path.join(usb_mount_dir, "magiclogger"))
    if ret:
        return str(msg)
    
    step = step + 1
    mia.set_step(operid, step, steps)
    start_operate('Sync files...')
    run_bash('/bin/sync')

    # umount the usb device.
    step = step + 1
    mia.set_step(operid, step, steps)
    start_operate('Umount...')
    ret, msg = umount_dev(usb_mount_dir)
    if not ret:
        return str(msg)

    return 0
Пример #3
0
def win_probe(mia, operid, hdpartlist):
    mia.set_step(operid, 0, -1)
    result = []
    all_drives = hdpartlist
    for (device, fstype, new_device) in all_drives:
        dolog('Search %s for Windows files\n' % device)
        if fstype not in CONF_FSTYPE_MAP or \
               CONF_FSTYPE_MAP[fstype][0] not in ('vfat', 'ntfs', 'ntfs-3g'):
            continue
        ret, mntdir = mount_dev(CONF_FSTYPE_MAP[fstype][0], device)
        if ret:
            if os.path.exists(os.path.join(mntdir, 'bootmgr')):
                result.append((new_device, 'vista/7'))
            elif os.path.exists(os.path.join(mntdir, 'ntldr')):
                result.append((new_device, 'winnt'))
            elif os.path.exists(os.path.join(mntdir, 'io.sys')):
                result.append((new_device, 'win98'))
            else:
                 result.append((new_device, 'win'))
            umount_dev(mntdir)
    return result
Пример #4
0
def prepare_grub(mia, operid, timeout, usepassword, password,
                 lba, options, entrylist, default, instpos, bootdev, mbrdev, windev, winfs):
    def get_grub_device_map():
        """The content of grub device map file is:
        (hd0)   /dev/hda
        (hd1)   /dev/hdd

        This function return a map of:
            {'hda': '(hd0)', 'hdd': '(hd1)'
        according to the map file."""
        #dm_file = os.path.join(CONF_TGTSYS_ROOT, 'grub.device.map')
        #os.system('echo quit | /usr/sbin/chroot %s grub --no-floppy --batch --device-map %s' % (CONF_TGTSYS_ROOT, 'grub.device.map'))
        # use grub in mi instead of in tgtsys (no dev file in tgtsys/dev with udev, which cause trouble)
        dm_file = os.path.join('/tmpfs', 'grub.device.map')
        #os.system('echo quit | /sbin/grub --no-floppy --batch --device-map %s' % dm_file)
        os.system('echo quit | grub --no-floppy --batch --device-map %s' % dm_file)
        device_map = {}
        try:
            dmf = file(dm_file, 'r')
            l = dmf.readline()
            while l:
                (grubdev, linuxdev) = string.split(l[:-1], '\t')
                device_map[os.path.basename(linuxdev)] = grubdev
                l = dmf.readline()
            dmf.close()
        except:
            dolog('Read the grub device file %s failed.\n' % dm_file)
        try:
            os.unlink(dm_file)
        except:
            pass
        dolog('The GRUB device map is: %s\n' % str(device_map))
        return  device_map

    def linuxdev2grubdev(devmap, linuxdev):
        """Convert Linux device name to Grub device name, and
        returns a tuple of grub device name and mbr name, e.g.
            /dev/hda1 --> ('(hd0,0)', '(hd0)' )
            /dev/hda  --> ('(hd0,-1)', '(hd0)')
        If not found, return (None, None)"""
        ld = os.path.basename(linuxdev)
        if len(ld) > 3:
            base = ld[:3]
            partnum = int(ld[3:]) -1
        else:
            base = ld
            partnum = -1
        if devmap.has_key(base):
            gd = devmap[base]
            return  ('(%s,%d)' % (gd[1:-1], partnum), gd)
        dolog('%s(%s) is not a key of GRUB device map.\n' % (base, linuxdev))
        return  (None, None)

    mia.set_step(operid, 0, -1)

    device_map = get_grub_device_map()
    dolog('(timeout, usepassword, password, lba, options, default, instpos, bootdev, mbrdev, windev, winfs) = %s\n' % \
          str((timeout, usepassword, password, lba, options, default, instpos, bootdev, mbrdev, windev, winfs)))
    dolog('entrylist = %s\n' % str(entrylist))

    #has_dos = len(entrylist) == 2
    entry_text = ''
    default_number = 0
    number = 0
    for (label, dev, default_or_not) in entrylist:
        #if has_dos and dev == windev:
        if dev == windev:
            text = 'title %s\n' % label
            text = text + '\trootnoverify %s\n' % \
                   (linuxdev2grubdev(device_map, dev)[0])
            text = text + '\tchainloader +1\n'
        else:
            text = 'title %s\n' % label
            if bootdev:
                (grubdev, grubmbrdev) = linuxdev2grubdev(device_map, bootdev)
                grubpath = ''
            else:
                (grubdev, grubmbrdev) = linuxdev2grubdev(device_map, dev)
                grubpath = 'boot'
            # get grub setup device
            grubsetupdev = None
            if instpos == 'mbr':
                grubsetupdev = linuxdev2grubdev(device_map, mbrdev)[1]
            elif instpos == 'boot':
                grubsetupdev = grubdev
            elif instpos == 'win':
                grubsetupdev = windev

            if grubdev == None or grubsetupdev == None:
                return 1 # Install grub failed.

            # init new bootsplash
            text = text + '\troot %s\n' % grubdev
            text = text + '\tkernel %s  ro root=%s %s\n' % \
                   (os.path.join('/', grubpath, CONF_KERNEL_FN), dev, options)
            text = text + '\tinitrd %s\n' % \
                   (os.path.join('/', grubpath, CONF_INITRD_FN))

            # normal graphics
            #text = text + 'title %s (Graphics Mode)\n' % label
            #text = text + '\troot %s\n' % grubdev
            #text = text + '\tkernel %s init 5 ro root=%s %s\n' % \
            #       (os.path.join('/', grubpath, CONF_KERNEL_FN), dev, options)
            #text = text + '\tinitrd %s\n' % \
            #       (os.path.join('/', grubpath, CONF_INITRD_FN))
            # add by yourfeng for init 3 init 1 init 5
            #init console
            #text = text + 'title %s (Console Mode)\n' % label
            #text = text + '\troot %s\n' % grubdev
            #text = text + '\tkernel %s init 3 ro root=%s %s\n' % \
            #        (os.path.join('/', grubpath, CONF_KERNEL_FN), dev, options)
            #text = text + '\tinitrd %s\n' % \
            #       (os.path.join('/', grubpath, CONF_INITRD_FN))
            #init 1
            text = text + 'title %s (Single Mode)\n' % label
            text = text + '\troot %s\n' % grubdev
            text = text + '\tkernel %s single ro root=%s %s\n' % \
                   (os.path.join('/', grubpath, CONF_KERNEL_FN), dev, options)
            text = text + '\tinitrd %s\n' % \
                   (os.path.join('/', grubpath, CONF_INITRD_FN))

        if default_or_not == 'true':
            default_number = number
        number = number + 1
        entry_text = entry_text + text

    # create grubdir
    if instpos == 'win':
        ret, win_mntdir = mount_dev(winfs, windev)
        if not ret:
            return 1
        #grubdir = os.path.join(win_mntdir, 'grub')
        grubdir = win_mntdir        # Put it in the root 
    else:
        grubdir = os.path.join(CONF_TGTSYS_ROOT, 'boot/grub')
    if not os.path.isdir(grubdir):
        os.makedirs(grubdir)

    # Check the grub.conf.bk[N].
    grubconf = os.path.join(grubdir, 'grub.conf')
    if os.path.exists(grubconf):
        # Try to backup the exists grub.conf
        bkcnt = 0
        while 1:
            bkfilename = os.path.join(grubdir, 'grub.conf.bk')
            if bkcnt > 0:
                bkfilename = bkfilename + str(bkcnt)
            if not os.path.exists(bkfilename):
                break
            bkcnt = bkcnt + 1
        try:
            os.rename(grubconf, bkfilename)
        except Exception, errmsg:
            dolog('rename(%s, %s) failed: %s\n' % \
                  (grubconf, bkfilename, str(errmsg)))