def recover_disk(base_disk, base_mem, overlay_mem, overlay_disk, recover_path, chunk_size): recover_fd = open(recover_path, "wb") # get delta list from file and recover it to origin delta_stream = open(overlay_disk, "r") recovered_memory = Recovered_delta(base_disk, base_mem, chunk_size, \ parent=base_disk, overlay_memory=overlay_mem) for delta_item in DeltaList.from_stream(delta_stream): recovered_memory.recover_item(delta_item) delta_list = recovered_memory.delta_list # overlay map chunk_list = [] # sort delta list using offset delta_list.sort(key=itemgetter('offset')) for delta_item in delta_list: if len(delta_item.data) != chunk_size: raise DiskError("recovered size is not same as page size") chunk_list.append("%ld:1" % (delta_item.offset/chunk_size)) recover_fd.seek(delta_item.offset) recover_fd.write(delta_item.data) last_write_offset = delta_item.offset + len(delta_item.data) # fill zero to the end of the modified file if last_write_offset: diff_offset = os.path.getsize(base_disk) - last_write_offset if diff_offset > 0: recover_fd.seek(diff_offset-1, os.SEEK_CUR) recover_fd.write('0') recover_fd.close() # overlay chunk format: chunk_1:1,chunk_2:1,... return ','.join(chunk_list)
def recover_disk(base_disk, base_mem, overlay_mem, overlay_disk, recover_path, chunk_size): recover_fd = open(recover_path, "wb") # get delta list from file and recover it to origin delta_stream = open(overlay_disk, "r") recovered_memory = Recovered_delta(base_disk, base_mem, chunk_size, \ parent=base_disk, overlay_memory=overlay_mem) for delta_item in DeltaList.from_stream(delta_stream): recovered_memory.recover_item(delta_item) delta_list = recovered_memory.delta_list # overlay map chunk_list = [] # sort delta list using offset delta_list.sort(key=itemgetter('offset')) for delta_item in delta_list: if len(delta_item.data) != chunk_size: raise DiskError("recovered size is not same as page size") chunk_list.append("%ld:1" % (delta_item.offset / chunk_size)) recover_fd.seek(delta_item.offset) recover_fd.write(delta_item.data) last_write_offset = delta_item.offset + len(delta_item.data) # fill zero to the end of the modified file if last_write_offset: diff_offset = os.path.getsize(base_disk) - last_write_offset if diff_offset > 0: recover_fd.seek(diff_offset - 1, os.SEEK_CUR) recover_fd.write('0') recover_fd.close() # overlay chunk format: chunk_1:1,chunk_2:1,... return ','.join(chunk_list)
def recover_memory(base_disk, base_mem, delta_path, out_path, verify_with_original=None): # Recover modified memory snapshot # base_path: base memory snapshot, delta pages will be applied over it # delta_path: memory overlay # out_path: path to recovered modified memory snapshot # verify_with_original: original modification file for recover verification recovered_memory = Recovered_delta(base_disk, base_mem, delta_path, out_path, \ Memory.RAM_PAGE_SIZE, parent=base_mem) chunk_list = [] for chunk_number in recovered_memory.recover_chunks(): chunk_list.append("%ld:1" % chunk_number) recovered_memory.finish() # varify with original if verify_with_original: modi_mem = open(verify_with_original, "rb") base_file = open(base_mem, "rb") delta_list_index = 0 while True: offset = base_file.tell() if len(delta_list) == delta_list_index: break base_data = base_file.read(Memory.RAM_PAGE_SIZE) #import pdb; pdb.set_trace() if offset != delta_list[delta_list_index].offset: #print "from base data: %d" % len(base_data) modi_mem.seek(offset) modi_data = modi_mem.read(len(base_data)) if modi_data != base_data: msg = "orignal data is not same at %ld" % offset raise MemoryError(msg) else: modi_mem.seek(offset) recover_data = delta_list[delta_list_index].data origin_data = modi_mem.read(len(recover_data)) #print "from recovered data: %d at %ld" % (len(recover_data), delta_list[delta_list_index].offset) delta_list_index += 1 if recover_data != origin_data: msg = "orignal data is not same at %ld" % offset raise MemoryError(msg) for delta_item in delta_list: offset = delta_item.offset data = delta_item.data modi_mem.seek(offset) origin_data = modi_mem.read(len(data)) if data != origin_data: msg = "orignal data is not same at %ld" % offset raise MemoryError(msg) print "Pass all varification - Successfully recovered" return ','.join(chunk_list)
def recover_memory(base_disk, base_mem, delta_path, out_path, verify_with_original=None): # Recover modified memory snapshot # base_path: base memory snapshot, delta pages will be applied over it # delta_path: memory overlay # out_path: path to recovered modified memory snapshot # verify_with_original: original modification file for recover verification recovered_memory = Recovered_delta(base_disk, base_mem, delta_path, out_path, \ Memory.RAM_PAGE_SIZE, parent=base_mem) chunk_list = [] for chunk_number in recovered_memory.recover_chunks(): chunk_list.append("%ld:1" % chunk_number) recovered_memory.finish() # varify with original if verify_with_original: modi_mem = open(verify_with_original, "rb") base_file = open(base_mem, "rb") delta_list_index = 0 while True: offset = base_file.tell() if len(delta_list) == delta_list_index: break base_data = base_file.read(Memory.RAM_PAGE_SIZE) if offset != delta_list[delta_list_index].offset: #LOG.debug("from base data: %d" % len(base_data)) modi_mem.seek(offset) modi_data = modi_mem.read(len(base_data)) if modi_data != base_data: msg = "orignal data is not same at %ld" % offset raise MemoryError(msg) else: modi_mem.seek(offset) recover_data = delta_list[delta_list_index].data origin_data = modi_mem.read(len(recover_data)) #LOG.debug("from recovered data: %d at %ld" % (len(recover_data), delta_list[delta_list_index].offset)) delta_list_index += 1 if recover_data != origin_data: msg = "orignal data is not same at %ld" % offset raise MemoryError(msg) for delta_item in delta_list: offset = delta_item.offset data = delta_item.data modi_mem.seek(offset) origin_data = modi_mem.read(len(data)) if data != origin_data: msg = "orignal data is not same at %ld" % offset raise MemoryError(msg) LOG.debug("Pass all varification - Successfully recovered") return ','.join(chunk_list)