From 3953da477b6244b15deac3e24f18c367ede635e8 Mon Sep 17 00:00:00 2001 From: lars Date: Wed, 15 Jul 2009 09:12:11 +0000 Subject: [PATCH] * use caching for "debian_live" check --- src/cryptobox/core/blockdevice.py | 77 ++++++++++++++++--------------- 1 file changed, 40 insertions(+), 37 deletions(-) diff --git a/src/cryptobox/core/blockdevice.py b/src/cryptobox/core/blockdevice.py index c0f6cc9..ac1fe29 100644 --- a/src/cryptobox/core/blockdevice.py +++ b/src/cryptobox/core/blockdevice.py @@ -51,6 +51,7 @@ MAJOR_DEVNUM_LOOP = 7 MAJOR_DEVNUM_FLOPPY = 2 MAJOR_DEVNUM_RAMDISK = 1 MAJOR_DEVNUM_MD_RAID = 9 +MOUNT_STATE_FILE = '/proc/mounts' ## cache settings CACHE_ENABLED = True @@ -248,48 +249,19 @@ class Blockdevice: else: return False + def is_debian_live(self): """is the device part of the Debian Live runtime """ - ##Debian Live ("iso" or "usb-hdd" versions) uses two block - ##devices as part of its runtime system. The raw boot media - ##(such as a usb key or cdrom) is mounted at /live_media, - ##and the root filesystem is mounted off of the live media - ##using /dev/loop0. - deb_live_devs = [] - for line in file("/proc/mounts"): - try: - fields = line.split(" ") - ##We are looking for, for example: - ##/dev/hdc /live_media iso9660 ro 0 0 - ##and - ##/dev/loop0 /filesystem.squashfs squashfs ro 0 0 - if (fields[1] == "/live_media" and - fields[3] == "ro"): - deb_live_devs.append(fields[0]) - elif (fields[1] == "/filesystem.squashfs" and - fields[3] == "ro"): - deb_live_devs.append(fields[0]) - except IndexError: - pass - - ##TODO: - ##Since the lines in /proc/mounts that affect deb_live_devs - ##don't change after startup, deb_live_devs could be cached here. - ## ##Check whether any Debian Live devices refer to the ##block device we were passed - matched = False - for dev in deb_live_devs: - for node in self.__get_device_nodes(): - if (node == dev): - matched = True - + deb_live_devs = _get_debian_live_devices() + ## check for matches in self.__get_device_nodes() and deb_live_devs + matching_devices = [ devnode for devnode in self.__get_device_nodes() + if devnode in deb_live_devs ] + ## return True, if a match was found + return len(matching_devices) > 0 - ##If no nodes of the block device match the Debian Live - ##devices, return False - return matched - def is_lvm_pv(self): """return if the device is a physical volume of a LVM @@ -1096,9 +1068,40 @@ def _load_preferences(): os.chdir(os.path.dirname(config_file)) return cryptobox.core.settings.CryptoBoxSettings(config_file) else: - raise CBConfigUnavailableError() + raise cryptobox.core.exceptions.CBConfigUnavailableError() +def _get_debian_live_devices(): + ##Debian Live ("iso" or "usb-hdd" versions) uses two block + ##devices as part of its runtime system. The raw boot media + ##(such as a usb key or cdrom) is mounted at /live_media, + ##and the root filesystem is mounted off of the live media + ##using /dev/loop0. + cache_link = ["debian_live_devices"] + cached = CACHE.get(cache_link) + if not cached is None: + return cached[:] + + ## result is not cached yet - we need to find it + deb_live_devs = [] + for line in file(MOUNT_STATE_FILE): + try: + fields = line.split(" ") + ##We are looking for, for example: + ##/dev/hdc /live_media iso9660 ro 0 0 + ##and + ##/dev/loop0 /filesystem.squashfs squashfs ro 0 0 + if (fields[1] == "/live_media" and + fields[3] == "ro"): + deb_live_devs.append(fields[0]) + elif (fields[1] == "/filesystem.squashfs" and + fields[3] == "ro"): + deb_live_devs.append(fields[0]) + except IndexError: + pass + CACHE.set(cache_link, deb_live_devs) + return deb_live_devs[:] + ## initialize cache CACHE = BlockdeviceCache()