Misc s/func/certmaster/ replacements
[certmaster.git] / certmaster / minion / modules / mount.py
1 ##
2 ## Mount manager
3 ##
4 ## Copyright 2007, Red Hat, Inc
5 ## John Eckersberg <jeckersb@redhat.com>
6 ##
7 ## This software may be freely redistributed under the terms of the GNU
8 ## general public license.
9 ##
10 ## You should have received a copy of the GNU General Public License
11 ## along with this program; if not, write to the Free Software
12 ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
13 ##
14
15 import sub_process, os
16 import func_module
17
18
19 class MountModule(func_module.FuncModule):
20
21 version = "0.0.1"
22 api_version = "0.0.1"
23 description = "Mounting, unmounting and getting information on mounted filesystems."
24
25 def list(self):
26 cmd = sub_process.Popen(["/bin/cat", "/proc/mounts"], executable="/bin/cat", stdout=sub_process.PIPE, shell=False)
27 data = cmd.communicate()[0]
28
29 mounts = []
30 lines = [l for l in data.split("\n") if l] #why must you append blank crap?
31
32 for line in lines:
33 curmount = {}
34 tokens = line.split()
35 curmount['device'] = tokens[0]
36 curmount['dir'] = tokens[1]
37 curmount['type'] = tokens[2]
38 curmount['options'] = tokens[3]
39 mounts.append(curmount)
40
41 return mounts
42
43 def mount(self, device, dir, type="auto", options=None, createdir=False):
44 cmdline = ["/bin/mount", "-t", type]
45 if options:
46 cmdline.append("-o")
47 cmdline.append(options)
48 cmdline.append(device)
49 cmdline.append(dir)
50 if createdir:
51 try:
52 os.makedirs(dir)
53 except:
54 return False
55 cmd = sub_process.Popen(cmdline, executable="/bin/mount", stdout=sub_process.PIPE, shell=False)
56 if cmd.wait() == 0:
57 return True
58 else:
59 return False
60
61 def umount(self, dir, killall=False, force=False, lazy=False):
62 # succeed if its not mounted
63 if not os.path.ismount(dir):
64 return True
65
66 if killall:
67 cmd = sub_process.Popen(["/sbin/fuser", "-mk", dir], executable="/sbin/fuser", stdout=sub_process.PIPE, shell=False)
68 cmd.wait()
69
70 cmdline = ["/bin/umount"]
71 if force:
72 cmdline.append("-f")
73 if lazy:
74 cmdline.append("-l")
75 cmdline.append(dir)
76
77 cmd = sub_process.Popen(cmdline, executable="/bin/umount", stdout=sub_process.PIPE, shell=False)
78 if cmd.wait() == 0:
79 return True
80 else:
81 return False
82
83 def inventory(self, flatten=True):
84 return self.list()