diff --git a/newvm.sh b/newvm.sh index 5b1527f..1bc9ba3 100755 --- a/newvm.sh +++ b/newvm.sh @@ -14,6 +14,27 @@ console=true default_groups=true graphics=false +gen_dracut_ipconfig() { + local hostname=$1 + local ip_address=$2 + local gateway=$3 + local nameserver=$4 + + local netmask=$(ipcalc --no-decorate --netmask "${ip_address}") + if [ -z "${gateway}" ]; then + gateway=$(ipcalc --no-decorate --minaddr "${ip_address}") + fi + ip_address=$(ipcalc --no-decorate --address "${ip_address}") + + printf 'ip=%s::%s:%s:%s::none:%s\n' \ + "${ip_address}" \ + "${gateway}" \ + "${netmask}" \ + "${hostname}" \ + "${nameserver}" +} + + usage() { printf 'usage: %s [options] name\n' "${0##*/}" } @@ -70,6 +91,27 @@ while [ $# -gt 0 ]; do --network=*) network="${1#*=}" ;; + --ip-address) + shift + ip_address=$1 + ;; + --ip-address=*) + ip_address=${1#*=} + ;; + --gateway) + shift + gateway=$1 + ;; + --gateway=*) + gateway=${1#*=} + ;; + --nameserver) + shift + nameserver=$1 + ;; + --nameserver=*) + nameserver=${1#=*} + ;; --domain) shift dnsdomain=$1 @@ -138,6 +180,24 @@ if [ -z "${LIBVIRT_DEFAULT_URI}" ]; then exit 1 fi +case ${ip_address} in +'') + ipconfig="ip=::::${hostname}::dhcp" + ;; +*/*) + ipconfig=$(gen_dracut_ipconfig \ + "${hostname}" \ + "${ip_address}" \ + "${gateway}" \ + "${nameserver}" + ) + ;; +*) + printf 'Invalid IP configuration: missing prefix length\n' >&2 + exit 1 + ;; +esac + if [ ${fedora} -gt 40 ] && [ ${memory} -lt 4096 ]; then printf 'WARNING Fedora 41+ requires at least 4 GB memory to install\n' >&2 memory=4096 @@ -163,7 +223,7 @@ else printf 'Using HTTP proxy: %s\n' "${http_proxy}" >&2 fi -extra_args="ip=::::${hostname}::dhcp inst.ks=${kickstart}" +extra_args="${ipconfig} inst.ks=${kickstart}" if [ -n "${http_proxy}" ]; then extra_args="${extra_args} inst.proxy=${http_proxy}" if [ ${fedora} -lt 40 ]; then