@@ -10,6 +10,7 @@ import (
1010 "maps"
1111 "net"
1212 "os"
13+ "path/filepath"
1314 "slices"
1415 "strings"
1516 "text/tabwriter"
@@ -18,12 +19,17 @@ import (
1819 "github.com/spf13/cobra"
1920
2021 "github.com/lima-vm/lima/v2/pkg/networks"
22+ "github.com/lima-vm/lima/v2/pkg/networks/usernet"
23+ "github.com/lima-vm/lima/v2/pkg/networks/usernet/filter"
2124 "github.com/lima-vm/lima/v2/pkg/yqutil"
2225)
2326
2427const networkCreateExample = ` Create a network:
2528 $ limactl network create foo --gateway 192.168.42.1/24
2629
30+ Create a network with policy filtering:
31+ $ limactl network create secure --gateway 192.168.42.1/24 --policy ~/policy.yaml
32+
2733 Connect VM instances to the newly created network:
2834 $ limactl create --network lima:foo --name vm1
2935 $ limactl create --network lima:foo --name vm2
@@ -144,6 +150,7 @@ func newNetworkCreateCommand() *cobra.Command {
144150 flags .String ("gateway" , "" , "gateway, e.g., \" 192.168.42.1/24\" " )
145151 flags .String ("interface" , "" , "interface for bridged mode" )
146152 _ = cmd .RegisterFlagCompletionFunc ("interface" , bashFlagCompleteNetworkInterfaceNames )
153+ flags .String ("policy" , "" , "path to policy file (YAML or JSON, user-v2 mode only)" )
147154 return cmd
148155}
149156
@@ -174,6 +181,38 @@ func networkCreateAction(cmd *cobra.Command, args []string) error {
174181 return err
175182 }
176183
184+ policyPath , err := flags .GetString ("policy" )
185+ if err != nil {
186+ return err
187+ }
188+
189+ // Handle policy file if provided
190+ if policyPath != "" {
191+ // Only user-v2 mode supports filtering
192+ if mode != networks .ModeUserV2 {
193+ logrus .Warnf ("Policy filtering is only supported for mode 'user-v2', ignoring --policy flag" )
194+ } else {
195+ // Load the policy to validate it
196+ pol , err := filter .LoadPolicy (policyPath )
197+ if err != nil {
198+ return fmt .Errorf ("failed to load policy: %w" , err )
199+ }
200+
201+ // Save as JSON in the network directory (~/.lima/_networks/<name>/policy.json)
202+ policyJSONPath , err := usernet .PolicyFile (name )
203+ if err != nil {
204+ return fmt .Errorf ("failed to get policy path: %w" , err )
205+ }
206+ // Ensure network directory exists (follows usernet convention)
207+ if err := os .MkdirAll (filepath .Dir (policyJSONPath ), 0o755 ); err != nil {
208+ return fmt .Errorf ("failed to create network directory: %w" , err )
209+ }
210+ if err := filter .SavePolicyJSON (pol , policyJSONPath ); err != nil {
211+ return fmt .Errorf ("failed to save policy: %w" , err )
212+ }
213+ }
214+ }
215+
177216 switch mode {
178217 case networks .ModeBridged :
179218 if gateway != "" {
0 commit comments