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