Skip to content

Commit

Permalink
Added Raspberry Pi OS support
Browse files Browse the repository at this point in the history
This commit adds support for the Raspberry Pi OS debian distribution.
It includes a distro definition, 3 modules and integration into other
systems like config generation.

Signed-off-by: paulober <44974737+paulober@users.noreply.github.com>
  • Loading branch information
paulober committed Oct 17, 2024
1 parent 9554338 commit 683ffc8
Show file tree
Hide file tree
Showing 42 changed files with 1,108 additions and 25 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
build
cloud_init.egg-info
dist
*.deb
*.tar.xz
*.tar.gz
*.build
*.dsc
*.changes
*.buildinfo
*.pyc
__pycache__
.tox
Expand Down Expand Up @@ -37,3 +44,4 @@ cloud-init_*.upload

# user test settings
tests/integration_tests/user_settings.py

2 changes: 1 addition & 1 deletion cloudinit/config/cc_apt_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

meta: MetaSchema = {
"id": "cc_apt_configure",
"distros": ["ubuntu", "debian"],
"distros": ["ubuntu", "debian", "raspberry-pi-os"],
"frequency": PER_INSTANCE,
"activate_by_schema_keys": [],
}
Expand Down
2 changes: 1 addition & 1 deletion cloudinit/config/cc_apt_pipelining.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

meta: MetaSchema = {
"id": "cc_apt_pipelining",
"distros": ["ubuntu", "debian"],
"distros": ["ubuntu", "debian", "raspberry-pi-os"],
"frequency": PER_INSTANCE,
"activate_by_schema_keys": ["apt_pipelining"],
}
Expand Down
2 changes: 1 addition & 1 deletion cloudinit/config/cc_byobu.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

meta: MetaSchema = {
"id": "cc_byobu",
"distros": ["ubuntu", "debian"],
"distros": ["ubuntu", "debian", "raspberry-pi-os"],
"frequency": PER_INSTANCE,
"activate_by_schema_keys": [],
}
Expand Down
11 changes: 9 additions & 2 deletions cloudinit/config/cc_ca_certs.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"alpine",
"debian",
"fedora",
"raspberry-pi-os",
"rhel",
"opensuse",
"opensuse-microos",
Expand Down Expand Up @@ -157,10 +158,16 @@ def disable_default_ca_certs(distro_name, distro_cfg):
"""
if distro_name in ["rhel", "photon"]:
remove_default_ca_certs(distro_cfg)
elif distro_name in ["alpine", "aosc", "debian", "ubuntu"]:
elif distro_name in [
"alpine",
"aosc",
"debian",
"raspberry-pi-os",
"ubuntu",
]:
disable_system_ca_certs(distro_cfg)

if distro_name in ["debian", "ubuntu"]:
if distro_name in ["debian", "raspberry-pi-os", "ubuntu"]:
debconf_sel = (
"ca-certificates ca-certificates/trust_new_crts " + "select no"
)
Expand Down
6 changes: 6 additions & 0 deletions cloudinit/config/cc_ntp.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"opensuse-tumbleweed",
"opensuse-leap",
"photon",
"raspberry-pi-os",
"rhel",
"rocky",
"sle_hpc",
Expand Down Expand Up @@ -211,6 +212,11 @@
"confpath": "/etc/systemd/timesyncd.conf",
},
},
"raspberry-pi-os": {
"chrony": {
"confpath": "/etc/chrony/chrony.conf",
},
},
"rhel": {
"ntp": {
"service_name": "ntpd",
Expand Down
47 changes: 47 additions & 0 deletions cloudinit/config/cc_rpi_connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright (C) 2024, Raspberry Pi Ltd.
#
# Author: Paul Oberosler <paul.oberosler@raspberrypi.com>
#
# This file is part of cloud-init. See LICENSE file for license information.

from cloudinit import subp
from cloudinit.cloud import Cloud
from cloudinit.config import Config
from cloudinit.config.schema import MetaSchema
from cloudinit.settings import PER_INSTANCE
import logging


LOG = logging.getLogger(__name__)
ENABLE_RPI_CONNECT_KEY = "enable_rpi_connect"

meta: MetaSchema = {
"id": "cc_rpi_connect",
"distros": ["raspberry-pi-os"],
"frequency": PER_INSTANCE,
"activate_by_schema_keys": [ENABLE_RPI_CONNECT_KEY],
}


def configure_rpi_connect(enable: bool) -> None:
LOG.debug(f"Configuring rpi-connect: {enable}")

num = 0 if enable else 1

try:
subp.subp(["/usr/bin/raspi-config", "do_rpi_connect", str(num)])
except subp.ProcessExecutionError as e:
LOG.error("Failed to configure rpi-connect: %s", e)


def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None:
if ENABLE_RPI_CONNECT_KEY in cfg:
# expect it to be a dictionary
enable = cfg[ENABLE_RPI_CONNECT_KEY]

if isinstance(enable, bool):
configure_rpi_connect(enable)
else:
LOG.warning(
"Invalid value for %s: %s", ENABLE_RPI_CONNECT_KEY, enable
)
180 changes: 180 additions & 0 deletions cloudinit/config/cc_rpi_interfaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
# Copyright (C) 2024, Raspberry Pi Ltd.
#
# Author: Paul Oberosler <paul.oberosler@raspberrypi.com>
#
# This file is part of cloud-init. See LICENSE file for license information.

from cloudinit import subp
from cloudinit.cloud import Cloud
from cloudinit.config import Config
from cloudinit.config.schema import MetaSchema
from cloudinit.settings import PER_INSTANCE
import logging


LOG = logging.getLogger(__name__)
RPI_INTERFACES_KEY = "rpi_interfaces"
SUPPORTED_INTERFACES = {
"spi": "do_spi",
"i2c": "do_i2c",
"serial": "do_serial",
"onewire": "do_onewire",
"remote_gpio": "do_rgpio",
"ssh": "enable_ssh",
}
RASPI_CONFIG_SERIAL_CONS_FN = "do_serial_cons"
RASPI_CONFIG_SERIAL_HW_FN = "do_serial_hw"

meta: MetaSchema = {
"id": "cc_rpi_interfaces",
"distros": ["raspberry-pi-os"],
"frequency": PER_INSTANCE,
"activate_by_schema_keys": [RPI_INTERFACES_KEY],
}


# TODO: test
def require_reboot(cfg: Config) -> None:
cfg["power_state"] = cfg.get("power_state", {})
cfg["power_state"]["mode"] = cfg["power_state"].get("mode", "reboot")
cfg["power_state"]["condition"] = True


def is_pifive() -> bool:
try:
subp.subp(["/usr/bin/raspi-config", "nonint", "is_pifive"])
return True
except subp.ProcessExecutionError:
return False


def configure_serial_interface(cfg: dict | bool, instCfg: Config) -> None:
enable_console = False
enable_hw = False

if isinstance(cfg, dict):
enable_console = cfg.get("console", False)
enable_hw = cfg.get("hardware", False)
elif isinstance(cfg, bool):
# default to enabling console as if < pi5
# this will also enable the hardware
enable_console = cfg

if not is_pifive() and enable_console:
# only pi5 has 2 usable UARTs
# on other models, enabling the console
# will also block the other UART
enable_hw = True

try:
subp.subp(
[
"/usr/bin/raspi-config",
"nonint",
RASPI_CONFIG_SERIAL_CONS_FN,
str(0 if enable_console else 1),
]
)

try:
subp.subp(
[
"/usr/bin/raspi-config",
"nonint",
RASPI_CONFIG_SERIAL_HW_FN,
str(0 if enable_hw else 1),
]
)
except subp.ProcessExecutionError as e:
LOG.error("Failed to configure serial hardware: %s", e)

require_reboot(instCfg)
except subp.ProcessExecutionError as e:
LOG.error("Failed to configure serial console: %s", e)


def enable_ssh(cfg: Config, enable: bool) -> None:
if not enable:
return

try:
subp.subp(
[
"/usr/lib/raspberry-pi-sys-mods/imager_custom",
SUPPORTED_INTERFACES["ssh"],
]
)
require_reboot(cfg)
except subp.ProcessExecutionError as e:
LOG.error("Failed to enable ssh: %s", e)


def configure_interface(iface: str, enable: bool) -> None:
assert (
iface in SUPPORTED_INTERFACES.keys() and iface != "serial"
), f"Unsupported interface: {iface}"

try:
subp.subp(
[
"/usr/bin/raspi-config",
"nonint",
SUPPORTED_INTERFACES[iface],
str(0 if enable else 1),
]
)
except subp.ProcessExecutionError as e:
LOG.error("Failed to configure %s: %s", iface, e)


def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None:
if RPI_INTERFACES_KEY not in cfg:
return
elif not isinstance(cfg[RPI_INTERFACES_KEY], dict):
LOG.warning(
"Invalid value for %s: %s",
RPI_INTERFACES_KEY,
cfg[RPI_INTERFACES_KEY],
)
return
elif not cfg[RPI_INTERFACES_KEY]:
LOG.debug("Empty value for %s. Skipping...", RPI_INTERFACES_KEY)
return

# check for supported ARM interfaces
for key in cfg[RPI_INTERFACES_KEY]:
if key not in SUPPORTED_INTERFACES.keys():
LOG.warning("Invalid key for %s: %s", RPI_INTERFACES_KEY, key)
continue

enable = cfg[RPI_INTERFACES_KEY][key]

if key == "serial":
if not isinstance(enable, dict) and not isinstance(enable, bool):
LOG.warning(
"Invalid value for %s.%s: %s",
RPI_INTERFACES_KEY,
key,
enable,
)
else:
configure_serial_interface(enable, cfg)
continue
elif key == "ssh":
if not isinstance(enable, bool):
LOG.warning(
"Invalid value for %s.%s: %s",
RPI_INTERFACES_KEY,
key,
enable,
)
else:
enable_ssh(cfg, enable)
continue

if isinstance(enable, bool):
configure_interface(key, enable)
else:
LOG.warning(
"Invalid value for %s.%s: %s", RPI_INTERFACES_KEY, key, enable
)
Loading

0 comments on commit 683ffc8

Please sign in to comment.