Class: Bootloader::SerialConsole

Inherits:
Object
  • Object
show all
Defined in:
src/lib/bootloader/serial_console.rb

Overview

Represents parameters for console. Its main intention is easy parsing serial console parameters parameters for grub or kernel and generate it to keep it in sync.

Constant Summary collapse

PARITY_MAP =
{
  "n" => "no",
  "o" => "odd",
  "e" => "even"
}.freeze
SPEED_DEFAULT =
9600
PARITY_DEFAULT =
"no"
WORD_DEFAULT =
""
KERNEL_PARAM_REGEXP =

REGEXP that separate usefull parts of kernel parameter for serial console matching groups are:

  1. serial console device
  2. console unit
  3. speed of serial console ( baud rate )
  4. parity of serial console ( just first letter )
  5. word length for serial console

For details see https://en.wikipedia.org/wiki/Serial_port

Examples:

serial console param ( on kernel cmdline "console=" )

"ttyS0,9600n8"

also partial specification works

"ttyAMA1"
/(ttyS|ttyAMA)([[:digit:]]*),?([[:digit:]]*)([noe]*)([[:digit:]]*)/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unit, speed = SPEED_DEFAULT, parity = PARITY_DEFAULT, word = WORD_DEFAULT) ⇒ SerialConsole

constuctor

Parameters:

  • unit (String)

    serial console unit

  • speed (String) (defaults to: SPEED_DEFAULT)

    speed how console can communicate

  • parity (String) (defaults to: PARITY_DEFAULT)

    if partity can be used. For possible values see grub2 documentation.

  • word (String) (defaults to: WORD_DEFAULT)

    word size. If empty then default is used.

See Also:



87
88
89
90
91
92
93
# File 'src/lib/bootloader/serial_console.rb', line 87

def initialize(unit, speed = SPEED_DEFAULT, parity = PARITY_DEFAULT,
  word = WORD_DEFAULT)
  @unit = unit
  @speed = speed
  @parity = parity
  @word = word
end

Class Method Details

.load_from_console_args(console_args) ⇒ Bootloader::SerialConsole?

Loads serial console configuration from parameters passed to grub

Examples:

console_arg = "serial --speed=38400 --unit=0 --word=8 --parity=no --stop=1"
SerialConsole.load_from_console_args(console_arg)

Parameters:

  • console_args (String)

    string passed to grub as configuration

Returns:



70
71
72
73
74
75
76
77
78
79
# File 'src/lib/bootloader/serial_console.rb', line 70

def self.load_from_console_args(console_args)
  unit = console_args[/--unit=(\S+)/, 1]
  return nil unless unit

  speed = console_args[/--speed=(\S+)/, 1] || SPEED_DEFAULT
  parity = console_args[/--parity=(\S+)/, 1] || PARITY_DEFAULT
  word = console_args[/--word=(\S+)/, 1] || WORD_DEFAULT

  new(unit, speed, parity, word)
end

.load_from_kernel_args(kernel_params) ⇒ Bootloader::SerialConsole?

Loads serial console configuration from parameters passed to kernel

Parameters:

  • kernel_params (ConfigFiles::Grub2::Default::KernelParams)

    to read

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'src/lib/bootloader/serial_console.rb', line 41

def self.load_from_kernel_args(kernel_params)
  console_parameters = kernel_params.parameter("console")
  return nil unless console_parameters

  console_parameters = Array(console_parameters)
  # use only the last parameter (bnc#870514)
  serial_console = console_parameters.last
  return nil if serial_console !~ /ttyS/ && serial_console !~ /ttyAMA/

  unit = serial_console[KERNEL_PARAM_REGEXP, 2]
  return nil if unit.empty?

  speed = serial_console[KERNEL_PARAM_REGEXP, 3]
  speed = SPEED_DEFAULT if speed.empty?
  parity = serial_console[KERNEL_PARAM_REGEXP, 4]
  parity = PARITY_DEFAULT[0] if parity.empty?
  parity = PARITY_MAP[parity]
  word = serial_console[KERNEL_PARAM_REGEXP, 5]

  new(unit, speed, parity, word)
end

Instance Method Details

#console_argsObject

generates serial command for grub2 GRUB_SERIAL_COMMAND



103
104
105
106
107
108
# File 'src/lib/bootloader/serial_console.rb', line 103

def console_args
  res = "serial --unit=#{@unit} --speed=#{@speed} --parity=#{@parity}"
  res += " --word=#{@word}" unless @word.empty?

  res
end

#kernel_argsObject

generates kernel argument usable for passing it with console=<result>



96
97
98
99
100
# File 'src/lib/bootloader/serial_console.rb', line 96

def kernel_args
  serial_console = Yast::Arch.aarch64 ? "ttyAMA" : "ttyS"

  "#{serial_console}#{@unit},#{@speed}#{@parity[0]}#{@word}"
end

#xen_hypervisor_argsObject

generates serial console parameters for grub2 GRUB_CMDLINE_XEN_DEFAULT



119
120
121
122
123
# File 'src/lib/bootloader/serial_console.rb', line 119

def xen_hypervisor_args
  # Notice that this is always com1, even if the host uses another serial tty.
  # See also https://wiki.xenproject.org/wiki/Xen_Serial_Console
  "console=com1 com1=#{@speed}"
end

#xen_kernel_argsObject

generates serial console parameters for grub2 GRUB_CMDLINE_LINUX_XEN_REPLACE_DEFAULT



112
113
114
115
# File 'src/lib/bootloader/serial_console.rb', line 112

def xen_kernel_args
  # This is always hvc0 (for HyperVisor Console).
  "console=hvc0"
end