def do_mount(self): if self.has_mounted: return True if not self.blk_path: logger.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): logger.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: logger.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: logger.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
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
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 CF.D.FSTYPE_MAP or \ CF.D.FSTYPE_MAP[fstype][0] not in ('vfat', 'ntfs', 'ntfs-3g'): continue ret, mntdir = mount_dev(CF.D.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
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(CF.D.TGTSYS_ROOT, 'grub.device.map') #os.system('echo quit | /usr/sbin/chroot %s grub --no-floppy --batch --device-map %s' % (CF.D.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() # eg. (10, 'false', '', 'false', 'vga=791 splash=silent,fadein,theme:default console=tty1', '', 'mbr', '', '/dev/sda', '', '') 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))) # eg. entrylist = [['', '/dev/sda2', 'true']] 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') # 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, CF.D.TARGET_KERNEL_FN), dev, options) text = text + '\tinitrd %s\n' % \ (os.path.join('/', grubpath, CF.D.TARGET_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, CF.D.TARGET_KERNEL_FN), dev, options) #text = text + '\tinitrd %s\n' % \ # (os.path.join('/', grubpath, CF.D.TARGET_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, CF.D.TARGET_KERNEL_FN), dev, options) #text = text + '\tinitrd %s\n' % \ # (os.path.join('/', grubpath, CF.D.TARGET_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, CF.D.TARGET_KERNEL_FN), dev, options) text = text + '\tinitrd %s\n' % \ (os.path.join('/', grubpath, CF.D.TARGET_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, _('Install position on windows partition(%s) failed!' % windev) #grubdir = os.path.join(win_mntdir, 'grub') grubdir = win_mntdir # Put it in the root else: grubdir = os.path.join(CF.D.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)))
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(CF.D.TGTSYS_ROOT, 'grub.device.map') #os.system('echo quit | /usr/sbin/chroot %s grub --no-floppy --batch --device-map %s' % (CF.D.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() # eg. (10, 'false', '', 'false', 'vga=791 splash=silent,fadein,theme:default console=tty1', '', 'mbr', '', '/dev/sda', '', '') 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))) # eg. entrylist = [['', '/dev/sda2', 'true']] 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') # 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, CF.D.TARGET_KERNEL_FN), dev, options) text = text + '\tinitrd %s\n' % \ (os.path.join('/', grubpath, CF.D.TARGET_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, CF.D.TARGET_KERNEL_FN), dev, options) #text = text + '\tinitrd %s\n' % \ # (os.path.join('/', grubpath, CF.D.TARGET_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, CF.D.TARGET_KERNEL_FN), dev, options) #text = text + '\tinitrd %s\n' % \ # (os.path.join('/', grubpath, CF.D.TARGET_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, CF.D.TARGET_KERNEL_FN), dev, options) text = text + '\tinitrd %s\n' % \ (os.path.join('/', grubpath, CF.D.TARGET_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, _('Install position on windows partition(%s) failed!' % windev) #grubdir = os.path.join(win_mntdir, 'grub') grubdir = win_mntdir # Put it in the root else: grubdir = os.path.join(CF.D.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)))
def mount_tgt_device(self): tgt_root_dev, tgt_root_type = self.get_devinfo_by_mpoint('/') tgt_swap_dev, tgt_swap_type = self.get_devinfo_by_mpoint('USE') if tgt_swap_dev and os.path.exists(tgt_swap_dev): os.system('swapon %s' % tgt_swap_dev) #### mount root device mnt_point = CF.D.TGTSYS_ROOT dev_tgt_root = MiDevice(tgt_root_dev, tgt_root_type, mnt_point) if not dev_tgt_root.do_mount(): #### NOTE: carefully handle this device's mount. logger.e('Mount device %s Failed, install operate can not continue' % dev_tgt_root.get_dev()) return False else: self.mounted_devs.append(dev_tgt_root) def mount_each(tgt_root_dev, tgt_other_dev, tgt_other_type, tgt_other_mpoint): if tgt_other_dev and tgt_root_dev != tgt_other_dev: mnt_point = os.path.join(CF.D.TGTSYS_ROOT, tgt_other_mpoint.lstrip('/')) dev_tgt_other = MiDevice(tgt_other_dev, tgt_other_type, mnt_point) if not dev_tgt_other.do_mount(): #### NOTE: carefully handle this device's mount. logger.e('Mount device %s Failed, install operate can not continue' % dev_tgt_other.get_dev()) self.umount_tgt_device() return False else: self.mounted_devs.append(dev_tgt_other) return True else: logger.e('Can not run to here!! The root file system had been mounted before!!') return False for mpoint, dev_, type_ in self.tgtsys_devinfo: if mpoint not in ('/', 'USE'): if not mount_each(tgt_root_dev, dev_, type_, mpoint): return False # mount -t proc proc myroot/proc/ mnt_point = os.path.join(CF.D.TGTSYS_ROOT, 'proc') ret, msg = mount_dev('proc', 'proc', mntdir=mnt_point,flags=None) if not ret: logger.e("Error: mount proc on target failed '%s'" % msg) return False self.mounted_devs.append(mnt_point) # mount -t sysfs sys myroot/sys/ mnt_point = os.path.join(CF.D.TGTSYS_ROOT, 'sys') ret, msg = mount_dev('sysfs', 'sys', mntdir=mnt_point,flags=None) if not ret: logger.e("Error: mount sys on target failed '%s'" % msg) return False self.mounted_devs.append(mnt_point) # mount -o bind /dev myroot/dev/ mnt_point = os.path.join(CF.D.TGTSYS_ROOT, 'dev') ret, msg = mount_dev(None, '/dev', mntdir=mnt_point,flags='bind') if not ret: logger.e("Error: mount /dev on target failed '%s'" % msg) return False self.mounted_devs.append(mnt_point) res = run_bash('mount') logger.d(''''Mount target system partition finished, check the mount output please:) %s %s ''' % ('\n'.join(res['out']), '\n'.join(res['err']))) return True