udevsettle.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package control
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "github.com/rancher/os/config"
  8. "github.com/rancher/os/pkg/log"
  9. "github.com/codegangsta/cli"
  10. )
  11. func udevSettleAction(c *cli.Context) {
  12. if err := extraRules(); err != nil {
  13. log.Error(err)
  14. }
  15. if err := UdevSettle(); err != nil {
  16. log.Fatal(err)
  17. }
  18. }
  19. func extraRules() error {
  20. cfg := config.LoadConfig()
  21. if len(cfg.Rancher.Network.ModemNetworks) > 0 {
  22. rules, err := ioutil.ReadDir(config.UdevRulesExtrasDir)
  23. if err != nil {
  24. return err
  25. }
  26. for _, r := range rules {
  27. if r.IsDir() || filepath.Ext(r.Name()) != ".rules" {
  28. continue
  29. }
  30. err := os.Symlink(filepath.Join(config.UdevRulesExtrasDir, r.Name()), filepath.Join(config.UdevRulesDir, r.Name()))
  31. if err != nil {
  32. return err
  33. }
  34. }
  35. } else {
  36. rules, err := ioutil.ReadDir(config.UdevRulesDir)
  37. if err != nil {
  38. return err
  39. }
  40. for _, r := range rules {
  41. if r.IsDir() || (filepath.Ext(r.Name()) != ".rules") || (r.Mode()&os.ModeSymlink != 0) {
  42. continue
  43. }
  44. err := os.Remove(filepath.Join(config.UdevRulesDir, r.Name()))
  45. if err != nil {
  46. return err
  47. }
  48. }
  49. }
  50. return nil
  51. }
  52. func UdevSettle() error {
  53. cmd := exec.Command("udevd", "--daemon")
  54. defer exec.Command("killall", "udevd").Run()
  55. cmd.Stdout = os.Stdout
  56. cmd.Stderr = os.Stderr
  57. if err := cmd.Run(); err != nil {
  58. return err
  59. }
  60. cmd = exec.Command("udevadm", "trigger", "--action=add")
  61. cmd.Stdout = os.Stdout
  62. cmd.Stderr = os.Stderr
  63. if err := cmd.Run(); err != nil {
  64. return err
  65. }
  66. cmd = exec.Command("udevadm", "settle")
  67. cmd.Stdout = os.Stdout
  68. cmd.Stderr = os.Stderr
  69. return cmd.Run()
  70. }