1 from os import system, listdir, statvfs, popen, makedirs
3 from Tools.Directories import SCOPE_HDD, resolveFilename
4 from Tools.CList import CList
6 from SystemInfo import SystemInfo
10 procFile = open(filename)
16 return "/proc/ide/hd" + ("a","b","c","d","e","f","g","h","i")[num] + "/"
19 def __init__(self, index):
22 host = (self.index & 2) >> 1
24 target = (self.index & 1)
26 self.prochdx = num2prochdx(index)
27 self.devidex = "/dev/ide/host%d/bus%d/target%d/lun0/" % (host, bus, target)
36 ret = "External (CF) - "
46 procfile = tryOpen(self.prochdx + "capacity")
51 line = procfile.readline()
59 return cap / 1000 * 512 / 1000
66 return "%d.%03d GB" % (cap/1024, cap%1024)
69 procfile = tryOpen(self.prochdx + "model")
74 line = procfile.readline()
80 procfile = tryOpen("/proc/mounts")
87 line = procfile.readline()
90 if line.startswith(self.devidex):
91 parts = line.strip().split(" ")
93 stat = statvfs(parts[1])
96 free = stat.f_bfree/1000 * stat.f_bsize/1000
101 def numPartitions(self):
103 idedir = listdir(self.devidex)
107 for filename in idedir:
108 if filename.startswith("disc"):
110 if filename.startswith("part"):
115 procfile = tryOpen("/proc/mounts")
122 for line in procfile:
123 if line.startswith(self.devidex):
125 cmd = ' '.join([cmd, parts[1]])
132 def createPartition(self):
133 cmd = "/sbin/sfdisk -f " + self.devidex + "disc"
134 sfdisk = popen(cmd, "w")
135 sfdisk.write("0,\n;\n;\n;\ny\n")
140 cmd = "/sbin/mkfs.ext3 "
141 if self.diskSize() > 4 * 1024:
142 cmd += "-T largefile "
143 cmd += "-m0 " + self.devidex + "part1"
148 cmd = "/bin/mount -t ext3 " + self.devidex + "part1"
152 def createMovieFolder(self):
154 makedirs(resolveFilename(SCOPE_HDD))
160 # We autocorrect any failures
161 # TODO: we could check if the fs is actually ext3
162 cmd = "/sbin/fsck.ext3 -f -p " + self.devidex + "part1"
166 errorList = [ _("Everything is fine"), _("Creating partition failed"), _("Mkfs failed"), _("Mount failed"), _("Create movie folder failed"), _("Fsck failed"), _("Please Reboot"), _("Filesystem contains uncorrectable errors"), _("Unmount failed")]
168 def initialize(self):
171 if self.createPartition() != 0:
177 if self.mount() != 0:
180 #only create a movie folder on the internal hdd
181 if not self.index & 2 and self.createMovieFolder() != 0:
196 if res != 0 and res != 1:
197 # A sum containing 1 will also include a failure
200 if self.mount() != 0:
206 mediafile = tryOpen(num2prochdx(num) + "media")
211 line = mediafile.readline()
214 if line.startswith("disk"):
220 def __init__(self, mountpoint, description = "", force_mounted = False):
221 self.mountpoint = mountpoint
222 self.description = description
223 self.force_mounted = force_mounted
224 self.is_hotplug = force_mounted # so far; this might change.
227 return statvfs(self.mountpoint)
232 return s.f_bavail * s.f_bsize
239 return s.f_blocks * s.f_bsize
244 # THANK YOU PYTHON FOR STRIPPING AWAY f_fsid.
245 # TODO: can os.path.ismount be used?
246 if self.force_mounted:
248 procfile = tryOpen("/proc/mounts")
249 for n in procfile.readlines():
250 if n.split(' ')[1] == self.mountpoint:
254 class HarddiskManager:
259 self.partitions = [ ]
261 self.on_partition_list_change = CList()
263 for hddNum in range(8):
265 hdd = Harddisk(hddNum)
268 self.enumerateBlockDevices()
270 SystemInfo["Harddisc"] = len(self.hdd) > 0
272 # currently, this is just an enumeration of what's possible,
273 # this probably has to be changed to support automount stuff.
274 # still, if stuff is mounted into the correct mountpoints by
275 # external tools, everything is fine (until somebody inserts
276 # a second usb stick.)
278 ("/media/hdd", _("Harddisk")),
279 ("/media/card", _("Card")),
280 ("/media/cf", _("Compact Flash")),
281 ("/media/mmc1", _("MMC Card")),
282 ("/media/net", _("Network Mount")),
283 ("/media/ram", _("Ram Disk")),
284 ("/media/usb", _("USB Stick")),
285 ("/", _("Internal Flash"))
289 self.partitions.append(Partition(mountpoint = x[0], description = x[1]))
291 def enumerateBlockDevices(self):
292 print "enumerating block devices..."
294 for blockdev in os.listdir("/sys/block"):
295 devpath = "/sys/block/" + blockdev
302 removable = bool(int(open(devpath + "/removable").read()))
303 dev = int(open(devpath + "/dev").read().split(':')[0])
304 if dev in [7, 31]: # loop, mtdblock
306 if blockdev[0:2] == 'sr':
308 if blockdev[0:2] == 'hd':
310 media = open("/proc/ide/%s/media" % blockdev).read()
311 if media.find("cdrom") != -1:
315 # check for partitions
317 for partition in os.listdir(devpath):
318 if partition[0:len(blockdev)] != blockdev:
320 partitions.append(partition)
323 print "found block device '%s':" % blockdev,
325 print "error querying properties"
329 print "ok, removable=%s, cdrom=%s, partitions=%s" % (removable, is_cdrom, partitions)
330 self.addHotplugPartition(blockdev, blockdev)
331 for part in partitions:
332 self.addHotplugPartition(part, part)
334 def getAutofsMountpoint(self, device):
335 return "/autofs/%s/" % (device)
337 def addHotplugPartition(self, device, description):
338 p = Partition(mountpoint = self.getAutofsMountpoint(device), description = description, force_mounted = True)
339 self.partitions.append(p)
340 self.on_partition_list_change("add", p)
342 def removeHotplugPartition(self, device):
343 mountpoint = self.getAutofsMountpoint(device)
344 for x in self.partitions[:]:
345 if x.mountpoint == mountpoint:
346 self.partitions.remove(x)
347 self.on_partition_list_change("remove", x)
355 hdd = hd.model() + " ("
361 list.append((hdd, hd))
365 def getMountedPartitions(self, onlyhotplug = False):
366 return [x for x in self.partitions if (x.is_hotplug or not onlyhotplug) and x.mounted()]
368 harddiskmanager = HarddiskManager()