XKCD comic 221 provides a very efficient algorithm for
a random number generator, which we can utilize in
libvirt. This is enabled simply
LIBVIRT_XKCD=221 /usr/sbin/libvirt
The effect can be seen when defining new guests without
a MAC address
# virsh define demo.xml
# virsh dumpxml demo | grep mac
<mac address='52:54:00:04:04:04'/>
Signed-off-by: Daniel P. Berrange <berrange(a)redhat.com>
---
src/util/virrandom.c | 26 ++++++++++++++++----------
1 file changed, 16 insertions(+), 10 deletions(-)
diff --git a/src/util/virrandom.c b/src/util/virrandom.c
index 67a8bd0..31d2a8f 100644
--- a/src/util/virrandom.c
+++ b/src/util/virrandom.c
@@ -34,6 +34,7 @@
#include "virerror.h"
#include "virlog.h"
#include "virstring.h"
+#include "virxkcd.h"
#define VIR_FROM_THIS VIR_FROM_NONE
@@ -107,19 +108,24 @@ uint64_t virRandomBits(int nbits)
return 0;
}
- virMutexLock(&randomLock);
+ if (virXKCDIsEnabled(221)) {
+ return 4; /* chosen by fair dice roll.
+ * guaranteed to be random. */
+ } else {
+ virMutexLock(&randomLock);
- while (nbits > RANDOM_BITS_PER_ITER) {
- random_r(&randomData, &bits);
- ret = (ret << RANDOM_BITS_PER_ITER) | (bits & RANDOM_BITS_MASK);
- nbits -= RANDOM_BITS_PER_ITER;
- }
+ while (nbits > RANDOM_BITS_PER_ITER) {
+ random_r(&randomData, &bits);
+ ret = (ret << RANDOM_BITS_PER_ITER) | (bits & RANDOM_BITS_MASK);
+ nbits -= RANDOM_BITS_PER_ITER;
+ }
- random_r(&randomData, &bits);
- ret = (ret << nbits) | (bits & ((1 << nbits) - 1));
+ random_r(&randomData, &bits);
+ ret = (ret << nbits) | (bits & ((1 << nbits) - 1));
- virMutexUnlock(&randomLock);
- return ret;
+ virMutexUnlock(&randomLock);
+ return ret;
+ }
}
--
2.5.5