rackspace - How to attach a Cloud Block Storage volume to an OnMetal server with pyrax? -
i automate attachment of cloud block storage volume onmetal server running centos 7 writing python script make uses of pyrax python module. know how it?
attaching cloud block storage volume onmetal server bit more complicated attaching normal rackspace virtual server. notice when try attach cloud block storage volume onmetal server in rackspace web interface cloud control panel, see text:
note: when attaching volumes onmetal servers, must log onmetal server set initiator name, discover targets , connect target.
so can attach volume in web interface additionally need log in onmetal server , run few commands. actual commands can copy-and-pasted web interface terminal of onmetal server.
also before detaching need run command.
but web interface not needed. can done python module pyrax.
first install rpm package iscsi-initiator-utils on onmetal server
[root@server-01 ~]# yum -y install iscsi-initiator-utils assuming volume_id , server_id known, python code first attaches volume , detaches volume. unfortunately, mount_point argument attach_to_instance() not working onmetal servers, need use command lsblk -n -d before , after attaching volume. comparing outputs deduce device name used attached volume. (the part deduce device name not taken care of following python code).
#/usr/bin/python # disclaimer: use script @ own risk! import json import os import paramiko import pyrax # replace server_id , volume_id # settings server_id = "cbdcb7e3-5231-40ad-bba6-45aaeabf0a8d" volume_id = "35abb4ba-caee-4cae-ada3-a16f6fa2ab50" # demonstrate mount_point argument # attach_to_instance() not working onmetal servers disk_device = "/dev/xvdd" def run_ssh_commands(ssh_client, remote_commands): remote_command in remote_commands: stdin, stdout, stderr = ssh_client.exec_command(remote_command) print("") print("command: " + remote_command) line in stdout.read().splitlines(): print(" stdout: " + line) exit_status = stdout.channel.recv_exit_status() if exit_status != 0: raise runtimeerror("the command :\n{}\n" "exited exit status: {}\n" "stderr: {}".format(remote_command, exit_status, stderr.read())) pyrax.set_setting("identity_type", "rackspace") pyrax.set_default_region('iad') creds_file = os.path.expanduser("~/.rackspace_cloud_credentials") pyrax.set_credential_file(creds_file) server = pyrax.cloudservers.servers.get(server_id) vol = pyrax.cloud_blockstorage.find(id = volume_id) vol.attach_to_instance(server, mountpoint=disk_device) pyrax.utils.wait_until(vol, "status", "in-use", interval=3, attempts=0, verbose=true) ssh_client = paramiko.sshclient() ssh_client.set_missing_host_key_policy(paramiko.autoaddpolicy()) ssh_client.connect(server.accessipv4, username='root', allow_agent=true) # new metadata available if get() server once more server = pyrax.cloudservers.servers.get(server_id) metadata = server.metadata["volumes_" + volume_id] parsed_json = json.loads(metadata) target_iqn = parsed_json["target_iqn"] target_portal = parsed_json["target_portal"] initiator_name = parsed_json["initiator_name"] run_ssh_commands(ssh_client, [ "lsblk -n -d", "echo initiatorname={} > /etc/iscsi/initiatorname.iscsi".format(initiator_name), "iscsiadm -m discovery --type sendtargets --portal {}".format(target_portal), "iscsiadm -m node --targetname={} --portal {} --login".format(target_iqn, target_portal), "lsblk -n -d", "iscsiadm -m node --targetname={} --portal {} --logout".format(target_iqn, target_portal), "lsblk -n -d" ]) vol.detach() pyrax.utils.wait_until(vol, "status", "available", interval=3, attempts=0, verbose=true) running python code looks this
user@ubuntu:~$ python attach.py 2> /dev/null current value of status: attaching (elapsed: 1.0 seconds) current value of status: in-use (elapsed: 4.9 seconds) command: lsblk -n -d stdout: sda 8:0 0 29.8g 0 disk command: echo initiatorname=iqn.2008-10.org.openstack:a24b6f80-cf02-48fc-9a25-ccc3ed3fb918 > /etc/iscsi/initiatorname.iscsi command: iscsiadm -m discovery --type sendtargets --portal 10.190.142.116:3260 stdout: 10.190.142.116:3260,1 iqn.2010-11.com.rackspace:35abb4bb-caee-4c5e-ad53-a16f6f12ab50 stdout: 10.69.193.1:3260,1 iqn.2010-11.com.rackspace:35abb4bb-caee-4c5e-ad53-a16f6f12ab50 command: iscsiadm -m node --targetname=iqn.2010-11.com.rackspace:35abb4bb-caee-4c5e-ad53-a16f6f12ab50 --portal 10.190.142.116:3260 --login stdout: logging in [iface: default, target: iqn.2010-11.com.rackspace:35abb4bb-caee-4c5e-ad53-a16f6f12ab50, portal: 10.190.142.116,3260] (multiple) stdout: login [iface: default, target: iqn.2010-11.com.rackspace:35abb4bb-caee-4c5e-ad53-a16f6f12ab50, portal: 10.190.142.116,3260] successful. command: lsblk -n -d stdout: sda 8:0 0 29.8g 0 disk stdout: sdb 8:16 0 50g 0 disk command: iscsiadm -m node --targetname=iqn.2010-11.com.rackspace:35abb4bb-caee-4c5e-ad53-a16f6f12ab50 --portal 10.190.142.116:3260 --logout stdout: logging out of session [sid: 5, target: iqn.2010-11.com.rackspace:35abb4bb-caee-4c5e-ad53-a16f6f12ab50, portal: 10.190.142.116,3260] stdout: logout of [sid: 5, target: iqn.2010-11.com.rackspace:35abb4bb-caee-4c5e-ad53-a16f6f12ab50, portal: 10.190.142.116,3260] successful. command: lsblk -n -d stdout: sda 8:0 0 29.8g 0 disk current value of status: detaching (elapsed: 0.8 seconds) current value of status: available (elapsed: 4.7 seconds) user@ubuntu:~$ just, 1 additional note:
although not mentioned in official rackspace documentation
https://support.rackspace.com/how-to/attach-a-cloud-block-storage-volume-to-an-onmetal-server/
in forum post 5 aug 2015 rackspace managed infrastructure support recommends running
iscsiadm -m node -t $target_iqn -p $target_portal --op update -n node.startup -v automatic to make connection persistent automatically restarts iscsi session upon startup.
update
regarding deducing new device name: major hayden writes in blog post
[root@server-01 ~]# ls /dev/disk/by-path/ could used find path new device. if deference symlinks, guess work
[root@server-01 ~]# find -l /dev/disk/by-path -maxdepth 1 -mindepth 1 -exec realpath {} \;
Comments
Post a Comment