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
8 procFile = open(filename)
14 def __init__(self, device):
16 procfile = tryOpen("/sys/block/"+self.device+"/dev")
17 tmp = procfile.readline().split(':')
20 for disc in listdir("/dev/discs"):
21 path = readlink('/dev/discs/'+disc)
22 devidex = '/dev/discs/'+disc+'/'
23 devidex2 = '/dev'+path[2:]+'/'
24 disc = devidex2+'disc'
25 ret = stat(disc).st_rdev
26 if s_major == major(ret) and s_minor == minor(ret):
27 self.devidex = devidex
28 self.devidex2 = devidex2
29 print "new Harddisk", device, '->', self.devidex, '->', self.devidex2
33 return self.device < ob.device
36 ide_cf = self.device.find("hd") == 0 and self.devidex2.find("host0") == -1 # 7025 specific
37 internal = self.device.find("hd") == 0
47 procfile = tryOpen("/sys/block/"+self.device+"/size")
50 line = procfile.readline()
56 return cap / 1000 * 512 / 1000
62 return "%d.%03d GB" % (cap/1024, cap%1024)
65 if self.device.find("hd") == 0:
66 procfile = tryOpen("/proc/ide/"+self.device+"/model")
69 line = procfile.readline()
72 elif self.device.find("sd") == 0:
73 procfile = tryOpen("/sys/block/"+self.device+"/device/vendor")
76 vendor = procfile.readline().strip()
78 procfile = tryOpen("/sys/block/"+self.device+"/device/model")
79 model = procfile.readline().strip()
80 return vendor+'('+model+')'
82 assert False, "no hdX or sdX"
85 procfile = tryOpen("/proc/mounts")
92 line = procfile.readline()
95 if line.startswith(self.devidex):
96 parts = line.strip().split(" ")
98 stat = statvfs(parts[1])
101 free = stat.f_bfree/1000 * stat.f_bsize/1000
106 def numPartitions(self):
108 idedir = listdir(self.devidex)
112 for filename in idedir:
113 if filename.startswith("disc"):
115 if filename.startswith("part"):
120 procfile = tryOpen("/proc/mounts")
127 for line in procfile:
128 if line.startswith(self.devidex):
130 cmd = ' '.join([cmd, parts[1]])
137 def createPartition(self):
138 cmd = "/sbin/sfdisk -f " + self.devidex + "disc"
139 sfdisk = popen(cmd, "w")
140 sfdisk.write("0,\n;\n;\n;\ny\n")
145 cmd = "/sbin/mkfs.ext3 "
146 if self.diskSize() > 4 * 1024:
147 cmd += "-T largefile "
148 cmd += "-m0 " + self.devidex + "part1"
153 cmd = "/bin/mount -t ext3 " + self.devidex + "part1"
157 def createMovieFolder(self):
159 makedirs(resolveFilename(SCOPE_HDD))
165 # We autocorrect any failures
166 # TODO: we could check if the fs is actually ext3
167 cmd = "/sbin/fsck.ext3 -f -p " + self.devidex + "part1"
171 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")]
173 def initialize(self):
176 if self.createPartition() != 0:
182 if self.mount() != 0:
185 if self.createMovieFolder() != 0:
200 if res != 0 and res != 1:
201 # A sum containing 1 will also include a failure
204 if self.mount() != 0:
209 def getDeviceDir(self):
212 def getDeviceName(self):
213 return self.getDeviceDir() + "disc"
216 def __init__(self, mountpoint, description = "", force_mounted = False):
217 self.mountpoint = mountpoint
218 self.description = description
219 self.force_mounted = force_mounted
220 self.is_hotplug = force_mounted # so far; this might change.
223 return statvfs(self.mountpoint)
228 return s.f_bavail * s.f_bsize
235 return s.f_blocks * s.f_bsize
240 # THANK YOU PYTHON FOR STRIPPING AWAY f_fsid.
241 # TODO: can os.path.ismount be used?
242 if self.force_mounted:
244 procfile = tryOpen("/proc/mounts")
245 for n in procfile.readlines():
246 if n.split(' ')[1] == self.mountpoint:
250 class HarddiskManager:
254 self.partitions = [ ]
256 self.on_partition_list_change = CList()
258 self.enumerateBlockDevices()
260 # currently, this is just an enumeration of what's possible,
261 # this probably has to be changed to support automount stuff.
262 # still, if stuff is mounted into the correct mountpoints by
263 # external tools, everything is fine (until somebody inserts
264 # a second usb stick.)
266 ("/media/hdd", _("Harddisk")),
267 ("/media/card", _("Card")),
268 ("/media/cf", _("Compact Flash")),
269 ("/media/mmc1", _("MMC Card")),
270 ("/media/net", _("Network Mount")),
271 ("/media/ram", _("Ram Disk")),
272 ("/media/usb", _("USB Stick")),
273 ("/", _("Internal Flash"))
277 self.partitions.append(Partition(mountpoint = x[0], description = x[1]))
279 def getBlockDevInfo(self, blockdev):
280 devpath = "/sys/block/" + blockdev
287 removable = bool(int(open(devpath + "/removable").read()))
288 dev = int(open(devpath + "/dev").read().split(':')[0])
289 if dev in [7, 31]: # loop, mtdblock
291 if blockdev[0:2] == 'sr':
293 if blockdev[0:2] == 'hd':
295 media = open("/proc/ide/%s/media" % blockdev).read()
296 if media.find("cdrom") != -1:
300 # check for partitions
302 for partition in listdir(devpath):
303 if partition[0:len(blockdev)] != blockdev:
305 partitions.append(partition)
310 return error, blacklisted, removable, is_cdrom, partitions
312 def enumerateBlockDevices(self):
313 print "enumerating block devices..."
314 for blockdev in listdir("/sys/block"):
315 error, blacklisted, removable, is_cdrom, partitions = self.getBlockDevInfo(blockdev)
316 print "found block device '%s':" % blockdev,
318 print "error querying properties"
322 print "ok, removable=%s, cdrom=%s, partitions=%s, device=%s" % (removable, is_cdrom, partitions, blockdev)
323 self.addHotplugPartition(blockdev, blockdev)
324 for part in partitions:
325 self.addHotplugPartition(part, part)
327 def getAutofsMountpoint(self, device):
328 return "/autofs/%s/" % (device)
330 def addHotplugPartition(self, device, description):
331 p = Partition(mountpoint = self.getAutofsMountpoint(device), description = description, force_mounted = True)
332 self.partitions.append(p)
333 self.on_partition_list_change("add", p)
335 if l and device[l-1] not in ('0','1','2','3','4','5','6','7','8','9'):
336 error, blacklisted, removable, is_cdrom, partitions = self.getBlockDevInfo(device)
337 if not blacklisted and not removable and not is_cdrom:
338 self.hdd.append(Harddisk(device))
340 SystemInfo["Harddisc"] = len(self.hdd) > 0
342 def removeHotplugPartition(self, device):
343 mountpoint = self.getAutofsMountpoint(device)
344 for x in self.partitions[:]:
345 if x.mountpoint == mountpoint:
346 self.partitions.remove(x)
347 self.on_partition_list_change("remove", x)
349 if l and device[l-1] not in ('0','1','2','3','4','5','6','7','8','9'):
352 if hdd.device == device:
355 SystemInfo["Harddisc"] = len(self.hdd) > 0
363 hdd = hd.model() + " - " + hd.bus()
366 hdd += " (" + cap + ")"
367 list.append((hdd, hd))
373 def getMountedPartitions(self, onlyhotplug = False):
374 return [x for x in self.partitions if (x.is_hotplug or not onlyhotplug) and x.mounted()]
376 harddiskmanager = HarddiskManager()