1 1.1 lukem #!/bin/sh 2 1.1 lukem # 3 1.12.16.1 christos # $NetBSD: swap1,v 1.12.16.1 2019/06/10 21:42:45 christos 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.12.16.1 christos dev_free_tmpfs() 17 1.12.16.1 christos { 18 1.12.16.1 christos # Generate a list of tmpfs filesystems that contain no device nodes, 19 1.12.16.1 christos # which can presumably be unmounted safetly at shutdown time. 20 1.12.16.1 christos # Filenames are quoted and the list contains no unquoted newlines, 21 1.12.16.1 christos # so that the output can be reparsed as a single argument list. 22 1.12.16.1 christos mount -t tmpfs | while read -r line 23 1.12.16.1 christos do 24 1.12.16.1 christos fs=${line#tmpfs on } 25 1.12.16.1 christos fs=${fs% type tmpfs*} 26 1.12.16.1 christos find -x "${fs}" \( -type b -or -type c \) -exit 1 && 27 1.12.16.1 christos echo -n "'${fs}' " 28 1.12.16.1 christos done 29 1.12.16.1 christos } 30 1.12.16.1 christos 31 1.12.16.1 christos umount_vm_consumers() 32 1.12.16.1 christos { 33 1.12.16.1 christos case ${swapoff_umount} in 34 1.12.16.1 christos auto) 35 1.12.16.1 christos swapoff_umount_fs="$(dev_free_tmpfs)" 36 1.12.16.1 christos ;; 37 1.12.16.1 christos manual) 38 1.12.16.1 christos # swapoff_umount_fs set manually 39 1.12.16.1 christos ;; 40 1.12.16.1 christos esac 41 1.12.16.1 christos # eval so that quotes within $swapoff_umount_fs are parsed properly 42 1.12.16.1 christos eval set -- "${swapoff_umount_fs}" 43 1.12.16.1 christos for fs in "$@" 44 1.12.16.1 christos do 45 1.12.16.1 christos echo "Forcibly unmounting ${fs}" 46 1.12.16.1 christos umount -ft tmpfs "${fs}" 47 1.12.16.1 christos done 48 1.12.16.1 christos } 49 1.12.16.1 christos 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.12.16.1 christos 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