Skip to content

Instantly share code, notes, and snippets.

@tsekiyama
Last active Aug 29, 2015
Embed
What would you like to do?
Attach a Cinder volume to the host
#!/usr/bin/python
import os
import socket
import sys
import logging
from cinderclient import client as cinder_client
from os_brick.initiator import connector
# Get volume id to attach from the command argument
volume_id = sys.argv[1]
use_multipath = False
# Set up logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
sh = logging.StreamHandler()
sh.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
logger.addHandler(sh)
sh.setFormatter(formatter)
# Initialize Cinderclient and connector
client = cinder_client.Client('2',
os.environ['OS_USERNAME'],
os.environ['OS_PASSWORD'],
os.environ['OS_TENANT_NAME'],
os.environ['OS_AUTH_URL'],
False,
region_name=os.environ['OS_REGION_NAME'])
root_helper = 'sudo'
# root_helper = 'sudo cinder-rootwrap /etc/cinder/rootwrap.conf'
properties = connector.get_connector_properties(root_helper,
socket.gethostname(),
multipath=use_multipath,
enforce_multipath=False)
# Attach the volume to the host
connection_info = client.volumes.initialize_connection(volume_id, properties)
conn = connector.InitiatorConnector.factory(
connection_info['driver_volume_type'],
root_helper,
use_multipath=use_multipath)
device = conn.connect_volume(connection_info['data'])
host_device = device['path']
print("\n**************************")
print("Connected! Host deivce = %s" % host_device)
# Wait for any input
print("Hit any key to disconnect.")
raw_input()
# Detach the volume
device = conn.disconnect_volume(connection_info['data'], device)
client.volumes.terminate_connection(volume_id, properties)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment