1 from os import system, listdir, statvfs, popen, makedirs, readlink, stat, major, minor
2 from Tools.Directories import SCOPE_HDD, resolveFilename
3 from Tools.CList import CList
4 from SystemInfo import SystemInfo
9 procFile = open(filename)
15 def __init__(self, device):
17 procfile = tryOpen("/sys/block/"+self.device+"/dev")
18 tmp = procfile.readline().split(':')
21 for disc in listdir("/dev/discs"):
22 path = readlink('/dev/discs/'+disc)
23 devidex = '/dev/discs/'+disc+'/'
24 devidex2 = '/dev'+path[2:]+'/'
25 disc = devidex2+'disc'
26 ret = stat(disc).st_rdev
27 if s_major == major(ret) and s_minor == minor(ret):
28 self.devidex = devidex
29 self.devidex2 = devidex2
30 print "new Harddisk", device, '->', self.devidex, '->', self.devidex2
34 return self.device < ob.device
37 ide_cf = self.device.find("hd") == 0 and self.devidex2.find("host0") == -1 # 7025 specific
38 internal = self.device.find("hd") == 0
48 procfile = tryOpen("/sys/block/"+self.device+"/size")
51 line = procfile.readline()
57 return cap / 1000 * 512 / 1000
63 return "%d.%03d GB" % (cap/1024, cap%1024)
66 if self.device.find("hd") == 0:
67 procfile = tryOpen("/proc/ide/"+self.device+"/model")
70 line = procfile.readline()
73 elif self.device.find("sd") == 0:
74 procfile = tryOpen("/sys/block/"+self.device+"/device/vendor")
77 vendor = procfile.readline().strip()
79 procfile = tryOpen("/sys/block/"+self.device+"/device/model")
80 model = procfile.readline().strip()
81 return vendor+'('+model+')'
83 assert False, "no hdX or sdX"
86 procfile = tryOpen("/proc/mounts")
93 line = procfile.readline()
96 if line.startswith(self.devidex):
97 parts = line.strip().split(" ")
99 stat = statvfs(parts[1])
102 free = stat.f_bfree/1000 * stat.f_bsize/1000
107 def numPartitions(self):
109 idedir = listdir(self.devidex)
113 for filename in idedir:
114 if filename.startswith("disc"):
116 if filename.startswith("part"):
121 procfile = tryOpen("/proc/mounts")
128 for line in procfile:
129 if line.startswith(self.devidex):
131 cmd = ' '.join([cmd, parts[1]])
138 def createPartition(self):
139 cmd = "/sbin/sfdisk -f " + self.devidex + "disc"
140 sfdisk = popen(cmd, "w")
141 sfdisk.write("0,\n;\n;\n;\ny\n")
146 cmd = "/sbin/mkfs.ext3 "
147 if self.diskSize() > 4 * 1024:
148 cmd += "-T largefile "
149 cmd += "-m0 " + self.devidex + "part1"
154 cmd = "/bin/mount -t ext3 " + self.devidex + "part1"
158 def createMovieFolder(self):
160 makedirs(resolveFilename(SCOPE_HDD))
166 # We autocorrect any failures
167 # TODO: we could check if the fs is actually ext3
168 cmd = "/sbin/fsck.ext3 -f -p " + self.devidex + "part1"
172 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")]
174 def initialize(self):
177 if self.createPartition() != 0:
183 if self.mount() != 0:
186 if self.createMovieFolder() != 0:
201 if res != 0 and res != 1:
202 # A sum containing 1 will also include a failure
205 if self.mount() != 0:
210 def getDeviceDir(self):
213 def getDeviceName(self):
214 return self.getDeviceDir() + "disc"
217 def __init__(self, mountpoint, device = None, description = "", force_mounted = False):
218 self.mountpoint = mountpoint
219 self.description = description
220 self.force_mounted = force_mounted
221 self.is_hotplug = force_mounted # so far; this might change.
225 return statvfs(self.mountpoint)
230 return s.f_bavail * s.f_bsize
237 return s.f_blocks * s.f_bsize
242 # THANK YOU PYTHON FOR STRIPPING AWAY f_fsid.
243 # TODO: can os.path.ismount be used?
244 if self.force_mounted:
246 procfile = tryOpen("/proc/mounts")
247 for n in procfile.readlines():
248 if n.split(' ')[1] == self.mountpoint:
255 "devices/platform/brcm-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0": "Front USB Slot",
256 "devices/platform/brcm-ehci.0/usb1/1-1/1-1.2/1-1.2:1.0": "Back, upper USB Slot",
257 "devices/platform/brcm-ehci.0/usb1/1-1/1-1.3/1-1.3:1.0": "Back, lower USB Slot",
260 class HarddiskManager:
264 self.partitions = [ ]
266 self.on_partition_list_change = CList()
268 self.enumerateBlockDevices()
270 # currently, this is just an enumeration of what's possible,
271 # this probably has to be changed to support automount stuff.
272 # still, if stuff is mounted into the correct mountpoints by
273 # external tools, everything is fine (until somebody inserts
274 # a second usb stick.)
276 ("/media/hdd", _("Harddisk")),
277 ("/media/card", _("Card")),
278 ("/media/cf", _("Compact Flash")),
279 ("/media/mmc1", _("MMC Card")),
280 ("/media/net", _("Network Mount")),
281 ("/media/ram", _("Ram Disk")),
282 ("/media/usb", _("USB Stick")),
283 ("/", _("Internal Flash"))
287 self.partitions.append(Partition(mountpoint = x[0], description = x[1]))
289 def getBlockDevInfo(self, blockdev):
290 devpath = "/sys/block/" + blockdev
297 removable = bool(int(open(devpath + "/removable").read()))
298 dev = int(open(devpath + "/dev").read().split(':')[0])
299 if dev in [7, 31]: # loop, mtdblock
301 if blockdev[0:2] == 'sr':
303 if blockdev[0:2] == 'hd':
305 media = open("/proc/ide/%s/media" % blockdev).read()
306 if media.find("cdrom") != -1:
310 # check for partitions
312 for partition in listdir(devpath):
313 if partition[0:len(blockdev)] != blockdev:
315 partitions.append(partition)
323 open("/dev/" + blockdev).close()
325 if err.errno == 159: # no medium present
328 return error, blacklisted, removable, is_cdrom, partitions, medium_found
330 def enumerateBlockDevices(self):
331 print "enumerating block devices..."
332 for blockdev in listdir("/sys/block"):
333 error, blacklisted, removable, is_cdrom, partitions, medium_found = self.getBlockDevInfo(blockdev)
334 print "found block device '%s':" % blockdev,
336 print "error querying properties"
339 elif not medium_found:
342 print "ok, removable=%s, cdrom=%s, partitions=%s, device=%s" % (removable, is_cdrom, partitions, blockdev)
344 self.addHotplugPartition(blockdev)
345 for part in partitions:
346 self.addHotplugPartition(part)
348 def getAutofsMountpoint(self, device):
349 return "/autofs/%s/" % (device)
351 def addHotplugPartition(self, device, physdev = None):
353 dev, part = self.splitDeviceName(device)
355 physdev = readlink("/sys/block/" + dev + "/device")[6:]
357 print "couldn't determine blockdev physdev for device", dev, "try", device, "now"
359 physdev = readlink("/sys/block/" + device + "/device")[6:]
362 print "couldn't determine blockdev physdev for device", device
364 # device is the device name, without /dev
365 # physdev is the physical device path, which we (might) use to determine the userfriendly name
366 description = self.getUserfriendlyDeviceName(device, physdev)
368 p = Partition(mountpoint = self.getAutofsMountpoint(device), description = description, force_mounted = True, device = device)
369 self.partitions.append(p)
370 self.on_partition_list_change("add", p)
372 # see if this is a harddrive
374 if l and device[l-1] not in string.digits:
375 error, blacklisted, removable, is_cdrom, partitions, medium_found = self.getBlockDevInfo(device)
376 if not blacklisted and not removable and not is_cdrom and medium_found:
377 self.hdd.append(Harddisk(device))
379 SystemInfo["Harddisk"] = len(self.hdd) > 0
381 def removeHotplugPartition(self, device):
382 mountpoint = self.getAutofsMountpoint(device)
383 for x in self.partitions[:]:
384 if x.mountpoint == mountpoint:
385 self.partitions.remove(x)
386 self.on_partition_list_change("remove", x)
388 if l and device[l-1] not in string.digits:
391 if hdd.device == device:
394 SystemInfo["Harddisk"] = len(self.hdd) > 0
402 hdd = hd.model() + " - " + hd.bus()
405 hdd += " (" + cap + ")"
406 list.append((hdd, hd))
412 def getMountedPartitions(self, onlyhotplug = False):
413 parts = [x for x in self.partitions if (x.is_hotplug or not onlyhotplug) and x.mounted()]
414 devs = set([x.device for x in parts])
415 for devname in devs.copy():
418 dev, part = self.splitDeviceName(devname)
419 if part and dev in devs: # if this is a partition and we still have the wholedisk, remove wholedisk
422 # return all devices which are not removed due to being a wholedisk when a partition exists
423 return [x for x in parts if not x.device or x.device in devs]
425 def splitDeviceName(self, devname):
429 if i in string.digits:
433 return dev, part and int(part) or 0
435 def getUserfriendlyDeviceName(self, dev, phys):
436 dev, part = self.splitDeviceName(dev)
437 description = "External Storage %s" % dev
439 description = open("/sys/" + phys + "/model").read().strip()
441 print "couldn't read model (from /sys/" + phys + "/model): ", s
442 for physdevprefix, pdescription in DEVICEDB.items():
443 if phys.startswith(physdevprefix):
444 description = pdescription
446 # not wholedisk and not partition 1
447 if part and part != 1:
448 description += " (Partition %d)" % part
451 harddiskmanager = HarddiskManager()