diff --git a/setup.sh b/setup.sh index cb8b0b9..5cccee2 100644 --- a/setup.sh +++ b/setup.sh @@ -832,43 +832,70 @@ function customize_dropbear_hooks () { fi } +function set_unique_ip_in_syslinux_kcl () { + local zbm_config default_ip + zbm_config="${1:?}" + default_ip="${2:?}" + + # First -e expression removes first looks for lines that contain + # 'APPEND' plus a space character and only in those lines removes all + # occurrences of ' ip=' followed by as many non-space characters as + # possible. This removes whatever 'ip=' definition already was present. + # + # Second -e expression similarly looks for lines that contain 'APPEND' + # plus a space character then at their end inserts a space plus our + # desired new 'ip=' definition. This puts in place the 'ip=' we want. + sed -ri \ + -e \\$'\x1''APPEND '$'\x1 s'$'\x1'' ip=([^[:space:]]*)'$'\x1'''$'\x1''gi' \ + -e \\$'\x1''APPEND '$'\x1 s'$'\x1''$'$'\x1'' '"${default_ip}"''$'\x1''gi' \ + "${zbm_config}" +} + function ensure_ip_in_kcl () { - local zbm_config kcl_length kcl_string default_ip ip_addr_found new_kcl first_kcl_elem - local -a kcl - paru_install 'go-yq' - - zbm_config='/etc/zfsbootmenu/config.yaml' - kcl_length="$(yq '.Kernel.CommandLine | length' "${zbm_config}")" - if [[ "${kcl_length}" -eq '0' ]]; then - >&3 printf -- '%s\n' \ - 'No .Kernel.CommandLine YAML element with content found in '"${zbm_config}"'. Exiting ...' - exit 77 - else - kcl_string="$(yq '.Kernel.CommandLine' "${zbm_config}")" - fi - + local default_ip default_ip='ip='"${ARCHZBM_NET_CLIENT_IP}"':'"${ARCHZBM_NET_SERVER_IP}"':'"${ARCHZBM_NET_GATEWAY_IP}"':'"${ARCHZBM_NET_NETMASK}"':'"${ARCHZBM_NET_HOSTNAME}"':'"${ARCHZBM_NET_DEVICE}"':'"${ARCHZBM_NET_AUTOCONF}" - mapfile -t kcl < <(<<<"${kcl_string}" tr ' ' '\n' | sed '/^$/d') - for kcl_elem in "${!kcl[@]}"; do - if grep -Piq -- 'ip=' <<<"${kcl[$kcl_elem]}"; then - ip_addr_found='true' - kcl["${kcl_elem}"]="${default_ip}" - fi - done - if [[ ! "${ip_addr_found}" ]]; then - kcl+=("${default_ip}") - fi - new_kcl='' - first_kcl_elem='true' - for kcl_elem in "${kcl[@]}"; do - if [[ ! "${first_kcl_elem}" ]]; then - new_kcl+=' '"${kcl_elem}" + + if [[ "${part_schema}" = 'gpt' ]]; then + local zbm_config kcl_length kcl_string ip_addr_found new_kcl first_kcl_elem + local -a kcl + paru_install 'go-yq' + + zbm_config='/etc/zfsbootmenu/config.yaml' + kcl_length="$(yq '.Kernel.CommandLine | length' "${zbm_config}")" + if [[ "${kcl_length}" -eq '0' ]]; then + >&3 printf -- '%s\n' \ + 'No .Kernel.CommandLine YAML element with content found in '"${zbm_config}"'. Exiting ...' + exit 77 else - new_kcl+="${kcl_elem}" - unset -v first_kcl_elem + kcl_string="$(yq '.Kernel.CommandLine' "${zbm_config}")" fi - done - yq -i '.Kernel.CommandLine = "'"${new_kcl}"'"' "${zbm_config}" + + mapfile -t kcl < <(<<<"${kcl_string}" tr ' ' '\n' | sed '/^$/d') + for kcl_elem in "${!kcl[@]}"; do + if grep -Piq -- 'ip=' <<<"${kcl[$kcl_elem]}"; then + ip_addr_found='true' + kcl["${kcl_elem}"]="${default_ip}" + fi + done + if [[ ! "${ip_addr_found}" ]]; then + kcl+=("${default_ip}") + fi + new_kcl='' + first_kcl_elem='true' + for kcl_elem in "${kcl[@]}"; do + if [[ ! "${first_kcl_elem}" ]]; then + new_kcl+=' '"${kcl_elem}" + else + new_kcl+="${kcl_elem}" + unset -v first_kcl_elem + fi + done + yq -i '.Kernel.CommandLine = "'"${new_kcl}"'"' "${zbm_config}" + else + local zbm_config + zbm_config='/boot/syslinux/syslinux.cfg' + set_unique_ip_in_syslinux_kcl "${zbm_config}" "${default_ip}" + fi } function set_pub_keys () {