| 1 | |
| 2 | |
| 3 | |
| 4 | package main |
| 5 | |
| 6 | import ( |
| 7 | "os" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/sirupsen/logrus" |
| 11 | "github.com/spf13/cobra" |
| 12 | |
| 13 | "github.com/lima-vm/lima/v2/pkg/networks" |
| 14 | "github.com/lima-vm/lima/v2/pkg/osutil" |
| 15 | "github.com/lima-vm/lima/v2/pkg/version" |
| 16 | ) |
| 17 | |
| 18 | func main() { |
| 19 | os.Setenv("PATH", strings.Join(safeDirs, ":")) |
| 20 | if err := newApp().Execute(); err != nil { |
| 21 | osutil.HandleExitError(err) |
| 22 | logrus.Fatal(err) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | func newApp() *cobra.Command { |
| 27 | rootCmd := &cobra.Command{ |
| 28 | Use: "lima-privileged-net", |
| 29 | Short: "Do not launch manually", |
| 30 | Long: `Privileged helper that manages the Linux bridges and tap devices of the |
| 31 | "shared", "host", and "bridged" networks. It is executed via sudo by limactl.`, |
| 32 | Version: strings.TrimPrefix(version.Version, "v"), |
| 33 | SilenceUsage: true, |
| 34 | SilenceErrors: true, |
| 35 | PersistentPreRunE: func(_ *cobra.Command, _ []string) error { |
| 36 | return verifySelf() |
| 37 | }, |
| 38 | } |
| 39 | rootCmd.AddCommand(newStartCommand(), newTapCommand()) |
| 40 | return rootCmd |
| 41 | } |
| 42 | |
| 43 | func newStartCommand() *cobra.Command { |
| 44 | var c netConfig |
| 45 | cmd := &cobra.Command{ |
| 46 | Use: "start", |
| 47 | Short: "Bring a network up and stay in the foreground until terminated", |
| 48 | Args: cobra.NoArgs, |
| 49 | RunE: func(cmd *cobra.Command, _ []string) error { |
| 50 | return c.run(cmd.Context()) |
| 51 | }, |
| 52 | } |
| 53 | f := cmd.Flags() |
| 54 | f.StringVar(&c.pidFile, "pidfile", "", "file to write the PID of this process to") |
| 55 | f.StringVar(&c.mode, "mode", "", "network mode: "+networks.ModeShared+", "+networks.ModeHost+", or "+networks.ModeBridged) |
| 56 | f.StringVar(&c.bridge, "bridge", "", "name of the bridge interface") |
| 57 | f.StringVar(&c.gateway, "gateway", "", "IPv4 address assigned to the bridge") |
| 58 | f.StringVar(&c.netmask, "netmask", "", "IPv4 netmask of the network") |
| 59 | f.StringVar(&c.dhcpEnd, "dhcp-end", "", "last IPv4 address handed out by DHCP") |
| 60 | return cmd |
| 61 | } |
| 62 | |
| 63 | func newTapCommand() *cobra.Command { |
| 64 | var bridge string |
| 65 | cmd := &cobra.Command{ |
| 66 | Use: "tap TAP", |
| 67 | Short: "Create a tap device owned by the calling user and attach it to a bridge", |
| 68 | Args: cobra.ExactArgs(1), |
| 69 | RunE: func(cmd *cobra.Command, args []string) error { |
| 70 | return tapUp(cmd.Context(), args[0], bridge) |
| 71 | }, |
| 72 | } |
| 73 | cmd.Flags().StringVar(&bridge, "bridge", "", "name of the bridge to attach the tap device to") |
| 74 | return cmd |
| 75 | } |