[rhel6-branch][PATCH] Limit the maximum swap size to 10 % of disk space

Vratislav Podzimek vpodzime at redhat.com
Fri Jul 19 08:40:07 UTC 2013


On e.g. virtual machines with smalls disks and high amount of
memory it doesn't make sense to create large swap space leaving
only a little for the system and user data.

Also change a comment to a docstring and document parameters.

Resolves: rhbz#878907
Related: rhbz#879232

Signed-off-by: Vratislav Podzimek <vpodzime at redhat.com>
---
 iutil.py                | 28 ++++++++++++++++++++++++----
 kickstart.py            | 18 ++++++++++++++++--
 storage/partitioning.py | 14 ++++++++++++++
 3 files changed, 54 insertions(+), 6 deletions(-)

diff --git a/iutil.py b/iutil.py
index 4553816..eaf2136 100644
--- a/iutil.py
+++ b/iutil.py
@@ -40,6 +40,9 @@ import logging
 log = logging.getLogger("anaconda")
 program_log = logging.getLogger("program")
 
+# maximum ratio of swap size to disk size (10 %)
+MAX_SWAP_DISK_RATIO = 0.1
+
 #Python reimplementation of the shell tee process, so we can
 #feed the pipe output into two places at the same time
 class tee(threading.Thread):
@@ -450,10 +453,17 @@ def memInstalled():
 
     return long(mem)
 
-## Suggest the size of the swap partition that will be created.
-# @param quiet Should size information be logged?
-# @return A tuple of the minimum and maximum swap size, in megabytes.
-def swapSuggestion(quiet=0, hibernation=False):
+def swapSuggestion(quiet=0, hibernation=False, disk_space=None):
+    """
+    Suggest the size of the swap partition that will be created.
+
+    @param quiet: Should size information be logged?
+    @param hibernation: Suggest size of the swap partition to support hibernation?
+    @param disk_space: Total disk space available
+    @return: A tuple of the minimum and maximum swap size, in megabytes.
+
+    """
+
     mem = memInstalled()/1024
     mem = ((mem/16)+1)*16
     if not quiet:
@@ -479,6 +489,16 @@ def swapSuggestion(quiet=0, hibernation=False):
         else:
             log.info("Ignoring --hibernation option on systems with 64 GB of RAM or more")
 
+    if disk_space is not None:
+        max_swap = int(disk_space * MAX_SWAP_DISK_RATIO)
+        if swap > max_swap:
+            log.info("Suggested swap size (%(swap)d M) exceeds %(percent)d %% of "
+                     "disk space, using %(percent)d %% of disk space (%(size)d M) "
+                     "instead." % {"percent": MAX_SWAP_DISK_RATIO*100,
+                                   "swap": swap,
+                                   "size": max_swap})
+            swap = max_swap
+
     if not quiet:
 	log.info("Swap attempt of %sM", swap)
 
diff --git a/kickstart.py b/kickstart.py
index e4b960a..0ee6621 100644
--- a/kickstart.py
+++ b/kickstart.py
@@ -481,11 +481,18 @@ class LogVolData(commands.logvol.RHEL6_LogVolData):
         # we might have truncated or otherwise changed the specified vg name
         vgname = anaconda.id.ksdata.onPart.get(self.vgname, self.vgname)
 
+        disk_space = 0
+        for disk in storage.disks:
+            if not storage.clearPartDisks \
+                    or disk.name in storage.clearPartDisks:
+                disk_space += disk.size
+
         if self.mountpoint == "swap":
             type = "swap"
             self.mountpoint = ""
             if self.recommended or self.hibernation:
-                (self.size, self.maxSizeMB) = iutil.swapSuggestion(hibernation=self.hibernation)
+                (self.size, self.maxSizeMB) = \
+                    iutil.swapSuggestion(hibernation=self.hibernation, disk_space=disk_space)
                 self.grow = True
         else:
             if self.fstype != "":
@@ -752,11 +759,18 @@ class PartitionData(commands.partition.RHEL6_PartData):
             if self.disk == "":
                 raise KickstartValueError, formatErrorMsg(self.lineno, msg="Specified BIOS disk %s cannot be determined" % self.onbiosdisk)
 
+        disk_space = 0
+        for disk in storage.disks:
+            if not storage.clearPartDisks \
+                    or disk.name in storage.clearPartDisks:
+                disk_space += disk.size
+
         if self.mountpoint == "swap":
             type = "swap"
             self.mountpoint = ""
             if self.recommended or self.hibernation:
-                (self.size, self.maxSizeMB) = iutil.swapSuggestion(hibernation=self.hibernation)
+                (self.size, self.maxSizeMB) = \
+                    iutil.swapSuggestion(hibernation=self.hibernation, disk_space=disk_space)
                 self.grow = True
 
         # if people want to specify no mountpoint for some reason, let them
diff --git a/storage/partitioning.py b/storage/partitioning.py
index e20a18f..16b5823 100644
--- a/storage/partitioning.py
+++ b/storage/partitioning.py
@@ -244,6 +244,20 @@ def doAutoPartition(anaconda):
     # sanity check the individual devices
     log.warning("not sanity checking devices because I don't know how yet")
 
+    # get a new swap suggestion, now that we know the total disk space we're
+    # gonna use
+    disk_space = 0
+    for disk in anaconda.id.storage.disks:
+        if not anaconda.id.storage.clearPartDisks \
+                or disk.name in anaconda.id.storage.clearPartDisks:
+            disk_space += disk.size
+
+    (min_size, max_size) = iutil.swapSuggestion(disk_space=disk_space)
+    for request in anaconda.id.storage.autoPartitionRequests:
+        if request.fstype == "swap":
+            request.size = min_size
+            request.maxSize= max_size
+
     # run the autopart function to allocate and grow partitions
     try:
         doPartitioning(anaconda.id.storage,
-- 
1.7.11.7



More information about the anaconda-patches mailing list