前面有篇《nova boot from volume代碼分析》http://iceyao.blog.51cto.com/9426658/1770927,今天這裏看下針對nova boot from volume沒法注入密碼的簡單hack。python
nova/virt/libvirt/driver.py中_inject_data函數部分代碼json
if any((key, net, metadata, admin_pass, files)): injection_p_w_picpath = self.p_w_picpath_backend.p_w_picpath( instance, 'disk' + suffix, p_w_picpath_type) img_id = instance.p_w_picpath_ref if not injection_p_w_picpath.check_p_w_picpath_exists(): LOG.warn(_LW('Image %s not found on disk storage. ' 'Continue without injecting data'), injection_p_w_picpath.path, instance=instance) return
其實主要問題是,nova boot from rbd volume的時候,虛擬機的系統盤是在雲硬盤那裏的,因此必須先定位雲硬盤的位置,才能夠注入密碼。默認狀況下boot from p_w_picpath,系統盤名字是<instance-uuid>_disk,boot from volume名字是volume-<volume-uuid>。app
nova/virt/libvirt/p_w_picpathbackend.pyide
class Rbd(Image): SUPPORTS_CLONE = True def __init__(self, instance=None, disk_name=None, path=None, **kwargs): super(Rbd, self).__init__("block", "rbd", is_block_dev=False) if path: try: self.rbd_name = path.split('/')[1] except IndexError: raise exception.InvalidDevicePath(path=path) else: self.rbd_name = '%s_%s' % (instance.uuid, disk_name) if not CONF.libvirt.p_w_picpaths_rbd_pool: raise RuntimeError(_('You should specify' ' p_w_picpaths_rbd_pool' ' flag to use rbd p_w_picpaths.')) self.pool = CONF.libvirt.p_w_picpaths_rbd_pool self.discard_mode = CONF.libvirt.hw_disk_discard self.rbd_user = CONF.libvirt.rbd_user self.ceph_conf = CONF.libvirt.p_w_picpaths_rbd_ceph_conf # 這裏的判斷邏輯不是很嚴謹,純碎演示 if not instance.p_w_picpath_ref: context = nova_context.get_admin_context() # 獲取bdms bdms = ( objects.BlockDeviceMappingList.get_by_instance_uuid( context, instance.uuid)) connection_info = jsonutils.loads(bdms[0].connection_info) # 得到系統盤volume-id,cinder rbd pool self.rbd_name = connection_info['data']['name'].split('/')[1] self.pool = connection_info['data']['name'].split('/')[0]