From 36a575b036e55c31ab624447c5d73bc56408a672 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Fri, 13 Nov 2020 06:57:33 -0800 Subject: [PATCH] Clean up create_disk() a little The create_disk() helper had some redundant checks and dead code. This refactors it to put all the stale cleanup at the top, and groups the new actions together with more relevant comments to make it easier to understand. Change-Id: I1f6218a1994e66786ed9a8065e30bcceec7b8956 --- functions | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/functions b/functions index e679b0f9bc..fc87a5512d 100644 --- a/functions +++ b/functions @@ -759,16 +759,14 @@ function create_disk { local loopback_disk_size=${3} local key - # Create a loopback disk and format it to XFS. - if [[ -e ${disk_image} ]]; then - if egrep -q ${storage_data_dir} /proc/mounts; then - sudo umount ${storage_data_dir} - sudo rm -f ${disk_image} - fi - fi + key=$(echo $disk_image | sed 's#/.##') + key="devstack-$key" - sudo mkdir -p ${storage_data_dir}/drives/images + destroy_disk $disk_image $storage_data_dir + # Create an empty file of the correct size (and ensure the + # directory structure up to that path exists) + sudo mkdir -p $(dirname ${disk_image}) sudo truncate -s ${loopback_disk_size} ${disk_image} # Make a fresh XFS filesystem. Use bigger inodes so xattr can fit in @@ -778,16 +776,9 @@ function create_disk { # Swift and Ceph. sudo mkfs.xfs -f -i size=1024 ${disk_image} - # Unmount the target, if mounted - if egrep -q $storage_data_dir /proc/mounts; then - sudo umount $storage_data_dir - fi - - # Clear any old fstab rules, install a new one for this disk, and mount it - key=$(echo $disk_image | sed 's#/.##') - key="devstack-$key" - sudo sed -i '/.*comment=$key.*/ d' /etc/fstab + # Install a new loopback fstab entry for this disk image, and mount it echo "$disk_image $storage_data_dir xfs loop,noatime,nodiratime,logbufs=8,comment=$key 0 0" | sudo tee -a /etc/fstab + sudo mkdir -p $storage_data_dir sudo mount -v $storage_data_dir } @@ -795,6 +786,10 @@ function create_disk { function destroy_disk { local disk_image=$1 local storage_data_dir=$2 + local key + + key=$(echo $disk_image | sed 's#/.##') + key="devstack-$key" # Unmount the target, if mounted if egrep -q $storage_data_dir /proc/mounts; then @@ -802,10 +797,10 @@ function destroy_disk { fi # Clear any fstab rules - sed -i '/.*comment=$key.*/ d' /etc/fstab + sudo sed -i '/.*comment=$key.*/ d' /etc/fstab # Delete the file - sudo rm $disk_image + sudo rm -f $disk_image }