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",
258 "/devices/platform/brcm-ehci-1.1/usb2/2-1/2-1:1.0/host1/target1:0:0/1:0:0:0": "DVD Drive",
261 class HarddiskManager:
265 self.partitions = [ ]
267 self.on_partition_list_change = CList()
269 self.enumerateBlockDevices()
271 # currently, this is just an enumeration of what's possible,
272 # this probably has to be changed to support automount stuff.
273 # still, if stuff is mounted into the correct mountpoints by
274 # external tools, everything is fine (until somebody inserts
275 # a second usb stick.)
277 ("/media/hdd", _("Harddisk")),
278 ("/media/card", _("Card")),
279 ("/media/cf", _("Compact Flash")),
280 ("/media/mmc1", _("MMC Card")),
281 ("/media/net", _("Network Mount")),
282 ("/media/ram", _("Ram Disk")),
283 ("/media/usb", _("USB Stick")),
284 ("/", _("Internal Flash"))
288 self.partitions.append(Partition(mountpoint = x[0], description = x[1]))
290 def getBlockDevInfo(self, blockdev):
291 devpath = "/sys/block/" + blockdev
298 removable = bool(int(open(devpath + "/removable").read()))
299 dev = int(open(devpath + "/dev").read().split(':')[0])
300 if dev in [7, 31]: # loop, mtdblock
302 if blockdev[0:2] == 'sr':
304 if blockdev[0:2] == 'hd':
306 media = open("/proc/ide/%s/media" % blockdev).read()
307 if media.find("cdrom") != -1:
311 # check for partitions
313 for partition in listdir(devpath):
314 if partition[0:len(blockdev)] != blockdev:
316 partitions.append(partition)
324 open("/dev/" + blockdev).close()
326 if err.errno == 159: # no medium present
329 return error, blacklisted, removable, is_cdrom, partitions, medium_found
331 def enumerateBlockDevices(self):
332 print "enumerating block devices..."
333 for blockdev in listdir("/sys/block"):
334 error, blacklisted, removable, is_cdrom, partitions, medium_found = self.getBlockDevInfo(blockdev)
335 print "found block device '%s':" % blockdev,
337 print "error querying properties"
340 elif not medium_found:
343 print "ok, removable=%s, cdrom=%s, partitions=%s, device=%s" % (removable, is_cdrom, partitions, blockdev)
345 self.addHotplugPartition(blockdev)
346 for part in partitions:
347 self.addHotplugPartition(part)
349 def getAutofsMountpoint(self, device):
350 return "/autofs/%s/" % (device)
352 def addHotplugPartition(self, device, physdev = None):
354 dev, part = self.splitDeviceName(device)
356 physdev = readlink("/sys/block/" + dev + "/device")[5:]
359 print "couldn't determine blockdev physdev for device", device
361 # device is the device name, without /dev
362 # physdev is the physical device path, which we (might) use to determine the userfriendly name
363 description = self.getUserfriendlyDeviceName(device, physdev)
365 p = Partition(mountpoint = self.getAutofsMountpoint(device), description = description, force_mounted = True, device = device)
366 self.partitions.append(p)
367 self.on_partition_list_change("add", p)
369 # see if this is a harddrive
371 if l and device[l-1] not in string.digits:
372 error, blacklisted, removable, is_cdrom, partitions, medium_found = self.getBlockDevInfo(device)
373 if not blacklisted and not removable and not is_cdrom and medium_found:
374 self.hdd.append(Harddisk(device))
376 SystemInfo["Harddisk"] = len(self.hdd) > 0
378 def removeHotplugPartition(self, device):
379 mountpoint = self.getAutofsMountpoint(device)
380 for x in self.partitions[:]:
381 if x.mountpoint == mountpoint:
382 self.partitions.remove(x)
383 self.on_partition_list_change("remove", x)
385 if l and device[l-1] not in string.digits:
388 if hdd.device == device:
391 SystemInfo["Harddisk"] = len(self.hdd) > 0
399 hdd = hd.model() + " - " + hd.bus()
402 hdd += " (" + cap + ")"
403 list.append((hdd, hd))
409 def getMountedPartitions(self, onlyhotplug = False):
410 parts = [x for x in self.partitions if (x.is_hotplug or not onlyhotplug) and x.mounted()]
411 devs = set([x.device for x in parts])
412 for devname in devs.copy():
415 dev, part = self.splitDeviceName(devname)
416 if part and dev in devs: # if this is a partition and we still have the wholedisk, remove wholedisk
419 # return all devices which are not removed due to being a wholedisk when a partition exists
420 return [x for x in parts if not x.device or x.device in devs]
422 def splitDeviceName(self, devname):
423 # this works for: sdaX, hdaX, sr0 (which is in fact dev="sr0", part=""). It doesn't work for other names like mtdblock3, but they are blacklisted anyway.
427 if p not in string.digits:
429 return dev, part and int(part) or 0
431 def getUserfriendlyDeviceName(self, dev, phys):
432 dev, part = self.splitDeviceName(dev)
433 description = "External Storage %s" % dev
435 description = open("/sys" + phys + "/model").read().strip()
437 print "couldn't read model: ", s
438 for physdevprefix, pdescription in DEVICEDB.items():
439 if phys.startswith(physdevprefix):
440 description = pdescription
442 # not wholedisk and not partition 1
443 if part and part != 1:
444 description += " (Partition %d)" % part
447 harddiskmanager = HarddiskManager()