def restoreImageToVolume(self, targetVolume): # ---- validate input and sanity check # targetVolume should be a container of type volume (not dmg) if not hasattr(targetVolume, 'isContainerType') or not targetVolume.isContainerType('volume') or targetVolume.isContainerType('dmg'): raise ValueError('Unable to restore onto: ' + str(targetVolume)) if self.outputFilePath is None: raise RuntimeError('Restoring a volume requires that the image have been built') # ---- process # unmount the volume targetVolume.unmount() # restore the image onto the volume by bsd path asrCommand = ['/usr/sbin/asr', 'restore', '--verbose', '--source', self.outputFilePath, '--target', targetVolume.bsdPath, '--erase', '--noprompt'] managedSubprocess(asrCommand) # bless the volume so that it is bootable blessCommand = ['/usr/sbin/bless', '--device', targetVolume.bsdPath, '--verbose'] managedSubprocess(blessCommand) # bless the volume so that it is the nextboot device blessCommand = ['/usr/sbin/bless', '--device', targetVolume.bsdPath, '--setBoot', '--nextonly', '--verbose'] managedSubprocess(blessCommand) # reboot rebootCommand = ['/usr/bin/osascript', '-e', 'tell application "System Events" to restart'] managedSubprocess(rebootCommand)
def restoreImageToVolume(self, targetVolume): # ---- validate input and sanity check # targetVolume should be a container of type volume (not dmg) if not hasattr(targetVolume, 'isContainerType') or not targetVolume.isContainerType( 'volume') or targetVolume.isContainerType('dmg'): raise ValueError('Unable to restore onto: ' + str(targetVolume)) if self.outputFilePath is None: raise RuntimeError( 'Restoring a volume requires that the image have been built') # ---- process # unmount the volume targetVolume.unmount() # restore the image onto the volume by bsd path asrCommand = [ '/usr/sbin/asr', 'restore', '--verbose', '--source', self.outputFilePath, '--target', targetVolume.bsdPath, '--erase', '--noprompt' ] managedSubprocess(asrCommand) # bless the volume so that it is bootable blessCommand = [ '/usr/sbin/bless', '--device', targetVolume.bsdPath, '--verbose' ] managedSubprocess(blessCommand) # bless the volume so that it is the nextboot device blessCommand = [ '/usr/sbin/bless', '--device', targetVolume.bsdPath, '--setBoot', '--nextonly', '--verbose' ] managedSubprocess(blessCommand) # reboot rebootCommand = [ '/usr/bin/osascript', '-e', 'tell application "System Events" to restart' ] managedSubprocess(rebootCommand)
sys.exit() # Note: at this point it is all-right to overwrite the file if it exists if options.automaticRun is False: choice = raw_input('Ready to produce "%s" from the volume "%s".\n\tContinue? (Y/N):' % (chosenFileName, chosenMount.getWorkingPath())) if not choice.lower() in ['y', 'yes']: print("Canceling") sys.exit() myStatusHandler = displayTools.statusHandler(taskMessage='Creating image from disc at: %s' % chosenMount.getWorkingPath()) diskFormat = 'UDZO' # default to zlib if options.compressionType == "bzip2": diskFormat = 'UDBZ' diskutilArguments = ['/usr/bin/hdiutil', 'create', '-ov', '-srcowners', 'on', '-srcfolder', chosenMount.getWorkingPath(), '-format', diskFormat] if options.compressionType == "zlib": # go for more compression diskutilArguments.append('-imagekey') diskutilArguments.append('zlib-level=6') diskutilArguments.append(targetPath) diskutilProcess = managedSubprocess(diskutilArguments) myStatusHandler.update(taskMessage='Image "%s" created in %s' % (chosenFileName, displayTools.secondsToReadableTime(time.time() - startTime))) myStatusHandler.finishLine() sys.exit(0) if __name__ == "__main__": main()
(options, arguments) = optionParser.parse_args() restorePoint = None restorePointName = None restorePointSizeInBytes = None if options.restoreVolume == None: optionParser.error('A target volume to restore to is required.') # check the volume that we are going to restore to, and make sure we have the /dev entry for it else: diskutilCommand = [ "/usr/sbin/diskutil", "info", "-plist", options.restoreVolume ] try: process = managedSubprocess(diskutilCommand, processAsPlist=True) diskutilOutput = process.getPlistObject() except RuntimeError: optionParser.error('Unable to find the restore target volume: ' + str(options.restoreVolume)) if diskutilOutput['DeviceIdentifier'] == diskutilOutput[ 'ParentWholeDisk']: optionParser.error( 'The restore target volume must be a partition, not a whole disk.' ) if diskutilOutput['MountPoint'] == '/': optionParser.error( 'The root volume can not be the restore target volume.')
optionParser = optparse.OptionParser(usage="usage: %prog --restore-volume=VOLUME [options] [test1.dmg [test2.dmg [...]]]") optionParser.add_option("-r", "--restore-volume", dest="restoreVolume", type="string", action="store", default=None, help="Restore over this volume. WARNING: all data on this volume will be lost!", metavar="Volume") (options, arguments) = optionParser.parse_args() restorePoint = None restorePointName = None restorePointSizeInBytes = None if options.restoreVolume == None: optionParser.error('A target volume to restore to is required.') # check the volume that we are going to restore to, and make sure we have the /dev entry for it else: diskutilCommand = ["/usr/sbin/diskutil", "info", "-plist", options.restoreVolume] try: process = managedSubprocess(diskutilCommand, processAsPlist=True) diskutilOutput = process.getPlistObject() except RuntimeError: optionParser.error('Unable to find the restore target volume: ' + str(options.restoreVolume)) if diskutilOutput['DeviceIdentifier'] == diskutilOutput['ParentWholeDisk']: optionParser.error('The restore target volume must be a partition, not a whole disk.') if diskutilOutput['MountPoint'] == '/': optionParser.error('The root volume can not be the restore target volume.') if diskutilOutput['WritableMedia'] is not True: optionParser.error('The restore target volume must be on writeable media.') if "VolumeName" in diskutilOutput: restorePointName = diskutilOutput["VolumeName"]
myStatusHandler = displayTools.statusHandler( taskMessage='Creating image from disc at: %s' % chosenMount.getWorkingPath()) diskFormat = 'UDZO' # default to zlib if options.compressionType == "bzip2": diskFormat = 'UDBZ' diskutilArguments = [ '/usr/bin/hdiutil', 'create', '-ov', '-srcowners', 'on', '-srcfolder', chosenMount.getWorkingPath(), '-format', diskFormat ] if options.compressionType == "zlib": # go for more compression diskutilArguments.append('-imagekey') diskutilArguments.append('zlib-level=6') diskutilArguments.append(targetPath) diskutilProcess = managedSubprocess(diskutilArguments) myStatusHandler.update( taskMessage='Image "%s" created in %s' % (chosenFileName, displayTools.secondsToReadableTime(time.time() - startTime))) myStatusHandler.finishLine() sys.exit(0) if __name__ == "__main__": main()