TranscodingSetup : fix misspelling name.
[vuplus_dvbapp] / lib / python / Components / Harddisk.py
1 from os import system, listdir, statvfs, popen, makedirs, stat, major, minor, path, access
2 #       ikseong
3 from Tools.Directories import SCOPE_HDD, resolveFilename, fileExists
4
5 from Tools.CList import CList
6 from SystemInfo import SystemInfo
7 import time
8 from Components.Console import Console
9
10 def MajorMinor(path):
11         rdev = stat(path).st_rdev
12         return (major(rdev),minor(rdev))
13
14 def readFile(filename):
15         file = open(filename)
16         data = file.read().strip()
17         file.close()
18         return data
19
20 DEVTYPE_UDEV = 0
21 DEVTYPE_DEVFS = 1
22
23 class Harddisk:
24         def __init__(self, device):
25                 self.device = device
26
27                 if access("/dev/.udev", 0):
28                         self.type = DEVTYPE_UDEV
29                 elif access("/dev/.devfsd", 0):
30                         self.type = DEVTYPE_DEVFS
31                 else:
32                         print "Unable to determine structure of /dev"
33
34                 self.max_idle_time = 0
35                 self.idle_running = False
36                 self.timer = None
37
38                 self.dev_path = ''
39                 self.disk_path = ''
40                 self.phys_path = path.realpath(self.sysfsPath('device'))
41
42                 if self.type == DEVTYPE_UDEV:
43                         self.dev_path = '/dev/' + self.device
44                         self.disk_path = self.dev_path
45
46                 elif self.type == DEVTYPE_DEVFS:
47                         tmp = readFile(self.sysfsPath('dev')).split(':')
48                         s_major = int(tmp[0])
49                         s_minor = int(tmp[1])
50                         for disc in listdir("/dev/discs"):
51                                 dev_path = path.realpath('/dev/discs/' + disc)
52                                 disk_path = dev_path + '/disc'
53                                 try:
54                                         rdev = stat(disk_path).st_rdev
55                                 except OSError:
56                                         continue
57                                 if s_major == major(rdev) and s_minor == minor(rdev):
58                                         self.dev_path = dev_path
59                                         self.disk_path = disk_path
60                                         break
61
62                 print "new Harddisk", self.device, '->', self.dev_path, '->', self.disk_path
63                 self.startIdle()
64
65         def __lt__(self, ob):
66                 return self.device < ob.device
67
68         def partitionPath(self, n):
69                 if self.type == DEVTYPE_UDEV:
70                         return self.dev_path + n
71                 elif self.type == DEVTYPE_DEVFS:
72                         return self.dev_path + '/part' + n
73
74         def sysfsPath(self, filename):
75                 return path.realpath('/sys/block/' + self.device + '/' + filename)
76
77         def stop(self):
78                 if self.timer:
79                         self.timer.stop()
80                         self.timer.callback.remove(self.runIdle)
81
82         def bus(self):
83                 # CF (7025 specific)
84                 if self.type == DEVTYPE_UDEV:
85                         ide_cf = False  # FIXME
86                 elif self.type == DEVTYPE_DEVFS:
87                         ide_cf = self.device[:2] == "hd" and "host0" not in self.dev_path
88
89                 internal = "pci" in self.phys_path
90
91                 if ide_cf:
92                         ret = "External (CF)"
93                 elif internal:
94                         ret = "Internal"
95                 else:
96                         ret = "External"
97                 return ret
98
99         def diskSize(self):
100                 #       ikseong
101                 try:
102                         line = readFile(self.sysfsPath('size'))
103                 except:
104                         harddiskmanager.removeHotplugPartition(self.device)
105                         print "error remove",self.device
106                         return -1
107                 try:
108                         cap = int(line)
109                 except:
110                         return 0;
111                 return cap / 1000 * 512 / 1000
112
113         def capacity(self):
114                 cap = self.diskSize()
115                 if cap == 0:
116                         return ""
117                 return "%d.%03d GB" % (cap/1000, cap%1000)
118
119         def model(self):
120                 #       ikseong
121                 try:
122                         if self.device[:2] == "hd":
123                                 return readFile('/proc/ide/' + self.device + '/model')
124                         elif self.device[:2] == "sd":
125                                 vendor = readFile(self.sysfsPath('device/vendor'))
126                                 model = readFile(self.sysfsPath('device/model'))
127                                 return vendor + '(' + model + ')'
128                         else:
129                                 assert False, "no hdX or sdX"
130                 except:
131                         harddiskmanager.removeHotplugPartition(self.device)
132                         print "error remove",self.device
133                         return -1
134
135         def free(self):
136                 try:
137                         mounts = open("/proc/mounts")
138                 except IOError:
139                         return -1
140
141                 lines = mounts.readlines()
142                 mounts.close()
143
144                 for line in lines:
145                         parts = line.strip().split(" ")
146                         real_path = path.realpath(parts[0])
147                         if not real_path[-1].isdigit():
148                                 continue
149                         try:
150                                 if MajorMinor(real_path) == MajorMinor(self.partitionPath(real_path[-1])):
151                                         stat = statvfs(parts[1])
152                                         return stat.f_bfree/1000 * stat.f_bsize/1000
153                         except OSError:
154                                 pass
155                 return -1
156
157         def numPartitions(self):
158                 numPart = -1
159                 if self.type == DEVTYPE_UDEV:
160                         try:
161                                 devdir = listdir('/dev')
162                         except OSError:
163                                 return -1
164                         for filename in devdir:
165                                 if filename.startswith(self.device):
166                                         numPart += 1
167
168                 elif self.type == DEVTYPE_DEVFS:
169                         try:
170                                 idedir = listdir(self.dev_path)
171                         except OSError:
172                                 return -1
173                         for filename in idedir:
174                                 if filename.startswith("disc"):
175                                         numPart += 1
176                                 if filename.startswith("part"):
177                                         numPart += 1
178                 return numPart
179
180         def unmount(self):
181                 try:
182                         mounts = open("/proc/mounts")
183                 except IOError:
184                         return -1
185
186                 lines = mounts.readlines()
187                 mounts.close()
188
189                 cmd = "umount"
190
191                 for line in lines:                                                                          
192                         parts = line.strip().split(" ")                                                     
193                         real_path = path.realpath(parts[0])                                                 
194                         if not real_path[-1].isdigit():                                                     
195                                 continue                                                                    
196                         try:                                                                                
197                                 if MajorMinor(real_path) == MajorMinor(self.partitionPath(real_path[-1])):
198                                         cmd = ' ' . join([cmd, parts[1]])
199                                         break
200                         except OSError:
201                                 pass
202
203                 res = system(cmd)
204                 return (res >> 8)
205
206         def checkPartionPath(self, path):
207                 import time, os
208                 for i in range(1,10):
209                         if os.path.exists(path):
210                                 return True
211                         time.sleep(1)
212                 return False
213
214         def createPartition(self):
215                 cmd = 'printf "8,\n;0,0\n;0,0\n;0,0\ny\n" | sfdisk -f -uS ' + self.disk_path
216                 res = system(cmd)
217                 if not self.checkPartionPath(self.partitionPath("1")):
218                         print "no exist : ", self.partitionPath("1")
219                         return 1
220                 return (res >> 8)
221
222         def mkfs(self):
223                 cmd = "mkfs.ext3 "
224                 if self.diskSize() > 4 * 1024:
225                         cmd += "-T largefile "
226                 cmd += "-m0 -O dir_index " + self.partitionPath("1")
227                 res = system(cmd)
228                 return (res >> 8)
229
230         def mount(self):
231                 try:
232                         fstab = open("/etc/fstab")
233                 except IOError:
234                         return -1
235
236                 lines = fstab.readlines()
237                 fstab.close()
238
239                 res = -1
240                 for line in lines:
241                         parts = line.strip().split(" ")
242                         real_path = path.realpath(parts[0])                                                 
243                         if not real_path[-1].isdigit():                                                     
244                                 continue                                                                    
245                         try:                                                                                
246                                 if MajorMinor(real_path) == MajorMinor(self.partitionPath(real_path[-1])):
247                                         cmd = "mount -t ext3 " + parts[0]
248                                         res = system(cmd)
249                                         break
250                         except OSError:
251                                 pass
252
253                 return (res >> 8)
254
255         def createMovieFolder(self):
256 #       ikseong
257                 try:
258                         if not fileExists("/hdd", 0):
259                                 print "not found /hdd"
260                                 system("ln -s /media/hdd /hdd")
261 #                               
262                         makedirs(resolveFilename(SCOPE_HDD))
263                 except OSError:
264                         return -1
265                 return 0
266
267         def fsck(self):
268                 # We autocorrect any failures
269                 # TODO: we could check if the fs is actually ext3
270                 cmd = "fsck.ext3 -f -p " + self.partitionPath("1")
271                 res = system(cmd)
272                 return (res >> 8)
273
274         def killPartition(self, n):
275                 part = self.partitionPath(n)
276
277                 if access(part, 0):
278                         cmd = 'dd bs=512 count=3 if=/dev/zero of=' + part
279                         res = system(cmd)
280                 else:
281                         res = 0
282
283                 return (res >> 8)
284
285         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")]
286
287         def initialize(self):
288                 self.unmount()
289
290                 # Udev tries to mount the partition immediately if there is an
291                 # old filesystem on it when fdisk reloads the partition table.
292                 # To prevent that, we overwrite the first 3 sectors of the
293                 # partition, if the partition existed before. That's enough for
294                 # ext3 at least.
295                 self.killPartition("1")
296
297                 if self.createPartition() != 0:
298                         return -1
299
300                 if self.mkfs() != 0:
301                         return -2
302
303                 if self.mount() != 0:
304                         return -3
305
306                 if self.createMovieFolder() != 0:
307                         return -4
308
309                 return 0
310
311         def check(self):
312                 self.unmount()
313
314                 res = self.fsck()
315                 if res & 2 == 2:
316                         return -6
317
318                 if res & 4 == 4:
319                         return -7
320
321                 if res != 0 and res != 1:
322                         # A sum containing 1 will also include a failure
323                         return -5
324
325                 if self.mount() != 0:
326                         return -3
327
328                 return 0
329
330         def getDeviceDir(self):
331                 return self.dev_path
332
333         def getDeviceName(self):
334                 return self.disk_path
335
336         # the HDD idle poll daemon.
337         # as some harddrives have a buggy standby timer, we are doing this by hand here.
338         # first, we disable the hardware timer. then, we check every now and then if
339         # any access has been made to the disc. If there has been no access over a specifed time,
340         # we set the hdd into standby.
341         def readStats(self):
342                 try:
343                         l = open("/sys/block/%s/stat" % self.device).read()
344                 except IOError:
345                         return -1,-1
346                 (nr_read, _, _, _, nr_write) = l.split()[:5]
347                 return int(nr_read), int(nr_write)
348
349         def startIdle(self):
350                 self.last_access = time.time()
351                 self.last_stat = 0
352                 self.is_sleeping = False
353                 from enigma import eTimer
354
355                 # disable HDD standby timer
356                 Console().ePopen(("hdparm", "hdparm", "-S0", self.disk_path))
357                 self.timer = eTimer()
358                 self.timer.callback.append(self.runIdle)
359                 self.idle_running = True
360                 self.setIdleTime(self.max_idle_time) # kick the idle polling loop
361
362         def runIdle(self):
363                 if not self.max_idle_time:
364                         return
365                 t = time.time()
366
367                 idle_time = t - self.last_access
368
369                 stats = self.readStats()
370                 #       ikseong
371                 if stats == -1:
372                         self.setIdleTime(0)
373                         return
374                 print "nr_read", stats[0], "nr_write", stats[1]
375                 l = sum(stats)
376                 print "sum", l, "prev_sum", self.last_stat
377
378                 if l != self.last_stat and l >= 0: # access
379                         print "hdd was accessed since previous check!"
380                         self.last_stat = l
381                         self.last_access = t
382                         idle_time = 0
383                         self.is_sleeping = False
384                 else:
385                         print "hdd IDLE!"
386
387                 print "[IDLE]", idle_time, self.max_idle_time, self.is_sleeping
388                 if idle_time >= self.max_idle_time and not self.is_sleeping:
389                         self.setSleep()
390                         self.is_sleeping = True
391
392         def setSleep(self):
393                 Console().ePopen(("hdparm", "hdparm", "-y", self.disk_path))
394
395         def setIdleTime(self, idle):
396                 self.max_idle_time = idle
397                 if self.idle_running:
398                         if not idle:
399                                 self.timer.stop()
400                         else:
401                                 self.timer.start(idle * 100, False)  # poll 10 times per period.
402
403         def isSleeping(self):
404                 return self.is_sleeping
405
406 class Partition:
407         def __init__(self, mountpoint, device = None, description = "", force_mounted = False):
408                 self.mountpoint = mountpoint
409                 self.description = description
410                 self.force_mounted = force_mounted
411                 self.is_hotplug = force_mounted # so far; this might change.
412                 self.device = device
413
414         def stat(self):
415                 return statvfs(self.mountpoint)
416
417         def free(self):
418                 try:
419                         s = self.stat()
420                         return s.f_bavail * s.f_bsize
421                 except OSError:
422                         return None
423
424         def total(self):
425                 try:
426                         s = self.stat()
427                         return s.f_blocks * s.f_bsize
428                 except OSError:
429                         return None
430
431         def mounted(self):
432                 # THANK YOU PYTHON FOR STRIPPING AWAY f_fsid.
433                 # TODO: can os.path.ismount be used?
434                 if self.force_mounted:
435                         return True
436
437                 try:
438                         mounts = open("/proc/mounts")
439                 except IOError:
440                         return False
441
442                 lines = mounts.readlines()
443                 mounts.close()
444
445                 for line in lines:
446                         if line.split(' ')[1] == self.mountpoint:
447                                 return True
448                 return False
449
450 DEVICEDB_SR = \
451         {"dm8000":
452                 {
453                         "/devices/pci0000:01/0000:01:00.0/host0/target0:0:0/0:0:0:0": _("DVD Drive"),
454                         "/devices/pci0000:01/0000:01:00.0/host1/target1:0:0/1:0:0:0": _("DVD Drive"),
455                         "/devices/platform/brcm-ehci-1.1/usb2/2-1/2-1:1.0/host3/target3:0:0/3:0:0:0": _("DVD Drive"),
456                 },
457         "dm800":
458         {
459         },
460         "dm7025":
461         {
462         }
463         }
464
465 DEVICEDB = \
466         {"dm8000":
467                 {
468                         "/devices/platform/brcm-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0": _("Front USB Slot"),
469                         "/devices/platform/brcm-ehci.0/usb1/1-1/1-1.2/1-1.2:1.0": _("Back, upper USB Slot"),
470                         "/devices/platform/brcm-ehci.0/usb1/1-1/1-1.3/1-1.3:1.0": _("Back, lower USB Slot"),
471                         "/devices/platform/brcm-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0": _("Front USB Slot"),
472                         "/devices/platform/brcm-ehci-1.1/usb2/2-1/2-1:1.0/": _("Internal USB Slot"),
473                         "/devices/platform/brcm-ohci-1.1/usb4/4-1/4-1:1.0/": _("Internal USB Slot"),
474                 },
475         "dm800":
476         {
477                 "/devices/platform/brcm-ehci.0/usb1/1-2/1-2:1.0": "Upper USB Slot",
478                 "/devices/platform/brcm-ehci.0/usb1/1-1/1-1:1.0": "Lower USB Slot",
479         },
480         "dm7025":
481         {
482                 "/devices/pci0000:00/0000:00:14.1/ide1/1.0": "CF Card Slot", #hdc
483                 "/devices/pci0000:00/0000:00:14.1/ide0/0.0": "Internal Harddisk"
484         }
485         }
486
487 class HarddiskManager:
488         def __init__(self):
489                 self.hdd = [ ]
490                 self.cd = ""
491                 self.partitions = [ ]
492                 self.devices_scanned_on_init = [ ]
493
494                 self.on_partition_list_change = CList()
495
496                 self.enumerateBlockDevices()
497
498                 # currently, this is just an enumeration of what's possible,
499                 # this probably has to be changed to support automount stuff.
500                 # still, if stuff is mounted into the correct mountpoints by
501                 # external tools, everything is fine (until somebody inserts
502                 # a second usb stick.)
503                 p = [
504                                         ("/media/hdd", _("Harddisk")),
505                                         ("/media/card", _("Card")),
506                                         ("/media/cf", _("Compact Flash")),
507                                         ("/media/mmc1", _("MMC Card")),
508                                         ("/media/net", _("Network Mount")),
509                                         ("/media/ram", _("Ram Disk")),
510                                         ("/media/usb", _("USB Stick")),
511                                         ("/", _("Internal Flash"))
512                                 ]
513
514                 self.partitions.extend([ Partition(mountpoint = x[0], description = x[1]) for x in p ])
515
516         def getBlockDevInfo(self, blockdev):
517                 devpath = "/sys/block/" + blockdev
518                 error = False
519                 removable = False
520                 blacklisted = False
521                 is_cdrom = False
522                 partitions = []
523                 try:
524                         removable = bool(int(readFile(devpath + "/removable")))
525                         dev = int(readFile(devpath + "/dev").split(':')[0])
526                         if dev in (7, 31): # loop, mtdblock
527                                 blacklisted = True
528                         if blockdev[0:2] == 'sr':
529                                 is_cdrom = True
530                         if blockdev[0:2] == 'hd':
531                                 try:
532                                         media = readFile("/proc/ide/%s/media" % blockdev)
533                                         if "cdrom" in media:
534                                                 is_cdrom = True
535                                 except IOError:
536                                         error = True
537                         # check for partitions
538                         if not is_cdrom:
539                                 for partition in listdir(devpath):
540                                         if partition[0:len(blockdev)] != blockdev:
541                                                 continue
542                                         partitions.append(partition)
543                         else:
544                                 self.cd = blockdev
545                 except IOError:
546                         error = True
547                 # check for medium
548                 medium_found = True
549                 try:
550                         open("/dev/" + blockdev).close()
551                 except IOError, err:
552                         if err.errno == 159: # no medium present
553                                 medium_found = False
554
555                 return error, blacklisted, removable, is_cdrom, partitions, medium_found
556
557         def enumerateBlockDevices(self):
558                 print "enumerating block devices..."
559                 for blockdev in listdir("/sys/block"):
560                         error, blacklisted, removable, is_cdrom, partitions, medium_found = self.addHotplugPartition(blockdev)
561                         if not error and not blacklisted:
562                                 if medium_found:
563                                         for part in partitions:
564                                                 self.addHotplugPartition(part)
565                                 self.devices_scanned_on_init.append((blockdev, removable, is_cdrom, medium_found))
566
567         def getAutofsMountpoint(self, device):
568                 return "/autofs/%s/" % (device)
569
570
571         def is_hard_mounted(self, device):
572                 mounts = file('/proc/mounts').read().split('\n')
573                 for x in mounts:
574                         if x.find('/autofs') == -1 and x.find(device) != -1:
575                                 return True
576                 return False
577
578         def addHotplugPartition(self, device, physdev = None):
579                 if not physdev:
580                         dev, part = self.splitDeviceName(device)
581                         try:
582                                 physdev = path.realpath('/sys/block/' + dev + '/device')[4:]
583                         except OSError:
584                                 physdev = dev
585                                 print "couldn't determine blockdev physdev for device", device
586
587                 error, blacklisted, removable, is_cdrom, partitions, medium_found = self.getBlockDevInfo(device)
588                 print "found block device '%s':" % device,
589
590                 if blacklisted:
591                         print "blacklisted"
592                 else:
593                         if error:
594                                 print "error querying properties"
595                         elif not medium_found:
596                                 print "no medium"
597                         else:
598                                 print "ok, removable=%s, cdrom=%s, partitions=%s" % (removable, is_cdrom, partitions)
599
600                         l = len(device)
601                         if l:
602                                 # see if this is a harddrive
603                                 if not device[l-1].isdigit() and not removable and not is_cdrom:
604                                         self.hdd.append(Harddisk(device))
605                                         self.hdd.sort()
606                                         SystemInfo["Harddisk"] = len(self.hdd) > 0
607
608                                 if (not removable or medium_found) and not self.is_hard_mounted(device):
609                                         # device is the device name, without /dev
610                                         # physdev is the physical device path, which we (might) use to determine the userfriendly name
611                                         description = self.getUserfriendlyDeviceName(device, physdev)
612                                         p = Partition(mountpoint = self.getAutofsMountpoint(device), description = description, force_mounted = True, device = device)
613                                         self.partitions.append(p)
614                                         self.on_partition_list_change("add", p)
615
616                 return error, blacklisted, removable, is_cdrom, partitions, medium_found
617
618         def removeHotplugPartition(self, device):
619                 mountpoint = self.getAutofsMountpoint(device)
620                 for x in self.partitions[:]:
621                         if x.mountpoint == mountpoint:
622                                 self.partitions.remove(x)
623                                 self.on_partition_list_change("remove", x)
624                 l = len(device)
625                 if l and not device[l-1].isdigit():
626                         for hdd in self.hdd:
627                                 if hdd.device == device:
628                                         hdd.stop()
629                                         self.hdd.remove(hdd)
630                                         break
631                         SystemInfo["Harddisk"] = len(self.hdd) > 0
632
633         def HDDCount(self):
634                 return len(self.hdd)
635
636         def HDDList(self):
637                 list = [ ]
638                 for hd in self.hdd:
639                         #       ikseong
640                         if hd.model() == -1:
641                                 continue
642                         hdd = hd.model() + " - " + hd.bus()
643                         cap = hd.capacity()
644                         if cap != "":
645                                 hdd += " (" + cap + ")"
646                         list.append((hdd, hd))
647                 return list
648
649         def getCD(self):
650                 return self.cd
651
652         def getMountedPartitions(self, onlyhotplug = False):
653                 parts = [x for x in self.partitions if (x.is_hotplug or not onlyhotplug) and x.mounted()]
654                 devs = set([x.device for x in parts])
655                 for devname in devs.copy():
656                         if not devname:
657                                 continue
658                         dev, part = self.splitDeviceName(devname)
659                         if part and dev in devs: # if this is a partition and we still have the wholedisk, remove wholedisk
660                                 devs.remove(dev)
661
662                 # return all devices which are not removed due to being a wholedisk when a partition exists
663                 return [x for x in parts if not x.device or x.device in devs]
664
665         def splitDeviceName(self, devname):
666                 # 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.
667                 dev = devname[:3]
668                 part = devname[3:]
669                 for p in part:
670                         if not p.isdigit():
671                                 return devname, 0
672                 return dev, part and int(part) or 0
673
674         def getUserfriendlyDeviceName(self, dev, phys):
675                 dev, part = self.splitDeviceName(dev)
676                 description = "External Storage %s" % dev
677                 have_model_descr = False
678                 try:
679                         description = readFile("/sys" + phys + "/model")
680                         have_model_descr = True
681                 except IOError, s:
682                         print "couldn't read model: ", s
683                 from Tools.HardwareInfo import HardwareInfo
684                 if dev.find('sr') == 0 and dev[2].isdigit():
685                         devicedb = DEVICEDB_SR
686                 else:
687                         devicedb = DEVICEDB
688                 for physdevprefix, pdescription in devicedb.get(HardwareInfo().device_name,{}).items():
689                         if phys.startswith(physdevprefix):
690                                 if have_model_descr:
691                                         description = pdescription + ' - ' + description
692                                 else:
693                                         description = pdescription
694                 # not wholedisk and not partition 1
695                 if part and part != 1:
696                         description += " (Partition %d)" % part
697                 return description
698
699         def addMountedPartition(self, device, desc):
700                 already_mounted = False
701                 for x in self.partitions[:]:
702                         if x.mountpoint == device:
703                                 already_mounted = True
704                 if not already_mounted:
705                         self.partitions.append(Partition(mountpoint = device, description = desc))
706
707         def removeMountedPartition(self, mountpoint):
708                 for x in self.partitions[:]:
709                         if x.mountpoint == mountpoint:
710                                 self.partitions.remove(x)
711                                 self.on_partition_list_change("remove", x)
712
713 harddiskmanager = HarddiskManager()