[PATCH 3/3] Wait for enough entropy when creating LUKS format

Vratislav Podzimek vpodzime at redhat.com
Fri May 24 17:01:44 UTC 2013


Also invoke the callback for such event (if any).

Signed-off-by: Vratislav Podzimek <vpodzime at redhat.com>
---
 blivet/__init__.py     |  3 ++-
 blivet/deviceaction.py | 17 +++++++++++++++++
 blivet/formats/luks.py |  1 +
 blivet/util.py         |  4 ++++
 4 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/blivet/__init__.py b/blivet/__init__.py
index 13cb523..0751be8 100644
--- a/blivet/__init__.py
+++ b/blivet/__init__.py
@@ -316,13 +316,14 @@ class Blivet(object):
                           that should be called (see below for more info)
         :type callbacks: dict (str -> function)
 
-        For now the following callbacks are supported (keywords and arguments
+        For now, the following callbacks are supported (keywords and arguments
         the called function gets):
 
         { "CreateFormatPre": (msg),
           "CreateFormatPost": (msg),
           "ResizeFormatPre": (msg),
           "ResizeFormatPost": (msg),
+          "WaitForEntropy": (msg, min_entropy),
           }
 
         """
diff --git a/blivet/deviceaction.py b/blivet/deviceaction.py
index 7a162cd..b0b42e0 100644
--- a/blivet/deviceaction.py
+++ b/blivet/deviceaction.py
@@ -23,12 +23,16 @@
 
 from udev import *
 import math
+import time
 
 from devices import StorageDevice
 from devices import PartitionDevice
 from devices import LVMLogicalVolumeDevice
 from formats import getFormat
 from errors import *
+from util import get_current_entropy
+from formats import luks
+
 from parted import partitionFlag, PARTITION_LBA
 
 import gettext
@@ -462,6 +466,19 @@ class ActionCreateFormat(DeviceAction):
 
             self.device.disk.format.commitToDisk()
 
+        if isinstance(self.device.format, luks.LUKS):
+            # LUKS needs to wait for random data entropy if it is too low
+            current_entropy = get_current_entropy()
+            if current_entropy < luks.MINIMAL_ENTROPY:
+                if callbacks and "WaitForEntropy" in callbacks:
+                    msg = "Not enough entropy to create LUKS format. "\
+                          "%d bits are needed." % luks.MINIMAL_ENTROPY
+                    callbacks["WaitForEntropy"](msg, luks.MINIMAL_ENTROPY)
+
+                while get_current_entropy() < luks.MINIMAL_ENTROPY:
+                    # wait for entropy to become high enough
+                    time.sleep(1)
+
         self.device.format.create(device=self.device.path,
                                   options=self.device.formatArgs)
         # Get the UUID now that the format is created
diff --git a/blivet/formats/luks.py b/blivet/formats/luks.py
index ab12eba..1a9d836 100644
--- a/blivet/formats/luks.py
+++ b/blivet/formats/luks.py
@@ -40,6 +40,7 @@ _ = lambda x: gettext.ldgettext("blivet", x)
 import logging
 log = logging.getLogger("blivet")
 
+MINIMAL_ENTROPY = 256
 
 class LUKS(DeviceFormat):
     """ A LUKS device. """
diff --git a/blivet/util.py b/blivet/util.py
index 3e51249..ca4ae0c 100644
--- a/blivet/util.py
+++ b/blivet/util.py
@@ -326,3 +326,7 @@ def insert_colons(a_string):
         return insert_colons(a_string[:-2]) + ':' + suffix
     else:
         return suffix
+
+def get_current_entropy():
+    with open("/proc/sys/kernel/random/entropy_avail", "r") as fobj:
+        return int(fobj.readline())
-- 
1.7.11.7



More information about the anaconda-patches mailing list