@@ -642,6 +642,7 @@ fm_prepare_mountpoints() {
642642fm_cmd_apply () {
643643 local target=" /etc/fstab"
644644 local tmpfiles_target=" /etc/tmpfiles.d/fnos-mount-manager.conf"
645+ local install_tmpfiles=" 0"
645646
646647 while [[ $# -gt 0 ]]; do
647648 case " $1 " in
@@ -651,6 +652,7 @@ fm_cmd_apply() {
651652 ;;
652653 --tmpfiles-target)
653654 tmpfiles_target=" $2 "
655+ install_tmpfiles=" 1"
654656 shift 2
655657 ;;
656658 --help | -h)
@@ -682,13 +684,14 @@ EOF
682684
683685 if [[ " ${target} " == " /etc/fstab" ]]; then
684686 fm_prepare_mountpoints
687+ install_tmpfiles=" 1"
685688 fi
686689
687690 fm_install_file " ${temp_output} " " ${target} "
688691 rm -f " ${temp_output} "
689692 fm_log " info" " Applied managed block to ${target} "
690693
691- if [[ " ${target } " == " /etc/fstab " ]]; then
694+ if [[ " ${install_tmpfiles } " == " 1 " ]]; then
692695 fm_install_file " $( fm_local_tmpfiles_path) " " ${tmpfiles_target} "
693696 fm_log " info" " Applied tmpfiles rules to ${tmpfiles_target} "
694697 fi
@@ -762,16 +765,18 @@ fm_check_rendered_file() {
762765 fi
763766
764767 local temp_render
768+ local temp_tmpfiles
765769 temp_render=" $( mktemp) "
770+ temp_tmpfiles=" $( mktemp) "
766771
767- fm_generate_scope " ${config_path} " " ${temp_render} " " ${source_label} "
772+ fm_generate_scope " ${config_path} " " ${temp_render} " " ${temp_tmpfiles} " " ${ source_label}"
768773
769774 if ! cmp -s " ${rendered_path} " " ${temp_render} " ; then
770775 fm_log " error" " Rendered file is stale: ${rendered_path} "
771776 error_count_ref=$(( error_count_ref + 1 ))
772777 fi
773778
774- rm -f " ${temp_render} "
779+ rm -f " ${temp_render} " " ${temp_tmpfiles} "
775780}
776781
777782# 检查已知的 shell 登录补挂载片段,避免把副作用留在非显式命令路径里。
@@ -1086,6 +1091,107 @@ EOF
10861091}
10871092
10881093
1094+ # --- remount.sh ---
1095+ # shellcheck shell=bash
1096+
1097+ if [[ -n " ${FNOS_MOUNT_MANAGER_REMOUNT_LOADED:- } " ]]; then
1098+ return 0
1099+ fi
1100+ FNOS_MOUNT_MANAGER_REMOUNT_LOADED=1
1101+
1102+ # 以更主动的方式把磁盘重新挂到受管挂载点:
1103+ # 若设备已经被挂到错误路径,会先卸载错误目标,再按我们的挂载点重挂。
1104+ fm_remount_disk () {
1105+ local index=" $1 "
1106+ local name=" ${FM_CONFIG_DISK_NAMES[${index}]} "
1107+ local source=" ${FM_CONFIG_DISK_SOURCES[${index}]} "
1108+ local mountpoint=" ${FM_CONFIG_DISK_MOUNTPOINTS[${index}]} "
1109+ local device_path
1110+ device_path=" $( fm_source_to_device_path " ${source} " ) "
1111+
1112+ local mounted_target=" "
1113+ mounted_target=" $( fm_find_mount_target_for_device " ${device_path} " ) "
1114+
1115+ if [[ " ${mounted_target} " == " ${mountpoint} " ]]; then
1116+ fm_log " info" " ${name} already mounted at ${mountpoint} "
1117+ return 0
1118+ fi
1119+
1120+ fm_run_privileged mkdir -p " ${mountpoint} "
1121+
1122+ if [[ -n " ${mounted_target} " ]]; then
1123+ fm_log " warn" " ${name} device is mounted at ${mounted_target} ; remounting to ${mountpoint} "
1124+
1125+ if ! fm_run_privileged umount " ${mounted_target} " > /dev/null 2>&1 ; then
1126+ # FNOS 相关服务可能仍持有旧路径句柄,显式 remount 时退到 lazy umount。
1127+ fm_run_privileged umount -l " ${mounted_target} " > /dev/null 2>&1 || true
1128+ fi
1129+
1130+ mounted_target=" $( fm_find_mount_target_for_device " ${device_path} " ) "
1131+ if [[ -n " ${mounted_target} " ]]; then
1132+ fm_log " warn" " ${name} device is still mounted at ${mounted_target} "
1133+ return 1
1134+ fi
1135+ fi
1136+
1137+ if command -v systemctl > /dev/null 2>&1 ; then
1138+ local mount_unit
1139+ mount_unit=" $( fm_unit_name_for_path " ${mountpoint} " mount) "
1140+ fm_run_privileged systemctl reset-failed " ${mount_unit} " > /dev/null 2>&1 || true
1141+ fm_run_privileged systemctl stop " ${mount_unit} " > /dev/null 2>&1 || true
1142+
1143+ local automount_unit
1144+ automount_unit=" $( fm_unit_name_for_path " ${mountpoint} " automount) "
1145+ fm_run_privileged systemctl reset-failed " ${automount_unit} " > /dev/null 2>&1 || true
1146+ fm_run_privileged systemctl stop " ${automount_unit} " > /dev/null 2>&1 || true
1147+ fi
1148+
1149+ if fm_run_privileged mount " ${mountpoint} " > /dev/null 2>&1 ; then
1150+ fm_log " info" " Remounted ${name} to ${mountpoint} "
1151+ return 0
1152+ fi
1153+
1154+ fm_log " warn" " Remount failed for ${name} "
1155+ return 1
1156+ }
1157+
1158+ # 显式接管错误挂载并切回受管命名。
1159+ fm_cmd_remount () {
1160+ while [[ $# -gt 0 ]]; do
1161+ case " $1 " in
1162+ --help | -h)
1163+ cat << 'EOF '
1164+ Usage: fnos-mount-manager remount
1165+
1166+ Force managed disks back onto the configured mountpoints.
1167+ EOF
1168+ return 0
1169+ ;;
1170+ * )
1171+ fm_die " Unknown remount option: $1 "
1172+ ;;
1173+ esac
1174+ done
1175+
1176+ [[ -f " $( fm_local_config_path) " ]] || fm_die " Local config not found: $( fm_local_config_path) "
1177+ fm_load_config " $( fm_local_config_path) "
1178+
1179+ local failures=0
1180+ local i
1181+ for (( i = 0 ; i < ${# FM_CONFIG_DISK_NAMES[@]} ; i += 1 )) ; do
1182+ if ! fm_remount_disk " ${i} " ; then
1183+ failures=$(( failures + 1 ))
1184+ fi
1185+ done
1186+
1187+ if (( failures > 0 )) ; then
1188+ fm_die " Remount completed with ${failures} failure(s)"
1189+ fi
1190+
1191+ fm_log " info" " Remount completed successfully"
1192+ }
1193+
1194+
10891195# --- main.sh ---
10901196#! /usr/bin/env bash
10911197set -Eeuo pipefail
@@ -1107,6 +1213,8 @@ if [[ -z "${FNOS_MANAGER_STANDALONE:-}" ]]; then
11071213 source " ${SCRIPT_DIR} /commands/status.sh"
11081214 # shellcheck source=linux/fnos/fnos-mount-manager/commands/repair.sh
11091215 source " ${SCRIPT_DIR} /commands/repair.sh"
1216+ # shellcheck source=linux/fnos/fnos-mount-manager/commands/remount.sh
1217+ source " ${SCRIPT_DIR} /commands/remount.sh"
11101218fi
11111219
11121220# 输出统一帮助信息,说明配置路径和常用命令。
@@ -1120,6 +1228,7 @@ Commands:
11201228 check Validate config drift and known conflicts
11211229 status Show current disk and unit status
11221230 repair Attempt a unified mount repair
1231+ remount Move disks back onto the managed mountpoints
11231232 help Show this help
11241233EOF
11251234}
@@ -1147,6 +1256,9 @@ fm_main() {
11471256 repair)
11481257 fm_cmd_repair " $@ "
11491258 ;;
1259+ remount)
1260+ fm_cmd_remount " $@ "
1261+ ;;
11501262 help | --help | -h)
11511263 fm_show_help
11521264 ;;
0 commit comments