@@ -17,7 +17,7 @@ import (
1717 "github.com/vishvananda/netns"
1818)
1919
20- var ip , command , gateway , intf , logLevel , nsPath string
20+ var ip , command , gateway , intf , logLevel , nsPath , mac string
2121var log = logrus .New ()
2222
2323func init () {
@@ -26,6 +26,7 @@ func init() {
2626 flag .StringVar (& command , "command" , "ip route" , "command to be executed" )
2727 flag .StringVar (& gateway , "gw" , "" , "gateway of the request (default will be the default route of the given interface)" )
2828 flag .StringVar (& logLevel , "log-level" , "info" , "min level of logs to print" )
29+ flag .StringVar (& mac , "mac" , "" , "mac address of the interface inside the namespace (default will be a random one)" )
2930 flag .StringVar (
3031 & nsPath ,
3132 "ns-path" ,
@@ -120,19 +121,28 @@ func main() {
120121 return
121122 }
122123
123- err = netlink .LinkSetDown (macVlan )
124- if err != nil {
125- log .Warn ("Error while setting macVlan down: " , err )
126- return
127- }
128-
129124 link , err := netlink .LinkByName ("peth0" )
130125 if err != nil {
131126 log .Warn ("Error while getting macVlan: " , err )
132127 return
133128 }
134129 log .Debugf ("MacVlan created : %+v" , link )
135130
131+ // If a mac was specified, set it now
132+ if mac != "" {
133+ log .Debugf ("Setting macVlan with specified MAC : %s" , mac )
134+ hardwareAddr , err := net .ParseMAC (mac )
135+ if err != nil {
136+ log .Warn ("Error while parsing given mac: " , err )
137+ return
138+ }
139+ err = netlink .LinkSetHardwareAddr (link , hardwareAddr )
140+ if err != nil {
141+ log .Warn ("Error while setting given mac on macVlan: " , err )
142+ return
143+ }
144+ }
145+
136146 // ============================== Create the new Namespace
137147
138148 newns , err := newNS ()
0 commit comments