From 1538ec33f7c34dfae0c47f514fab98b2aa7de8de Mon Sep 17 00:00:00 2001 From: Oleg Kalachev Date: Sun, 4 Aug 2019 00:40:27 +0300 Subject: [PATCH] Rework assigning hostname and service files (#150) * Rework assigning hostname and service files * docs: small fix in hostname article * Correct path to setup.bash in clever.service * docs: correct ip in hostname article * init_rpi: put normal and .local hostname on one line in hosts * docs: add English version of hostname article * clever.service: use sh instead of bash * docs: Spellcheck english version, add note about hostname vs SSID * clever.service: return roscore requirement back --- builder/assets/clever.service | 6 +++--- builder/assets/clever_rename | 35 ---------------------------------- builder/assets/init_rpi.sh | 6 +++++- builder/assets/roscore.env | 10 ---------- builder/assets/roscore.service | 3 +-- builder/image-build.sh | 2 -- docs/en/SUMMARY.md | 1 + docs/en/hostname.md | 31 ++++++++++++++++++++++++++++++ docs/ru/SUMMARY.md | 1 + docs/ru/hostname.md | 31 ++++++++++++++++++++++++++++++ 10 files changed, 73 insertions(+), 53 deletions(-) delete mode 100755 builder/assets/clever_rename delete mode 100644 builder/assets/roscore.env create mode 100644 docs/en/hostname.md create mode 100644 docs/ru/hostname.md diff --git a/builder/assets/clever.service b/builder/assets/clever.service index 84bd7987..b432e96b 100644 --- a/builder/assets/clever.service +++ b/builder/assets/clever.service @@ -1,12 +1,12 @@ [Unit] Description=Clever ROS package Requires=roscore.service -After=roscore.service +After=network.target [Service] User=pi -EnvironmentFile=/lib/systemd/system/roscore.env -ExecStart=/opt/ros/kinetic/bin/roslaunch clever clever.launch --wait --screen +ExecStart=/bin/sh -c ". /home/pi/catkin_ws/devel/setup.sh; \ + ROS_HOSTNAME=`hostname`.local exec roslaunch clever clever.launch --wait --screen --skip-log-check" Restart=on-failure RestartSec=3 diff --git a/builder/assets/clever_rename b/builder/assets/clever_rename deleted file mode 100755 index 8312af36..00000000 --- a/builder/assets/clever_rename +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env bash - -# Set Clever hostname to the specified value - -set -e - -NEW_NAME_OPT=$1 - -if [[ -z ${NEW_NAME_OPT} ]]; then - echo "Please specify new name for this Clever" - exit 1 -fi - -NEW_NAME=$(echo ${NEW_NAME_OPT} | tr '[:upper:]' '[:lower:]') - -echo "Setting name to ${NEW_NAME}" - -echo "Backing up /etc/hostname" -cp /etc/hostname /etc/hostname.bak -echo "Writing new /etc/hostname" -echo ${NEW_NAME} > /etc/hostname - -echo "Backing up /etc/hosts" -cp /etc/hosts /etc/hosts.bak -echo "Rewriting /etc/hosts with new values" -sed -i 's/127\.0\.1\.1.*/127.0.1.1\t'${NEW_NAME}'/g' /etc/hosts - -echo "Changing hostname in /lib/systemd/system/roscore.env" -sed -i 's/ROS_HOSTNAME=.*/ROS_HOSTNAME='${NEW_NAME}'.local/g' /lib/systemd/system/roscore.env - -echo "Changing hostname in .bashrc" -sed -i 's/export ROS_HOSTNAME=.*/export ROS_HOSTNAME='${NEW_NAME}'.local/g' /home/pi/.bashrc - -echo "Done, reboot your Clever to see the results" - diff --git a/builder/assets/init_rpi.sh b/builder/assets/init_rpi.sh index f393ef9e..d900b60c 100755 --- a/builder/assets/init_rpi.sh +++ b/builder/assets/init_rpi.sh @@ -38,7 +38,11 @@ echo_stamp() { echo_stamp "Rename SSID" NEW_SSID='CLEVER-'$(head -c 100 /dev/urandom | xxd -ps -c 100 | sed -e "s/[^0-9]//g" | cut -c 1-4) sudo sed -i.OLD "s/CLEVER/${NEW_SSID}/" /etc/wpa_supplicant/wpa_supplicant.conf -clever_rename ${NEW_SSID} + +echo_stamp "Rename hostname to $NEW_SSID" +hostnamectl set-hostname $NEW_SSID +sed -i 's/127\.0\.1\.1.*/127.0.1.1\t'${NEW_SSID}' '${NEW_SSID}'.local/g' /etc/hosts +# .local (mdns) hostname added to make it accesable when wlan and ethernet interfaces down echo_stamp "Harware setup" /root/hardware_setup.sh diff --git a/builder/assets/roscore.env b/builder/assets/roscore.env deleted file mode 100644 index 28387bf1..00000000 --- a/builder/assets/roscore.env +++ /dev/null @@ -1,10 +0,0 @@ -ROS_ROOT=/opt/ros/kinetic/share/ros -ROS_DISTRO=kinetic -ROS_PACKAGE_PATH=/home/pi/catkin_ws/src:/opt/ros/kinetic/share -ROS_PORT=11311 -ROS_MASTER_URI=http://localhost:11311 -CMAKE_PREFIX_PATH=/home/pi/catkin_ws/devel:/opt/ros/kinetic -PATH=/opt/ros/kinetic/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin -LD_LIBRARY_PATH=/opt/ros/kinetic/lib -PYTHONPATH=/home/pi/catkin_ws/devel/lib/python2.7/dist-packages:/opt/ros/kinetic/lib/python2.7/dist-packages -ROS_HOSTNAME=raspberrypi.local diff --git a/builder/assets/roscore.service b/builder/assets/roscore.service index 44575cac..ccbc92e0 100644 --- a/builder/assets/roscore.service +++ b/builder/assets/roscore.service @@ -4,8 +4,7 @@ After=network.target [Service] User=pi -EnvironmentFile=/lib/systemd/system/roscore.env -ExecStart=/opt/ros/kinetic/bin/roscore +ExecStart=/bin/sh -c ". /opt/ros/kinetic/setup.sh; ROS_HOSTNAME=`hostname`.local exec roscore" Restart=on-failure RestartSec=3 diff --git a/builder/image-build.sh b/builder/image-build.sh index 427d1e51..4d1c84e9 100755 --- a/builder/image-build.sh +++ b/builder/image-build.sh @@ -109,7 +109,6 @@ ${BUILDER_DIR}/image-chroot.sh ${IMAGE_PATH} exec ${SCRIPTS_DIR}'/image-network. [[ $(arch) == 'armv7l' ]] && NUMBER_THREADS=1 || NUMBER_THREADS=$(nproc --all) # Clever ${BUILDER_DIR}/image-chroot.sh ${IMAGE_PATH} copy ${SCRIPTS_DIR}'/assets/clever.service' '/lib/systemd/system/' -${BUILDER_DIR}/image-chroot.sh ${IMAGE_PATH} copy ${SCRIPTS_DIR}'/assets/roscore.env' '/lib/systemd/system/' ${BUILDER_DIR}/image-chroot.sh ${IMAGE_PATH} copy ${SCRIPTS_DIR}'/assets/roscore.service' '/lib/systemd/system/' ${BUILDER_DIR}/image-chroot.sh ${IMAGE_PATH} copy ${SCRIPTS_DIR}'/assets/kinetic-rosdep-clever.yaml' '/etc/ros/rosdep/' ${BUILDER_DIR}/image-chroot.sh ${IMAGE_PATH} copy ${SCRIPTS_DIR}'/assets/ros_python_paths' '/etc/sudoers.d/' @@ -118,7 +117,6 @@ ${BUILDER_DIR}/image-chroot.sh ${IMAGE_PATH} copy ${SCRIPTS_DIR}'/assets/pigpiod # Add PX4 udev rules ${BUILDER_DIR}/image-chroot.sh ${IMAGE_PATH} copy ${REPO_DIR}'/clever/config/99-px4fmu.rules' '/lib/udev/rules.d/' # Add rename script -${BUILDER_DIR}/image-chroot.sh ${IMAGE_PATH} copy ${SCRIPTS_DIR}'/assets/clever_rename' '/usr/local/bin/clever_rename' ${BUILDER_DIR}/image-chroot.sh ${IMAGE_PATH} exec ${SCRIPTS_DIR}'/image-ros.sh' ${REPO_URL} ${IMAGE_VERSION} false false ${NUMBER_THREADS} ${BUILDER_DIR}/image-chroot.sh ${IMAGE_PATH} exec ${SCRIPTS_DIR}'/image-validate.sh' diff --git a/docs/en/SUMMARY.md b/docs/en/SUMMARY.md index 1927eeb0..bf8cb2a7 100644 --- a/docs/en/SUMMARY.md +++ b/docs/en/SUMMARY.md @@ -50,6 +50,7 @@ * [Interfacing with a laser rangefinder](laser.md) * [PX4 Simulation](sitl.md) * [Software autorun](autolaunch.md) + * [Hostname](hostname.md) * [Controlling the copter from Arduino](arduino.md) * [Using an external 3G modem](3g.md) * Clever-based projects diff --git a/docs/en/hostname.md b/docs/en/hostname.md new file mode 100644 index 00000000..d8b32e0c --- /dev/null +++ b/docs/en/hostname.md @@ -0,0 +1,31 @@ +# Hostname + +[By default](microsd_images.md) the hostname of the Clever drone is set to `clever-xxxx`, where `xxxx` are random numbers. These numbers are the same as in the [Wi-Fi SSID](wifi.md). + +Thus, Clever is accessible on machines that support mDNS as `clever-xxxx.local`. You can use this name to access Clever over SSH: + +```bash +ssh pi@clever-xxxx.local +``` + +Also, this name can be used in place of IP-address to open Clever web pages in browser, accessing ROS master, etc. + +## Changing hostname + +In some situations it is necessary to change Clever's hostname. You can use the `hostnamectl` utility for that: + +```bash +sudo hostnamectl set-hostname newname +``` + +Where `newname` is the new name of the machine. `hostnamectl` utility will change the name in `/etc/hostname` file. + +You should also put the new name to `/etc/hosts` file: + +```txt +127.0.1.1 newname newname.local +``` + +Setting `newname.local` is necessary to allow ROS to resolve this name in situations where all the network interfaces are down (when Wi-Fi is turned off or disconnected). + +> **Note** Changing the hostname does not affect the Wi-Fi SSID (and vice versa, changing the Wi-Fi SSID won't affect the hostname). diff --git a/docs/ru/SUMMARY.md b/docs/ru/SUMMARY.md index b43cdb5b..a44d4b5a 100644 --- a/docs/ru/SUMMARY.md +++ b/docs/ru/SUMMARY.md @@ -54,6 +54,7 @@ * [Работа с SITL](sitl.md) * [Контейнер с симулятором](sitl_docker.md) * [Автозапуск ПО](autolaunch.md) + * [Имя хоста](hostname.md) * [Взаимодействие с Arduino](arduino.md) * [3G-модем](3g.md) * [Установка ROS Kinetic](ros-install.md) diff --git a/docs/ru/hostname.md b/docs/ru/hostname.md new file mode 100644 index 00000000..11cb3511 --- /dev/null +++ b/docs/ru/hostname.md @@ -0,0 +1,31 @@ +# Имя хоста + +[По умолчанию](microsd_images.md) на Клевере установлено имя хоста (hostname) `clever-xxxx`, где `xxxx` – случайные цифры. Имя хоста соответствует SSID [точки доступа Wi-Fi](wifi.md). + +Таким образом, Клевер доступен на машинах, поддерживающих mDNS, под именем `clever-xxxx.local`. Вы можете использовать это имя для SSH-доступа на Клевер: + +```bash +ssh pi@clever-xxxx.local +``` + +Также это имя может быть использовано вместо IP-адреса для открытия страницы Клевера в браузере и т. д. + +## Изменение имени хоста + +В некоторых ситуациях необходимо изменение имени хоста Клевера. Для это используйте утилиту `hostnamectl`: + +```bash +sudo hostnamectl set-hostname newname +``` + +Где `newname` – новое имя машины. Утилита `hostnamectl` поменяет имя в файле `/etc/hostname`. + +Также необходимо прописывание нового имени в файл `/etc/hosts`: + +```txt +127.0.1.1 newname newname.local +``` + +Прописывание `newname.local` необходимо, чтобы ROS смог разрешить это имя в ситуациях, когда все сетевые интерфейсы неактивны (отключение/разрыв связи Wi-Fi). + +> **Note** Изменение имени хоста не повлечёт за собой изменений SSID точки доступа Wi-Fi (и наоборот, изменение SSID точки доступа не поменяет имя хоста).