linux_init.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package fsdriver
  15. import (
  16. "fmt"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/onecloud/pkg/util/procutils"
  19. )
  20. const (
  21. unitDirPath = "/usr/lib/systemd/system"
  22. enableDirPath = "/etc/systemd/system/multi-user.target.wants"
  23. )
  24. func (d *sLinuxRootFs) isSupportSystemd() bool {
  25. return d.rootFs.Exists(unitDirPath, false) && d.rootFs.Exists(enableDirPath, false)
  26. }
  27. func (d *sLinuxRootFs) installInitScript(name, cmd string, oneshot bool) error {
  28. if d.isSupportSystemd() {
  29. return d.installSystemd(name, cmd, oneshot)
  30. } else {
  31. return d.installCrond(cmd)
  32. }
  33. }
  34. func (d *sLinuxRootFs) installCrond(cmd string) error {
  35. cronJob := fmt.Sprintf("@reboot %s", cmd)
  36. if procutils.NewCommand("chroot", d.rootFs.GetMountPath(), "crontab", "-l", "|", "grep", cronJob).Run() == nil {
  37. // if cronjob exist, return success
  38. return nil
  39. }
  40. output, err := procutils.NewCommand("chroot", d.rootFs.GetMountPath(), "sh", "-c",
  41. fmt.Sprintf("(crontab -l 2>/dev/null; echo '%s') |crontab -", cronJob),
  42. ).Output()
  43. if err != nil {
  44. return errors.Wrapf(err, "add crontab %s", output)
  45. }
  46. return nil
  47. }
  48. func (d *sLinuxRootFs) installSystemd(name, cmd string, oneshot bool) error {
  49. var serviceName = fmt.Sprintf("%s.service", name)
  50. var unitPath = fmt.Sprintf("%s/%s", unitDirPath, serviceName)
  51. var enablePath = fmt.Sprintf("%s/%s", enableDirPath, serviceName)
  52. var serviceType = "simple"
  53. if oneshot {
  54. serviceType = "oneshot"
  55. }
  56. var unitContent = fmt.Sprintf(`[Unit]
  57. Description=Run once
  58. After=local-fs.target
  59. After=network.target
  60. [Service]
  61. LimitNOFILE=65535
  62. CapabilityBoundingSet=CAP_NET_RAW
  63. AmbientCapabilities=CAP_NET_RAW
  64. ExecStart=%s
  65. RemainAfterExit=true
  66. Type=%s
  67. [Install]
  68. WantedBy=multi-user.target
  69. `, cmd, serviceType)
  70. err := d.rootFs.FilePutContents(unitPath, unitContent, false, false)
  71. if err != nil {
  72. return errors.Wrap(err, "save user_data unit fail")
  73. }
  74. err = d.rootFs.Symlink(unitPath, enablePath, false)
  75. if err != nil {
  76. return errors.Wrap(err, "create user_data symlink fail")
  77. }
  78. return nil
  79. }
  80. func (d *sLinuxRootFs) InstallQemuGuestAgentSystemd() error {
  81. var serviceName = "qemu-guest-agent.service"
  82. var unitPath = fmt.Sprintf("%s/%s", unitDirPath, serviceName)
  83. var unitContent = fmt.Sprintf(`[Unit]
  84. Description=QEMU Guest Agent
  85. BindsTo=dev-virtio\x2dports-org.qemu.guest_agent.0.device
  86. After=dev-virtio\x2dports-org.qemu.guest_agent.0.device
  87. [Service]
  88. ExecStart=/usr/bin/qemu-ga
  89. Restart=always
  90. RestartSec=0
  91. [Install]
  92. `)
  93. err := d.rootFs.FilePutContents(unitPath, unitContent, false, false)
  94. if err != nil {
  95. return errors.Wrap(err, "save qemu-guest-agent.service unit fail")
  96. }
  97. return nil
  98. }