0105a1d8ef
Refactor the install code to be generic, so we only need to add the new path in one place when adding a new file. Put the full path into the filename to make this possible. I've tried to mimic the final directory structure in the aport first (e.g. $pkgdir/sbin/swapfile -> main/postmarketos-base/sbin/swapfile), but that leads to conflicts as abuild only takes the filename for checksums (conflict with etc/conf.d/swapfile): https://gitlab.alpinelinux.org/alpine/abuild/-/issues/10013 We don't really need checksum verification for the files shipped in the same directory (not downloaded), but nevertheless this is a bug. It leads to confusing behavior and might be fixed by letting abuild demand that there is only one source file with the same name, as I suggested in the issue linked above. So let's avoid this altogether with the flat file name.
95 lines
2.5 KiB
Bash
95 lines
2.5 KiB
Bash
#!/bin/sh
|
|
#################################################################################
|
|
# Copyright 2017 Clayton Craft <clayton@craftyguy.net>
|
|
#
|
|
# This file is part of postmarketos-base
|
|
#
|
|
# postmarketos-base is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# postmarketos-base is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this postmarketos-base. If not, see <http://www.gnu.org/licenses/>.
|
|
#################################################################################
|
|
|
|
# $1 = swap file location
|
|
function remove_old_swap() {
|
|
local _swap_file=$1
|
|
swapoff ${_swap_file} 2>/dev/null
|
|
rm ${_swap_file}
|
|
if [ -e ${_swap_file} ]; then
|
|
echo "Unable to remove old swap file!"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# $1 = swap file location
|
|
# $2 = swap file size
|
|
function make_swap() {
|
|
local _swap_file=$1
|
|
local _swap_size=$2
|
|
|
|
# Allocate space on disk for swap
|
|
fallocate -l ${_swap_size}M ${_swap_file}
|
|
|
|
# Set correct file permissions for swap file
|
|
chown root:root ${_swap_file}
|
|
chmod 600 ${_swap_file}
|
|
|
|
#format swap file
|
|
mkswap ${_swap_file} 1>/dev/null
|
|
}
|
|
|
|
if [ "$(id -u)" != "0" ]; then
|
|
echo "This script can only be run as root"
|
|
exit 1
|
|
fi
|
|
|
|
config=/etc/conf.d/swapfile
|
|
if [ ! -f ${config} ]; then
|
|
echo "Unable to find config at: ${config}"
|
|
exit 1
|
|
fi
|
|
|
|
# deviceinfo is a pmOS-specific thing, but it's not *required*
|
|
source /etc/deviceinfo
|
|
source /etc/conf.d/swapfile
|
|
|
|
# Configure swap size. Size is in MB
|
|
# If size in config is unset then use value from deviceinfo,
|
|
# or default to 0 if neither is set
|
|
if [ -z "${swap_size}" ]; then
|
|
if [ -n "${deviceinfo_swap_size_recommended}" ]; then
|
|
swap_size=${deviceinfo_swap_size_recommended}
|
|
else
|
|
swap_size=0
|
|
fi
|
|
fi
|
|
|
|
if [ "${swap_size}" == "0" ]; then
|
|
echo "Configured swap file size is 0, skipping creation."
|
|
exit 0
|
|
fi
|
|
|
|
swap_file="${swap_file:-/swapfile}"
|
|
|
|
echo "Setting up swap file of size ${swap_size}MB at ${swap_file}..."
|
|
|
|
# Clean up old swap file
|
|
if [ -f ${swap_file} ]; then
|
|
remove_old_swap ${swap_file}
|
|
fi
|
|
|
|
# Create swap file
|
|
make_swap ${swap_file} ${swap_size}
|
|
|
|
# Activate swap file
|
|
swapon ${swap_file}
|
|
|
|
echo "Successfully activated swap file!"
|
|
exit 0
|