Skip to content
Newer
Older
100755 196 lines (183 sloc) 8.29 KB
4024bf7 @gholms Create loop devices upon NC, cloud startup
gholms authored
1 #!/usr/bin/python -tt
2
df1fb37 @gholms Update GPL file headers
gholms authored
3 # Copyright 2009-2012 Eucalyptus Systems, Inc.
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; version 3 of the License.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see http://www.gnu.org/licenses/.
16 #
17 # Please contact Eucalyptus Systems, Inc., 6755 Hollister Ave., Goleta
18 # CA 93117, USA or visit http://www.eucalyptus.com/licenses/ if you need
19 # additional information or have any questions.
47b5fb5 @gholms Tweak GPL headers
gholms authored
20 #
21 # This file may incorporate work covered under the following copyright
22 # and permission notice:
23 #
24 # Software License Agreement (BSD License)
25 #
26 # Copyright (c) 2008, Regents of the University of California
27 # All rights reserved.
28 #
29 # Redistribution and use of this software in source and binary forms,
30 # with or without modification, are permitted provided that the
31 # following conditions are met:
32 #
33 # Redistributions of source code must retain the above copyright
34 # notice, this list of conditions and the following disclaimer.
35 #
36 # Redistributions in binary form must reproduce the above copyright
37 # notice, this list of conditions and the following disclaimer
38 # in the documentation and/or other materials provided with the
39 # distribution.
40 #
41 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
42 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
43 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
44 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
45 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
46 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
47 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
51 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52 # POSSIBILITY OF SUCH DAMAGE. USERS OF THIS SOFTWARE ACKNOWLEDGE
53 # THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE LICENSED MATERIAL,
54 # COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS SOFTWARE,
55 # AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
56 # IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA,
57 # SANTA BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY,
58 # WHICH IN THE REGENTS' DISCRETION MAY INCLUDE, WITHOUT LIMITATION,
59 # REPLACEMENT OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO
60 # IDENTIFIED, OR WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT
61 # NEEDED TO COMPLY WITH ANY SUCH LICENSES OR RIGHTS.
df1fb37 @gholms Update GPL file headers
gholms authored
62
4024bf7 @gholms Create loop devices upon NC, cloud startup
gholms authored
63 import grp
64 import optparse
65 import os
66 import os.path
67 import platform
68 import stat
69 import warnings
70
71 try:
72 import selinux
73 HAVE_LIBSELINUX = True
74 except ImportError:
75 HAVE_LIBSELINUX = False
76
77 def get_max_part():
78 """
79 Linux 2.6.26 and later allow loop devices to contain partitions. When this
80 behavior is enabled by passing a max_part option to the loop driver, loop
81 devices' minor device numbers increase by that value plus 1 instead of just
82 1, so we need to know this value when creating loop devices so we don't
83 break things when max_part is nonzero.
84 """
85
86 if os.path.isfile('/sys/module/loop/parameters/max_part'):
87 max_part_file = open('/sys/module/loop/parameters/max_part')
88 max_part = int(max_part_file.readline().strip())
89 max_part_file.close()
90 else:
91 # Linux 2's loop driver does not export its parameters to sysfs, so
92 # guess the correct value by computing the difference between the
93 # device numbers of /dev/loop0 and /dev/loop1. Hopefully they exist.
94 loop0_minor = os.minor(os.stat('/dev/loop0').st_rdev)
95 loop1_minor = os.minor(os.stat('/dev/loop1').st_rdev)
96 max_part = loop1_minor - loop0_minor - 1
97 assert max_part >= 0
98 return max_part
99
100 def loop_devs_are_limited():
101 """
102 Return whether or not the max_loop option for the driver is nonzero. When
103 is nonzero, two things happen:
104 - The driver creates a given number of loop devices automatically
105 - The driver sets a hard limit on the number of loop devices
106
107 The hard limit means that when max_loop is nonzero any device nodes we
108 create will be useless.
109
110 On Linux 2.6.22 and later this value defaults to 0. On earlier versions it
111 has a default value of 8, a minimum of 1, and a maximum of 256.
112 """
113 if os.path.isfile('/sys/module/loop/parameters/max_loop'):
114 # Linux >= 3
115 max_loop_file = open('/sys/module/loop/parameters/max_loop')
116 max_loop = int(max_loop_file.readline().strip())
117 max_loop_file.close()
118 elif (int(platform.release().split('.')[0].split('-')[0]) == 2 and
119 int(platform.release().split('.')[1].split('-')[0]) == 6 and
120 int(platform.release().split('.')[2].split('-')[0]) >= 22):
121 # Linux >= 2.6.22
122 # Since Linux 2's loop driver doesn't export its parameters to sysfs we
123 # have no easy way to query what the value of max_loop is. It might be
124 # in the kernel parameters...
125 kcmdline_file = open('/proc/cmdline')
126 kcmdline = kcmdline_file.readline().strip()
127 kcmdline_file.close()
128 max_loop_opts = [opt for opt in kcmdline.split() if 'max_loop' in opt]
129 for opt in max_loop_opts:
130 if opt.startswith('max_loop=') or opt.startswith('loop.max_loop='):
131 max_loop = int(opt.split('=')[-1])
132 else:
133 # At this point the only option is to try to find modprobe
134 # configuration. That is a lot of work, so just assume 0. If
135 # things don't work the worst that will happen is broken loop
136 # devices, which doesn't break components any more than they
137 # would already be if the devices did not exist.
138 max_loop = 0
139 else:
140 # Linux < 2.6.22
141 # It can't be 0, so the actual value doesn't actually matter.
142 max_loop = 8
143 return max_loop != 0
144
145 def create_loop_devices(total, max_part):
146 """
147 Create devices /dev/loop0 through /dev/loopN, where N is total - 1.
148 """
149 try:
150 for i in range(0, total):
151 fname = '/dev/loop{0}'.format(i)
152 major = 7
153 minor = i * (max_part + 1)
154 if minor < 2 ** 20:
155 if not os.path.exists(fname):
156 os.mknod(fname, stat.S_IFBLK, os.makedev(major, minor))
157 # Set the permissions to something sane. These seem to be
158 # the defaults on pretty much every distro; a better source
159 # of information would be nice.
160 try:
161 gid = grp.getgrnam('disk').gr_gid
162 os.chown(fname, 0, gid)
163 os.chmod(fname, 0660)
164 except KeyError:
165 os.chown(fname, 0, 0)
166 os.chmod(fname, 0600)
167 try:
168 if HAVE_LIBSELINUX and selinux.is_selinux_enabled():
169 selinux.restorecon(fname)
170 except OSError:
171 warnings.warn('Failed to set SELinux context on ' +
172 fname, RuntimeWarning)
173 else:
174 warnings.warn(('Minor device numbers too large to create all '
175 'device nodes'), RuntimeWarning)
176 break
177 except OverflowError:
178 # The native code can barf when given really big numbers for device
179 # nodes. (i.e. when os.makedev(major, minor) > 2 ** 31 - 1)
180 warnings.warn('Device numbers too large to create all device nodes',
181 RuntimeWarning)
182
183 def main():
184 parser = optparse.OptionParser()
185 (options, args) = parser.parse_args()
186 if len(args) > 0:
187 ndevs = int(args[0])
188 else:
189 ndevs = 256
190
191 if not loop_devs_are_limited():
192 create_loop_devices(ndevs, get_max_part())
193
194 if __name__ == '__main__':
195 main()
Something went wrong with that request. Please try again.