Skip to content

Commit

Permalink
Adjust the swap creation method
Browse files Browse the repository at this point in the history
  • Loading branch information
ophub committed Sep 28, 2024
1 parent fce6e13 commit 9dad36c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,37 @@
# File path: /etc/custom_service/start_service.sh
#
#========================================================================================
#
# Find the partition where root is located
ROOT_PTNAME="$(df -h /boot | tail -n1 | awk '{print $1}' | awk -F '/' '{print $3}')"
if [[ -n "${ROOT_PTNAME}" ]]; then

# Find the disk where the partition is located, only supports mmcblk?p? sd?? hd?? vd?? and other formats
case "${ROOT_PTNAME}" in
mmcblk?p[1-4])
DISK_NAME="${ROOT_PTNAME:0:-2}"
PARTITION_NAME="p"
;;
[hsv]d[a-z][1-4])
DISK_NAME="${ROOT_PTNAME:0:-1}"
PARTITION_NAME=""
;;
nvme?n?p[1-4])
DISK_NAME="${ROOT_PTNAME:0:-2}"
PARTITION_NAME="p"
;;
*)
DISK_NAME=""
PARTITION_NAME=""
;;
esac

PARTITION_PATH="/mnt/${DISK_NAME}${PARTITION_NAME}4"
else
PARTITION_PATH=""
fi
#
#========================================================================================

# Custom Service Log
custom_log="/tmp/ophub_start_service.log"
Expand Down Expand Up @@ -53,5 +84,17 @@ todo_rootfs_resize="/root/.todo_rootfs_resize"
echo "[$(date +"%Y.%m.%d.%H:%M:%S")] Automatically expand the partition successfully." >>${custom_log}
}

# Set swap check file
sawp_check_file="${PARTITION_PATH}/.swap/swapfile"
[[ -f "${sawp_check_file}" ]] && {
# Set swap space
swap_loopdev=$(losetup -f)
# Mount swap file
losetup ${swap_loopdev} ${sawp_check_file}
# Enable swap
swapon ${swap_loopdev}
[[ "${?}" == 0 ]] && echo "[$(date +"%Y.%m.%d.%H:%M:%S")] The swap file enabled successfully." >>${custom_log}
}

# Add custom log
echo "[$(date +"%Y.%m.%d.%H:%M:%S")] All custom services executed successfully!" >>${custom_log}
19 changes: 7 additions & 12 deletions make-openwrt/openwrt-files/common-files/usr/sbin/openwrt-swap
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ delete_current_swap() {
echo -e "${STEPS} Current swap size is: [ ${1} ] bytes, start to delete..."
current_swap_file="$(cat /proc/swaps | sed -n '$p' | awk '{print $1}' | echo $(xargs))"
swapoff ${current_swap_file} 2>/dev/null && sync
rm -f ${current_swap_file} 2>/dev/null && sync
echo -e "${SUCCESS} Delete successfully!"
}

Expand All @@ -83,19 +82,15 @@ create_the_swap() {
echo -e "${INFO} The swap size is [ ${swap_gb} ] GiB, partition types is [ ${ROOTFS_TYPE} ], Please wait..."

# create swapfile
touch ${SWAP_PATH}/swapfile
chmod 600 ${SWAP_PATH}/swapfile
dd if="/dev/zero" of="${SWAP_PATH}/swapfile" bs="1024" count="$((1048576 * ${swap_gb}))" conv="fsync"
mkswap -L SWAP ${SWAP_PATH}/swapfile
swapon -a
chmod 600 ${SWAP_PATH}/swapfile
mkswap ${SWAP_PATH}/swapfile
swap_loop="$(losetup -Pf --show ${SWAP_PATH}/swapfile)"
swapon ${swap_loop}
# Set up automatic mount at boot
sed -i '/swapfile/d' /etc/fstab
echo "${SWAP_PATH}/swapfile none swap defaults 0 0" >>/etc/fstab

# Add swap to the mount point
uci delete fstab.@swap[0] 2>/dev/null
uci add fstab swap
uci set fstab.@swap[0].enabled='1'
uci set fstab.@swap[0].device="${SWAP_PATH}/swapfile"
uci commit fstab
#
sync && sleep 3
echo -e "${INFO} Current swap: \n$(cat /proc/swaps)"
echo -e "${SUCCESS} Created successfully!"
Expand Down

0 comments on commit 9dad36c

Please sign in to comment.