Home | History | Annotate | Line # | Download | only in rc.d
      1   1.1    lukem #!/bin/sh
      2   1.1    lukem #
      3  1.14   andvar # $NetBSD: swap1,v 1.14 2022/04/16 18:15:20 andvar Exp $
      4   1.1    lukem #
      5   1.1    lukem 
      6   1.1    lukem # PROVIDE: localswap
      7  1.10    joerg # REQUIRE: DISKS root
      8   1.7    lukem # KEYWORD: shutdown
      9   1.1    lukem 
     10   1.9  mycroft $_rc_subr_loaded . /etc/rc.subr
     11   1.1    lukem 
     12   1.1    lukem name="swap1"
     13   1.6  msaitoh start_cmd="swap1_start"
     14   1.7    lukem stop_cmd="swap1_stop"
     15   1.1    lukem 
     16  1.13   martin dev_free_tmpfs()
     17  1.13   martin {
     18  1.13   martin 	# Generate a list of tmpfs filesystems that contain no device nodes,
     19  1.14   andvar 	# which can presumably be unmounted safely at shutdown time.
     20  1.13   martin 	# Filenames are quoted and the list contains no unquoted newlines,
     21  1.13   martin 	# so that the output can be reparsed as a single argument list.
     22  1.13   martin 	mount -t tmpfs | while read -r line
     23  1.13   martin 	do
     24  1.13   martin 		fs=${line#tmpfs on }
     25  1.13   martin 		fs=${fs% type tmpfs*}
     26  1.13   martin 		find -x "${fs}" \( -type b -or -type c \) -exit 1 &&
     27  1.13   martin 			echo -n "'${fs}' "
     28  1.13   martin 	done
     29  1.13   martin }
     30  1.13   martin 
     31  1.13   martin umount_vm_consumers()
     32  1.13   martin {
     33  1.13   martin 	case ${swapoff_umount} in
     34  1.13   martin 		auto)
     35  1.13   martin 			swapoff_umount_fs="$(dev_free_tmpfs)"
     36  1.13   martin 			;;
     37  1.13   martin 		manual)
     38  1.13   martin 			# swapoff_umount_fs set manually
     39  1.13   martin 			;;
     40  1.13   martin 	esac
     41  1.13   martin 	# eval so that quotes within $swapoff_umount_fs are parsed properly
     42  1.13   martin 	eval set -- "${swapoff_umount_fs}"
     43  1.13   martin 	for fs in "$@"
     44  1.13   martin 	do
     45  1.13   martin 		echo "Forcibly unmounting ${fs}"
     46  1.13   martin 		umount -ft tmpfs "${fs}"
     47  1.13   martin 	done
     48  1.13   martin }
     49  1.13   martin 
     50   1.1    lukem #		Add all block-type swap devices; these might be necessary
     51   1.1    lukem #		during disk checks.
     52   1.1    lukem #
     53   1.6  msaitoh swap1_start()
     54   1.6  msaitoh {
     55  1.11      apb 	# Treat exit status 2 from swapctl(8) as successful; it means
     56  1.11      apb 	# "no error, but no suitable swap devices were configured".
     57  1.11      apb 	#
     58   1.6  msaitoh 	if ! checkyesno no_swap; then
     59  1.11      apb 		swapctl -A -t blk || [ $? = 2 ]
     60   1.6  msaitoh 	fi
     61   1.6  msaitoh }
     62   1.1    lukem 
     63   1.1    lukem #		Remove all block-type swap devices
     64   1.1    lukem #
     65   1.7    lukem swap1_stop()
     66   1.7    lukem {
     67   1.8    lukem 	if checkyesno swapoff || [ -n "$rc_force" ]; then
     68  1.13   martin 		umount_vm_consumers
     69   1.7    lukem 		echo "Removing block-type swap devices"
     70  1.11      apb 		swapctl -U -t blk || [ $? = 2 ]
     71   1.7    lukem 	fi
     72   1.7    lukem }
     73   1.1    lukem 
     74   1.5    lukem load_rc_config swap
     75   1.1    lukem run_rc_command "$1"
     76