summaryrefslogtreecommitdiffstats
path: root/openlmi-storage-demo.py
blob: 8915e99f6e662413e35ce3358b8bfe5a2aed964c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# !/usr/bin/env lmishell

'''
Created on Apr 8, 2013

@author: sgallagh
'''

import sys
import time
from scripton.lmi_sync_job import LMISyncJob
from scripton.lmi_storage_view import LMIStorageView
import demoutils.democolor as democolor

# Machine to contact
server = "openlmi-demo.example.com"
username = "root"
password = "redhat"

# Drives to work with
avail_drives = ["/dev/vdb", "/dev/vdc", "/dev/vdd"]

# Connect to the example VM
print democolor.hilite("\nConnecting to remote server \"{0}\"".format(server),
                       democolor.XTERM_WHITE)
c = connect(server, username, password)

# Shorthand the cimv2 namespace
ns = c.root.cimv2

# Connect to the partitioning service
partitioning_service = ns.LMI_DiskPartitionConfigurationService.first_instance(
                           Key="Name",
                           Value="LMI_DiskPartitionConfigurationService")

# Connect to the storage configuration service
storage_service = ns.LMI_StorageConfigurationService.first_instance(
                      Key="Name",
                      Value="LMI_StorageConfigurationService")

# Connect to the storage filesystem service
filesystem_service = ns.LMI_FileSystemConfigurationService.first_instance(
                         Key="Name",
                         Value="LMI_FileSystemConfigurationService")

# Get the GPT partition type object
gpt_caps = ns.LMI_DiskPartitionConfigurationCapabilities.first_instance(
               Key="InstanceID",
               Value="LMI:LMI_DiskPartitionConfigurationCapabilities:GPT")


# Display the current configuration of the system
print democolor.hilite("\n== Initial configuration of {0} ==".format(server),
                       democolor.XTERM_MAGENTA, True)
storage_view = LMIStorageView(ns)
storage_view.print_all()

print democolor.hilite("\n== Creating RAID set on unused drives ==".format(server),
                       democolor.XTERM_MAGENTA, True)

# Prep the first three drives
for drive in avail_drives:
    print "Processing drive {0}".format(drive)
    physical_device = ns.LMI_StorageExtent.first_instance(
                          Key="Name",
                          Value=drive)

    # Create a new GPT partition table on this disk
    print "Creating GPT partition table on {0}".format(physical_device.DeviceID)
    job = LMISyncJob(ns,
                     partitioning_service.SetPartitionStyle(
                                Extent=physical_device,
                                PartitionStyle=gpt_caps))
    (ret, outparams, err) = job.process()
    if ret != LMISyncJob.SUCCESS:
        print "Error creating partition table: {0}({1})".format(err, ret)
        sys.exit(1)

    # Create a single partition covering the whole disk
    print "Creating partition on {0}".format(physical_device.DeviceID)
    job = LMISyncJob(ns,
                     partitioning_service.LMI_CreateOrModifyPartition(
                                              extent=physical_device))
    (ret, outparams, err) = job.process()
    if (ret != LMISyncJob.SUCCESS):
        # Job did not complete successfully
        print "Error creating partition on {0}: {1}({2})".format(
               drive, err, ret)
        sys.exit(2)
    sys.stdout.write(u"\n")

# Find the devices we want to add to MD RAID
# (filtering one CIM_StorageExtent.instances()
# call would be faster, but this is easier to read)

vdb1 = ns.CIM_StorageExtent.first_instance(
        Key="Name", Value="/dev/vdb1")
vdc1 = ns.CIM_StorageExtent.first_instance(
        Key="Name", Value="/dev/vdc1")
vdd1 = ns.CIM_StorageExtent.first_instance(
        Key="Name", Value="/dev/vdd1")

# Create the RAID set
print "Creating raid set on {0}, {1}, {2}".format(vdb1.Name, vdc1.Name,
                                                  vdd1.Name)

job = LMISyncJob(ns,
                 storage_service.CreateOrModifyMDRAID(
                     ElementName="myRAID",
                     InExtents=[vdb1.path, vdc1.path, vdd1.path],
                     Level=5))
(ret, outparams, err) = job.process()

if ret != LMISyncJob.SUCCESS:
    # Job did not complete successfully
    print "Error creating RAID set: {0}({1})".format(err, ret)
    sys.exit(2)

raid = ns.LMI_StorageExtent.first_instance(Key="Name", Value="/dev/md/myRAID")
print democolor.hilite(
    "Created RAID device {0} at level {1} of size {2} MB".format(
        raid.DeviceID, raid.Level,
        raid.BlockSize * raid.NumberOfBlocks / 1024 / 1024),
    democolor.XTERM_WHITE)


print "Creating EXT4 file system"
job = LMISyncJob(ns,
                 filesystem_service.LMI_CreateFileSystem(
                     FileSystemType=32769,  # 32769 = EXT4
                     InExtents=[raid.path]))
(ret, outparams, err) = job.process()
if (ret != LMISyncJob.SUCCESS):
    # Job did not complete successfully
    print "Error creating filesystem: {0}({1})".format(err, ret)
    sys.exit(2)

print "Filesystem created on {0}".format(raid.Name)

# Display the final configuration of the system
print democolor.hilite("\n== Final configuration of {0} ==".format(server),
                       democolor.XTERM_MAGENTA, True)
storage_view = LMIStorageView(ns)
storage_view.print_all()