From a91eafdf95bcbcc1bd2ba5aa991ac6a8f0d14446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Mon, 10 Jun 2013 15:31:09 +0200 Subject: [PATCH] cli: 'con add': make ifname mandatory (except bond,bridge,vlan) (bgo #698113) Optional 'ifname' allowed creating connection applicable to all interfaces, which was confusing for some users. Now we require the user to provide ifname to lock the connection for an interface. An "unbound" connection can be created with ifname "*". $ nmcli connection add type eth ifname eth0 $ nmcli connection add type eth now becomes $ nmcli connection add type eth ifname "*" bond, bridge: - when ifname is not specified or is "*", interface name is generated (nm-bond, nm-bridge) vlan: - when ifname is not specified or is "*", vlan device is named "dev.id" Note: the quotes around * are required to suppress shell expansion. --- cli/src/connections.c | 29 +++++++++++++++++++++++++---- man/nmcli.1.in | 7 +++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/cli/src/connections.c b/cli/src/connections.c index b01fcbdae..a7b41e58a 100644 --- a/cli/src/connections.c +++ b/cli/src/connections.c @@ -217,9 +217,9 @@ usage_connection_add (void) " OPTIONS := COMMON_OPTIONS TYPE_SPECIFIC_OPTIONS IP_OPTIONS\n\n" " COMMON_OPTIONS:\n" " type \n" + " ifname | \"*\"\n" " [con-name ]\n" - " [autoconnect yes|no]\n" - " [ifname ]\n\n" + " [autoconnect yes|no]\n\n" " TYPE_SPECIFIC_OPTIONS:\n" " ethernet: [mac ]\n" " [cloned-mac ]\n" @@ -2925,6 +2925,8 @@ do_connection_add (NmCli *nmc, int argc, char **argv) const char *autoconnect = NULL; gboolean auto_bool = TRUE; const char *ifname = NULL; + char *ifname_ask = NULL; + gboolean ifname_mandatory = TRUE; AddConnectionInfo *info = NULL; const char *setting_name; GError *error = NULL; @@ -2972,13 +2974,31 @@ do_connection_add (NmCli *nmc, int argc, char **argv) goto error; } } + + /* ifname is mandatory for all connection types except virtual ones (bond, bridge, vlan) */ + if ( strcmp (type, NM_SETTING_BOND_SETTING_NAME) == 0 + || strcmp (type, NM_SETTING_BRIDGE_SETTING_NAME) == 0 + || strcmp (type, NM_SETTING_VLAN_SETTING_NAME) == 0) + ifname_mandatory = FALSE; + + if (!ifname && ifname_mandatory && nmc->ask) + ifname = ifname_ask = nmc_get_user_input (_("Interface name: ")); + if (!ifname && ifname_mandatory) { + g_string_printf (nmc->return_text, _("Error: 'ifname' argument is required.")); + nmc->return_value = NMC_RESULT_ERROR_USER_INPUT; + goto error; + } if (ifname) { - if (!nm_utils_iface_valid_name (ifname)) { + if (!nm_utils_iface_valid_name (ifname) && strcmp (ifname, "*") != 0) { g_string_printf (nmc->return_text, - _("Error: 'ifname': '%s' is not a valid interface."), + _("Error: 'ifname': '%s' is not a valid interface nor '*'."), ifname); + nmc->return_value = NMC_RESULT_ERROR_USER_INPUT; goto error; } + /* Special value of '*' means no specific interface name */ + if (strcmp (ifname, "*") == 0) + ifname = NULL; } /* Create a new connection object */ @@ -3041,6 +3061,7 @@ error: if (connection) g_object_unref (connection); g_free (type_ask); + g_free (ifname_ask); nmc->should_wait = FALSE; return nmc->return_value; diff --git a/man/nmcli.1.in b/man/nmcli.1.in index f40895f31..77675d3c6 100644 --- a/man/nmcli.1.in +++ b/man/nmcli.1.in @@ -325,12 +325,15 @@ Add a connection for NetworkManager. Arguments differ according to connection ty .B COMMON_OPTIONS: .IP "\fItype \fP" 42 \(en connection type; see bellow \fBTYPE_SPECIFIC_OPTIONS\fP for allowed values; (mandatory) +.IP "\fIifname | \(dq\&*\(dq\&\fP" 42 +\(en interface to bind the connection to. The connection will only be applicable to this +interface name. A special value of "\fB*\fP" can be used for interface-independent connections. +The \fIifname\fP argument is mandatory for all connection types except bond, bridge and vlan. +Note: use quotes around \fB*\fP to suppress shell expansion. .IP "\fI[con-name ]\fP" 42 \(en connection name (when not provided a default name is generated: [-][-]) .IP "\fI[autoconnect yes|no]\fP" 42 \(en whether the connection can auto-connect or not -.IP "\fI[ifname ]\fP" 42 -\(en interface to bind the connection to .RE .RS .TP