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