uvol: update to version 0.4
authorDaniel Golle <daniel@makrotopia.org>
Sun, 15 Aug 2021 03:59:56 +0000 (04:59 +0100)
committerDaniel Golle <daniel@makrotopia.org>
Sun, 15 Aug 2021 14:48:17 +0000 (15:48 +0100)
Create UCI fstab mount sections for existing volumes on firstboot.
Introduce special handling for volumes with non-[:alnum:] prefix:
 - do not create UCI sections for these 'hidden' volumes
 - create UCI section for '.uxc' special volume with target
   /var/state/uxc to be used for uxc config.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
utils/uvol/Makefile
utils/uvol/files/common.sh
utils/uvol/files/lvm.sh
utils/uvol/files/ubi.sh
utils/uvol/files/uvol.defaults [new file with mode: 0644]

index f929ba51546f655498d15738fe48927dc1a96437..e988d5168272abbb98bbd9f315f2b95e8af54240 100644 (file)
@@ -1,7 +1,7 @@
 include $(TOPDIR)/rules.mk
 
 PKG_NAME:=uvol
-PKG_VERSION:=0.3
+PKG_VERSION:=0.4
 PKG_RELEASE:=$(AUTORELEASE)
 
 PKG_MAINTAINER:=Daniel Golle <daniel@makrotopia.org>
@@ -64,12 +64,13 @@ define Package/autopart/install
 endef
 
 define Package/uvol/install
-       $(INSTALL_DIR) $(1)/etc/init.d $(1)/usr/libexec/uvol $(1)/usr/sbin $(1)/lib/functions
+       $(INSTALL_DIR) $(1)/etc/init.d $(1)/usr/libexec/uvol $(1)/usr/sbin $(1)/lib/functions $(1)/etc/uci-defaults
        $(INSTALL_BIN) ./files/uvol.init $(1)/etc/init.d/uvol
        $(INSTALL_BIN) ./files/common.sh $(1)/lib/functions/uvol.sh
        $(INSTALL_BIN) ./files/ubi.sh $(1)/usr/libexec/uvol/20-ubi.sh
        $(INSTALL_BIN) ./files/lvm.sh $(1)/usr/libexec/uvol/50-lvm.sh
        $(INSTALL_BIN) ./files/uvol $(1)/usr/sbin
+       $(INSTALL_BIN) ./files/uvol.defaults $(1)/etc/uci-defaults/90-uvol-restore-uci
 endef
 
 $(eval $(call BuildPackage,autopart))
index 0eee6d432f4e5a630c115ccf255289d759e4025d..e3b554e1809bf780371851efceb3ed1fd857a34d 100644 (file)
@@ -11,15 +11,27 @@ uvol_uci_add() {
        local volname="$1"
        local devname="$2"
        local mode="$3"
-       local autofs uuid uciname
+       local autofs=0
+       local target="/var/run/uvol/$volname"
+       local uuid uciname
 
-       uciname=${volname//-/_}
+       [ "$mode" = "ro" ] && autofs=1
+       uciname="${volname//[-.]/_}"
+       uciname="${uciname//[!([:alnum:]_)]}"
        uuid="$(/sbin/block info | grep "^$2" | xargs -n 1 echo | grep "^UUID=.*")"
        [ "$uuid" ] || return 22
-       _uvol_init_spooldir
        uuid="${uuid:5}"
-       autofs=0
-       [ "$mode" = "ro" ] && autofs=1
+
+       case "$uciname" in
+               "_uxc")
+                       target="/var/state/uxc"
+                       ;;
+               "_"*)
+                       return 1
+                       ;;
+       esac
+
+       _uvol_init_spooldir
        if [ -e "${UCI_SPOOLDIR}/remove-$1" ]; then
                rm "${UCI_SPOOLDIR}/remove-$1"
        fi
@@ -27,11 +39,10 @@ uvol_uci_add() {
        cat >"${UCI_SPOOLDIR}/add-$1" <<EOF
 set fstab.$uciname=mount
 set fstab.$uciname.uuid=$uuid
-set fstab.$uciname.target=/var/run/uvol/$volname
+set fstab.$uciname.target=$target
 set fstab.$uciname.options=$mode
 set fstab.$uciname.autofs=$autofs
 set fstab.$uciname.enabled=1
-commit fstab
 EOF
 }
 
@@ -39,7 +50,8 @@ uvol_uci_remove() {
        local volname="$1"
        local uciname
 
-       uciname=${volname//-/_}
+       uciname="${volname//-/_}"
+       uciname="${uciname//[!([:alnum:]_)]}"
        if [ -e "${UCI_SPOOLDIR}/add-$1" ]; then
                rm "${UCI_SPOOLDIR}/add-$1"
                return
@@ -47,20 +59,25 @@ uvol_uci_remove() {
        _uvol_init_spooldir
        cat >"${UCI_SPOOLDIR}/remove-$1" <<EOF
 delete fstab.$uciname
-commit fstab
 EOF
 }
 
 uvol_uci_commit() {
        local volname="$1"
+       local ucibatch
 
-       if [ -e "${UCI_SPOOLDIR}/add-$1" ]; then
-               uci batch < "${UCI_SPOOLDIR}/add-$1"
-               rm "${UCI_SPOOLDIR}/add-$1"
-       elif [ -e "${UCI_SPOOLDIR}/remove-$1" ]; then
-               uci batch < "${UCI_SPOOLDIR}/remove-$1"
-               rm "${UCI_SPOOLDIR}/remove-$1"
-       fi
+       for ucibatch in "${UCI_SPOOLDIR}/"*"-$volname"${volname+*} ; do
+               [ -e "$ucibatch" ] || break
+               uci batch < "$ucibatch"
+               [ $? -eq 0 ] && rm "$ucibatch"
+       done
 
+       uci commit fstab
        return $?
 }
+
+uvol_uci_init() {
+       uci -q get fstab.@uvol[0] && return
+       uci add fstab uvol
+       uci set fstab.@uvol[-1].initialized=1
+}
index 20ad0bcb5de777c6ffac20fd1e8d80dd2dc74e6c..95281194bad632b98e48b7298a33b2796efa3edf 100644 (file)
@@ -177,8 +177,8 @@ activatevol() {
                        return 22
                        ;;
                *)
-                       [ "$lv_active" = "active" ] && return 0
                        uvol_uci_commit "$1"
+                       [ "$lv_active" = "active" ] && return 0
                        lvm_cmd lvchange -k n "$lv_full_name" || return $?
                        lvm_cmd lvchange -a y "$lv_full_name" || return $?
                        return 0
@@ -319,10 +319,37 @@ listvols() {
        done
 }
 
-boot() {
-       local reports rep lv lvs lv_name lv_dm_path lv_mode volname
+
+detect() {
+       local reports rep lv lvs lv_name lv_full_name lv_mode volname devname lv_skip_activation
+       local temp_up=""
+
+       json_init
+       json_load "$(lvs -o lv_full_name -S "lv_name=~^[rw][owp]_.*\$ && vg_name=$vg_name && lv_skip_activation!=0")"
+       json_select report
+       json_get_keys reports
+       for rep in $reports; do
+               json_select "$rep"
+               json_select lv
+               json_get_keys lvs
+               for lv in $lvs; do
+                       json_select "$lv"
+                       json_get_vars lv_full_name
+                       echo "lvchange -a y $lv_full_name"
+                       lvm_cmd lvchange -k n "$lv_full_name"
+                       lvm_cmd lvchange -a y "$lv_full_name"
+                       temp_up="$temp_up $lv_full_name"
+                       json_select ..
+               done
+               json_select ..
+               break
+       done
+       sleep 1
+
+       uvol_uci_init
+
        json_init
-       json_load "$(lvs -o lv_name,lv_dm_path -S "lv_name=~^[rw][ow]_.*\$ && vg_name=$vg_name && lv_active=active")"
+       json_load "$(lvs -o lv_name,lv_dm_path -S "lv_name=~^[rw][owp]_.*\$ && vg_name=$vg_name")"
        json_select report
        json_get_keys reports
        for rep in $reports; do
@@ -334,11 +361,25 @@ boot() {
                        json_get_vars lv_name lv_dm_path
                        lv_mode="${lv_name:0:2}"
                        lv_name="${lv_name:3}"
+                       echo uvol_uci_add "$lv_name" "/dev/$(getdev "$lv_name")" "$lv_mode"
+                       uvol_uci_add "$lv_name" "/dev/$(getdev "$lv_name")" "$lv_mode"
                        json_select ..
                done
                json_select ..
                break
        done
+
+       uvol_uci_commit
+
+       for lv_full_name in $temp_up; do
+               echo "lvchange -a n $lv_full_name"
+               lvm_cmd lvchange -a n "$lv_full_name"
+               lvm_cmd lvchange -k y "$lv_full_name"
+       done
+}
+
+boot() {
+       true ; # nothing to do, lvm does it all for us
 }
 
 exportpv
@@ -354,6 +395,9 @@ case "$cmd" in
        total)
                totalbytes
                ;;
+       detect)
+               detect
+               ;;
        boot)
                boot
                ;;
index 0a43224fbb87fd47bf316a9acdf1e6e2823a694c..b0b363d7ed7b076a72c567a449c2059977a847dd 100644 (file)
@@ -230,6 +230,63 @@ bootvols() {
        done
 }
 
+detect() {
+       local volname voldev volmode voldev fstype tmpdev=""
+       for voldir in "/sys/devices/virtual/ubi/${ubidev}/${ubidev}_"*; do
+               read -r volname < "$voldir/name"
+               voldev="$(basename "$voldir")"
+               fstype=
+               case "$volname" in
+                       uvol-r[od]-*)
+                               if ! [ -e "/dev/ubiblock${voldev:3}" ]; then
+                                       ubiblock --create "/dev/$voldev" || return $?
+                               fi
+                               case "$volname" in
+                               uvol-rd-*)
+                                       tmpdev="$tmpdev $voldev"
+                                       ;;
+                               esac
+                               ;;
+                       *)
+                               continue
+                               ;;
+               esac
+               volmode="${volname:5:2}"
+               volname="${volname:8}"
+       done
+
+       uvol_uci_init
+
+       for voldir in "/sys/devices/virtual/ubi/${ubidev}/${ubidev}_"*; do
+               read -r volname < "$voldir/name"
+               voldev="$(basename "$voldir")"
+               case "$volname" in
+                       uvol-[rw][wod]*)
+                               true
+                               ;;
+                       *)
+                               continue
+                               ;;
+               esac
+               volmode="${volname:5:2}"
+               volname="${volname:8}"
+               case "$volmode" in
+               "ro" | "rd")
+                       uvol_uci_add "$volname" "/dev/ubiblock${voldev:3}" "ro"
+                       ;;
+               "rw" | "wd")
+                       uvol_uci_add "$volname" "/dev/${voldev}" "rw"
+                       ;;
+               esac
+       done
+
+       uvol_uci_commit
+
+       for voldev in $tmpdev ; do
+               ubiblock --remove "/dev/$voldev" || return $?
+       done
+}
+
 case "$cmd" in
        align)
                echo "$ebsize"
@@ -240,6 +297,9 @@ case "$cmd" in
        total)
                totalbytes
                ;;
+       detect)
+               detect
+               ;;
        boot)
                bootvols
                ;;
diff --git a/utils/uvol/files/uvol.defaults b/utils/uvol/files/uvol.defaults
new file mode 100644 (file)
index 0000000..cbd53a3
--- /dev/null
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+uci -q get fstab.@uvol[0].initialized >/dev/null || uvol detect || true