Class: Yast::SystemdClass

Inherits:
Module
  • Object
show all
Defined in:
library/systemd/src/modules/Systemd.rb

Instance Method Summary collapse

Instance Method Details

#DefaultRunlevelFixnum

Get the default runlevel for systemd

Returns:

  • (Fixnum)

    the default runlevel (or nil on error or unknown runlevel)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'library/systemd/src/modules/Systemd.rb', line 95

def DefaultRunlevel
  target = Convert.to_string(
    SCR.Read(path(".target.symlink"), @default_target_symlink)
  )
  Builtins.y2milestone("Default symlink points to: %1", target)

  if target.nil?
    Builtins.y2error(
      "Cannot read symlink target of %1",
      @default_target_symlink
    )
    return nil
  end

  # check runlevel<number>.target
  runlevel = Builtins.regexpsub(target, "/runlevel([0-6]).target$", "\\1")
  if !runlevel.nil?
    ret = Builtins.tointeger(runlevel)
    Builtins.y2milestone("Default runlevel: %1", ret)

    return ret
  end

  # check runlevel specified by a symbolic name
  # (this is written in systemd documentation how to change the default,
  # YaST should also support this style in case users do a manual change)
  runlevel_name = Builtins.regexpsub(target, "/([^/]*).target$", "\\1")
  if !runlevel_name.nil?
    Builtins.y2milestone(
      "Detected default runlevel name: %1",
      runlevel_name
    )
    mapping = {
      "poweroff"   => 0,
      "rescue"     => 1,
      # this is ambiguous, runlevels 2 and 4 also point to multi-user
      # assume runlevel 3 in this case (the most probable)
      "multi-user" => 3,
      "graphical"  => 5,
      "reboot"     => 6
    }

    ret = Ops.get(mapping, runlevel_name)
    Builtins.y2milestone("Default runlevel: %1", ret)

    return ret
  end

  Builtins.y2error("Cannot determine the default runlevel")
  nil
end

#InstalledObject

Check whether the systemd package is installed


46
47
48
49
# File 'library/systemd/src/modules/Systemd.rb', line 46

def Installed
  # check for systemd executable
  Ops.greater_or_equal(SCR.Read(path(".target.size"), @systemd_path), 0)
end

#mainObject


35
36
37
38
39
40
41
42
43
# File 'library/systemd/src/modules/Systemd.rb', line 35

def main
  Yast.import "FileUtils"

  @systemd_path = "/usr/lib/systemd/systemd"
  @default_target_symlink = "/etc/systemd/system/default.target"
  @systemd_targets_dir = "/usr/lib/systemd/system"

  textdomain "base"
end

#RunningObject

Check whether systemd init is currently running

Returns:

  • boolean true if systemd init is running


53
54
55
# File 'library/systemd/src/modules/Systemd.rb', line 53

def Running
  WFM.Read(path(".local.string"), "/proc/1/comm")&.chomp == "systemd"
end

#SetDefaultRunlevel(selected_runlevel) ⇒ Boolean

Set default runlevel for systemd (assumes systemd is installed)

Parameters:

  • runlevel (Integer)

    the default runlevel to set (integer in range 0..6)

Returns:

  • (Boolean)

    true on success


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'library/systemd/src/modules/Systemd.rb', line 60

def SetDefaultRunlevel(selected_runlevel)
  if selected_runlevel.nil? || Ops.less_than(selected_runlevel, 0) ||
      Ops.greater_than(selected_runlevel, 6)
    Builtins.y2error(
      "Invalid default runlevel (must be in range 0..6): %1",
      selected_runlevel
    )
    return false
  end

  Builtins.y2milestone(
    "Setting systemd default runlevel: %1",
    selected_runlevel
  )

  # create symbolic link, -f to rewrite the current link (if exists)
  command = Builtins.sformat(
    "/bin/ln -s -f %1/runlevel%2.target %3",
    @systemd_targets_dir.shellescape,
    selected_runlevel.to_s.shellescape,
    @default_target_symlink.shellescape
  )
  Builtins.y2milestone("Executing: %1", command)

  res = Convert.to_integer(SCR.Execute(path(".target.bash"), command))
  Builtins.y2debug("Result: %1", res)

  ret = res == 0
  Builtins.y2milestone("Default runlevel set: %1", ret)

  ret
end