blob: 24fc9b7dd5b670b1af8727e1b88fbdd3ef4920c6 [file] [log] [blame]
mblighd7fb4a62006-10-01 00:57:53 +00001__author__ = """Copyright Martin J. Bligh, Google, 2006"""
2
3import os
4from autotest_utils import *
5
6def list_mount_devices():
7 devices = []
8 # list mounted filesystems
9 for line in system_output('mount').splitlines():
10 devices.append(line.split()[0])
11 # list mounted swap devices
12 for line in system_output('swapon -s').splitlines():
13 if line.startswith('/'): # skip header line
14 devices.append(line.split()[0])
15 return devices
16
17
18def list_mount_points():
19 mountpoints = []
20 for line in system_output('mount').splitlines():
21 mountpoints.append(line.split()[2])
22 return mountpoints
23
24
25class filesystem:
26 """
27 Class for handling filesystems
28 """
29
30 def __init__(self, device, mountpoint):
31 """
32 device should be able to be a file as well
33 which we mount as loopback
34
35 device
36 The device in question (eg "/dev/hda2")
37 mountpoint
38 Default mountpoint for the device.
39 """
40 self.device = device
41 self.mountpoint = mountpoint
42
43
44 def mkfs(self, fstype = 'ext2'):
45 """
46 Format a partition to fstype
47 """
48 if list_mount_devices().count(self.device):
49 raise NameError('Attempted to format mounted device')
50 system("yes | mkfs -t %s %s" % (fstype, self.device))
51
52
53 def fsck(self, args = ''):
54 ret = system('fsck %s %s' % (self.device, args), ignorestatus=1)
55 return not ret
56
57
58 def mount(self, mountpoint=None):
59 if not mountpoint:
60 mountpoint = self.mountpoint
61 if list_mount_devices().count(self.device):
62 raise NameError('Attempted to mount mounted device')
63 if list_mount_points().count(mountpoint):
64 raise NameError('Attempted to mount busy mountpoint')
65 system("mount %s %s" % (self.device, mountpoint))
66
67
68 def unmount(self, handle=None):
69 if not handle:
70 handle = self.device
71 system("umount " + handle)