Skip to content

Commit

Permalink
Fix 41_snapshots-btrs bashish [[ ]]
Browse files Browse the repository at this point in the history
  • Loading branch information
bastien-roucaries committed Jan 27, 2024
1 parent 490720a commit 7aa227b
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions 41_snapshots-btrfs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ set -e
sysconfdir="/etc"
grub_btrfs_config="${sysconfdir}/default/grub-btrfs/config"

[[ -f "$grub_btrfs_config" ]] && . "$grub_btrfs_config"
[[ -f "${sysconfdir}/default/grub" ]] && . "${sysconfdir}/default/grub"
[ -f "$grub_btrfs_config" ] && . "$grub_btrfs_config"
[ -f "${sysconfdir}/default/grub" ] && . "${sysconfdir}/default/grub"

## Error Handling
print_error()
Expand Down Expand Up @@ -75,9 +75,9 @@ while getopts :V-: opt; do
done

## Exit the script, if:
[[ "${GRUB_BTRFS_DISABLE,,}" == "true" ]] && print_error "GRUB_BTRFS_DISABLE is set to true (default=false)"
[ "${GRUB_BTRFS_DISABLE,,}" = "true" ] && print_error "GRUB_BTRFS_DISABLE is set to true (default=false)"
if ! type btrfs >/dev/null 2>&1; then print_error "btrfs-progs isn't installed"; fi
[[ -f "${GRUB_BTRFS_MKCONFIG_LIB:-/usr/share/grub/grub-mkconfig_lib}" ]] && . "${GRUB_BTRFS_MKCONFIG_LIB:-/usr/share/grub/grub-mkconfig_lib}" || print_error "grub-mkconfig_lib couldn't be found"
[ -f "${GRUB_BTRFS_MKCONFIG_LIB:-/usr/share/grub/grub-mkconfig_lib}" ] && . "${GRUB_BTRFS_MKCONFIG_LIB:-/usr/share/grub/grub-mkconfig_lib}" || print_error "grub-mkconfig_lib couldn't be found"
[[ "$(btrfs filesystem df / 2>&1)" == *"not a btrfs filesystem"* ]] && print_error "Root filesystem isn't btrfs"

printf "Detecting snapshots ...\n" >&2 ;
Expand Down Expand Up @@ -150,7 +150,7 @@ detect_rootflags()

unmount_grub_btrfs_mount_point()
{
if [[ -d "$grub_btrfs_mount_point" ]]; then
if [ -d "$grub_btrfs_mount_point" ]; then
local wait=true
local wait_max=0
printf "Unmount %s .." "$grub_btrfs_mount_point" >&2;
Expand All @@ -160,7 +160,7 @@ if [[ -d "$grub_btrfs_mount_point" ]]; then
if umount "$grub_btrfs_mount_point" >/dev/null 2>&1; then
wait=false # umount successful
printf " Success\n" >&2;
elif [[ $wait_max = 10 ]]; then
elif [ $wait_max -eq 10 ]; then
printf "\nWarning: Unable to unmount %s in %s\n" "$root_device" "$grub_btrfs_mount_point" >&2;
break;
else
Expand All @@ -172,7 +172,7 @@ if [[ -d "$grub_btrfs_mount_point" ]]; then
printf " Success\n" >&2;
fi
done
if [[ "$wait" != true ]]; then
if [ "$wait" != true ]; then
if ! rm -d "$grub_btrfs_mount_point" >/dev/null 2>&1; then
printf "Unable to delete %s: Device or ressource is busy\n" "$grub_btrfs_mount_point" >&2;
fi
Expand All @@ -193,10 +193,10 @@ make_menu_entries()
entry "submenu '${title_menu}' {
submenu '${title_submenu}' { echo }"
for k in "${name_kernel[@]}"; do
[[ ! -f "${boot_dir}"/"${k}" ]] && continue;
[ ! -f "${boot_dir}"/"${k}" ] && continue;
kversion=${k#*"-"}
for i in "${name_initramfs[@]}"; do
if [[ "${name_initramfs}" != "x" ]] ; then
if [ "${name_initramfs}" != "x" ] ; then
# prefix_i=${i%%"-"*}
suffix_i=${i#*"-"}
# alt_suffix_i=${i##*"-"}
Expand Down Expand Up @@ -228,7 +228,7 @@ make_menu_entries()
echo 'Loading Snapshot: "${snap_date_trim}" "${snap_dir_name_trim}"'
echo 'Loading Kernel: "${k}" ...'
linux \"${boot_dir_root_grub}/"${k}"\" root="${LINUX_ROOT_DEVICE}" ${kernel_parameters} ${rootflags}subvol=\""${snap_dir_name_trim}"\""
if [[ "${name_microcode}" != "x" ]] ; then
if [ "${name_microcode}" != "x" ] ; then
entry "\
echo 'Loading Microcode & Initramfs: "${u}" "${i}" ...'
initrd \"${boot_dir_root_grub}/"${u}"\" \"${boot_dir_root_grub}/"${i}"\""
Expand All @@ -242,7 +242,7 @@ make_menu_entries()
done
else
for u in "${name_microcode[@]}"; do
if [[ "${name_microcode}" != "x" ]] ; then
if [ "${name_microcode}" != "x" ] ; then
entry "
menuentry ' "${k}" & "${u}"' ${CLASS} "\$menuentry_id_option" 'gnulinux-snapshots-$boot_uuid' {"
else
Expand Down Expand Up @@ -406,14 +406,14 @@ detect_kernel()
for okernel in "${boot_dir}"/vmlinuz-* \
"${boot_dir}"/vmlinux-* \
"${boot_dir}"/kernel-* ; do
[[ ! -f "${okernel}" ]] && continue;
[ ! -f "${okernel}" ] && continue;
list_kernel+=("$okernel")
done

# Custom name kernel in "GRUB_BTRFS_NKERNEL"
if [ -n "${GRUB_BTRFS_NKERNEL}" ] ; then
for ckernel in "${boot_dir}/${GRUB_BTRFS_NKERNEL[@]}" ; do
[[ ! -f "${ckernel}" ]] && continue;
[ ! -f "${ckernel}" ] && continue;
list_kernel+=("$ckernel")
done
fi
Expand All @@ -427,14 +427,14 @@ detect_initramfs()
for oinitramfs in "${boot_dir}"/initrd.img-* \
"${boot_dir}"/initramfs-* \
"${boot_dir}"/initrd-* ; do
[[ ! -f "${oinitramfs}" ]] && continue;
[ ! -f "${oinitramfs}" ] && continue;
list_initramfs+=("$oinitramfs")
done

# Custom name initramfs in "GRUB_BTRFS_NINIT"
if [ -n "${GRUB_BTRFS_NINIT}" ] ; then
for cinitramfs in "${boot_dir}/${GRUB_BTRFS_NINIT[@]}" ; do
[[ ! -f "${cinitramfs}" ]] && continue;
[ ! -f "${cinitramfs}" ] && continue;
list_initramfs+=("$cinitramfs")
done
fi
Expand All @@ -453,7 +453,7 @@ detect_microcode()
"${boot_dir}"/amd-ucode.img \
"${boot_dir}"/early_ucode.cpio \
"${boot_dir}"/microcode.cpio; do
[[ ! -f "${oiucode}" ]] && continue;
[ ! -f "${oiucode}" ] && continue;
list_ucode+=("$oiucode")
done

Expand Down Expand Up @@ -509,7 +509,7 @@ boot_bounded()
# Initialize menu entries
IFS=$'\n'
for item in $(snapshot_list); do
[[ ${limit_snap_show} -le 0 ]] && break; # fix: limit_snap_show=0
[ "${limit_snap_show}" -le 0 ] && break; # fix: limit_snap_show=0
IFS=$oldIFS
parse_snapshot_list
boot_dir="$grub_btrfs_mount_point/$snap_dir_name_trim$boot_directory"
Expand All @@ -525,12 +525,12 @@ boot_bounded()
boot_dir_root_grub="$(make_system_path_relative_to_its_root "${boot_dir}")" # convert "boot_directory" to root of GRUB (e.g /boot become /)
make_menu_entries
# show snapshot found during run "grub-mkconfig"
if [[ "${GRUB_BTRFS_SHOW_SNAPSHOTS_FOUND:-"true"}" = "true" ]]; then
if [ "${GRUB_BTRFS_SHOW_SNAPSHOTS_FOUND:-"true"}" = "true" ]; then
printf "Found snapshot: %s\n" "$item" >&2 ;
fi
# Limit snapshots found during run "grub-mkconfig"
count_limit_snap=$((1+count_limit_snap))
[[ $count_limit_snap -ge $limit_snap_show ]] && break;
[ "$count_limit_snap" -ge "$limit_snap_show" ] && break;
done
IFS=$oldIFS
}
Expand All @@ -550,19 +550,19 @@ boot_separate()
# Initialize menu entries
IFS=$'\n'
for item in $(snapshot_list); do
[[ ${limit_snap_show} -le 0 ]] && break; # fix: limit_snap_show=0
[ "${limit_snap_show}" -le 0 ] && break; # fix: limit_snap_show=0
IFS=$oldIFS
parse_snapshot_list
detect_rootflags
title_format
make_menu_entries
# show snapshot found during run "grub-mkconfig"
if [[ "${GRUB_BTRFS_SHOW_SNAPSHOTS_FOUND:-"true"}" = "true" ]]; then
if [ "${GRUB_BTRFS_SHOW_SNAPSHOTS_FOUND:-"true"}" = "true" ]; then
printf "Found snapshot: %s\n" "$item" >&2 ;
fi
# Limit snapshots found during run "grub-mkconfig"
count_limit_snap=$((1+count_limit_snap))
[[ $count_limit_snap -ge $limit_snap_show ]] && break;
[ "$count_limit_snap" -ge "$limit_snap_show" ] && break;
done
IFS=$oldIFS
}
Expand All @@ -574,15 +574,15 @@ if [ -e "$grub_btrfs_directory/grub-btrfs.cfg" ]; then
mv -f "$grub_btrfs_directory/grub-btrfs.cfg" "$grub_btrfs_directory/grub-btrfs.cfg.bkp"
fi
# Create mount point then mounting
[[ ! -d $grub_btrfs_mount_point ]] && mkdir -p "$grub_btrfs_mount_point"
[ ! -d "$grub_btrfs_mount_point" ] && mkdir -p "$grub_btrfs_mount_point"
mount -o ro,subvolid=5 /dev/disk/by-uuid/"$root_uuid" "$grub_btrfs_mount_point/" > /dev/null
trap "unmount_grub_btrfs_mount_point" EXIT # unmounting mount point on EXIT signal
count_warning_menuentries=0 # Count menuentries
count_limit_snap=0 # Count snapshots
check_uuid_required
# Detects if /boot is a separate partition
[[ "${GRUB_BTRFS_OVERRIDE_BOOT_PARTITION_DETECTION,,}" == "true" ]] && printf "Override boot partition detection : enable \n" >&2 && boot_separate;
if [[ "$root_uuid" != "$boot_uuid" ]] || [[ "$root_uuid_subvolume" != "$boot_uuid_subvolume" ]]; then boot_separate ; else boot_bounded ; fi
if [ "$root_uuid" != "$boot_uuid" ] || [ "$root_uuid_subvolume" != "$boot_uuid_subvolume" ]; then boot_separate ; else boot_bounded ; fi
# Make a submenu in GRUB (grub.cfg)
cat << EOF
if [ ! -e "${grub_btrfs_search_directory}/grub-btrfs.cfg" ]; then
Expand All @@ -594,13 +594,13 @@ submenu '${submenuname}' ${protection_authorized_users}${unrestricted_access_sub
fi
EOF
# Show warn, menuentries exceeds 250 entries
[[ $count_warning_menuentries -ge 250 ]] && printf "Generated %s total GRUB entries. You might experience issues loading snapshots menu in GRUB.\n" "${count_warning_menuentries}" >&2 ;
[ $count_warning_menuentries -ge 250 ] && printf "Generated %s total GRUB entries. You might experience issues loading snapshots menu in GRUB.\n" "${count_warning_menuentries}" >&2 ;
# Show total found snapshots
if [[ "${GRUB_BTRFS_SHOW_TOTAL_SNAPSHOTS_FOUND:-"true"}" = "true" && -n "${count_limit_snap}" && "${count_limit_snap}" != "0" ]]; then
if [ "${GRUB_BTRFS_SHOW_TOTAL_SNAPSHOTS_FOUND:-"true"}" = "true" ] && [ -n "${count_limit_snap}" ] && [ "${count_limit_snap}" != "0" ]; then
printf "Found %s snapshot(s)\n" "${count_limit_snap}" >&2 ;
fi
# if no snapshot found, delete the "$grub_btrfs_directory/grub-btrfs.new" file and the "$grub_btrfs_directory/grub-btrfs.cfg.bkp" file and exit
if [[ "${count_limit_snap}" = "0" || -z "${count_limit_snap}" ]]; then
if [ "${count_limit_snap}" = "0" ] || [ -z "${count_limit_snap}" ]; then
rm -f "$grub_btrfs_directory/grub-btrfs.new" "$grub_btrfs_directory/grub-btrfs.cfg.bkp"
print_error "No snapshots found."
fi
Expand Down

0 comments on commit 7aa227b

Please sign in to comment.