Merge branch 'master' of git.opendreambox.org:/git/enigma2
[enigma2.git] / lib / python / Components / Harddisk.py
1 from os import system, listdir, statvfs, popen, makedirs, readlink, stat, major, minor
2 from Tools.Directories import SCOPE_HDD, resolveFilename
3 from Tools.CList import CList
4 from SystemInfo import SystemInfo
5 import string, time
6 from Components.Console import Console
7
8 def tryOpen(filename):
9         try:
10                 procFile = open(filename)
11         except IOError:
12                 return ""
13         return procFile
14
15 class Harddisk:
16         def __init__(self, device):
17                 self.device = device
18                 procfile = tryOpen("/sys/block/"+self.device+"/dev")
19                 tmp = procfile.readline().split(':')
20                 s_major = int(tmp[0])
21                 s_minor = int(tmp[1])
22                 self.max_idle_time = 0
23                 self.idle_running = False
24                 for disc in listdir("/dev/discs"):
25                         path = readlink('/dev/discs/'+disc)
26                         devidex = '/dev/discs/'+disc+'/'
27                         devidex2 = '/dev'+path[2:]+'/'
28                         disc = devidex2+'disc'
29                         ret = stat(disc).st_rdev
30                         if s_major == major(ret) and s_minor == minor(ret):
31                                 self.devidex = devidex
32                                 self.devidex2 = devidex2
33                                 print "new Harddisk", device, '->', self.devidex, '->', self.devidex2
34                                 self.startIdle()
35                                 break
36
37         def __lt__(self, ob):
38                 return self.device < ob.device
39
40         def bus(self):
41                 ide_cf = self.device.find("hd") == 0 and self.devidex2.find("host0") == -1 # 7025 specific
42                 internal = self.device.find("hd") == 0
43                 if ide_cf:
44                         ret = "External (CF)"
45                 elif internal:
46                         ret = "Internal"
47                 else:
48                         ret = "External"
49                 return ret
50
51         def diskSize(self):
52                 procfile = tryOpen("/sys/block/"+self.device+"/size")
53                 if procfile == "":
54                         return 0
55                 line = procfile.readline()
56                 procfile.close()
57                 try:
58                         cap = int(line)
59                 except:
60                         return 0;
61                 return cap / 1000 * 512 / 1000
62
63         def capacity(self):
64                 cap = self.diskSize()
65                 if cap == 0:
66                         return ""
67                 return "%d.%03d GB" % (cap/1024, cap%1024)
68
69         def model(self):
70                 if self.device.find("hd") == 0:
71                         procfile = tryOpen("/proc/ide/"+self.device+"/model")
72                         if procfile == "":
73                                 return ""
74                         line = procfile.readline()
75                         procfile.close()
76                         return line.strip()
77                 elif self.device.find("sd") == 0:
78                         procfile = tryOpen("/sys/block/"+self.device+"/device/vendor")
79                         if procfile == "":
80                                 return ""
81                         vendor = procfile.readline().strip()
82                         procfile.close()
83                         procfile = tryOpen("/sys/block/"+self.device+"/device/model")
84                         model = procfile.readline().strip()
85                         return vendor+'('+model+')'
86                 else:
87                         assert False, "no hdX or sdX"
88
89         def free(self):
90                 procfile = tryOpen("/proc/mounts")
91                 
92                 if procfile == "":
93                         return -1
94
95                 free = -1
96                 while 1:
97                         line = procfile.readline()
98                         if line == "":
99                                 break
100                         if line.startswith(self.devidex) or line.startswith(self.devidex2):
101                                 parts = line.strip().split(" ")
102                                 try:
103                                         stat = statvfs(parts[1])
104                                 except OSError:
105                                         continue
106                                 free = stat.f_bfree/1000 * stat.f_bsize/1000
107                                 break
108                 procfile.close()
109                 return free
110
111         def numPartitions(self):
112                 try:
113                         idedir = listdir(self.devidex)
114                 except OSError:
115                         return -1
116                 numPart = -1
117                 for filename in idedir:
118                         if filename.startswith("disc"):
119                                 numPart += 1
120                         if filename.startswith("part"):
121                                 numPart += 1
122                 return numPart
123
124         def unmount(self):
125                 procfile = tryOpen("/proc/mounts")
126
127                 if procfile == "":
128                         return -1
129
130                 cmd = "/bin/umount"
131
132                 for line in procfile:
133                         if line.startswith(self.devidex) or line.startswith(self.devidex2):
134                                 parts = line.split()
135                                 cmd = ' '.join([cmd, parts[1]])
136
137                 procfile.close()
138
139                 res = system(cmd)
140                 return (res >> 8)
141
142         def createPartition(self):
143                 cmd = "/sbin/sfdisk -f " + self.devidex + "disc"
144                 sfdisk = popen(cmd, "w")
145                 sfdisk.write("0,\n;\n;\n;\ny\n")
146                 sfdisk.close()
147                 return 0
148
149         def mkfs(self):
150                 cmd = "/sbin/mkfs.ext3 "
151                 if self.diskSize() > 4 * 1024:
152                         cmd += "-T largefile "
153                 cmd += "-m0 " + self.devidex + "part1"
154                 res = system(cmd)
155                 return (res >> 8)
156
157         def mount(self):
158                 cmd = "/bin/mount -t ext3 " + self.devidex + "part1"
159                 res = system(cmd)
160                 return (res >> 8)
161
162         def createMovieFolder(self):
163                 try:
164                         makedirs(resolveFilename(SCOPE_HDD))
165                 except OSError:
166                         return -1
167                 return 0
168
169         def fsck(self):
170                 # We autocorrect any failures
171                 # TODO: we could check if the fs is actually ext3
172                 cmd = "/sbin/fsck.ext3 -f -p " + self.devidex + "part1"
173                 res = system(cmd)
174                 return (res >> 8)
175
176         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")]
177
178         def initialize(self):
179                 self.unmount()
180
181                 if self.createPartition() != 0:
182                         return -1
183
184                 if self.mkfs() != 0:
185                         return -2
186
187                 if self.mount() != 0:
188                         return -3
189
190                 if self.createMovieFolder() != 0:
191                         return -4
192
193                 return 0
194
195         def check(self):
196                 self.unmount()
197
198                 res = self.fsck()
199                 if res & 2 == 2:
200                         return -6
201
202                 if res & 4 == 4:
203                         return -7
204
205                 if res != 0 and res != 1:
206                         # A sum containing 1 will also include a failure
207                         return -5
208
209                 if self.mount() != 0:
210                         return -3
211
212                 return 0
213         
214         def getDeviceDir(self):
215                 return self.devidex
216         
217         def getDeviceName(self):
218                 return self.getDeviceDir() + "disc"
219
220         # the HDD idle poll daemon.
221         # as some harddrives have a buggy standby timer, we are doing this by hand here.
222         # first, we disable the hardware timer. then, we check every now and then if
223         # any access has been made to the disc. If there has been no access over a specifed time,
224         # we set the hdd into standby.
225         def readStats(self):
226                 l = open("/sys/block/%s/stat" % self.device).read()
227                 nr_read = int(l[:8].strip())
228                 nr_write = int(l[4*9:4*9+8].strip())
229                 return nr_read, nr_write
230
231         def startIdle(self):
232                 self.last_access = time.time()
233                 self.last_stat = 0
234                 self.is_sleeping = False
235                 from enigma import eTimer
236
237                 # disable HDD standby timer
238                 Console().ePopen(("hdparm", "hdparm", "-S0", (self.devidex + "disc")))
239                 self.timer = eTimer()
240                 self.timer.callback.append(self.runIdle)
241                 self.idle_running = True
242                 self.setIdleTime(self.max_idle_time) # kick the idle polling loop
243
244         def runIdle(self):
245                 if not self.max_idle_time:
246                         return
247                 t = time.time()
248
249                 idle_time = t - self.last_access
250
251                 stats = self.readStats()
252                 print "nr_read", stats[0], "nr_write", stats[1]
253                 l = sum(stats)
254                 print "sum", l, "prev_sum", self.last_stat
255
256                 if l != self.last_stat: # access
257                         print "hdd was accessed since previous check!"
258                         self.last_stat = l
259                         self.last_access = t
260                         self.idle_time = 0
261                         self.is_sleeping = False
262                 else:
263                         print "hdd IDLE!"
264
265                 print "[IDLE]", idle_time, self.max_idle_time, self.is_sleeping
266                 if idle_time >= self.max_idle_time and not self.is_sleeping:
267                         self.setSleep()
268                         self.is_sleeping = True
269
270         def setSleep(self):
271                 Console().ePopen(("hdparm", "hdparm", "-y", (self.devidex + "disc")))
272
273         def setIdleTime(self, idle):
274                 self.max_idle_time = idle
275                 if self.idle_running:
276                         if not idle:
277                                 self.timer.stop()
278                         else:
279                                 self.timer.start(idle * 100, False)  # poll 10 times per period.
280
281         def isSleeping(self):
282                 return self.is_sleeping
283
284 class Partition:
285         def __init__(self, mountpoint, device = None, description = "", force_mounted = False):
286                 self.mountpoint = mountpoint
287                 self.description = description
288                 self.force_mounted = force_mounted
289                 self.is_hotplug = force_mounted # so far; this might change.
290                 self.device = device
291
292         def stat(self):
293                 return statvfs(self.mountpoint)
294
295         def free(self):
296                 try:
297                         s = self.stat()
298                         return s.f_bavail * s.f_bsize
299                 except OSError:
300                         return None
301         
302         def total(self):
303                 try:
304                         s = self.stat()
305                         return s.f_blocks * s.f_bsize
306                 except OSError:
307                         return None
308
309         def mounted(self):
310                 # THANK YOU PYTHON FOR STRIPPING AWAY f_fsid.
311                 # TODO: can os.path.ismount be used?
312                 if self.force_mounted:
313                         return True
314                 procfile = tryOpen("/proc/mounts")
315                 for n in procfile.readlines():
316                         if n.split(' ')[1] == self.mountpoint:
317                                 return True
318                 return False
319
320 DEVICEDB =  \
321         {
322                 # dm8000:
323                 "/devices/platform/brcm-ehci.0/usb1/1-1/1-1.1/1-1.1:1.0": "Front USB Slot",
324                 "/devices/platform/brcm-ehci.0/usb1/1-1/1-1.2/1-1.2:1.0": "Back, upper USB Slot",
325                 "/devices/platform/brcm-ehci.0/usb1/1-1/1-1.3/1-1.3:1.0": "Back, lower USB Slot",
326                 "/devices/platform/brcm-ehci-1.1/usb2/2-1/2-1:1.0/host1/target1:0:0/1:0:0:0": "DVD Drive",
327         }
328
329 class HarddiskManager:
330         def __init__(self):
331                 self.hdd = [ ]
332                 self.cd = ""
333                 self.partitions = [ ]
334                 
335                 self.on_partition_list_change = CList()
336                 
337                 self.enumerateBlockDevices()
338                 
339                 # currently, this is just an enumeration of what's possible,
340                 # this probably has to be changed to support automount stuff.
341                 # still, if stuff is mounted into the correct mountpoints by
342                 # external tools, everything is fine (until somebody inserts 
343                 # a second usb stick.)
344                 p = [
345                                         ("/media/hdd", _("Harddisk")), 
346                                         ("/media/card", _("Card")), 
347                                         ("/media/cf", _("Compact Flash")),
348                                         ("/media/mmc1", _("MMC Card")),
349                                         ("/media/net", _("Network Mount")),
350                                         ("/media/ram", _("Ram Disk")),
351                                         ("/media/usb", _("USB Stick")),
352                                         ("/", _("Internal Flash"))
353                                 ]
354                 
355                 for x in p:
356                         self.partitions.append(Partition(mountpoint = x[0], description = x[1]))
357
358         def getBlockDevInfo(self, blockdev):
359                 devpath = "/sys/block/" + blockdev
360                 error = False
361                 removable = False
362                 blacklisted = False
363                 is_cdrom = False
364                 partitions = []
365                 try:
366                         removable = bool(int(open(devpath + "/removable").read()))
367                         dev = int(open(devpath + "/dev").read().split(':')[0])
368                         if dev in [7, 31]: # loop, mtdblock
369                                 blacklisted = True
370                         if blockdev[0:2] == 'sr':
371                                 is_cdrom = True
372                         if blockdev[0:2] == 'hd':
373                                 try:
374                                         media = open("/proc/ide/%s/media" % blockdev).read()
375                                         if media.find("cdrom") != -1:
376                                                 is_cdrom = True
377                                 except IOError:
378                                         error = True
379                         # check for partitions
380                         if not is_cdrom:
381                                 for partition in listdir(devpath):
382                                         if partition[0:len(blockdev)] != blockdev:
383                                                 continue
384                                         partitions.append(partition)
385                         else:
386                                 self.cd = blockdev
387                 except IOError:
388                         error = True
389                 # check for medium
390                 medium_found = True
391                 try:
392                         open("/dev/" + blockdev).close()
393                 except IOError, err:
394                         if err.errno == 159: # no medium present
395                                 medium_found = False
396                         
397                 return error, blacklisted, removable, is_cdrom, partitions, medium_found
398
399         def enumerateBlockDevices(self):
400                 print "enumerating block devices..."
401                 for blockdev in listdir("/sys/block"):
402                         error, blacklisted, removable, is_cdrom, partitions, medium_found = self.getBlockDevInfo(blockdev)
403                         print "found block device '%s':" % blockdev, 
404                         if error:
405                                 print "error querying properties"
406                         elif blacklisted:
407                                 print "blacklisted"
408                         elif not medium_found:
409                                 print "no medium"
410                         else:
411                                 print "ok, removable=%s, cdrom=%s, partitions=%s, device=%s" % (removable, is_cdrom, partitions, blockdev)
412
413                                 self.addHotplugPartition(blockdev)
414                                 for part in partitions:
415                                         self.addHotplugPartition(part)
416
417         def getAutofsMountpoint(self, device):
418                 return "/autofs/%s/" % (device)
419
420         def addHotplugPartition(self, device, physdev = None):
421                 if not physdev:
422                         dev, part = self.splitDeviceName(device)
423                         try:
424                                 physdev = readlink("/sys/block/" + dev + "/device")[5:]
425                         except OSError:
426                                 physdev = dev
427                                 print "couldn't determine blockdev physdev for device", device
428
429                 # device is the device name, without /dev 
430                 # physdev is the physical device path, which we (might) use to determine the userfriendly name
431                 description = self.getUserfriendlyDeviceName(device, physdev)
432                 
433                 p = Partition(mountpoint = self.getAutofsMountpoint(device), description = description, force_mounted = True, device = device)
434                 self.partitions.append(p)
435                 self.on_partition_list_change("add", p)
436
437                 # see if this is a harddrive
438                 l = len(device)
439                 if l and device[l-1] not in string.digits:
440                         error, blacklisted, removable, is_cdrom, partitions, medium_found = self.getBlockDevInfo(device)
441                         if not blacklisted and not removable and not is_cdrom and medium_found:
442                                 self.hdd.append(Harddisk(device))
443                                 self.hdd.sort()
444                                 SystemInfo["Harddisk"] = len(self.hdd) > 0
445
446         def removeHotplugPartition(self, device):
447                 mountpoint = self.getAutofsMountpoint(device)
448                 for x in self.partitions[:]:
449                         if x.mountpoint == mountpoint:
450                                 self.partitions.remove(x)
451                                 self.on_partition_list_change("remove", x)
452                 l = len(device)
453                 if l and device[l-1] not in string.digits:
454                         idx = 0
455                         for hdd in self.hdd:
456                                 if hdd.device == device:
457                                         del self.hdd[idx]
458                                         break
459                         SystemInfo["Harddisk"] = len(self.hdd) > 0
460
461         def HDDCount(self):
462                 return len(self.hdd)
463
464         def HDDList(self):
465                 list = [ ]
466                 for hd in self.hdd:
467                         hdd = hd.model() + " - " + hd.bus()
468                         cap = hd.capacity()
469                         if cap != "":
470                                 hdd += " (" + cap + ")"
471                         list.append((hdd, hd))
472                 return list
473
474         def getCD(self):
475                 return self.cd
476
477         def getMountedPartitions(self, onlyhotplug = False):
478                 parts = [x for x in self.partitions if (x.is_hotplug or not onlyhotplug) and x.mounted()]
479                 devs = set([x.device for x in parts])
480                 for devname in devs.copy():
481                         if not devname:
482                                 continue
483                         dev, part = self.splitDeviceName(devname)
484                         if part and dev in devs: # if this is a partition and we still have the wholedisk, remove wholedisk
485                                 devs.remove(dev)
486
487                 # return all devices which are not removed due to being a wholedisk when a partition exists
488                 return [x for x in parts if not x.device or x.device in devs]
489
490         def splitDeviceName(self, devname):
491                 # 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.
492                 dev = devname[:3]
493                 part = devname[3:]
494                 for p in part:
495                         if p not in string.digits:
496                                 return devname, 0
497                 return dev, part and int(part) or 0
498
499         def getUserfriendlyDeviceName(self, dev, phys):
500                 dev, part = self.splitDeviceName(dev)
501                 description = "External Storage %s" % dev
502                 try:
503                         description = open("/sys" + phys + "/model").read().strip()
504                 except IOError, s:
505                         print "couldn't read model: ", s
506                 for physdevprefix, pdescription in DEVICEDB.items():
507                         if phys.startswith(physdevprefix):
508                                 description = pdescription
509
510                 # not wholedisk and not partition 1
511                 if part and part != 1:
512                         description += " (Partition %d)" % part
513                 return description
514
515 harddiskmanager = HarddiskManager()