From 40b4d5ca2e6f59a7ada953c58c66c0d236aa98c2 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Wed, 19 May 2021 13:18:02 +0200 Subject: [PATCH] Start Home Assistant CLI on tty1 without login (#1366) * Start ha-cli on tty1 instead of a getty Instead of starting a getty start the ha-cli directly. This will show the banner right on startup with the important information such as IP address of the instance or the URL to reach it. * Use default shell as root shell instead of HA CLI Instead of using the ha-cli.sh script as login shell use the regular shell. Amongst other things, this allows to run VS Code devcontainers remotely via SSH or using scp. The HA CLI is still available using the `ha` command. --- .../configs/generic_x86_64_defconfig | 1 - .../configs/odroid_c2_defconfig | 1 - .../configs/odroid_c4_defconfig | 1 - .../configs/odroid_n2_defconfig | 1 - .../configs/odroid_xu4_defconfig | 1 - buildroot-external/configs/ova_defconfig | 1 - buildroot-external/configs/rpi0_w_defconfig | 1 - buildroot-external/configs/rpi2_defconfig | 1 - buildroot-external/configs/rpi3_64_defconfig | 1 - buildroot-external/configs/rpi3_defconfig | 1 - buildroot-external/configs/rpi4_64_defconfig | 1 - buildroot-external/configs/rpi4_defconfig | 1 - buildroot-external/configs/rpi_defconfig | 1 - buildroot-external/configs/tinker_defconfig | 1 - .../system/getty@.service.d/hassos.conf | 3 -- .../usr/lib/systemd/system/ha-cli@.service | 41 +++++++++++++++++ .../system/ha-cli@.service.d/haos.conf | 2 + .../rootfs-overlay/usr/sbin/hassos-cli | 44 +++++++------------ buildroot-external/scripts/post-build.sh | 1 - buildroot-external/scripts/rootfs-layer.sh | 11 ----- 20 files changed, 59 insertions(+), 57 deletions(-) delete mode 100644 buildroot-external/rootfs-overlay/etc/systemd/system/getty@.service.d/hassos.conf create mode 100644 buildroot-external/rootfs-overlay/usr/lib/systemd/system/ha-cli@.service create mode 100644 buildroot-external/rootfs-overlay/usr/lib/systemd/system/ha-cli@.service.d/haos.conf diff --git a/buildroot-external/configs/generic_x86_64_defconfig b/buildroot-external/configs/generic_x86_64_defconfig index e84f79bbb..e7ad8758e 100644 --- a/buildroot-external/configs/generic_x86_64_defconfig +++ b/buildroot-external/configs/generic_x86_64_defconfig @@ -14,7 +14,6 @@ BR2_GCC_ENABLE_LTO=y BR2_TARGET_GENERIC_HOSTNAME="homeassistant" BR2_TARGET_GENERIC_ISSUE="Welcome to Home Assistant" BR2_INIT_SYSTEMD=y -BR2_TARGET_GENERIC_GETTY_PORT="tty1" # BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set BR2_ROOTFS_OVERLAY="$(BR2_EXTERNAL_HASSOS_PATH)/rootfs-overlay" BR2_ROOTFS_POST_BUILD_SCRIPT="$(BR2_EXTERNAL_HASSOS_PATH)/scripts/post-build.sh" diff --git a/buildroot-external/configs/odroid_c2_defconfig b/buildroot-external/configs/odroid_c2_defconfig index a36a6820c..6b94de6c0 100644 --- a/buildroot-external/configs/odroid_c2_defconfig +++ b/buildroot-external/configs/odroid_c2_defconfig @@ -14,7 +14,6 @@ BR2_GCC_ENABLE_LTO=y BR2_TARGET_GENERIC_HOSTNAME="homeassistant" BR2_TARGET_GENERIC_ISSUE="Welcome to Home Assistant" BR2_INIT_SYSTEMD=y -BR2_TARGET_GENERIC_GETTY_PORT="tty1" # BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set BR2_ROOTFS_OVERLAY="$(BR2_EXTERNAL_HASSOS_PATH)/rootfs-overlay" BR2_ROOTFS_POST_BUILD_SCRIPT="$(BR2_EXTERNAL_HASSOS_PATH)/scripts/post-build.sh" diff --git a/buildroot-external/configs/odroid_c4_defconfig b/buildroot-external/configs/odroid_c4_defconfig index dd3edaf15..1c6a41cee 100644 --- a/buildroot-external/configs/odroid_c4_defconfig +++ b/buildroot-external/configs/odroid_c4_defconfig @@ -14,7 +14,6 @@ BR2_GCC_ENABLE_LTO=y BR2_TARGET_GENERIC_HOSTNAME="homeassistant" BR2_TARGET_GENERIC_ISSUE="Welcome to Home Assistant" BR2_INIT_SYSTEMD=y -BR2_TARGET_GENERIC_GETTY_PORT="tty1" # BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set BR2_ROOTFS_OVERLAY="$(BR2_EXTERNAL_HASSOS_PATH)/rootfs-overlay" BR2_ROOTFS_POST_BUILD_SCRIPT="$(BR2_EXTERNAL_HASSOS_PATH)/scripts/post-build.sh" diff --git a/buildroot-external/configs/odroid_n2_defconfig b/buildroot-external/configs/odroid_n2_defconfig index c040de5d0..f298a9c4b 100644 --- a/buildroot-external/configs/odroid_n2_defconfig +++ b/buildroot-external/configs/odroid_n2_defconfig @@ -14,7 +14,6 @@ BR2_GCC_ENABLE_LTO=y BR2_TARGET_GENERIC_HOSTNAME="homeassistant" BR2_TARGET_GENERIC_ISSUE="Welcome to Home Assistant" BR2_INIT_SYSTEMD=y -BR2_TARGET_GENERIC_GETTY_PORT="tty1" # BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set BR2_ROOTFS_OVERLAY="$(BR2_EXTERNAL_HASSOS_PATH)/rootfs-overlay" BR2_ROOTFS_POST_BUILD_SCRIPT="$(BR2_EXTERNAL_HASSOS_PATH)/scripts/post-build.sh" diff --git a/buildroot-external/configs/odroid_xu4_defconfig b/buildroot-external/configs/odroid_xu4_defconfig index 6a1c65e5f..c5dc3af30 100644 --- a/buildroot-external/configs/odroid_xu4_defconfig +++ b/buildroot-external/configs/odroid_xu4_defconfig @@ -15,7 +15,6 @@ BR2_GCC_ENABLE_LTO=y BR2_TARGET_GENERIC_HOSTNAME="homeassistant" BR2_TARGET_GENERIC_ISSUE="Welcome to Home Assistant" BR2_INIT_SYSTEMD=y -BR2_TARGET_GENERIC_GETTY_PORT="tty1" # BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set BR2_ROOTFS_OVERLAY="$(BR2_EXTERNAL_HASSOS_PATH)/rootfs-overlay" BR2_ROOTFS_POST_BUILD_SCRIPT="$(BR2_EXTERNAL_HASSOS_PATH)/scripts/post-build.sh" diff --git a/buildroot-external/configs/ova_defconfig b/buildroot-external/configs/ova_defconfig index 2f56e478e..d66a678fa 100644 --- a/buildroot-external/configs/ova_defconfig +++ b/buildroot-external/configs/ova_defconfig @@ -15,7 +15,6 @@ BR2_TARGET_GENERIC_HOSTNAME="homeassistant" BR2_TARGET_GENERIC_ISSUE="Welcome to Home Assistant" BR2_INIT_SYSTEMD=y BR2_TARGET_GENERIC_GETTY=y -BR2_TARGET_GENERIC_GETTY_PORT="tty1" # BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set BR2_ROOTFS_OVERLAY="$(BR2_EXTERNAL_HASSOS_PATH)/rootfs-overlay" BR2_ROOTFS_POST_BUILD_SCRIPT="$(BR2_EXTERNAL_HASSOS_PATH)/scripts/post-build.sh" diff --git a/buildroot-external/configs/rpi0_w_defconfig b/buildroot-external/configs/rpi0_w_defconfig index 16fb5fb83..70d9f7815 100644 --- a/buildroot-external/configs/rpi0_w_defconfig +++ b/buildroot-external/configs/rpi0_w_defconfig @@ -16,7 +16,6 @@ BR2_GCC_ENABLE_LTO=y BR2_TARGET_GENERIC_HOSTNAME="homeassistant" BR2_TARGET_GENERIC_ISSUE="Welcome to Home Assistant" BR2_INIT_SYSTEMD=y -BR2_TARGET_GENERIC_GETTY_PORT="tty1" # BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set BR2_ROOTFS_OVERLAY="$(BR2_EXTERNAL_HASSOS_PATH)/rootfs-overlay $(BR2_EXTERNAL_HASSOS_PATH)/board/raspberrypi/rootfs-overlay" BR2_ROOTFS_POST_BUILD_SCRIPT="$(BR2_EXTERNAL_HASSOS_PATH)/scripts/post-build.sh" diff --git a/buildroot-external/configs/rpi2_defconfig b/buildroot-external/configs/rpi2_defconfig index 7bc7cb14d..19e034767 100644 --- a/buildroot-external/configs/rpi2_defconfig +++ b/buildroot-external/configs/rpi2_defconfig @@ -16,7 +16,6 @@ BR2_GCC_ENABLE_LTO=y BR2_TARGET_GENERIC_HOSTNAME="homeassistant" BR2_TARGET_GENERIC_ISSUE="Welcome to Home Assistant" BR2_INIT_SYSTEMD=y -BR2_TARGET_GENERIC_GETTY_PORT="tty1" # BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set BR2_ROOTFS_OVERLAY="$(BR2_EXTERNAL_HASSOS_PATH)/rootfs-overlay $(BR2_EXTERNAL_HASSOS_PATH)/board/raspberrypi/rootfs-overlay" BR2_ROOTFS_POST_BUILD_SCRIPT="$(BR2_EXTERNAL_HASSOS_PATH)/scripts/post-build.sh" diff --git a/buildroot-external/configs/rpi3_64_defconfig b/buildroot-external/configs/rpi3_64_defconfig index 749a64fc4..6eb173f0c 100644 --- a/buildroot-external/configs/rpi3_64_defconfig +++ b/buildroot-external/configs/rpi3_64_defconfig @@ -16,7 +16,6 @@ BR2_GCC_ENABLE_LTO=y BR2_TARGET_GENERIC_HOSTNAME="homeassistant" BR2_TARGET_GENERIC_ISSUE="Welcome to Home Assistant" BR2_INIT_SYSTEMD=y -BR2_TARGET_GENERIC_GETTY_PORT="tty1" # BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set BR2_ROOTFS_OVERLAY="$(BR2_EXTERNAL_HASSOS_PATH)/rootfs-overlay $(BR2_EXTERNAL_HASSOS_PATH)/board/raspberrypi/rootfs-overlay" BR2_ROOTFS_POST_BUILD_SCRIPT="$(BR2_EXTERNAL_HASSOS_PATH)/scripts/post-build.sh" diff --git a/buildroot-external/configs/rpi3_defconfig b/buildroot-external/configs/rpi3_defconfig index 7016cd0d1..cddfbe4ef 100644 --- a/buildroot-external/configs/rpi3_defconfig +++ b/buildroot-external/configs/rpi3_defconfig @@ -16,7 +16,6 @@ BR2_GCC_ENABLE_LTO=y BR2_TARGET_GENERIC_HOSTNAME="homeassistant" BR2_TARGET_GENERIC_ISSUE="Welcome to Home Assistant" BR2_INIT_SYSTEMD=y -BR2_TARGET_GENERIC_GETTY_PORT="tty1" # BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set BR2_ROOTFS_OVERLAY="$(BR2_EXTERNAL_HASSOS_PATH)/rootfs-overlay $(BR2_EXTERNAL_HASSOS_PATH)/board/raspberrypi/rootfs-overlay" BR2_ROOTFS_POST_BUILD_SCRIPT="$(BR2_EXTERNAL_HASSOS_PATH)/scripts/post-build.sh" diff --git a/buildroot-external/configs/rpi4_64_defconfig b/buildroot-external/configs/rpi4_64_defconfig index ccb8c414a..f42e5844c 100644 --- a/buildroot-external/configs/rpi4_64_defconfig +++ b/buildroot-external/configs/rpi4_64_defconfig @@ -16,7 +16,6 @@ BR2_GCC_ENABLE_LTO=y BR2_TARGET_GENERIC_HOSTNAME="homeassistant" BR2_TARGET_GENERIC_ISSUE="Welcome to Home Assistant" BR2_INIT_SYSTEMD=y -BR2_TARGET_GENERIC_GETTY_PORT="tty1" # BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set BR2_ROOTFS_OVERLAY="$(BR2_EXTERNAL_HASSOS_PATH)/rootfs-overlay $(BR2_EXTERNAL_HASSOS_PATH)/board/raspberrypi/rootfs-overlay" BR2_ROOTFS_POST_BUILD_SCRIPT="$(BR2_EXTERNAL_HASSOS_PATH)/scripts/post-build.sh" diff --git a/buildroot-external/configs/rpi4_defconfig b/buildroot-external/configs/rpi4_defconfig index 4b7982083..4f34deeb9 100644 --- a/buildroot-external/configs/rpi4_defconfig +++ b/buildroot-external/configs/rpi4_defconfig @@ -16,7 +16,6 @@ BR2_GCC_ENABLE_LTO=y BR2_TARGET_GENERIC_HOSTNAME="homeassistant" BR2_TARGET_GENERIC_ISSUE="Welcome to Home Assistant" BR2_INIT_SYSTEMD=y -BR2_TARGET_GENERIC_GETTY_PORT="tty1" # BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set BR2_ROOTFS_OVERLAY="$(BR2_EXTERNAL_HASSOS_PATH)/rootfs-overlay $(BR2_EXTERNAL_HASSOS_PATH)/board/raspberrypi/rootfs-overlay" BR2_ROOTFS_POST_BUILD_SCRIPT="$(BR2_EXTERNAL_HASSOS_PATH)/scripts/post-build.sh" diff --git a/buildroot-external/configs/rpi_defconfig b/buildroot-external/configs/rpi_defconfig index bc90ea7d6..a8bd075d1 100644 --- a/buildroot-external/configs/rpi_defconfig +++ b/buildroot-external/configs/rpi_defconfig @@ -16,7 +16,6 @@ BR2_GCC_ENABLE_LTO=y BR2_TARGET_GENERIC_HOSTNAME="homeassistant" BR2_TARGET_GENERIC_ISSUE="Welcome to Home Assistant" BR2_INIT_SYSTEMD=y -BR2_TARGET_GENERIC_GETTY_PORT="tty1" # BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set BR2_ROOTFS_OVERLAY="$(BR2_EXTERNAL_HASSOS_PATH)/rootfs-overlay $(BR2_EXTERNAL_HASSOS_PATH)/board/raspberrypi/rootfs-overlay" BR2_ROOTFS_POST_BUILD_SCRIPT="$(BR2_EXTERNAL_HASSOS_PATH)/scripts/post-build.sh" diff --git a/buildroot-external/configs/tinker_defconfig b/buildroot-external/configs/tinker_defconfig index be7270df0..a9ad8e242 100644 --- a/buildroot-external/configs/tinker_defconfig +++ b/buildroot-external/configs/tinker_defconfig @@ -16,7 +16,6 @@ BR2_GCC_ENABLE_LTO=y BR2_TARGET_GENERIC_HOSTNAME="homeassistant" BR2_TARGET_GENERIC_ISSUE="Welcome to Home Assistant" BR2_INIT_SYSTEMD=y -BR2_TARGET_GENERIC_GETTY_PORT="tty1" # BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set BR2_ROOTFS_OVERLAY="$(BR2_EXTERNAL_HASSOS_PATH)/rootfs-overlay" BR2_ROOTFS_POST_BUILD_SCRIPT="$(BR2_EXTERNAL_HASSOS_PATH)/scripts/post-build.sh" diff --git a/buildroot-external/rootfs-overlay/etc/systemd/system/getty@.service.d/hassos.conf b/buildroot-external/rootfs-overlay/etc/systemd/system/getty@.service.d/hassos.conf deleted file mode 100644 index 2217bfed9..000000000 --- a/buildroot-external/rootfs-overlay/etc/systemd/system/getty@.service.d/hassos.conf +++ /dev/null @@ -1,3 +0,0 @@ -[Unit] -Wants=hassos-supervisor.service -After=hassos-supervisor.service diff --git a/buildroot-external/rootfs-overlay/usr/lib/systemd/system/ha-cli@.service b/buildroot-external/rootfs-overlay/usr/lib/systemd/system/ha-cli@.service new file mode 100644 index 000000000..f5ada5e72 --- /dev/null +++ b/buildroot-external/rootfs-overlay/usr/lib/systemd/system/ha-cli@.service @@ -0,0 +1,41 @@ +[Unit] +Description=Home Assistant CLI +Wants=hassos-supervisor.service +After=systemd-user-sessions.service plymouth-quit-wait.service getty-pre.target hassos-supervisor.service +Conflicts=getty@%i.service + +# If additional gettys are spawned during boot then we should make +# sure that this is synchronized before getty.target, even though +# getty.target didn't actually pull it in. +Before=getty.target +IgnoreOnIsolate=yes + +# IgnoreOnIsolate causes issues with sulogin, if someone isolates +# rescue.target or starts rescue.service from multi-user.target or +# graphical.target. +Conflicts=rescue.service +Before=rescue.service + +[Service] +# the VT is cleared by TTYVTDisallocate +# The '-o' option value tells agetty to replace 'login' arguments with an +# option to preserve environment (-p), followed by '--' for safety, and then +# the entered username. +ExecStart=/usr/sbin/hassos-cli +RemainAfterExit=yes +Type=idle +Restart=always +RestartSec=0 +UtmpIdentifier=%I +TTYPath=/dev/%I +TTYReset=yes +TTYVHangup=yes +TTYVTDisallocate=yes +IgnoreSIGPIPE=no +SendSIGHUP=yes +StandardInput=tty-force +StandardOutput=inherit +StandardError=inherit + +[Install] +WantedBy=multi-user.target diff --git a/buildroot-external/rootfs-overlay/usr/lib/systemd/system/ha-cli@.service.d/haos.conf b/buildroot-external/rootfs-overlay/usr/lib/systemd/system/ha-cli@.service.d/haos.conf new file mode 100644 index 000000000..7918f33a5 --- /dev/null +++ b/buildroot-external/rootfs-overlay/usr/lib/systemd/system/ha-cli@.service.d/haos.conf @@ -0,0 +1,2 @@ +[Install] +DefaultInstance=tty1 diff --git a/buildroot-external/rootfs-overlay/usr/sbin/hassos-cli b/buildroot-external/rootfs-overlay/usr/sbin/hassos-cli index 3aa662ed5..b682e2cb4 100755 --- a/buildroot-external/rootfs-overlay/usr/sbin/hassos-cli +++ b/buildroot-external/rootfs-overlay/usr/sbin/hassos-cli @@ -3,35 +3,23 @@ # Run logging cli # ============================================================================== -# Setup terminal size on serial console -if [ "${TERM}" = "vt220" ] || [ "${TERM}" = "vt102" ] || \ - [ "${TERM}" = "vt100" ]; then - resize -fi +echo "Waiting for the Home Assistant CLI to be ready..." -if [ "$(systemctl is-active default.target)" != "active" ]; then - echo "[INFO] System not in default.target! Jump into emergency console..." - exec /bin/ash -l -fi - -if [ ! "$(findmnt /mnt/data)" ]; then - echo "[WARN] Data partition not mounted! Jump into emergency console..." - exec /bin/ash -l -fi - -# Run CLI container -if [ "$(docker ps -q -f name=hassio_cli)" ]; then - docker container exec \ - -ti hassio_cli \ - /usr/bin/cli.sh \ - - # Jump to root login shell - if [ $? -eq 10 ]; then - /bin/ash -l +i=0 +while [ ! "$(docker ps -q -f name=hassio_cli)" ]; do + sleep 1 + i=$((i+1)) + if [ $i = 60 ]; then + echo "[WARN] Home Assistant CLI not starting! Jump into emergency console..." + exec /bin/ash -l fi -else - echo "[WARN] Home Assistant CLI is not running! Jump into emergency console..." +done + +docker container exec \ + -ti hassio_cli \ + /usr/bin/cli.sh + +# Jump to root login shell +if [ $? -eq 10 ]; then exec /bin/ash -l fi - -exit diff --git a/buildroot-external/scripts/post-build.sh b/buildroot-external/scripts/post-build.sh index f36286dd5..2ad3de6a0 100755 --- a/buildroot-external/scripts/post-build.sh +++ b/buildroot-external/scripts/post-build.sh @@ -16,7 +16,6 @@ BOARD_DIR=${2} # HassOS tasks fix_rootfs install_tini_docker -install_hassos_cli # Write os-release # shellcheck disable=SC2153 diff --git a/buildroot-external/scripts/rootfs-layer.sh b/buildroot-external/scripts/rootfs-layer.sh index 76e05969c..8f913acc2 100755 --- a/buildroot-external/scripts/rootfs-layer.sh +++ b/buildroot-external/scripts/rootfs-layer.sh @@ -37,17 +37,6 @@ function fix_rootfs() { } -function install_hassos_cli() { - - # shellcheck disable=SC1117 - sed -i "s|\(root:.*\)/bin/sh|\1/usr/sbin/hassos-cli|" "${TARGET_DIR}/etc/passwd" - - if ! grep "hassos-cli" "${TARGET_DIR}/etc/shells"; then - echo "/usr/sbin/hassos-cli" >> "${TARGET_DIR}/etc/shells" - fi -} - - function install_tini_docker() { ln -fs /usr/bin/tini "${TARGET_DIR}/usr/bin/docker-init" }