]>
Commit | Line | Data |
---|---|---|
d2e6a577 FG |
1 | import errno |
2 | import os | |
3 | import pwd | |
4 | import platform | |
5 | import uuid | |
6 | from ceph_volume import process | |
7 | from . import as_string | |
8 | ||
9 | ||
10 | # TODO: get these out of here and into a common area for others to consume | |
11 | if platform.system() == 'FreeBSD': | |
12 | FREEBSD = True | |
13 | DEFAULT_FS_TYPE = 'zfs' | |
14 | PROCDIR = '/compat/linux/proc' | |
15 | # FreeBSD does not have blockdevices any more | |
16 | BLOCKDIR = '/dev' | |
17 | ROOTGROUP = 'wheel' | |
18 | else: | |
19 | FREEBSD = False | |
20 | DEFAULT_FS_TYPE = 'xfs' | |
21 | PROCDIR = '/proc' | |
22 | BLOCKDIR = '/sys/block' | |
23 | ROOTGROUP = 'root' | |
24 | ||
25 | ||
26 | def generate_uuid(): | |
27 | return str(uuid.uuid4()) | |
28 | ||
29 | ||
30 | def get_ceph_user_ids(): | |
31 | """ | |
32 | Return the id and gid of the ceph user | |
33 | """ | |
34 | try: | |
35 | user = pwd.getpwnam('ceph') | |
36 | except KeyError: | |
37 | # is this even possible? | |
38 | raise RuntimeError('"ceph" user is not available in the current system') | |
39 | return user[2], user[3] | |
40 | ||
41 | ||
42 | def mkdir_p(path, chown=True): | |
43 | """ | |
44 | A `mkdir -p` that defaults to chown the path to the ceph user | |
45 | """ | |
46 | try: | |
47 | os.mkdir(path) | |
48 | except OSError as e: | |
49 | if e.errno == errno.EEXIST: | |
50 | pass | |
51 | else: | |
52 | raise | |
53 | if chown: | |
54 | uid, gid = get_ceph_user_ids() | |
55 | os.chown(path, uid, gid) | |
56 | ||
57 | ||
58 | def chown(path, recursive=True): | |
59 | """ | |
60 | ``chown`` a path to the ceph user (uid and guid fetched at runtime) | |
61 | """ | |
62 | uid, gid = get_ceph_user_ids() | |
63 | if os.path.islink(path): | |
64 | path = os.path.realpath(path) | |
65 | if recursive: | |
66 | process.run(['chown', '-R', 'ceph:ceph', path]) | |
67 | else: | |
68 | os.chown(path, uid, gid) | |
69 | ||
70 | ||
71 | def is_mounted(source, destination=None): | |
72 | """ | |
73 | Check if the given device is mounted, optionally validating destination. | |
74 | This relies on absolute path devices, it will ignore non-absolute | |
75 | entries like:: | |
76 | ||
77 | tmpfs /run tmpfs rw,seclabel,nosuid,nodev,mode=755 0 0 | |
78 | ||
79 | But will parse paths that are absolute like:: | |
80 | ||
81 | /dev/sdc2 /boot xfs rw,attr2,inode64,noquota 0 0 | |
82 | ||
83 | When destination is passed in, it will check that the entry where the | |
84 | source appears is mounted to where destination defines. This is useful so | |
85 | that an error message can report that a source is not mounted at an | |
86 | expected destination. | |
87 | """ | |
88 | dev = os.path.realpath(source) | |
89 | with open(PROCDIR + '/mounts', 'rb') as proc_mounts: | |
90 | for line in proc_mounts: | |
91 | fields = line.split() | |
92 | if len(fields) < 3: | |
93 | continue | |
94 | mounted_device = fields[0] | |
95 | mounted_path = fields[1] | |
96 | if os.path.isabs(mounted_device) and os.path.exists(mounted_device): | |
97 | mounted_device = os.path.realpath(mounted_device) | |
98 | if as_string(mounted_device) == dev: | |
99 | if destination: | |
100 | destination = os.path.realpath(destination) | |
101 | return destination == as_string(os.path.realpath(mounted_path)) | |
102 | else: | |
103 | return True | |
104 | return False |