add option to use dd for swapfile creation

fallocate is faster than using dd to create a thick swapfile with
zeros, but it is not supported on all filesystems (specifically ext3).

This commit adds a fallback to using dd if fallocate fails

Closes-Bug: #1458841

Change-Id: Ie4adf625d85f84a0d89a108ef0438622ab763b9d
This commit is contained in:
Darren Birkett 2015-05-26 12:23:34 +01:00 committed by kevin
parent a2a4bb6e3b
commit ccb1676323
2 changed files with 7 additions and 6 deletions

View File

@ -200,9 +200,9 @@ mount -a || true
if [ ! "$(swapon -s | grep -v Filename)" ]; then
memory_kb=$(awk '/MemTotal/ {print $2}' /proc/meminfo)
if [ "${memory_kb}" -lt "8388608" ]; then
swap_size="4G"
swap_size="4294967296"
else
swap_size="8G"
swap_size="8589934592"
fi
loopback_create "/opt/swap.img" ${swap_size} thick swap
# Ensure swap will be used on the host
@ -215,7 +215,7 @@ fi
# Build the loopback drive for cinder to use
CINDER="cinder.img"
if ! vgs cinder-volumes; then
loopback_create "/opt/${CINDER}" 1000G thin rc
loopback_create "/opt/${CINDER}" 1073741824000 thin rc
CINDER_DEVICE=$(losetup -a | awk -F: "/${CINDER}/ {print \$1}")
pvcreate ${CINDER_DEVICE}
pvscan
@ -234,7 +234,7 @@ if [ "${DEPLOY_SWIFT}" == "yes" ]; then
# build the loopback drives for swift to use
for SWIFT in swift1 swift2 swift3; do
if ! grep "${SWIFT}" /proc/mounts > /dev/null; then
loopback_create "/opt/${SWIFT}.img" 1000G thin none
loopback_create "/opt/${SWIFT}.img" 1073741824000 thin none
if ! grep -w "^/opt/${SWIFT}.img" /etc/fstab > /dev/null; then
echo "/opt/${SWIFT}.img /srv/${SWIFT}.img xfs loop,noatime,nodiratime,nobarrier,logbufs=8 0 0" >> /etc/fstab
fi

View File

@ -142,9 +142,10 @@ function loopback_create() {
if [ "${LOOP_FILE_TYPE}" = "thin" ]; then
truncate -s ${LOOP_FILESIZE} ${LOOP_FILENAME}
elif [ "${LOOP_FILE_TYPE}" = "thick" ]; then
fallocate -l ${LOOP_FILESIZE} ${LOOP_FILENAME}
fallocate -l ${LOOP_FILESIZE} ${LOOP_FILENAME} &> /dev/null || \
dd if=/dev/zero of=${LOOP_FILENAME} bs=1M count=$(( ${LOOP_FILESIZE} / 1024 / 1024 ))
else
exit_fail 'No valid option ${LOOP_FILE_TYPE} found.'
exit_fail "No valid option ${LOOP_FILE_TYPE} found."
fi
fi