1 from os import system, listdir, statvfs, popen, makedirs, stat, major, minor, path, access
3 from Tools.Directories import SCOPE_HDD, resolveFilename, fileExists
5 from Tools.CList import CList
6 from SystemInfo import SystemInfo
8 from Components.Console import Console
13 rdev = stat(path).st_rdev
14 return (major(rdev),minor(rdev))
16 def readFile(filename):
18 data = file.read().strip()
24 mounts = open("/proc/mounts", 'r')
26 print "[Harddisk] Failed to open /proc/mounts", ex
28 result = [line.strip().split(' ') for line in mounts]
30 # Spaces are encoded as \040 in mounts
31 item[1] = item[1].replace('\\040', ' ')
35 cmd = 'sfdisk --version'
36 lines = popen(cmd).readlines()
38 if l.find("sfdisk from util-linux") != -1:
39 ver = l.split()[-1].strip()
44 ver = '.'.join(vs[:2])
48 print "[CheckSfdiskVer] check parted version Failed!"
52 def enableUdevEvent(enable = True):
54 option = '--start-exec-queue'
56 option = '--stop-exec-queue'
57 cmd = "udevadm control %s" % option
61 def findMountPoint(path):
62 'Example: findMountPoint("/media/hdd/some/file") returns "/media/hdd"'
63 path = os.path.abspath(path)
64 while not os.path.ismount(path):
65 path = os.path.dirname(path)
68 def getDeviceFile(dev_path):
69 for parts in getProcMounts():
70 if os.path.realpath(parts[1]).startswith(dev_path):
73 def getMountPath(mountPath):
74 mountPath = os.path.realpath(mountPath)
75 while not os.path.ismount(mountPath):
76 mountPath = os.path.dirname(mountPath)
80 def getDeviceInterface(mountPath):
81 mountPath = getMountPath(mountPath)
86 from Components.Harddisk import getDeviceFile
87 dev = getDeviceFile(mountPath)
89 if dev and dev.startswith("/dev/sd"):
90 dev = os.path.basename(dev)
91 phyPath = os.path.realpath('/sys/block/' + dev[:3])
94 for x in glob.glob("/sys/bus/usb/devices/usb*"):
95 if phyPath.find(os.path.realpath(x)) != -1:
96 from Tools.HardwareInfo import HardwareInfo
97 if (HardwareInfo().get_vu_device_name() == "zero4k") and (phyPath.find("f0b00500.ehci_v2/usb2/2-1") != -1):
103 if phyPath.find("/ata") != -1:
108 def isUsbStorage(dirname):
109 return getDeviceInterface(dirname) in (None, "usb")
115 def __init__(self, device):
118 if access("/dev/.udev", 0):
119 self.type = DEVTYPE_UDEV
120 elif access("/dev/.devfsd", 0):
121 self.type = DEVTYPE_DEVFS
123 print "Unable to determine structure of /dev"
125 self.max_idle_time = 0
126 self.idle_running = False
131 self.mount_path = None
132 self.mount_device = None
133 self.phys_path = path.realpath(self.sysfsPath('device'))
135 if self.type == DEVTYPE_UDEV:
136 self.dev_path = '/dev/' + self.device
137 self.disk_path = self.dev_path
139 elif self.type == DEVTYPE_DEVFS:
140 tmp = readFile(self.sysfsPath('dev')).split(':')
141 s_major = int(tmp[0])
142 s_minor = int(tmp[1])
143 for disc in listdir("/dev/discs"):
144 dev_path = path.realpath('/dev/discs/' + disc)
145 disk_path = dev_path + '/disc'
147 rdev = stat(disk_path).st_rdev
150 if s_major == major(rdev) and s_minor == minor(rdev):
151 self.dev_path = dev_path
152 self.disk_path = disk_path
155 print "new Harddisk", self.device, '->', self.dev_path, '->', self.disk_path
158 def __lt__(self, ob):
159 return self.device < ob.device
161 def partitionPath(self, n):
162 if self.type == DEVTYPE_UDEV:
163 return self.dev_path + n
164 elif self.type == DEVTYPE_DEVFS:
165 return self.dev_path + '/part' + n
167 def sysfsPath(self, filename):
168 return path.realpath('/sys/block/' + self.device + '/' + filename)
173 self.timer.callback.remove(self.runIdle)
177 if self.type == DEVTYPE_UDEV:
178 ide_cf = False # FIXME
179 elif self.type == DEVTYPE_DEVFS:
180 ide_cf = self.device[:2] == "hd" and "host0" not in self.dev_path
182 internal = "pci" in self.phys_path
185 ret = "External (CF)"
194 line = readFile(self.sysfsPath('size'))
196 harddiskmanager.removeHotplugPartition(self.device)
197 print "error remove",self.device
203 return cap / 1000 * 512 / 1000
206 cap = self.diskSize()
209 return "%d.%03d GB" % (cap/1000, cap%1000)
213 if self.device[:2] == "hd":
214 return readFile('/proc/ide/' + self.device + '/model')
215 elif self.device[:2] == "sd":
216 vendor = readFile(self.sysfsPath('device/vendor'))
217 model = readFile(self.sysfsPath('device/model'))
218 return vendor + '(' + model + ')'
220 assert False, "no hdX or sdX"
222 harddiskmanager.removeHotplugPartition(self.device)
223 print "error remove",self.device
228 mounts = open("/proc/mounts")
232 lines = mounts.readlines()
236 parts = line.strip().split(" ")
237 real_path = path.realpath(parts[0])
238 if not real_path[-1].isdigit():
241 if MajorMinor(real_path) == MajorMinor(self.partitionPath(real_path[-1])):
242 stat = statvfs(parts[1])
243 return stat.f_bfree/1000 * stat.f_bsize/1000
248 def numPartitions(self):
250 if self.type == DEVTYPE_UDEV:
252 devdir = listdir('/dev')
255 for filename in devdir:
256 if filename.startswith(self.device):
259 elif self.type == DEVTYPE_DEVFS:
261 idedir = listdir(self.dev_path)
264 for filename in idedir:
265 if filename.startswith("disc"):
267 if filename.startswith("part"):
271 def mountDevice(self):
272 for parts in getProcMounts():
273 if path.realpath(parts[0]).startswith(self.dev_path):
274 self.mount_device = parts[0]
275 self.mount_path = parts[1]
279 if self.mount_path is None:
280 return self.mountDevice()
281 return self.mount_path
285 mounts = open("/proc/mounts")
289 lines = mounts.readlines()
295 parts = line.strip().split(" ")
296 real_path = path.realpath(parts[0])
297 if not real_path[-1].isdigit():
300 if MajorMinor(real_path) == MajorMinor(self.partitionPath(real_path[-1])):
301 cmd = ' ' . join([cmd, parts[1]])
310 def checkPartionPath(self, path):
312 for i in range(1,10):
313 if os.path.exists(path):
318 def updatePartition(self):
319 sfdiskVer = CheckSfdiskVer()
320 if sfdiskVer < 2.26: # sfdisk -R option is deprecated at sfdiskVer >= 2.26
321 cmd = 'sfdisk -R %s; sleep 5' % (self.disk_path)
322 elif path.exists('/usr/sbin/partprobe'):
323 cmd = 'partprobe %s; sleep 5' % (self.disk_path)
324 elif path.exists('/usr/sbin/partx'):
325 cmd = 'partx -u %s' % (self.disk_path)
334 def createPartition(self):
335 def CheckPartedVer():
336 cmd = 'parted --version'
337 lines = popen(cmd).readlines()
339 if l.find("parted (GNU parted)") != -1:
340 ver = l.split()[3].strip()
345 print "[CheckPartedVer] check parted version Failed!"
349 disk_size = self.diskSize()
351 if disk_size > 2.2 * 1000 * 1000: # if 2.2 TB
353 partedVer = CheckPartedVer()
354 if partedVer >= 2.1: # align option is supported in version 2.1 or later
355 setAlign = "--align optimal"
356 cmd = 'parted %s %s --script mklabel gpt mkpart disk ext2 0%% 100%%' % ( setAlign, self.disk_path )
359 sfdiskVer = CheckSfdiskVer()
360 if sfdiskVer <= 2.21:
361 cmd = 'printf "8,\n;0,0\n;0,0\n;0,0\ny\n" | sfdisk -f -uS ' + self.disk_path
363 cmd = 'printf "8,\nquit\nY\n" | sfdisk -f -uS ' + self.disk_path
368 if not self.checkPartionPath(self.partitionPath("1")):
369 print "no exist : ", self.partitionPath("1")
375 if self.diskSize() > 4 * 1024:
376 cmd += "-T largefile "
377 cmd += "-m0 -O dir_index " + self.partitionPath("1")
384 fstab = open("/etc/fstab")
388 lines = fstab.readlines()
394 parts = line.strip().split(" ")
395 real_path = path.realpath(parts[0])
396 if not real_path[-1].isdigit():
399 if MajorMinor(real_path) == MajorMinor(self.partitionPath(real_path[-1])):
400 mount_point = parts[0]
405 if mount_point is None:
408 cmd = "mount -t ext3 " + mount_point
415 if self.createMovieFolder() != 0:
420 def createMovieFolder(self):
422 if not fileExists("/hdd", 0):
423 print "not found /hdd"
424 system("ln -s /media/hdd /hdd")
426 makedirs(resolveFilename(SCOPE_HDD))
432 # We autocorrect any failures
433 # TODO: we could check if the fs is actually ext3
434 cmd = "fsck.ext3 -f -p " + self.partitionPath("1")
438 def killPartition(self, n):
439 part = self.partitionPath(n)
442 cmd = 'dd bs=512 count=3 if=/dev/zero of=' + part
450 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"), _("partx failed")]
452 def initialize(self):
453 enableUdevEvent(False)
456 # Udev tries to mount the partition immediately if there is an
457 # old filesystem on it when fdisk reloads the partition table.
458 # To prevent that, we overwrite the first 3 sectors of the
459 # partition, if the partition existed before. That's enough for
461 self.killPartition("1")
463 if self.updatePartition() != 0:
466 elif self.createPartition() != 0:
469 elif self.updatePartition() != 0:
472 elif self.mkfs() != 0:
478 enableUdevEvent(True)
491 if res != 0 and res != 1:
492 # A sum containing 1 will also include a failure
495 if self.mount() != 0:
500 def getDeviceDir(self):
503 def getDeviceName(self):
504 return self.disk_path
506 # the HDD idle poll daemon.
507 # as some harddrives have a buggy standby timer, we are doing this by hand here.
508 # first, we disable the hardware timer. then, we check every now and then if
509 # any access has been made to the disc. If there has been no access over a specifed time,
510 # we set the hdd into standby.
513 l = open("/sys/block/%s/stat" % self.device).read()
516 (nr_read, _, _, _, nr_write) = l.split()[:5]
517 return int(nr_read), int(nr_write)
520 self.last_access = time.time()
522 self.is_sleeping = False
523 from enigma import eTimer
525 # disable HDD standby timer
526 Console().ePopen(("hdparm", "hdparm", "-S0", self.disk_path))
527 self.timer = eTimer()
528 self.timer.callback.append(self.runIdle)
529 self.idle_running = True
530 self.setIdleTime(self.max_idle_time) # kick the idle polling loop
533 if not self.max_idle_time:
537 idle_time = t - self.last_access
539 stats = self.readStats()
544 print "nr_read", stats[0], "nr_write", stats[1]
546 print "sum", l, "prev_sum", self.last_stat
548 if l != self.last_stat and l >= 0: # access
549 print "hdd was accessed since previous check!"
553 self.is_sleeping = False
557 print "[IDLE]", idle_time, self.max_idle_time, self.is_sleeping
558 if idle_time >= self.max_idle_time and not self.is_sleeping:
560 self.is_sleeping = True
563 Console().ePopen(("hdparm", "hdparm", "-y", self.disk_path))
565 def setIdleTime(self, idle):
566 self.max_idle_time = idle
567 if self.idle_running:
571 self.timer.start(idle * 100, False) # poll 10 times per period.
573 def isSleeping(self):
574 return self.is_sleeping
577 def __init__(self, mountpoint, device = None, description = "", force_mounted = False):
578 self.mountpoint = mountpoint
579 self.description = description
580 self.force_mounted = force_mounted
581 self.is_hotplug = force_mounted # so far; this might change.
585 return statvfs(self.mountpoint)
590 return s.f_bavail * s.f_bsize
597 return s.f_blocks * s.f_bsize
602 # THANK YOU PYTHON FOR STRIPPING AWAY f_fsid.
603 # TODO: can os.path.ismount be used?
604 if self.force_mounted:
608 mounts = open("/proc/mounts")
612 lines = mounts.readlines()
616 if line.split(' ')[1] == self.mountpoint:
623 "/devices/pci0000:01/0000:01:00.0/host0/target0:0:0/0:0:0:0": _("DVD Drive"),
624 "/devices/pci0000:01/0000:01:00.0/host1/target1:0:0/1:0:0:0": _("DVD Drive"),
625 "/devices/platform/brcm-ehci-1.1/usb2/2-1/2-1:1.0/host3/target3:0:0/3:0:0:0": _("DVD Drive"),
638 "/devices/platform/brcm-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0": _("Front USB Slot"),
639 "/devices/platform/brcm-ehci.0/usb1/1-1/1-1.2/1-1.2:1.0": _("Back, upper USB Slot"),
640 "/devices/platform/brcm-ehci.0/usb1/1-1/1-1.3/1-1.3:1.0": _("Back, lower USB Slot"),
641 "/devices/platform/brcm-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0": _("Front USB Slot"),
642 "/devices/platform/brcm-ehci-1.1/usb2/2-1/2-1:1.0/": _("Internal USB Slot"),
643 "/devices/platform/brcm-ohci-1.1/usb4/4-1/4-1:1.0/": _("Internal USB Slot"),
647 "/devices/platform/brcm-ehci.0/usb1/1-2/1-2:1.0": "Upper USB Slot",
648 "/devices/platform/brcm-ehci.0/usb1/1-1/1-1:1.0": "Lower USB Slot",
652 "/devices/pci0000:00/0000:00:14.1/ide1/1.0": "CF Card Slot", #hdc
653 "/devices/pci0000:00/0000:00:14.1/ide0/0.0": "Internal Harddisk"
657 class HarddiskManager:
661 self.partitions = [ ]
662 self.devices_scanned_on_init = [ ]
664 self.on_partition_list_change = CList()
666 self.enumerateBlockDevices()
668 # currently, this is just an enumeration of what's possible,
669 # this probably has to be changed to support automount stuff.
670 # still, if stuff is mounted into the correct mountpoints by
671 # external tools, everything is fine (until somebody inserts
672 # a second usb stick.)
674 ("/media/hdd", _("Harddisk")),
675 ("/media/card", _("Card")),
676 ("/media/cf", _("Compact Flash")),
677 ("/media/mmc1", _("MMC Card")),
678 ("/media/net", _("Network Mount")),
679 ("/media/ram", _("Ram Disk")),
680 ("/media/usb", _("USB Stick")),
681 ("/", _("Internal Flash"))
684 self.partitions.extend([ Partition(mountpoint = x[0], description = x[1]) for x in p ])
686 def getBlockDevInfo(self, blockdev):
687 devpath = "/sys/block/" + blockdev
694 removable = bool(int(readFile(devpath + "/removable")))
695 dev = int(readFile(devpath + "/dev").split(':')[0])
696 if dev in (7, 31, 179): # loop, mtdblock, mmcblock
698 if blockdev[0:2] == 'sr':
700 if blockdev[0:2] == 'hd':
702 media = readFile("/proc/ide/%s/media" % blockdev)
707 # check for partitions
709 for partition in listdir(devpath):
710 if partition[0:len(blockdev)] != blockdev:
712 partitions.append(partition)
720 open("/dev/" + blockdev).close()
722 if err.errno == 159: # no medium present
725 return error, blacklisted, removable, is_cdrom, partitions, medium_found
727 def enumerateBlockDevices(self):
728 print "enumerating block devices..."
729 for blockdev in listdir("/sys/block"):
730 error, blacklisted, removable, is_cdrom, partitions, medium_found = self.addHotplugPartition(blockdev)
731 if not error and not blacklisted:
733 for part in partitions:
734 self.addHotplugPartition(part)
735 self.devices_scanned_on_init.append((blockdev, removable, is_cdrom, medium_found))
737 def getAutofsMountpoint(self, device):
738 return "/autofs/%s/" % (device)
741 def is_hard_mounted(self, device):
742 mounts = file('/proc/mounts').read().split('\n')
744 if x.find('/autofs') == -1 and x.find(device) != -1:
748 def getMountpoint(self, device):
749 dev = "/dev/%s" % device
750 for item in getProcMounts():
751 if item[0] == dev and item[1].find('/autofs') == -1:
755 def addHotplugPartition(self, device, physdev = None):
757 dev, part = self.splitDeviceName(device)
759 physdev = path.realpath('/sys/block/' + dev + '/device')[4:]
762 print "couldn't determine blockdev physdev for device", device
764 error, blacklisted, removable, is_cdrom, partitions, medium_found = self.getBlockDevInfo(device)
765 print "found block device '%s':" % device,
771 print "error querying properties"
772 elif not medium_found:
775 print "ok, removable=%s, cdrom=%s, partitions=%s" % (removable, is_cdrom, partitions)
779 # see if this is a harddrive
780 if not device[l-1].isdigit() and not removable and not is_cdrom:
781 self.hdd.append(Harddisk(device))
783 SystemInfo["Harddisk"] = len(self.hdd) > 0
785 if not removable or medium_found:
786 # device is the device name, without /dev
787 # physdev is the physical device path, which we (might) use to determine the userfriendly name
788 description = self.getUserfriendlyDeviceName(device, physdev)
789 p = Partition(mountpoint = self.getAutofsMountpoint(device), description = description, force_mounted = True, device = device)
790 self.partitions.append(p)
791 self.on_partition_list_change("add", p)
793 return error, blacklisted, removable, is_cdrom, partitions, medium_found
795 def removeHotplugPartition(self, device):
796 for x in self.partitions[:]:
797 if x.device == device:
798 self.partitions.remove(x)
800 self.on_partition_list_change("remove", x)
802 if l and not device[l-1].isdigit():
804 if hdd.device == device:
808 SystemInfo["Harddisk"] = len(self.hdd) > 0
818 hdd = hd.model() + " - " + hd.bus()
821 hdd += " (" + cap + ")"
822 list.append((hdd, hd))
828 def getMountedPartitions(self, onlyhotplug = False):
829 parts = [x for x in self.partitions if (x.is_hotplug or not onlyhotplug) and x.mounted() and x.mountpoint]
830 devs = set([x.device for x in parts])
831 for devname in devs.copy():
834 dev, part = self.splitDeviceName(devname)
835 if part and dev in devs: # if this is a partition and we still have the wholedisk, remove wholedisk
838 # remove duplicate device
839 no_force_mounted_list = [x.mountpoint for x in self.partitions if not x.force_mounted]
842 mp = self.getMountpoint(x.device)
843 if mp and mp in no_force_mounted_list:
844 devs.remove(x.device)
846 # return all devices which are not removed due to being a wholedisk when a partition exists
847 return [x for x in parts if not x.device or x.device in devs]
849 def getLabelFromDevName(self, dev):
852 data = os.popen("e2label %s" % dev).read().strip()
860 def splitDeviceName(self, devname):
861 # 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.
867 return dev, part and int(part) or 0
869 def getUserfriendlyDeviceName(self, dev, phys):
870 label = self.getLabelFromDevName("/dev/" + dev)
871 dev, part = self.splitDeviceName(dev)
872 description = "External Storage %s" % dev
873 have_model_descr = False
877 have_model_descr = True
880 description = readFile("/sys" + phys + "/model")
881 have_model_descr = True
883 print "couldn't read model: ", s
884 from Tools.HardwareInfo import HardwareInfo
885 if dev.find('sr') == 0 and dev[2].isdigit():
886 devicedb = DEVICEDB_SR
889 for physdevprefix, pdescription in devicedb.get(HardwareInfo().device_name,{}).items():
890 if phys.startswith(physdevprefix):
892 description = pdescription + ' - ' + description
894 description = pdescription
895 # not wholedisk and not partition 1
896 if part and part != 1:
897 description += " (Partition %d)" % part
900 def addMountedPartition(self, device, desc):
901 already_mounted = False
902 for x in self.partitions[:]:
903 if x.mountpoint == device:
904 already_mounted = True
905 if not already_mounted:
906 self.partitions.append(Partition(mountpoint = device, description = desc))
908 def removeMountedPartition(self, mountpoint):
909 for x in self.partitions[:]:
910 if x.mountpoint == mountpoint:
911 self.partitions.remove(x)
912 self.on_partition_list_change("remove", x)
914 harddiskmanager = HarddiskManager()