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