Best Syzkaller code snippet using config.SaveData
nic_physical.go
Source:nic_physical.go
1package device2import (3 "fmt"4 deviceConfig "github.com/lxc/lxd/lxd/device/config"5 pcidev "github.com/lxc/lxd/lxd/device/pci"6 "github.com/lxc/lxd/lxd/instance"7 "github.com/lxc/lxd/lxd/instance/instancetype"8 "github.com/lxc/lxd/lxd/ip"9 "github.com/lxc/lxd/lxd/network"10 "github.com/lxc/lxd/lxd/revert"11 "github.com/lxc/lxd/lxd/util"12 "github.com/lxc/lxd/shared"13)14type nicPhysical struct {15 deviceCommon16}17// CanHotPlug returns whether the device can be managed whilst the instance is running. Returns true.18func (d *nicPhysical) CanHotPlug() bool {19 return true20}21// validateConfig checks the supplied config for correctness.22func (d *nicPhysical) validateConfig(instConf instance.ConfigReader) error {23 if !instanceSupported(instConf.Type(), instancetype.Container, instancetype.VM) {24 return ErrUnsupportedDevType25 }26 requiredFields := []string{"parent"}27 optionalFields := []string{28 "name",29 "maas.subnet.ipv4",30 "maas.subnet.ipv6",31 "boot.priority",32 "gvrp",33 }34 if instConf.Type() == instancetype.Container || instConf.Type() == instancetype.Any {35 optionalFields = append(optionalFields, "mtu", "hwaddr", "vlan")36 }37 err := d.config.Validate(nicValidationRules(requiredFields, optionalFields, instConf))38 if err != nil {39 return err40 }41 return nil42}43// validateEnvironment checks the runtime environment for correctness.44func (d *nicPhysical) validateEnvironment() error {45 if d.inst.Type() == instancetype.VM && shared.IsTrue(d.inst.ExpandedConfig()["migration.stateful"]) {46 return fmt.Errorf("Network physical devices cannot be used when migration.stateful is enabled")47 }48 if d.inst.Type() == instancetype.Container && d.config["name"] == "" {49 return fmt.Errorf("Requires name property to start")50 }51 if !shared.PathExists(fmt.Sprintf("/sys/class/net/%s", d.config["parent"])) {52 return fmt.Errorf("Parent device '%s' doesn't exist", d.config["parent"])53 }54 return nil55}56// Start is run when the device is added to a running instance or instance is starting up.57func (d *nicPhysical) Start() (*deviceConfig.RunConfig, error) {58 err := d.validateEnvironment()59 if err != nil {60 return nil, err61 }62 // Lock to avoid issues with containers starting in parallel.63 networkCreateSharedDeviceLock.Lock()64 defer networkCreateSharedDeviceLock.Unlock()65 saveData := make(map[string]string)66 revert := revert.New()67 defer revert.Fail()68 // pciIOMMUGroup, used for VM physical passthrough.69 var pciIOMMUGroup uint6470 // If VM, then try and load the vfio-pci module first.71 if d.inst.Type() == instancetype.VM {72 err = util.LoadModule("vfio-pci")73 if err != nil {74 return nil, fmt.Errorf("Error loading %q module: %w", "vfio-pci", err)75 }76 }77 // Record the host_name device used for restoration later.78 saveData["host_name"] = network.GetHostDevice(d.config["parent"], d.config["vlan"])79 if d.inst.Type() == instancetype.Container {80 statusDev, err := networkCreateVlanDeviceIfNeeded(d.state, d.config["parent"], saveData["host_name"], d.config["vlan"], shared.IsTrue(d.config["gvrp"]))81 if err != nil {82 return nil, err83 }84 // Record whether we created this device or not so it can be removed on stop.85 saveData["last_state.created"] = fmt.Sprintf("%t", statusDev != "existing")86 if shared.IsTrue(saveData["last_state.created"]) {87 revert.Add(func() {88 _ = networkRemoveInterfaceIfNeeded(d.state, saveData["host_name"], d.inst, d.config["parent"], d.config["vlan"])89 })90 }91 // If we didn't create the device we should track various properties so we can restore them when the92 // instance is stopped or the device is detached.93 if shared.IsFalse(saveData["last_state.created"]) {94 err = networkSnapshotPhysicalNIC(saveData["host_name"], saveData)95 if err != nil {96 return nil, err97 }98 }99 // Set the MAC address.100 if d.config["hwaddr"] != "" {101 link := &ip.Link{Name: saveData["host_name"]}102 err := link.SetAddress(d.config["hwaddr"])103 if err != nil {104 return nil, fmt.Errorf("Failed to set the MAC address: %s", err)105 }106 }107 // Set the MTU.108 if d.config["mtu"] != "" {109 link := &ip.Link{Name: saveData["host_name"]}110 err := link.SetMTU(d.config["mtu"])111 if err != nil {112 return nil, fmt.Errorf("Failed setting MTU %q on %q: %w", d.config["mtu"], saveData["host_name"], err)113 }114 }115 } else if d.inst.Type() == instancetype.VM {116 // Get PCI information about the network interface.117 ueventPath := fmt.Sprintf("/sys/class/net/%s/device/uevent", saveData["host_name"])118 pciDev, err := pcidev.ParseUeventFile(ueventPath)119 if err != nil {120 return nil, fmt.Errorf("Failed to get PCI device info for %q: %w", saveData["host_name"], err)121 }122 saveData["last_state.pci.slot.name"] = pciDev.SlotName123 saveData["last_state.pci.driver"] = pciDev.Driver124 pciIOMMUGroup, err = pcidev.DeviceIOMMUGroup(saveData["last_state.pci.slot.name"])125 if err != nil {126 return nil, err127 }128 err = pcidev.DeviceDriverOverride(pciDev, "vfio-pci")129 if err != nil {130 return nil, err131 }132 }133 err = d.volatileSet(saveData)134 if err != nil {135 return nil, err136 }137 runConf := deviceConfig.RunConfig{}138 runConf.NetworkInterface = []deviceConfig.RunConfigItem{139 {Key: "type", Value: "phys"},140 {Key: "name", Value: d.config["name"]},141 {Key: "flags", Value: "up"},142 {Key: "link", Value: saveData["host_name"]},143 }144 if d.inst.Type() == instancetype.VM {145 runConf.NetworkInterface = append(runConf.NetworkInterface,146 []deviceConfig.RunConfigItem{147 {Key: "devName", Value: d.name},148 {Key: "pciSlotName", Value: saveData["last_state.pci.slot.name"]},149 {Key: "pciIOMMUGroup", Value: fmt.Sprintf("%d", pciIOMMUGroup)},150 }...)151 }152 revert.Success()153 return &runConf, nil154}155// Stop is run when the device is removed from the instance.156func (d *nicPhysical) Stop() (*deviceConfig.RunConfig, error) {157 v := d.volatileGet()158 runConf := deviceConfig.RunConfig{159 PostHooks: []func() error{d.postStop},160 NetworkInterface: []deviceConfig.RunConfigItem{161 {Key: "link", Value: v["host_name"]},162 },163 }164 return &runConf, nil165}166// postStop is run after the device is removed from the instance.167func (d *nicPhysical) postStop() error {168 defer func() {169 _ = d.volatileSet(map[string]string{170 "host_name": "",171 "last_state.hwaddr": "",172 "last_state.mtu": "",173 "last_state.created": "",174 "last_state.pci.slot.name": "",175 "last_state.pci.driver": "",176 })177 }()178 v := d.volatileGet()179 // If VM physical pass through, unbind from vfio-pci and bind back to host driver.180 if d.inst.Type() == instancetype.VM && v["last_state.pci.slot.name"] != "" {181 vfioDev := pcidev.Device{182 Driver: "vfio-pci",183 SlotName: v["last_state.pci.slot.name"],184 }185 err := pcidev.DeviceDriverOverride(vfioDev, v["last_state.pci.driver"])186 if err != nil {187 return err188 }189 } else if d.inst.Type() == instancetype.Container {190 hostName := network.GetHostDevice(d.config["parent"], d.config["vlan"])191 // This will delete the parent interface if we created it for VLAN parent.192 if shared.IsTrue(v["last_state.created"]) {193 err := networkRemoveInterfaceIfNeeded(d.state, hostName, d.inst, d.config["parent"], d.config["vlan"])194 if err != nil {195 return err196 }197 } else if v["last_state.pci.slot.name"] == "" {198 err := networkRestorePhysicalNIC(hostName, v)199 if err != nil {200 return err201 }202 }203 }204 return nil205}...
infiniband_physical.go
Source:infiniband_physical.go
1package device2import (3 "fmt"4 deviceConfig "github.com/lxc/lxd/lxd/device/config"5 pcidev "github.com/lxc/lxd/lxd/device/pci"6 "github.com/lxc/lxd/lxd/instance"7 "github.com/lxc/lxd/lxd/instance/instancetype"8 "github.com/lxc/lxd/lxd/ip"9 "github.com/lxc/lxd/lxd/resources"10 "github.com/lxc/lxd/lxd/util"11 "github.com/lxc/lxd/shared"12)13type infinibandPhysical struct {14 deviceCommon15}16// validateConfig checks the supplied config for correctness.17func (d *infinibandPhysical) validateConfig(instConf instance.ConfigReader) error {18 requiredFields := []string{"parent"}19 optionalFields := []string{20 "name",21 "mtu",22 "hwaddr",23 }24 rules := nicValidationRules(requiredFields, optionalFields, instConf)25 rules["hwaddr"] = func(value string) error {26 if value == "" {27 return nil28 }29 return infinibandValidMAC(value)30 }31 err := d.config.Validate(rules)32 if err != nil {33 return err34 }35 return nil36}37// validateEnvironment checks the runtime environment for correctness.38func (d *infinibandPhysical) validateEnvironment() error {39 if d.inst.Type() == instancetype.Container && d.config["name"] == "" {40 return fmt.Errorf("Requires name property to start")41 }42 if !shared.PathExists(fmt.Sprintf("/sys/class/net/%s", d.config["parent"])) {43 return fmt.Errorf("Parent device '%s' doesn't exist", d.config["parent"])44 }45 return nil46}47// Start is run when the device is added to a running instance or instance is starting up.48func (d *infinibandPhysical) Start() (*deviceConfig.RunConfig, error) {49 err := d.validateEnvironment()50 if err != nil {51 return nil, err52 }53 saveData := make(map[string]string)54 // pciIOMMUGroup, used for VM physical passthrough.55 var pciIOMMUGroup uint6456 // If VM, then try and load the vfio-pci module first.57 if d.inst.Type() == instancetype.VM {58 err = util.LoadModule("vfio-pci")59 if err != nil {60 return nil, fmt.Errorf("Error loading %q module: %w", "vfio-pci", err)61 }62 }63 runConf := deviceConfig.RunConfig{}64 // Load network interface info.65 nics, err := resources.GetNetwork()66 if err != nil {67 return nil, err68 }69 // Filter the network interfaces to just infiniband devices related to parent.70 ibDevs := infinibandDevices(nics, d.config["parent"])71 ibDev, found := ibDevs[d.config["parent"]]72 if !found {73 return nil, fmt.Errorf("Specified infiniband device \"%s\" not found", d.config["parent"])74 }75 saveData["host_name"] = ibDev.ID76 if d.inst.Type() == instancetype.Container {77 // Record hwaddr and mtu before potentially modifying them.78 err = networkSnapshotPhysicalNIC(saveData["host_name"], saveData)79 if err != nil {80 return nil, err81 }82 // Set the MAC address.83 if d.config["hwaddr"] != "" {84 err := infinibandSetDevMAC(saveData["host_name"], d.config["hwaddr"])85 if err != nil {86 return nil, fmt.Errorf("Failed to set the MAC address: %s", err)87 }88 }89 // Set the MTU.90 if d.config["mtu"] != "" {91 link := &ip.Link{Name: saveData["host_name"]}92 err := link.SetMTU(d.config["mtu"])93 if err != nil {94 return nil, fmt.Errorf("Failed setting MTU %q on %q: %w", d.config["mtu"], saveData["host_name"], err)95 }96 }97 // Configure runConf with infiniband setup instructions.98 err = infinibandAddDevices(d.state, d.inst.DevicesPath(), d.name, ibDev, &runConf)99 if err != nil {100 return nil, err101 }102 } else if d.inst.Type() == instancetype.VM {103 // Get PCI information about the network interface.104 ueventPath := fmt.Sprintf("/sys/class/net/%s/device/uevent", saveData["host_name"])105 pciDev, err := pcidev.ParseUeventFile(ueventPath)106 if err != nil {107 return nil, fmt.Errorf("Failed to get PCI device info for %q: %w", saveData["host_name"], err)108 }109 saveData["last_state.pci.slot.name"] = pciDev.SlotName110 saveData["last_state.pci.driver"] = pciDev.Driver111 err = pcidev.DeviceDriverOverride(pciDev, "vfio-pci")112 if err != nil {113 return nil, err114 }115 pciIOMMUGroup, err = pcidev.DeviceIOMMUGroup(saveData["last_state.pci.slot.name"])116 if err != nil {117 return nil, err118 }119 // Record original driver used by device for restore.120 saveData["last_state.pci.driver"] = pciDev.Driver121 }122 err = d.volatileSet(saveData)123 if err != nil {124 return nil, err125 }126 runConf.NetworkInterface = []deviceConfig.RunConfigItem{127 {Key: "type", Value: "phys"},128 {Key: "name", Value: d.config["name"]},129 {Key: "flags", Value: "up"},130 {Key: "link", Value: saveData["host_name"]},131 }132 if d.inst.Type() == instancetype.VM {133 runConf.NetworkInterface = append(runConf.NetworkInterface,134 []deviceConfig.RunConfigItem{135 {Key: "devName", Value: d.name},136 {Key: "pciSlotName", Value: saveData["last_state.pci.slot.name"]},137 {Key: "pciIOMMUGroup", Value: fmt.Sprintf("%d", pciIOMMUGroup)},138 }...)139 }140 return &runConf, nil141}142// Stop is run when the device is removed from the instance.143func (d *infinibandPhysical) Stop() (*deviceConfig.RunConfig, error) {144 v := d.volatileGet()145 runConf := deviceConfig.RunConfig{146 PostHooks: []func() error{d.postStop},147 NetworkInterface: []deviceConfig.RunConfigItem{148 {Key: "link", Value: v["host_name"]},149 },150 }151 if d.inst.Type() == instancetype.Container {152 err := unixDeviceRemove(d.inst.DevicesPath(), IBDevPrefix, d.name, "", &runConf)153 if err != nil {154 return nil, err155 }156 }157 return &runConf, nil158}159// postStop is run after the device is removed from the instance.160func (d *infinibandPhysical) postStop() error {161 defer func() {162 _ = d.volatileSet(map[string]string{163 "host_name": "",164 "last_state.hwaddr": "",165 "last_state.mtu": "",166 "last_state.pci.slot.name": "",167 "last_state.pci.driver": "",168 })169 }()170 v := d.volatileGet()171 // If VM physical pass through, unbind from vfio-pci and bind back to host driver.172 if d.inst.Type() == instancetype.VM && v["last_state.pci.slot.name"] != "" {173 vfioDev := pcidev.Device{174 Driver: "vfio-pci",175 SlotName: v["last_state.pci.slot.name"],176 }177 // Unbind device from the host so that the restored settings will take effect when we rebind it.178 err := pcidev.DeviceUnbind(vfioDev)179 if err != nil {180 return err181 }182 err = pcidev.DeviceDriverOverride(vfioDev, v["last_state.pci.driver"])183 if err != nil {184 return err185 }186 } else if d.inst.Type() == instancetype.Container {187 // Remove infiniband host files for this device.188 err := unixDeviceDeleteFiles(d.state, d.inst.DevicesPath(), IBDevPrefix, d.name, "")189 if err != nil {190 return fmt.Errorf("Failed to delete files for device '%s': %w", d.name, err)191 }192 }193 // Restore hwaddr and mtu.194 if v["host_name"] != "" {195 err := networkRestorePhysicalNIC(v["host_name"], v)196 if err != nil {197 return err198 }199 }200 return nil201}...
SaveData
Using AI Code Generation
1import "fmt"2import "github.com/astaxie/beego/config"3func main() {4 conf, err := config.NewConfig("ini", "config.ini")5 if err != nil {6 fmt.Println(err)7 }8 conf.SaveData("data", "hello world")9 conf.SaveData("data", "hello world")10}11import "fmt"12import "github.com/astaxie/beego/config"13func main() {14 conf, err := config.NewConfig("ini", "config.ini")15 if err != nil {16 fmt.Println(err)17 }18 fmt.Println(conf.GetSection("data"))19}20import "fmt"21import "github.com/astaxie/beego/config"22func main() {23 conf, err := config.NewConfig("ini", "config.ini")24 if err != nil {25 fmt.Println(err)26 }27 fmt.Println(conf.GetSectionList())28}29import "fmt"30import "github.com/astaxie/beego/config"31func main() {32 conf, err := config.NewConfig("ini", "config.ini")33 if err != nil {34 fmt.Println(err)35 }36 fmt.Println(conf.GetSections())37}38import "fmt"39import "github.com/astaxie/beego/config"40func main() {41 conf, err := config.NewConfig("ini", "config.ini")42 if err != nil {43 fmt.Println(err)44 }45 fmt.Println(conf.GetSectionComments("data"))46}
SaveData
Using AI Code Generation
1import (2func main() {3 c := config.New()4 c.Set("name", "John")5 c.Set("age", 42)6 c.Set("married", true)7 c.Set("kids", []string{"Alice", "Bob"})8 c.SaveData("config.json")9}10import (11func main() {12 c := config.New()13 c.LoadData("config.json")14}
SaveData
Using AI Code Generation
1import (2func main() {3 d := config.NewConfig("config.json")4 d.SaveData("name", "Akshay Babloo")5 d.SaveData("age", 21)6 d.SaveData("address", "Bangalore")7 d.SaveData("hobbies", []string{"Coding", "Playing Games", "Watching Movies"})8 d.SaveData("isHuman", true)9 d.SaveData("isMale", true)10 d.SaveData("isMarried", false)11}12import (13func main() {14 d := config.NewConfig("config.json")15 fmt.Println(d.GetData("name"))16 fmt.Println(d.GetData("age"))17 fmt.Println(d.GetData("address"))18 fmt.Println(d.GetData("hobbies"))19 fmt.Println(d.GetData("isHuman"))20 fmt.Println(d.GetData("isMale"))21 fmt.Println(d.GetData("isMarried"))22}23import (24func main() {25 d := config.NewConfig("config.json")26 d.DeleteData("name")27 d.DeleteData("age")28 d.DeleteData("address")29 d.DeleteData("hobbies")30 d.DeleteData("isHuman")31 d.DeleteData("isMale")32 d.DeleteData("isMarried")33}34import (35func main() {36 d := config.NewConfig("config.json")37 d.SaveData("name", "Akshay Babloo")38 d.SaveData("age", 21)39 d.SaveData("address", "Bangalore")40 d.SaveData("hobbies", []string{"Coding", "Playing Games", "Watching Movies"})41 d.SaveData("isHuman", true)42 d.SaveData("isMale", true)43 d.SaveData("isMarried", false)44 d.SaveData("
SaveData
Using AI Code Generation
1import (2func main() {3 config := config.NewConfig("config.json")4 config.SaveData("test", "test")5 fmt.Println(config.ReadData("test"))6}7{8}9import (10func main() {11 config := config.NewConfig("config.json")12 config.SaveData("test", "test")13 fmt.Println(config.ReadData("test"))14}15{16}17import (18func main() {19 config := config.NewConfig("config.json")20 config.SaveData("test", "test")21 fmt.Println(config.ReadData("test"))22}23{24}25import (26func main() {27 config := config.NewConfig("config.json")28 config.SaveData("test", "test")29 fmt.Println(config.ReadData("test"))30}31{32}33import (34func main() {35 config := config.NewConfig("config.json")36 config.SaveData("test", "test")37 fmt.Println(config.ReadData("test"))38}39{40}41import (42func main() {43 config := config.NewConfig("config.json")44 config.SaveData("
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!