Skip to content

Commit

Permalink
Fixed bug in zfs-hardlink-rebalance.sh script that would remove the h…
Browse files Browse the repository at this point in the history
…ardlink but exit out if the

checksum flag or the number of passes was changed.
Removed empty quotes in sed command to make compatible with linux
  • Loading branch information
hbilbo committed Oct 17, 2024
1 parent 8a98551 commit 26d1164
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 185 deletions.
41 changes: 0 additions & 41 deletions notes.txt

This file was deleted.

127 changes: 0 additions & 127 deletions testing-hardlinks.sh

This file was deleted.

41 changes: 25 additions & 16 deletions zfs-hardlink-rebalancing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ Cyan='\033[0;36m' # Cyan

# print a help message
function print_usage() {
echo "Usage: zfs-inplace-rebalancing --checksum true --passes 1 /data/source /data/dest"
echo "Usage: zfs-inplace-rebalancing --checksum true --passes 1 source dest"
echo "Note: hardlinks in the 'dest' path will be temporarily deleted during the rebalance."
}

# print a given text entirely in a given color
Expand Down Expand Up @@ -81,7 +82,7 @@ function rebalance () {
echo "Unsupported OS type: $OSTYPE"
exit 1
fi

if [ "${hardlink_count}" -ne 2 ]; then
echo "Skipping non hard-linked file: ${file_path}"
return
Expand All @@ -91,10 +92,21 @@ function rebalance () {
progress_percent=$(printf '%0.2f' "$((current_index*10000/file_count))e-2")
color_echo "${Cyan}" "Progress -- Files: ${current_index}/${file_count} (${progress_percent}%)"

# skip if the source file is no longer there
if [[ ! -f "${file_path}" ]]; then
color_echo "${Yellow}" "File is missing, skipping: ${file_path}"
return
fi

# skip if hardlink file is not found
inode_val=$(ls -i "${file_path}" | awk '{print $1}')
hardlink_path=$(find "${hardlink_dir}" -inum ${inode_val})
if [[ ! -f "${hardlink_path}" ]]; then
color_echo "${Yellow}" "Hardlink is missing, skipping: ${file_path}"
return
fi

# skip if target number of passes is reached
if [ "${passes_flag}" -ge 1 ]; then
# check if target rebalance count is reached
rebalance_count=$(get_rebalance_count "${file_path}")
Expand All @@ -107,13 +119,7 @@ function rebalance () {
tmp_extension=".balance"
tmp_file_path="${file_path}${tmp_extension}"

# Find other hardlinked file and remove it
inode_val=$(ls -i "${file_path}" | awk '{print $1}')
hardlink_path=$(find "${hardlink_dir}" -inum ${inode_val})
echo "Removing hardlink '${hardlink_path}'..."
rm "${hardlink_path}"

# Continue with rebalance
# create copy of file with .balance suffix
echo "Copying '${file_path}' to '${tmp_file_path}'..."
if [[ "${OSTYPE,,}" == "linux-gnu"* ]]; then
# Linux
Expand Down Expand Up @@ -190,6 +196,9 @@ function rebalance () {
fi
fi

echo "Removing hardlink '${hardlink_path}'..."
rm "${hardlink_path}"

echo "Removing original '${file_path}'..."
rm "${file_path}"

Expand All @@ -203,21 +212,21 @@ function rebalance () {
# update rebalance "database"
line_nr=$(grep -xF -n "${file_path}" "./${rebalance_db_file_name}" | head -n 1 | cut -d: -f1)
if [ -z "${line_nr}" ]; then
rebalance_count=1
echo "${file_path}" >> "./${rebalance_db_file_name}"
echo "${rebalance_count}" >> "./${rebalance_db_file_name}"
rebalance_count=1
echo "${file_path}" >> "./${rebalance_db_file_name}"
echo "${rebalance_count}" >> "./${rebalance_db_file_name}"
else
rebalance_count_line_nr="$((line_nr + 1))"
rebalance_count="$((rebalance_count + 1))"
sed -i '' "${rebalance_count_line_nr}s/.*/${rebalance_count}/" "./${rebalance_db_file_name}"
rebalance_count_line_nr="$((line_nr + 1))"
rebalance_count="$((rebalance_count + 1))"
sed -i "${rebalance_count_line_nr}s/.*/${rebalance_count}/" "./${rebalance_db_file_name}"
fi
fi
}

checksum_flag='true'
passes_flag='1'

if [[ "$#" -ne 2 ]]; then
if [[ "$#" -eq 0 ]]; then
print_usage
exit 0
fi
Expand Down
2 changes: 1 addition & 1 deletion zfs-inplace-rebalancing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ function rebalance () {
else
rebalance_count_line_nr="$((line_nr + 1))"
rebalance_count="$((rebalance_count + 1))"
sed -i '' "${rebalance_count_line_nr}s/.*/${rebalance_count}/" "./${rebalance_db_file_name}"
sed -i "${rebalance_count_line_nr}s/.*/${rebalance_count}/" "./${rebalance_db_file_name}"
fi
fi
}
Expand Down

0 comments on commit 26d1164

Please sign in to comment.