Merge lp:~blair/cloud-init/cloud-init into lp:~cloud-init-dev/cloud-init/trunk

Proposed by Blair Zajac
Status: Merged
Merged at revision: 798
Proposed branch: lp:~blair/cloud-init/cloud-init
Merge into: lp:~cloud-init-dev/cloud-init/trunk
Diff against target: 227 lines (+149/-34)
2 files modified
cloudinit/util.py (+54/-34)
tests/unittests/test_util.py (+95/-0)
To merge this branch: bzr merge lp:~blair/cloud-init/cloud-init
Reviewer Review Type Date Requested Status
Scott Moser Pending
Review via email: mp+152733@code.launchpad.net

Description of the change

Commits to implement Scott's requested features for btrfs resize
support.

1) Refactor util.get_mount_info() to facilitate unit testing.
2) Add unit tests for /proc/$$/mountinfo parsing.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'cloudinit/util.py'
2--- cloudinit/util.py 2013-03-07 21:27:47 +0000
3+++ cloudinit/util.py 2013-03-11 17:32:22 +0000
4@@ -1576,44 +1576,29 @@
5 return pkglist
6
7
8-def get_mount_info(path, log=LOG):
9- # Use /proc/$$/mountinfo to find the device where path is mounted.
10- # This is done because with a btrfs filesystem using os.stat(path)
11- # does not return the ID of the device.
12- #
13- # Here, / has a device of 18 (decimal).
14- #
15- # $ stat /
16- # File: '/'
17- # Size: 234 Blocks: 0 IO Block: 4096 directory
18- # Device: 12h/18d Inode: 256 Links: 1
19- # Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
20- # Access: 2013-01-13 07:31:04.358011255 +0000
21- # Modify: 2013-01-13 18:48:25.930011255 +0000
22- # Change: 2013-01-13 18:48:25.930011255 +0000
23- # Birth: -
24- #
25- # Find where / is mounted:
26- #
27- # $ mount | grep ' / '
28- # /dev/vda1 on / type btrfs (rw,subvol=@,compress=lzo)
29- #
30- # And the device ID for /dev/vda1 is not 18:
31- #
32- # $ ls -l /dev/vda1
33- # brw-rw---- 1 root disk 253, 1 Jan 13 08:29 /dev/vda1
34- #
35- # So use /proc/$$/mountinfo to find the device underlying the
36- # input path.
37+def parse_mount_info(path, mountinfo_lines, log=LOG):
38+ """Return the mount information for PATH given the lines from
39+ /proc/$$/mountinfo."""
40+
41 path_elements = [e for e in path.split('/') if e]
42 devpth = None
43 fs_type = None
44 match_mount_point = None
45 match_mount_point_elements = None
46- mountinfo_path = '/proc/%s/mountinfo' % os.getpid()
47- for line in load_file(mountinfo_path).splitlines():
48+ for i, line in enumerate(mountinfo_lines):
49 parts = line.split()
50
51+ # Completely fail if there is anything in any line that is
52+ # unexpected, as continuing to parse past a bad line could
53+ # cause an incorrect result to be returned, so it's better
54+ # return nothing than an incorrect result.
55+
56+ # The minimum number of elements in a valid line is 10.
57+ if len(parts) < 10:
58+ log.debug("Line %d has two few columns (%d): %s",
59+ i + 1, len(parts), line)
60+ return None
61+
62 mount_point = parts[4]
63 mount_point_elements = [e for e in mount_point.split('/') if e]
64
65@@ -1638,8 +1623,8 @@
66 try:
67 i = parts.index('-')
68 except ValueError:
69- log.debug("Did not find column named '-' in %s",
70- mountinfo_path)
71+ log.debug("Did not find column named '-' in line %d: %s",
72+ i + 1, line)
73 return None
74
75 # Get the path to the device.
76@@ -1647,7 +1632,8 @@
77 fs_type = parts[i + 1]
78 devpth = parts[i + 2]
79 except IndexError:
80- log.debug("Too few columns in %s after '-' column", mountinfo_path)
81+ log.debug("Too few columns after '-' column in line %d: %s",
82+ i + 1, line)
83 return None
84
85 match_mount_point = mount_point
86@@ -1657,3 +1643,37 @@
87 return (devpth, fs_type, match_mount_point)
88 else:
89 return None
90+
91+
92+def get_mount_info(path, log=LOG):
93+ # Use /proc/$$/mountinfo to find the device where path is mounted.
94+ # This is done because with a btrfs filesystem using os.stat(path)
95+ # does not return the ID of the device.
96+ #
97+ # Here, / has a device of 18 (decimal).
98+ #
99+ # $ stat /
100+ # File: '/'
101+ # Size: 234 Blocks: 0 IO Block: 4096 directory
102+ # Device: 12h/18d Inode: 256 Links: 1
103+ # Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
104+ # Access: 2013-01-13 07:31:04.358011255 +0000
105+ # Modify: 2013-01-13 18:48:25.930011255 +0000
106+ # Change: 2013-01-13 18:48:25.930011255 +0000
107+ # Birth: -
108+ #
109+ # Find where / is mounted:
110+ #
111+ # $ mount | grep ' / '
112+ # /dev/vda1 on / type btrfs (rw,subvol=@,compress=lzo)
113+ #
114+ # And the device ID for /dev/vda1 is not 18:
115+ #
116+ # $ ls -l /dev/vda1
117+ # brw-rw---- 1 root disk 253, 1 Jan 13 08:29 /dev/vda1
118+ #
119+ # So use /proc/$$/mountinfo to find the device underlying the
120+ # input path.
121+ mountinfo_path = '/proc/%s/mountinfo' % os.getpid()
122+ lines = load_file(mountinfo_path).splitlines()
123+ return parse_mount_info(path, lines, log)
124
125=== modified file 'tests/unittests/test_util.py'
126--- tests/unittests/test_util.py 2012-11-09 00:30:57 +0000
127+++ tests/unittests/test_util.py 2013-03-11 17:32:22 +0000
128@@ -248,4 +248,99 @@
129 myobj)
130
131
132+class TestMountinfoParsing(TestCase):
133+ precise_ext4_mountinfo = \
134+"""15 20 0:14 / /sys rw,nosuid,nodev,noexec,relatime - sysfs sysfs rw
135+16 20 0:3 / /proc rw,nosuid,nodev,noexec,relatime - proc proc rw
136+17 20 0:5 / /dev rw,relatime - devtmpfs udev rw,size=16422216k,nr_inodes=4105554,mode=755
137+18 17 0:11 / /dev/pts rw,nosuid,noexec,relatime - devpts devpts rw,gid=5,mode=620,ptmxmode=000
138+19 20 0:15 / /run rw,nosuid,relatime - tmpfs tmpfs rw,size=6572812k,mode=755
139+20 1 252:1 / / rw,relatime - ext4 /dev/mapper/vg0-root rw,errors=remount-ro,data=ordered
140+21 15 0:16 / /sys/fs/cgroup rw,relatime - tmpfs cgroup rw,mode=755
141+22 15 0:17 / /sys/fs/fuse/connections rw,relatime - fusectl none rw
142+23 15 0:6 / /sys/kernel/debug rw,relatime - debugfs none rw
143+25 15 0:10 / /sys/kernel/security rw,relatime - securityfs none rw
144+26 19 0:19 / /run/lock rw,nosuid,nodev,noexec,relatime - tmpfs none rw,size=5120k
145+27 19 0:20 / /run/shm rw,nosuid,nodev,relatime - tmpfs none rw
146+28 19 0:21 / /run/user rw,nosuid,nodev,noexec,relatime - tmpfs none rw,size=102400k,mode=755
147+24 21 0:18 / /sys/fs/cgroup/cpuset rw,relatime - cgroup cgroup rw,cpuset
148+29 21 0:22 / /sys/fs/cgroup/cpu rw,relatime - cgroup cgroup rw,cpu
149+30 21 0:23 / /sys/fs/cgroup/cpuacct rw,relatime - cgroup cgroup rw,cpuacct
150+31 21 0:24 / /sys/fs/cgroup/memory rw,relatime - cgroup cgroup rw,memory
151+32 21 0:25 / /sys/fs/cgroup/devices rw,relatime - cgroup cgroup rw,devices
152+33 21 0:26 / /sys/fs/cgroup/freezer rw,relatime - cgroup cgroup rw,freezer
153+34 21 0:27 / /sys/fs/cgroup/blkio rw,relatime - cgroup cgroup rw,blkio
154+35 21 0:28 / /sys/fs/cgroup/perf_event rw,relatime - cgroup cgroup rw,perf_event
155+36 20 9:0 / /boot rw,relatime - ext4 /dev/md0 rw,data=ordered
156+37 16 0:29 / /proc/sys/fs/binfmt_misc rw,nosuid,nodev,noexec,relatime - binfmt_misc binfmt_misc rw
157+39 28 0:30 / /run/user/foobar/gvfs rw,nosuid,nodev,relatime - fuse.gvfsd-fuse gvfsd-fuse rw,user_id=1000,group_id=1000"""
158+
159+ raring_btrfs_mountinfo = \
160+"""15 20 0:14 / /sys rw,nosuid,nodev,noexec,relatime - sysfs sysfs rw
161+16 20 0:3 / /proc rw,nosuid,nodev,noexec,relatime - proc proc rw
162+17 20 0:5 / /dev rw,relatime - devtmpfs udev rw,size=865556k,nr_inodes=216389,mode=755
163+18 17 0:11 / /dev/pts rw,nosuid,noexec,relatime - devpts devpts rw,gid=5,mode=620,ptmxmode=000
164+19 20 0:15 / /run rw,nosuid,relatime - tmpfs tmpfs rw,size=348196k,mode=755
165+20 1 0:16 /@ / rw,relatime - btrfs /dev/vda1 rw,compress=lzo,space_cache
166+21 15 0:19 / /sys/fs/fuse/connections rw,relatime - fusectl none rw
167+22 15 0:6 / /sys/kernel/debug rw,relatime - debugfs none rw
168+23 15 0:10 / /sys/kernel/security rw,relatime - securityfs none rw
169+24 19 0:20 / /run/lock rw,nosuid,nodev,noexec,relatime - tmpfs none rw,size=5120k
170+25 19 0:21 / /run/shm rw,nosuid,nodev,relatime - tmpfs none rw
171+26 19 0:22 / /run/user rw,nosuid,nodev,noexec,relatime - tmpfs none rw,size=102400k,mode=755
172+27 20 0:16 /@home /home rw,relatime - btrfs /dev/vda1 rw,compress=lzo,space_cache"""
173+
174+ def test_invalid_mountinfo(self):
175+ line = "20 1 252:1 / / rw,relatime - ext4 /dev/mapper/vg0-root rw,errors=remount-ro,data=ordered"
176+ elements = line.split()
177+ for i in range(len(elements) + 1):
178+ lines = [' '.join(elements[0:i])]
179+ if i < 10:
180+ expected = None
181+ else:
182+ expected = ('/dev/mapper/vg0-root', 'ext4', '/')
183+ self.assertEqual(expected, util.parse_mount_info('/', lines))
184+
185+ def test_precise_ext4_root(self):
186+ lines = TestMountinfoParsing.precise_ext4_mountinfo.splitlines()
187+
188+ expected = ('/dev/mapper/vg0-root', 'ext4', '/')
189+ self.assertEqual(expected, util.parse_mount_info('/', lines))
190+ self.assertEqual(expected, util.parse_mount_info('/usr', lines))
191+ self.assertEqual(expected, util.parse_mount_info('/usr/bin', lines))
192+
193+ expected = ('/dev/md0', 'ext4', '/boot')
194+ self.assertEqual(expected, util.parse_mount_info('/boot', lines))
195+ self.assertEqual(expected, util.parse_mount_info('/boot/grub', lines))
196+
197+ expected = ('/dev/mapper/vg0-root', 'ext4', '/')
198+ self.assertEqual(expected, util.parse_mount_info('/home', lines))
199+ self.assertEqual(expected, util.parse_mount_info('/home/me', lines))
200+
201+ expected = ('tmpfs', 'tmpfs', '/run')
202+ self.assertEqual(expected, util.parse_mount_info('/run', lines))
203+
204+ expected = ('none', 'tmpfs', '/run/lock')
205+ self.assertEqual(expected, util.parse_mount_info('/run/lock', lines))
206+
207+ def test_raring_btrfs_root(self):
208+ lines = TestMountinfoParsing.raring_btrfs_mountinfo.splitlines()
209+
210+ expected = ('/dev/vda1', 'btrfs', '/')
211+ self.assertEqual(expected, util.parse_mount_info('/', lines))
212+ self.assertEqual(expected, util.parse_mount_info('/usr', lines))
213+ self.assertEqual(expected, util.parse_mount_info('/usr/bin', lines))
214+ self.assertEqual(expected, util.parse_mount_info('/boot', lines))
215+ self.assertEqual(expected, util.parse_mount_info('/boot/grub', lines))
216+
217+ expected = ('/dev/vda1', 'btrfs', '/home')
218+ self.assertEqual(expected, util.parse_mount_info('/home', lines))
219+ self.assertEqual(expected, util.parse_mount_info('/home/me', lines))
220+
221+ expected = ('tmpfs', 'tmpfs', '/run')
222+ self.assertEqual(expected, util.parse_mount_info('/run', lines))
223+
224+ expected = ('none', 'tmpfs', '/run/lock')
225+ self.assertEqual(expected, util.parse_mount_info('/run/lock', lines))
226+
227 # vi: ts=4 expandtab