1import os, logging 2from autotest_lib.client.common_lib import error 3from autotest_lib.client.bin import utils 4from autotest_lib.client.virt import virt_utils 5 6 7def run_image_copy(test, params, env): 8 """ 9 Copy guest images from nfs server. 10 1) Mount the NFS share directory 11 2) Check the existence of source image 12 3) If it exists, copy the image from NFS 13 14 @param test: kvm test object 15 @param params: Dictionary with the test parameters 16 @param env: Dictionary with test environment. 17 """ 18 mount_dest_dir = params.get('dst_dir', '/mnt/images') 19 if not os.path.exists(mount_dest_dir): 20 try: 21 os.makedirs(mount_dest_dir) 22 except OSError, err: 23 logging.warning('mkdir %s error:\n%s', mount_dest_dir, err) 24 25 if not os.path.exists(mount_dest_dir): 26 raise error.TestError('Failed to create NFS share dir %s' % 27 mount_dest_dir) 28 29 src = params.get('images_good') 30 image = '%s.%s' % (os.path.split(params['image_name'])[1], 31 params['image_format']) 32 src_path = os.path.join(mount_dest_dir, image) 33 dst_path = '%s.%s' % (params['image_name'], params['image_format']) 34 cmd = 'cp %s %s' % (src_path, dst_path) 35 36 if not virt_utils.mount(src, mount_dest_dir, 'nfs', 'ro'): 37 raise error.TestError('Could not mount NFS share %s to %s' % 38 (src, mount_dest_dir)) 39 40 # Check the existence of source image 41 if not os.path.exists(src_path): 42 raise error.TestError('Could not find %s in NFS share' % src_path) 43 44 logging.debug('Copying image %s...', image) 45 utils.system(cmd) 46