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