A typical use case within the cloud is attaching/eradicating volumes dynamically. Figuring out the hooked up disk machine within the working system of the VSI is just not all the time apparent.
In IBM Cloud Virtual Server for VPC, the disk that’s hooked up to the VSI is recognized by the amount attachment identifier. Each quantity has a UUID, however the attachment between the amount and the VSI additionally has a UUID. The UUID of that quantity attachment is what can be utilized to determine the backing disk within the VSI.
UI-based instance
Itemizing the amount attachment identifiers is easy with the ibmcloud CLI:
Drews-MBP ~ % ibmcloud is in-vols 0727_fa4230b2-e167-4a09-8d7c-2822bdb016bf
Itemizing quantity attachments of occasion 0727_fa4230b2-e167-4a09-8d7c-2822bdb016bf underneath account Drew Thorstensen's Account as person thorst@us.ibm.com...
ID Identify Quantity Standing Sort System Auto delete
0727-7db66159-5910-4ddc-a5b4-51f9333a9c3a secrecy-robust-anyone-agile test-metadata-volume hooked up knowledge 0727-7db66159-5910-4ddc-a5b4-51f9333a9c3a-4xjjc false
0727-4b98cbf2-4e57-4b3b-9bd2-83f2439da3bc breath-manger-bird-axiom test-metadata-boot-1649335137000 hooked up boot 0727-4b98cbf2-4e57-4b3b-9bd2-83f2439da3bc-5jx6t true
This exhibits two volumes hooked up to the VSI: the boot after which a knowledge quantity. If we log in to the VSI, we are able to see the amount disks:
[root@test-metadata ~]# ls -la /dev/disk/by-id
complete 0
drwxr-xr-x. 2 root root 200 Apr 7 12:58 .
drwxr-xr-x. 7 root root 140 Apr 7 12:51 ..
lrwxrwxrwx. 1 root root 9 Apr 7 12:52 virtio-0727-4b98cbf2-4e57-4 -> ../../vda
lrwxrwxrwx. 1 root root 10 Apr 7 12:52 virtio-0727-4b98cbf2-4e57-4-part1 -> ../../vda1
lrwxrwxrwx. 1 root root 10 Apr 7 12:52 virtio-0727-4b98cbf2-4e57-4-part2 -> ../../vda2
lrwxrwxrwx. 1 root root 10 Apr 7 12:52 virtio-0727-4b98cbf2-4e57-4-part3 -> ../../vda3
lrwxrwxrwx. 1 root root 9 Apr 7 12:58 virtio-0727-7db66159-5910-4 -> ../../vdd
lrwxrwxrwx. 1 root root 10 Apr 7 12:58 virtio-0727-7db66159-5910-4-part1 -> ../../vdd1
lrwxrwxrwx. 1 root root 9 Apr 7 12:52 virtio-cloud-init- -> ../../vdc
lrwxrwxrwx. 1 root root 9 Apr 7 12:52 virtio-cloud-init-0727_fa42 -> ../../vdb
If we wish to discover the information quantity named test-metadata-volume
, we see that it’s the vdd
disk. The primary 20 characters of the amount attachment identifier are used for the disk id. The symbolic hyperlink exhibits us the identify of the disk that it maps to, as nicely.
Simplifying with the metadata service
Most customers are in search of a faster option to determine the disk identify, and these customers may also probably be beginning with the amount reasonably than the amount attachment. Thankfully, that is straightforward to do.
Lately, IBM Cloud VPC launched the metadata service. This functionality permits the tip person to question knowledge about itself and get id tokens and extra from the VSI. One of many use instances it might probably assist with helps simplify getting the disk identify primarily based off a quantity identify.
The steps to seek out the disk identify inside a VSI from a given quantity are as follows:
- Deploy the VSI with metadata turned on
- Get an occasion id token
- Question the metadata service to record the amount attachments
- Discover the amount attachment that has the amount specified
- Lookup the disk identify primarily based off the amount attachment
Hooked up to this put up is a script that can be utilized to retrieve the disk identify for a given quantity UUID (not the attachment UUID). The next is the pattern output:
[root@test-metadata ~]# python3 scripts/find_disk.py -v 0727-7db66159-5910-4ddc-a5b4-51f9333a9c3a-4xjjc
vdd
On this case, the disk is called vdd
throughout the VSI for quantity 0727-7db66159-5910-4ddc-a5b4-51f9333a9c3a-4xjjc
.
Code
The next code could be embedded into your occasion or person knowledge to assist rapidly determine disk names throughout the VSI given the amount UUID. Remember to do not forget that the metadata service have to be operating for this to work correctly:
# ======================================================================
# Copyright 2022 IBM Corp.
#
# Licensed underneath the Apache License, Model 2.0 (the "License");
# you could not use this file besides in compliance with the License.
# You might acquire a replica of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Except required by relevant legislation or agreed to in writing, software program
# distributed underneath the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, both specific or implied.
# See the License for the particular language governing permissions and
# limitations underneath the License.
# ======================================================================
import argparse
import json
import os
import requests
import sys
def init_argparse():
parser = argparse.ArgumentParser(
description=("Finds a neighborhood disk by UUID")
)
parser.add_argument("-v", "--volume-uuid", required=True,
assist=("The quantity UUID."))
return parser.parse_args()
def get_token():
outcome = requests.put("http://169.254.169.254/instance_identity/v1/token?model=2021-10-12",
headers={'Metadata-Taste': 'ibm'})
return json.hundreds(outcome.textual content)['access_token']
def get_volume_attachments(access_token):
outcome = requests.get("http://169.254.169.254/metadata/v1/occasion/volume_attachments?model=2021-10-12",
headers={'Authorization': f"Bearer {access_token}"})
return json.hundreds(outcome.textual content)['volume_attachments']
def get_disk_device(attachment_uuid):
expected_path = f'/dev/disk/by-id/virtio-{attachment_uuid[:20]}'
full_path = os.path.realpath(expected_path)
return full_path.cut up('/')[-1]
def major():
args = init_argparse()
access_token = get_token()
volume_attachments = get_volume_attachments(access_token)
for volume_attachment in volume_attachments:
if volume_attachment['device']['id'] == args.volume_uuid:
print(get_disk_device(volume_attachment['id']))
sys.exit(0)
else:
print(f"Unable to seek out disk by quantity {args.volume_uuid}")
sys.exit(1)
if __name__ == '__main__':
major()
Conclusion
The metadata service is a strong service that makes it straightforward for a given occasion to assemble details about itself. Without having to name out to any API, customers can detect the disk identify from throughout the VSI for a given quantity UUID. These scripts could be prolonged to help extra use instances, together with wanting up the volumes and community interfaces.
To be taught extra about IBM Cloud VPC, please visit our website. You too can get USD 1,000 in credit to make use of towards any of your new VPC assets – together with all compute, community and storage parts.