From 435e0856904ab591c9e97f60759497f25aede884 Mon Sep 17 00:00:00 2001 From: Hector Palacios Date: Thu, 12 Sep 2019 16:37:30 +0200 Subject: [PATCH] udev-extraconf: update mount scripts to support systemd The current scripts used the standard mount binary at /bin/mount for mounting the partitions. Systemd however seems to have a monitor that eventually umounts such partitions if not mounted by systemd's specific command 'systemd-mount`. For platforms using systemd, we need to use this binary together with parameter --no-block so that partitions are mounted correctly during the boot process. This commit merges the two scripts for Digi-handled partitions into just one: mount_bootparts.sh \ +--> mount_digiparts.sh / mount_partition.sh The merged script: - checks if running with systemd, to use its binary. - checks if running with busybox, to not use the unsupported '-o' attribute. - checks if mounting the 'linux' partition, to mount it read-only. - checks for already-attached UBI devices, to avoid re-attaching. This commit also combines the Digi-handled partitions (linux, update) into the same udev rule, to simplify it, and breaks the rules lines for readability. Signed-off-by: Hector Palacios https://jira.digi.com/browse/DEL-6744 --- .../udev/udev-extraconf/automount.rules | 14 ++- .../udev/udev-extraconf/mount_bootparts.sh | 27 ------ .../udev/udev-extraconf/mount_digiparts.sh | 92 +++++++++++++++++++ .../udev/udev-extraconf/mount_partition.sh | 46 ---------- .../udev/udev-extraconf_1.1.bbappend | 8 +- 5 files changed, 102 insertions(+), 85 deletions(-) delete mode 100644 meta-digi-arm/recipes-core/udev/udev-extraconf/mount_bootparts.sh create mode 100644 meta-digi-arm/recipes-core/udev/udev-extraconf/mount_digiparts.sh delete mode 100644 meta-digi-arm/recipes-core/udev/udev-extraconf/mount_partition.sh diff --git a/meta-digi-arm/recipes-core/udev/udev-extraconf/automount.rules b/meta-digi-arm/recipes-core/udev/udev-extraconf/automount.rules index 388c485f8..22534848d 100644 --- a/meta-digi-arm/recipes-core/udev/udev-extraconf/automount.rules +++ b/meta-digi-arm/recipes-core/udev/udev-extraconf/automount.rules @@ -13,19 +13,17 @@ # %% the '%' char itself # -# Boot partitions -SUBSYSTEM=="block", ENV{ID_PART_ENTRY_NAME}=="linux*", ACTION=="add", RUN+="/etc/udev/scripts/mount_bootparts.sh", GOTO="automount_rules_end" +# Digi-mounted partitions: linux, update +SUBSYSTEM=="block", ENV{ID_PART_ENTRY_NAME}=="linux*|update*", ACTION=="add", RUN+="/etc/udev/scripts/mount_digiparts.sh", GOTO="automount_rules_end" +SUBSYSTEM=="mtd", ATTRS{name}=="linux*|update*", ACTION=="add", RUN+="/etc/udev/scripts/mount_digiparts.sh", GOTO="automount_rules_end" -# Update partition -SUBSYSTEM=="block", ENV{ID_PART_ENTRY_NAME}=="update*", ACTION=="add", RUN+="/etc/udev/scripts/mount_partition.sh 'update'", GOTO="automount_rules_end" -SUBSYSTEM=="mtd", ATTRS{name}=="update", ACTION=="add", RUN+="/etc/udev/scripts/mount_partition.sh 'update'", GOTO="automount_rules_end" - -# Recovery partition +# Avoid mounting recovery partition SUBSYSTEM=="block", ENV{ID_PART_ENTRY_NAME}=="recovery*", ACTION=="add", GOTO="automount_rules_end" -SUBSYSTEM=="mtd", ATTRS{name}=="recovery", ACTION=="add", GOTO="automount_rules_end" +SUBSYSTEM=="mtd", ATTRS{name}=="recovery*", ACTION=="add", GOTO="automount_rules_end" # Media automounting SUBSYSTEM=="block", ACTION=="add" RUN+="/etc/udev/scripts/mount.sh" SUBSYSTEM=="block", ACTION=="remove" RUN+="/etc/udev/scripts/mount.sh" SUBSYSTEM=="block", ACTION=="change", ENV{DISK_MEDIA_CHANGE}=="1" RUN+="/etc/udev/scripts/mount.sh" + LABEL="automount_rules_end" diff --git a/meta-digi-arm/recipes-core/udev/udev-extraconf/mount_bootparts.sh b/meta-digi-arm/recipes-core/udev/udev-extraconf/mount_bootparts.sh deleted file mode 100644 index 1e5982d93..000000000 --- a/meta-digi-arm/recipes-core/udev/udev-extraconf/mount_bootparts.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/sh -#=============================================================================== -# -# mount_bootparts.sh -# -# Copyright (C) 2014 by Digi International Inc. -# All rights reserved. -# -# This program is free software; you can redistribute it and/or modify it -# under the terms of the GNU General Public License version 2 as published by -# the Free Software Foundation. -# -# !Description: Attempt to mount boot partitions read-only (called from udev) -# -#=============================================================================== - -MOUNT="/bin/mount" - -# Use 'silent' if util-linux's mount (busybox's does not support that option) -[ "$(readlink ${MOUNT})" = "/bin/mount.util-linux" ] && MOUNT="${MOUNT} -o silent" - -MOUNTPOINT="/mnt/${ID_PART_ENTRY_NAME}" -mkdir -p ${MOUNTPOINT} -if ! ${MOUNT} -t auto -r ${DEVNAME} ${MOUNTPOINT}; then - logger -t udev "mount_bootparts.sh: mount ${DEVNAME} under ${MOUNTPOINT} failed!" - rmdir --ignore-fail-on-non-empty ${MOUNTPOINT} -fi diff --git a/meta-digi-arm/recipes-core/udev/udev-extraconf/mount_digiparts.sh b/meta-digi-arm/recipes-core/udev/udev-extraconf/mount_digiparts.sh new file mode 100644 index 000000000..08772a222 --- /dev/null +++ b/meta-digi-arm/recipes-core/udev/udev-extraconf/mount_digiparts.sh @@ -0,0 +1,92 @@ +#!/bin/sh +#=============================================================================== +# +# mount_bootparts.sh +# +# Copyright (C) 2014-2019 by Digi International Inc. +# All rights reserved. +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 2 as published by +# the Free Software Foundation. +# +# !Description: Attempt to mount boot partitions read-only (called from udev) +# +#=============================================================================== + +BASE_INIT="$(readlink -f "@base_sbindir@/init")" +INIT_SYSTEMD="@systemd_unitdir@/systemd" + +if [ "${SUBSYSTEM}" = "block" ]; then + PARTNAME="${ID_PART_ENTRY_NAME}" +elif [ "${SUBSYSTEM}" = "mtd" ]; then + MTDN="$(echo ${DEVNAME} | cut -f 3 -d /)" + PARTNAME="$(grep ${MTDN} /proc/mtd | sed -ne 's,.*"\(.*\)",\1,g;T;p')" +fi + +MOUNT_PARAMS="-o silent" +# Mount 'linux' partition as read-only +if [ "${PARTNAME}" = "linux" ]; then + MOUNT_PARAMS="${MOUNT_PARAMS} -o ro" +fi + +if [ "x$BASE_INIT" = "x$INIT_SYSTEMD" ];then + # systemd as init uses systemd-mount to mount block devices + MOUNT="/usr/bin/systemd-mount" + MOUNT_PARAMS="${MOUNT_PARAMS} --no-block" + + if [ -x "$MOUNT" ]; + then + logger "Using systemd-mount to finish mount" + else + logger "Linux init is using systemd, so please install systemd-mount to finish mount" + exit 1 + fi +else + MOUNT="/bin/mount" + if [ "$(readlink ${MOUNT})" != "/bin/mount.util-linux" ]; then + # Busybox mount. Clear default params + MOUNT_PARAMS="" + # Mount 'linux' partition as read-only + if [ "${PARTNAME}" = "linux" ]; then + MOUNT_PARAMS="${MOUNT_PARAMS} -r" + fi + fi +fi + +# Create mount point if needed +MOUNTPOINT="/mnt/${PARTNAME}" +[ -d "${MOUNTPOINT}" ] || mkdir -p ${MOUNTPOINT} + +if [ "${SUBSYSTEM}" = "block" ]; then + if ! ${MOUNT} -t auto ${MOUNT_PARAMS} ${DEVNAME} ${MOUNTPOINT}; then + logger -t udev "ERROR: Could not mount ${DEVNAME} under ${MOUNTPOINT}" + rmdir --ignore-fail-on-non-empty ${MOUNTPOINT} + fi +elif [ "${SUBSYSTEM}" = "mtd" ]; then + # Before attaching, find out if partition already attached + MTD_NUM="$(echo ${MTDN} | sed -ne 's,.*mtd\([0-9]\+\),\1,g;T;p')" + for ubidev in /sys/devices/virtual/ubi/*; do + echo "${ubidev}" | grep -qs '/sys/devices/virtual/ubi/\*' && continue + mtd_att="$(cat ${ubidev}/mtd_num)" + if [ "${mtd_att}" = "${MTD_NUM}" ]; then + dev_number="$(echo ${ubidev} | sed -ne 's,.*ubi\([0-9]\+\),\1,g;T;p')" + fi + done + + # If not already attached, attach and get UBI device number + if [ -z "${dev_number}" ]; then + dev_number="$(ubiattach -p ${DEVNAME} 2>/dev/null | sed -ne 's,.*device number \([0-9]\).*,\1,g;T;p' 2>/dev/null)" + fi + # Check if volume exists. + if ubinfo /dev/ubi${dev_number} -N ${PARTNAME} >/dev/null 2>&1; then + # Mount the volume. + if ! mount -t ubifs ubi${dev_number}:${PARTNAME} ${MOUNT_PARAMS} ${MOUNTPOINT}; then + logger -t udev "ERROR: Could not mount '${PARTNAME}' partition" + rmdir --ignore-fail-on-non-empty ${MOUNTPOINT} + fi + else + logger -t udev "ERROR: Could not mount '${PARTNAME}' partition, volume not found" + rmdir --ignore-fail-on-non-empty ${MOUNTPOINT} + fi +fi diff --git a/meta-digi-arm/recipes-core/udev/udev-extraconf/mount_partition.sh b/meta-digi-arm/recipes-core/udev/udev-extraconf/mount_partition.sh deleted file mode 100644 index 05f719b18..000000000 --- a/meta-digi-arm/recipes-core/udev/udev-extraconf/mount_partition.sh +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/sh -#=============================================================================== -# -# mount_partition.sh -# -# Copyright (C) 2017 by Digi International Inc. -# All rights reserved. -# -# This program is free software; you can redistribute it and/or modify it -# under the terms of the GNU General Public License version 2 as published by -# the Free Software Foundation. -# -# !Description: Attempt to mount the partition triggered by udev -# -#=============================================================================== - -MOUNT="/bin/mount" -PARTITION_NAME="${1}" -MOUNTPOINT="/mnt/${PARTITION_NAME}" - -# Use 'silent' if util-linux's mount (busybox's does not support that option) -[ "$(readlink ${MOUNT})" = "/bin/mount.util-linux" ] && MOUNT="${MOUNT} -o silent" - -if mkdir -p "${MOUNTPOINT}" && ! mountpoint -q "${MOUNTPOINT}"; then - if [ "${SUBSYSTEM}" = "block" ]; then - FSTYPE="$(blkid ${DEVNAME} | sed -e 's,.*TYPE="\([^"]\+\)".*,\1,g')" - if ! mount ${FSTYPE:+-t ${FSTYPE}} "${DEVNAME}" "${MOUNTPOINT}"; then - logger -t udev "ERROR: Could not mount '${PARTITION_NAME}' partition" - rmdir --ignore-fail-on-non-empty ${MOUNTPOINT} - fi - elif [ "${SUBSYSTEM}" = "mtd" ]; then - # Attach and get UBI device number - dev_number="$(ubiattach -p ${DEVNAME} 2>/dev/null | sed -ne 's,.*device number \([0-9]\).*,\1,g;T;p' 2>/dev/null)" - # Check if volume exists. - if ubinfo "/dev/ubi${dev_number}" -N "${PARTITION_NAME}" >/dev/null 2>&1; then - # Mount the volume. - if ! mount -t ubifs "ubi${dev_number}:${PARTITION_NAME}" "${MOUNTPOINT}"; then - logger -t udev "ERROR: Could not mount '${PARTITION_NAME}' partition" - rmdir --ignore-fail-on-non-empty ${MOUNTPOINT} - fi - else - logger -t udev "ERROR: Could not mount '${PARTITION_NAME}' partition, volume not found" - rmdir --ignore-fail-on-non-empty ${MOUNTPOINT} - fi - fi -fi diff --git a/meta-digi-arm/recipes-core/udev/udev-extraconf_1.1.bbappend b/meta-digi-arm/recipes-core/udev/udev-extraconf_1.1.bbappend index 954c41f74..2657a90d9 100644 --- a/meta-digi-arm/recipes-core/udev/udev-extraconf_1.1.bbappend +++ b/meta-digi-arm/recipes-core/udev/udev-extraconf_1.1.bbappend @@ -3,15 +3,15 @@ FILESEXTRAPATHS_prepend := "${THISDIR}/${BPN}:" SRC_URI += " \ - file://mount_bootparts.sh \ - file://mount_partition.sh \ + file://mount_digiparts.sh \ file://81-spi-spidev.rules \ file://blacklist.conf \ " do_install_append() { - install -m 0755 ${WORKDIR}/mount_bootparts.sh ${D}${sysconfdir}/udev/scripts/ - install -m 0755 ${WORKDIR}/mount_partition.sh ${D}${sysconfdir}/udev/scripts/ + install -m 0755 ${WORKDIR}/mount_digiparts.sh ${D}${sysconfdir}/udev/scripts/ + sed -i -e 's|@base_sbindir@|${base_sbindir}|g' ${D}${sysconfdir}/udev/scripts/mount_digiparts.sh + sed -i -e 's|@systemd_unitdir@|${systemd_unitdir}|g' ${D}${sysconfdir}/udev/scripts/mount_digiparts.sh install -m 0644 ${WORKDIR}/81-spi-spidev.rules ${D}${sysconfdir}/udev/rules.d/ # Bluetooth tty symlink