create dynamic devfs ruleset from configured or default

This commit is contained in:
yo 2022-06-18 20:07:57 +02:00
parent 9218ffafe1
commit c585678be9

View File

@ -414,6 +414,59 @@ func getDatastoreFromArray(name string, dsa []Datastore) (*Datastore, error) {
return &Datastore{}, errors.New("Datastore not found")
}
func getDevfsRuleset(ruleset int) []string {
cmd := fmt.Sprintf("/sbin/devfs rule -s %d show", ruleset)
out, err := executeCommand(cmd)
if err != nil {
return []string{}
}
// Get rid of the last "\n"
return strings.Split(out, "\n")[:len(strings.Split(out, "\n"))-1]
}
func copyDevfsRuleset(ruleset int, srcrs int) error {
// Resulting ruleset as an array of line
//var result []string
out := getDevfsRuleset(srcrs)
for _, line := range out {
//fields := strings.Fields(line)
cmd := fmt.Sprintf("/sbin/devfs rule -s %d add %s", ruleset, line)
out, err := executeCommand(cmd)
if err != nil {
return errors.New(fmt.Sprintf("Error adding rule \"%s\" to ruleset %d: %s", line, ruleset, out))
}
}
return nil
}
/********************************************************************************
* Add a rule to specified ruleset
* Ex.: addDevfsRuleToRuleset("path bpf* unhide", 1002)
*******************************************************************************/
func addDevfsRuleToRuleset(rule string, ruleset int) error {
// TODO: Check if rule not already enabled. We will need to recurse into includes.
// Get last rule index
rules := getDevfsRuleset(ruleset)
if len(rules) == 0 {
fmt.Printf("Error listing ruleset %d\n", ruleset)
return errors.New(fmt.Sprintf("Error listing rueset %d\n", ruleset))
}
f := strings.Fields(rules[(len(rules)-1)])
//fmt.Printf("Dernier index du ruleset %d: %s\n", ruleset, f[0])
index, _ := strconv.Atoi(f[0])
index += 100
cmd := fmt.Sprintf("/sbin/devfs rule -s %d add %d %s", ruleset, index, rule)
out, err := executeCommand(cmd)
if err != nil {
return errors.New(fmt.Sprintf("Error adding rule \"%s\" to ruleset %d: %s", rule, ruleset, out))
}
return nil
}
/******************************************************************************
* Return the quantity of jails with the name passed as parameter
*****************************************************************************/