diff --git a/shared/macros/10-bash.jinja b/shared/macros/10-bash.jinja index f1c1c2502bc..7ea3235fca0 100644 --- a/shared/macros/10-bash.jinja +++ b/shared/macros/10-bash.jinja @@ -1722,10 +1722,10 @@ Ensures that given mount point is in :code:`/etc/fstab`. #}} {{% macro bash_ensure_mount_option_in_fstab(mount_point, mount_opt, fs_spec, type) -%}} -mount_point_match_regexp="$(printf "[^#].*[[:space:]]%s[[:space:]]" {{{ mount_point }}})" +mount_point_match_regexp="$(printf "^[[:space:]]*[^#].*[[:space:]]%s[[:space:]]" {{{ mount_point }}})" # If the mount point is not in /etc/fstab, get previous mount options from /etc/mtab -if ! grep "$mount_point_match_regexp" /etc/fstab; then +if ! grep -q "$mount_point_match_regexp" /etc/fstab; then # runtime opts without some automatic kernel/userspace-added defaults previous_mount_opts=$(grep "$mount_point_match_regexp" /etc/mtab | head -1 | awk '{print $4}' \ | sed -E "s/(rw|defaults|seclabel|{{{ mount_opt }}})(,|$)//g;s/,$//") @@ -1738,7 +1738,7 @@ if ! grep "$mount_point_match_regexp" /etc/fstab; then fi echo "{{{ fs_spec }}} {{{ mount_point }}} {{{ type }}} defaults,${previous_mount_opts}{{{ mount_opt }}} 0 0" >> /etc/fstab # If the mount_opt option is not already in the mount point's /etc/fstab entry, add it -elif ! grep "$mount_point_match_regexp" /etc/fstab | grep "{{{ mount_opt }}}"; then +elif ! grep "$mount_point_match_regexp" /etc/fstab | grep -q "{{{ mount_opt }}}"; then previous_mount_opts=$(grep "$mount_point_match_regexp" /etc/fstab | awk '{print $4}') sed -i "s|\(${mount_point_match_regexp}.*${previous_mount_opts}\)|\1,{{{ mount_opt }}}|" /etc/fstab fi @@ -1750,7 +1750,7 @@ fi #}} {{% macro bash_assert_mount_point_in_fstab(mount_point) -%}} -mount_point_match_regexp="$(printf "[^#].*[[:space:]]%s[[:space:]]" "{{{ mount_point }}}")" +mount_point_match_regexp="$(printf "^[[:space:]]*[^#].*[[:space:]]%s[[:space:]]" "{{{ mount_point }}}")" {{# This macro gets expanded to code that will return 1 if MOUNTPOINT is not in /etc/fstab; This is consistent with the behavior prior to converting this function to a jinja macro diff --git a/shared/templates/mount_option/tests/fstab_commented_correct_then_uncommented_wrong.fail.sh b/shared/templates/mount_option/tests/fstab_commented_correct_then_uncommented_wrong.fail.sh new file mode 100644 index 00000000000..54c5cc8c5dd --- /dev/null +++ b/shared/templates/mount_option/tests/fstab_commented_correct_then_uncommented_wrong.fail.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# platform = multi_platform_all +. $SHARED/partition.sh + +clean_up_partition {{{ MOUNTPOINT }}} + +create_partition +make_fstab_given_partition_line {{{ MOUNTPOINT }}} ext2 {{{ MOUNTOPTION }}} + +# comment last line added above to be ignored +sed -Ei '${s/^/#/}' /etc/fstab + +make_fstab_given_partition_line {{{ MOUNTPOINT }}} ext2 defaults + +mount_partition {{{ MOUNTPOINT }}} || true