Commit bddf7543 authored by Nikos Skalkotos's avatar Nikos Skalkotos
Browse files

Support making swap in secondary disk

Allow making a whole disk as SWAP. To do this, you need to define the
SWAP image property with a letter, denoting a hard disk numbering. If
you use: SWAP=b then snf-image will configure the second disk of the
image as swap.

This resolves #63
parent 2f9eac66
...@@ -179,17 +179,20 @@ All image formats properties ...@@ -179,17 +179,20 @@ All image formats properties
`here <http://pythonhosted.org/passlib/modular_crypt_format.html#mcf-identifiers>`_ `here <http://pythonhosted.org/passlib/modular_crypt_format.html#mcf-identifiers>`_
for more info). for more info).
* **SWAP=<partition id>:<size>** * **SWAP=<partition id>:<size>|<disk letter>**
If this property is defined, *snf-image* will create a swap partition with If this property is defined, *snf-image* will create a swap device in the
the specified size in MB. The *partition id* is the number that the Linux VM. If the first form is used, then a swap partition with the specified size
in MB will be created. The *partition id* is the number that the Linux
kernel will assign to this partition. For example, if you have a disk with kernel will assign to this partition. For example, if you have a disk with
an MSDOS partition table on it and one primary partition, the image an MSDOS partition table on it and one primary partition, an image property:
property *SWAP=2:512* would instruct *snf-image* to create a 512MB long *SWAP=2:512* would instruct *snf-image* to create a 512MB long primary
primary partition for swap with id=2. On the other hand, if the *SWAP* partition for swap with id=2. On the other hand, if the *SWAP* property was
property had this form: *SWAP=5:512*, since primary partitions may have an defined like this: *SWAP=5:512*, since primary partitions may have an id
id from 1 to 4, *snf-image* would create a 512MB extended partition with from 1 to 4, *snf-image* would create a 512MB extended partition with id=2
id=2 and a logical swap partition with id=5 in it with the same size. This and a logical swap partition of the same size with id=5 in it. If the second
property only applies to Linux instances. form is specified, then a whole secondary disk will be configured
to be swap. Defining *SWAP=c* will configure the third disk of the VM to be
swap.This property only applies to Linux instances.
* **EXCLUDE_ALL_TASKS=yes** * **EXCLUDE_ALL_TASKS=yes**
If this property is defined with a value other than null, then during the If this property is defined with a value other than null, then during the
......
...@@ -73,9 +73,9 @@ last_part_id=$(cut -d':' -f1 <<< "$last_part") ...@@ -73,9 +73,9 @@ last_part_id=$(cut -d':' -f1 <<< "$last_part")
# Check if swap is defined... # Check if swap is defined...
if [ -n "$SNF_IMAGE_PROPERTY_SWAP" ]; then if [ -n "$SNF_IMAGE_PROPERTY_SWAP" ]; then
if [[ "$SNF_IMAGE_PROPERTY_SWAP" =~ ^([0-9]+):([0-9]+)$ ]]; then if [[ "$SNF_IMAGE_PROPERTY_SWAP" =~ ^(([A-Za-z])|(([1-9]([0-9])*):([1-9]([0-9])*)))$ ]]; then
swap_num=${BASH_REMATCH[1]} swap_num=${BASH_REMATCH[4]}
swap_size=${BASH_REMATCH[2]} swap_size=${BASH_REMATCH[6]}
swap_unit="MB" swap_unit="MB"
else else
log_error "SWAP property \`$SNF_IMAGE_PROPERTY_SWAP' is not valid" log_error "SWAP property \`$SNF_IMAGE_PROPERTY_SWAP' is not valid"
......
...@@ -47,16 +47,33 @@ if [ -z "$SNF_IMAGE_PROPERTY_SWAP" ]; then ...@@ -47,16 +47,33 @@ if [ -z "$SNF_IMAGE_PROPERTY_SWAP" ]; then
exit 0 exit 0
fi fi
if [[ "$SNF_IMAGE_PROPERTY_SWAP" =~ ^([0-9]+):[0-9]+$ ]]; then if [[ "$SNF_IMAGE_PROPERTY_SWAP" =~ ^(([A-Za-z])|(([1-9]([0-9])*):([1-9]([0-9])*)))$ ]]; then
swap_id=${BASH_REMATCH[1]} swap_disk=${BASH_REMATCH[2],,} # Make it lowercase
swap_id=${BASH_REMATCH[4]}
if [ -n "$swap_id" ]; then
swap_dev="${SNF_IMAGE_DEV}${swap_id}"
else
letters=( {a..z} )
for ((i=0; i < ${#letters[@]}; i++)); do
if [ "$swap_disk" = ${letters[$i]} ]; then
break
fi
done
if [ $i -eq 0 ]; then
log_error "Swap disk cannot be the first disk"
elif [ $i -ge $SNF_IMAGE_DEV_COUNT ]; then
log_error "Swap disk (=$i) is not available"
fi
eval swap_dev=\${SNF_IMAGE_DEV_$i}
fi
else else
log_error "SWAP property \`$SNF_IMAGE_PROPERTY_SWAP' is not valid" log_error "SWAP property \`$SNF_IMAGE_PROPERTY_SWAP' is not valid"
fi fi
swap_dev="${SNF_IMAGE_DEV}${swap_id}"
if [ ! -b "$swap_dev" ]; then if [ ! -b "$swap_dev" ]; then
log_error "Swap partition \`$swap_dev' is missing." log_error "Swap device \`$swap_dev' is missing."
fi fi
$MKSWAP "$swap_dev" $MKSWAP "$swap_dev"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment