diff --git a/README.md b/README.md index 67949f0..ed50188 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,17 @@ ## systemd-swap Script for manage swap on: +* zswap - Enable/Configure * zram - Autoconfigurating -* zswap - configure -* block devices - auto find and do swapon * files - (sparse files for saving space, support btrfs) -* vram - EXPERIMENTAL: for creating swap on video memory +* block devices - auto find and do swapon -It is configurable in /etc/systemd-swap.conf. +It is configurable in /etc/systemd/swap.conf. ## Files placed: ``` -/etc/systemd-swap.conf +/etc/systemd/swap.conf /usr/lib/systemd/system/systemd-swap.service -/usr/lib/systemd/scripts/systemd-swap.sh +/usr/bin/systemd-swap ``` ## Please not forget to enable by: @@ -34,4 +33,3 @@ $ sudo dpkg -i ././systemd-swap/systemd-swap-*any.deb ``` options zram max_devices=32 ``` -* For vram as swap, you need open source drivers [Arch wiki](https://wiki.archlinux.org/index.php/Swap_on_video_ram) diff --git a/install.sh b/install.sh index 312281f..939b179 100755 --- a/install.sh +++ b/install.sh @@ -7,7 +7,7 @@ ERRO(){ echo -n "ERRO: "; echo -n "$@" ; echo " Abort!"; exit 1;} PREFIX="/" case $1 in - PREFIX=*) PREFIX=$(echo $1 | cut -d'=' -f2);; + PREFIX=*) PREFIX="${PREFIX//PREFIX=/}";; esac cd "$(dirname $0)" @@ -20,19 +20,18 @@ if [ "$PREFIX" == "/" ]; then fi fi -if [ -f /etc/systemd-swap.conf ]; then - INFO "File $PREFIX/etc/systemd-swap.conf already exists" - if cmp -s ./systemd-swap.conf $PREFIX/etc/systemd-swap.conf; then +$SUDO mkdir -p $PREFIX/etc/systemd/ +if [ -f $PREFIX/etc/systemd/swap.conf ]; then + INFO "File $PREFIX/etc/systemd/swap.conf already exists" + if cmp -s ./systemd/swap.conf $PREFIX/etc/systemd/swap.conf; then : else - INFO "New config saved as /etc/systemd-swap.conf.new" - $SUDO install -Dm 644 ./systemd-swap.conf $PREFIX/etc/systemd-swap.conf.new + INFO "New config saved as $PREFIX/etc/systemd/swap.conf.new" + $SUDO install -Dm 644 ./swap.conf $PREFIX/etc/systemd/swap.conf.new fi else - $SUDO install -Dm 644 ./systemd-swap.conf $PREFIX/etc/systemd-swap.conf + $SUDO install -Dm 644 ./swap.conf $PREFIX/etc/systemd/swap.conf fi -$SUDO install -Dm755 ./systemd-swap.sh $PREFIX/usr/lib/systemd/scripts/systemd-swap.sh -$SUDO mkdir -p $PREFIX/etc/systemd/ -$SUDO ln -svf /etc/systemd-swap.conf $PREFIX/etc/systemd/swap.conf +$SUDO install -Dm755 ./systemd-swap $PREFIX/usr/bin/systemd-swap $SUDO install -Dm644 ./systemd-swap.service $PREFIX/usr/lib/systemd/system/systemd-swap.service diff --git a/swap.conf b/swap.conf new file mode 100644 index 0000000..221b977 --- /dev/null +++ b/swap.conf @@ -0,0 +1,45 @@ +################################################################################ +# Defaults are optimized for general usage +################################################################################ + +################################################################################ +# Zswap +# +# Kernel >= 3.11 +# Zswap create compress cache between swap and memory for reduce IO +# https://www.kernel.org/doc/Documentation/vm/zswap.txt + +zswap_enabled=Y +zswap_compressor=lz4 +zswap_max_pool_percent=25 +zswap_zpool=z3fold + +################################################################################ +# ZRam +# +# Kernel >= 3.15 +# Zram compression streams count for additional information see: +# https://www.kernel.org/doc/Documentation/blockdev/zram.txt + +zram_enabled=0 +zram_size=$(($ram_size/4))K # This is 1/4 of ram size by default. +zram_streams=$cpu_count +zram_alg=lz4 # lzo lz4 deflate lz4hc 842 - for Linux 4.8.4 +zram_prio=32767 + +################################################################################ +# Swap file Universal +# loop + swapfile = support any fs (also btrfs) +swapfu_enabled=0 +# File is sparse and dynamically allocated. +swapfu_size=${ram_size}K # Size of swap file. +# File will not be available in fs after script start +# Make sure what script can access to this path during the boot process. +# Full path to swapfile +swapfu_path=/var/swap +swapfu_prio=-1024 + +################################################################################ +# Swap devices +# Find and auto swapon all available swap devices +swapd_auto_swapon=1 diff --git a/systemd-swap b/systemd-swap new file mode 100755 index 0000000..b3b7ea3 --- /dev/null +++ b/systemd-swap @@ -0,0 +1,202 @@ +#!/bin/bash +################################################################################ +# echo wrappers +INFO(){ echo -n "INFO: "; echo "$@" ;} +WARN(){ echo -n "WARN: "; echo "$@" ;} +ERRO(){ echo -n "ERRO: "; echo -n "$@" ; echo " Abort!"; exit 1;} + +################################################################################ +# Helpers +YN(){ + case "$1" in + Yes|Y|1|true) return 0 ;; + *) return 1 ;; + esac +} + +write(){ + DATA="$1" FILE="$2" + [ -z "$DATA" ] && return + [ -z "$FILE" ] && return + echo "$DATA" > "$FILE" +} + +help(){ + echo "$0 start|stop" +} +################################################################################ +# Initialization +check_root_rights(){ [ "$UID" == "0" ] || ERRO "Script must be runned as root!"; } + +# get cpu count from cpuinfo +cpu_count=$(nproc) +# get total ram size for meminfo +ram_size=$(awk '/MemTotal:/ { print $2 }' /proc/meminfo) + +WORK_DIR=/run/systemd/swap +TMPDIR=$WORK_DIR +CONF=/etc/systemd/swap.conf +B_CONF=$WORK_DIR/swap.conf + +case "$1" in + start) + INFO "Check config" + [ ! -f $CONF ] && ERRO "Missing config: /etc/systemd/swap.conf - try reinstall package" + check_root_rights + mkdir -p $WORK_DIR + [ -f $B_CONF ] && ERRO "systemd-swap already started!" + INFO "Backup config" + cp $CONF $B_CONF + INFO "Load config" + . $B_CONF + + if YN $zswap_enabled; then + [ ! -d /sys/module/zswap ] && ERRO "Zswap - not supported on current kernel" + ZSWAP_P=/sys/module/zswap/parameters/ + INFO "Zswap: backup current configuration: start" + mkdir -p $WORK_DIR/zswap/ + for file in $ZSWAP_P/*; do + cp $file $WORK_DIR/zswap/$(basename $file) + done + INFO "Zswap: backup current configuration: complete" + INFO "Zswap: set new parameters: start" + write $zswap_enabled $ZSWAP_P/enabled + write $zswap_compressor $ZSWAP_P/compressor + write $zswap_max_pool_percent $ZSWAP_P/max_pool_percent + write $zswap_zpool $ZSWAP_P/zpool + INFO "Zswap: set new parameters: complete" + fi + + if YN $zram_enabled; then + [ -z "$zram_size" ] && zram_size=$((ram_size/4))K + zram_streams=${zram_streams:-$cpu_count} + zram_alg=${zram_alg:-"lz4"} + zram_prio=${zram_prio:-"32767"} + zram_dev="" + INFO "Zram: check availability" + if [ ! -d /sys/module/zram ]; then + INFO "Zram: not part of kernel, so try find zram module" + modprobe -n zram || ERRO "Zram: can't find zram module!" + # Wrapper, for handling zram initialization problems + for (( i = 0; i < 10; i++ )); do + if [ ! -d /sys/module/zram ]; then + modprobe zram + sleep 1 + fi + done + INFO "Zram: module a successful loaded" + fi + INFO "Zram: module already loaded" + for (( i = 0; i < 10; i++ )); do + INFO "Zram: try initialize free device" + # zramctl is a external program -> return name of first free device + TMP=$(mktemp) + zramctl -f -a $zram_alg -t $zram_streams -s $zram_size &> $TMP + read -r OUTPUT < $TMP + rm $TMP + case "$OUTPUT" in + *"failed to reset: Device or resource busy"*) sleep 1 ;; + *"zramctl: no free zram device found"*) + WARN "Zram: zramctl can't find free device" + INFO "Zram: use workaround hook for hot add" + [ ! -f /sys/class/zram-control/hot_add ] && \ + ERRO "Zram: this kernel not support hot add zram device, please use 4.2+ kernels or see modinfo zram and make modprobe rule" + NEW_ZRAM=$(cat /sys/class/zram-control/hot_add) + INFO "Zram: success: new device /dev/zram$NEW_ZRAM" + ;; + /dev/zram*) + [ -b "$OUTPUT" ] || continue + zram_dev="$OUTPUT" + break + ;; + esac + done + INFO "Zram: initialized: $zram_dev" + mkdir -p $WORK_DIR/zram/ + mkswap "$zram_dev" &> /dev/null && \ + swapon -d -p $zram_prio "$zram_dev" && \ + ln -s $zram_dev $WORK_DIR/zram/ + fi + + if YN $swapf_enabled; then + swapf_size=${swapf_size:-"${ram_size}K"} + swapf_path=${swapf_path:-"/var/swap"} + swapf_prio=${swapf_prio:-"-1024"} + truncate -s $swapf_size $swapf_path + INFO "swapF: search free loop" + swapf_loop=$(losetup -f --show $swapf_path) + INFO "swapF: use $swapf_loop" + # loop use file descriptor, file still exist, but no have path + # When loop deatach file, file will be deleted. + rm $swapf_path + mkswap $swapf_loop &> /dev/null + swapon -p $swapf_prio -d $swapf_loop + # set autoclear flag + losetup -d $swapf_loop + mkdir -p $WORK_DIR/swapf + ln -s $swapf_loop $WORK_DIR/swapf/ + fi + + if YN $swapd_auto_swapon; then + INFO "swapD: search swap devices" + mkdir -p $WORK_DIR/swapd/ + for device in $(blkid -t TYPE=swap -o device | grep -vE '(zram|loop)'); do + for used_device in $(swapon --show=NAME --noheadings); do + [ "$device" == "$used_device" ] && unset device + done + [ ! -b "$device" ] && continue + swapon -d $device &> /dev/null && \ + ln -s $device $WORK_DIR/swapd/ && \ + INFO "swapD: enable device: $device" + done + fi + ;; + stop) + check_root_rights + [ ! -f $B_CONF ] && ERRO "systemd-swap not started!" + INFO "Load config" + . $B_CONF + if YN $zswap_enabled; then + [ ! -d /sys/module/zswap ] && ERRO "Zswap - not supported on current kernel" + ZSWAP_P=/sys/module/zswap/parameters/ + INFO "Zswap: restore configuration: start" + mkdir -p $WORK_DIR/zswap/ + for file in $WORK_DIR/zswap/*; do + cp $file $ZSWAP_P/$(basename $file) + done + INFO "Zswap: restore configuration: complete" + fi + + if YN $zram_enabled; then + for zram in $WORK_DIR/zram/*; do + [ ! -b $zram ] && continue + INFO "Zram: remove: /dev/$(basename $zram)" + swapoff $zram && \ + zramctl -r $(basename $zram) && \ + rm $zram && \ + INFO "Zram: removed: /dev/$(basename $zram)" + done + fi + + if YN $swapf_enabled; then + for device in $WORK_DIR/swapf/*; do + [ ! -b "$device" ] && continue + swapoff $device && \ + rm $device && \ + INFO "swapF: stopped /dev/$(basename $device)" + done + fi + + if YN $swapd_auto_swapon; then + for device in $WORK_DIR/swapd/*; do + [ ! -b "$device" ] && continue + swapoff $device && \ + rm $device && \ + INFO "swapD: disable device: $device" + done + fi + [ -d $WORK_DIR ] && rm -rf $WORK_DIR + ;; + + *) help ;; +esac diff --git a/systemd-swap.conf b/systemd-swap.conf deleted file mode 100644 index 67dbf53..0000000 --- a/systemd-swap.conf +++ /dev/null @@ -1,84 +0,0 @@ -## Config for systemd-swap: /usr/lib/systemd/scripts/systemd-swap.sh -################################################################################ - -################################################################################ -## If zram/zswap used -> try increase vm.swappiness -## If big swap on disk/file used -> try set vm.overcommit_memory=1 -## (for disable early OOM killer) -################################################################################ - -################################################################################ -## Internal variables in config: -## ${sys[cpu_count]} # -## ${sys[ram_size]} # in KB -################################################################################ - -################################################################################ -## Uncomment to enable setting -## if value is non zero - feature enabled -## if empty (commented out) - feature disabled -################################################################################ - -################################################################################ -# ZRam part -# -# zram[size]=$[${sys[ram_size]}/4]K # This is 1/4 of ram size by default. -# -# Kernel >= 3.15 -# Zram compression streams count for additional information see: -# https://www.kernel.org/doc/Documentation/blockdev/zram.txt -# -# zram[streams]=${sys[cpu_count]} -# zram[alg]=lz4 -################################################################################ - -################################################################################ -# Zswap part -# -# Kernel >= 3.11 -# Zswap create compress cache between swap and memory for reduce IO -# https://www.kernel.org/doc/Documentation/vm/zswap.txt -# -# If your kernel compiled with zram as part of kernel (not a module) -# Set it to false -# zram[module]=true -# -# zswap[enabled]=1 -# zswap[compressor]=lz4 -# zswap[max_pool_percent]=20 -# zswap[zpool]=zbud -################################################################################ - -################################################################################ -# Swap devices part, use fstab and nofail for static partitions -# swapd[devs]="UUID=FF-32 LABEL=swap /dev/sdz31" #staticaly swap devs -# swapd[parse]=1 # Try to find and use swap devices -################################################################################ - -################################################################################ -# Swap file part -# -# swapf[Poff]=1 # swap partition found -> don't create swap file -# swapf[fstab]=1 # If swap founded in fstab, disable swap file -# -# File is sparse and dynamically allocated. -# swapf[size]=${sys[ram_size]}K # Size of swap file. -# -# Full path to swapfile -# File will not be available in fs after script start -# Make sure what script can access to this path during the boot process. -# swapf[path]=/var/swap -################################################################################ - -################################################################################ -# WARNING! EXPERIMENTAL! -# -# Swap on video ram -# More info: https://wiki.archlinux.org/index.php/Swap_on_video_ram -# Systemd swap only checks what you not break boundaries of VRAMs -# Start address of swap space in video ram as you wish -# vramswap[region_start]=fffffffff -# Size of swapspace in video ram as you wish -# You can use: $ printf "%x" SIZE_IN_BYTES for generate this values -# vramswap[region_size]=ff -################################################################################ diff --git a/systemd-swap.service b/systemd-swap.service index fbf5e22..9b7e5f8 100644 --- a/systemd-swap.service +++ b/systemd-swap.service @@ -4,8 +4,15 @@ After=local-fs.target [Service] RemainAfterExit=yes -ExecStart=/usr/lib/systemd/scripts/systemd-swap.sh start -ExecStop=/usr/lib/systemd/scripts/systemd-swap.sh stop +ExecStart=/usr/bin/systemd-swap start +ExecStop=/usr/bin/systemd-swap stop +Nice=-19 +OOMScoreAdjust=-999 +CPUAccounting=true +CPUQuota=5% +MemoryHigh=16M +MemoryMax=64M +ProtectHome=true [Install] -WantedBy=local-fs.target \ No newline at end of file +WantedBy=local-fs.target diff --git a/systemd-swap.sh b/systemd-swap.sh deleted file mode 100755 index bde5d30..0000000 --- a/systemd-swap.sh +++ /dev/null @@ -1,293 +0,0 @@ -#!/bin/bash -e -################################################################################ -# echo wrappers -INFO(){ echo -n "INFO: "; echo "$@" ;} -WARN(){ echo -n "WARN: "; echo "$@" ;} -ERRO(){ echo -n "ERRO: "; echo -n "$@" ; echo " Abort!"; exit 1;} - -################################################################################ -# helper function for see information about writing data -write(){ - [ "$#" == "2" ] || return 0 - val="$1" file="$2" - [ ! -z "$val" ] || return 0 - [ ! -z "$file" ] || return 0 - INFO "$val >> $file" - echo "$val" >> "$file" || \ - WARN "Problem with writing $val >> $file" -} - -zram_hot_add(){ - WARN "zramctl can't find free device" - INFO "Use workaround hook for hot add" - if [ -f /sys/class/zram-control/hot_add ]; then - NEW_ZRAM=$(cat /sys/class/zram-control/hot_add) - INFO "Success: new device /dev/zram$NEW_ZRAM" - else - ERRO "This kernel not support hot add zram device, please use 4.2+ kernels or see modinfo zram and make modprobe rule" - fi -} - -manage_zram(){ - case $1 in - start) - [ -z "${zram[size]}" ] && return 0 - zram[alg]=${zram[alg]:-lzo} - zram[streams]=${zram[streams]:-${sys[cpu_count]}} - zram[force]=${zram[force]:-true} - zram[module]=${zram[module]:-true} - - if [[ "${zram[module]}" = "true" ]]; then - # Wrapper, for handling zram initialization problems - for (( i = 0; i < 10; i++ )); do - [ -d /sys/module/zram ] || modprobe zram - done - fi - - for (( i = 0; i < 10; i++ )); do - # zramctl is a external program -> return name of first free device - OUTPUT="$(zramctl -f -a ${zram[alg]} -t ${zram[streams]} -s ${zram[size]} 2>&1 || :)" - if echo "$OUTPUT" | grep -q "failed to reset: Device or resource busy"; then - sleep 1 - continue - elif echo "$OUTPUT" | grep -q "zramctl: no free zram device found"; then - zram_hot_add - elif echo "$OUTPUT" | grep -q "/dev/zram"; then - [ -b "$OUTPUT" ] || continue - zram[dev]="$OUTPUT" - break - else - : - fi - done - mkswap "${zram[dev]}" - swapon -d -p 32767 "${zram[dev]}" - write "zram[dev]=${zram[dev]}" ${lock[zram]} - ;; - stop) - # read info from zram lock file - . "${lock[zram]}" - swapoff "${zram[dev]}" - zramctl -r "${zram[dev]}" - rm "${lock[zram]}" - ;; - esac -} - -manage_swapf(){ - case $1 in - start) - [ -z ${swapf[path]} ] && return 0 - [ -z ${swapf[size]} ] && return 0 - # Create sparse file for swap - truncate -s ${swapf[size]} ${swapf[path]} || return 0 - # get first free loop device and - # use swap file through loop, for avoid error: - # skipping - it appears to have holes - swapf[loop]=`losetup -f --show ${swapf[path]}` - # loop use file descriptor, file still exist, but no have path - # When loop deatach file, file will be deleted. - rm ${swapf[path]} - mkswap ${swapf[loop]} - swapon -d ${swapf[loop]} - # set autoclear flag - losetup -d ${swapf[loop]} - write "swapf[loop]=${swapf[loop]}" ${lock[swapf]} - ;; - stop) - . ${lock[swapf]} - if [ ! -z ${swapf[loop]} ]; then - swapoff ${swapf[loop]} - fi - rm ${lock[swapf]} - ;; - esac -} - -manage_swapdev(){ - case $1 in - start) - [ -z "${swapd[devs]}" ] && return 0 - for i in `echo ${swapd[devs]}`; do - if swapon -d -p 1 $i; then - write $i ${lock[dev]} - else - : - fi - done - ;; - stop) - for i in `cat ${lock[dev]}`; do - swapoff $i || : - done - rm ${lock[dev]} - ;; - esac -} - -manage_zswap(){ - ZSWAP_P=/sys/module/zswap/parameters/ - case $1 in - start) - [ -f "${lock[zswap]}" ] && return 0 - declare -A local - for param in enabled compressor max_pool_percent zpool; do - local["$param"]="$(cat $ZSWAP_P/$param)" - write "zswap[$param]=${local[$param]}" "${lock[zswap]}" - write "${zswap[$param]}" "$ZSWAP_P/$param" - done - ;; - stop) - . "${lock[zswap]}" - for param in enabled compressor max_pool_percent zpool; do - write "${zswap[$param]}" "$ZSWAP_P/$param" - done - rm ${lock[zswap]} - ;; - esac -} - -read_line(){ - FILE=$1 NUM=$2 - head -n $NUM $FILE | tail -n 1 -} - -gen_vram_bounds(){ - FILE_TMP="$(mktemp)" - lspci | grep VGA > $FILE_TMP - VGA_COUNT="$(cat -n $FILE_TMP | wc -l)" - for a in $(seq 1 $VGA_COUNT); do - PCI_SLOT=$(read_line $FILE_TMP $a| awk '{print $1}') - FILE_REGIONS_TMP="$(mktemp)" - lspci -v -s $PCI_SLOT | grep '(64-bit, prefetchable)' > $FILE_REGIONS_TMP - REGION_COUNT="$(cat -n $FILE_REGIONS_TMP | tail -n 1 | awk '{print $1}')" - for b in $(seq 1 $REGION_COUNT); do - LINE=$(read_line $FILE_REGIONS_TMP $b) - REGION_START=$( echo $LINE | awk '{print $3}' ) - REGION_START_BYTE="$((16#$REGION_START))" - REGION_LENGHT=$( echo $LINE | awk '{print $6}' | cut -d'=' -f2 | tr -d ']' ) - if echo $REGION_LENGHT | grep -q M; then - REGION_LENGHT_MB="$(echo $REGION_LENGHT | tr -d 'M')" - REGION_LENGHT_BYTE=$[$REGION_LENGHT_MB*1024*1024] - REGION_END=$[$REGION_START_BYTE+$REGION_LENGHT_BYTE] - vramswap_regions[${a}_${b}]="$REGION_START_BYTE $REGION_END" - else - ERRO "Can't compute VRAM Region size for $PCI_SLOT!" - fi - done - done - rm $FILE_TMP -} - -manage_vramswap(){ - case $1 in - start) - [ -f "${lock[vramswap]}" ] && return 0 - [ -z "${vramswap[region_start]}" ] && return 0 - [ -z "${vramswap[region_size]}" ] && return 0 - if [ -b /dev/mtdblock0 ]; then - ERRO "Can't handle VRAM SWAP if /dev/mtdblock0 exist before first systemd-swap initialization!" - fi - gen_vram_bounds - U_REG_START="${vramswap[region_start]}" - U_REG_START="$((16#$U_REG_START))" - U_REG_END="${vramswap[region_size]}" - U_REG_END="$((16#$U_REG_END))" - U_REG_END="$[$U_REG_START+$U_REG_END]" - MEM_REGION_OKAY=false - for region in "${vramswap_regions[@]}"; do - break - START=$(echo $region | cut -d' ' -f1) - END=$(echo $region | cut -d' ' -f2) - if (( $U_REG_START >= $START )) && (( $U_REG_START < $END )); then - if (( $U_REG_END <= $END )); then - MEM_REGION_OKAY=true - else - continue - fi - else - continue - fi - done - if $MEM_REGION_OKAY; then - modprobe slram map=VRAM,0x${vramswap[region_start]},+0x${vramswap[region_size]} - modprobe mtdblock - if [ -b /dev/mtdblock0 ]; then - mkswap -L VRAM /dev/mtdblock0 - swapon -p 32767 /dev/mtdblock0 - fi - write /dev/mtdblock0 ${lock[vramswap]} - else - ERRO "No one parsed region is acceptable for VRAM!" - fi - ;; - stop) - swapoff "$(cat ${lock[vramswap]})" - rmmod slram mtdblock - ;; - esac -} - -############################################################################### -# Script body -# Create associative arrays -declare -A sys zram lock swapf swapd zswap vramswap vramswap_regions - -parse_config(){ - # get cpu count from cpuinfo - sys[cpu_count]=$(nproc) - # get total ram size for meminfo - sys[ram_size]=$(awk '/MemTotal:/ { print $2 }' /proc/meminfo) - - # get values from /etc/systemd-swap.conf - . $config - - # Parse fstab for swap mounts - [ -z ${swapf[fstab]} ] || \ - if [ ! -z "`grep '^[^#]*swap' /etc/fstab || :`" ]; then - unset swapf - INFO "Swap already specified in fstab, so disable swap file creation" - fi - - # Try to auto found swap partitions - if [ ! -z ${swapd[parse]} ]; then - swapd[devs]=" `blkid -t TYPE=swap -o device | grep -vE '(zram|loop)' || :` - ${swapd[devs]}" - [ ! -z ${swapf[Poff]} ] && [ ! -z "${swapd[devs]}" ] && unset swapf || : - fi -} - -manage_config(){ - config=/etc/systemd-swap.conf - if [ -f $config ]; then - parse_config - else - ERRO "Config $config deleted, reinstall package" - fi -} - -############################################################################### -lock[zram]=/run/.systemd-swap.zram -lock[dev]=/run/.systemd-swap.dev -lock[swapf]=/run/.systemd-swap.swapf -lock[zswap]=/run/.systemd-swap.zswap -lock[vramswap]=/run/.systemd-swap.vramswap -case $1 in - start) - manage_config - # start several independent threads - [ -f ${lock[zram]} ] || manage_zram $1 & - [ -f ${lock[dev]} ] || manage_swapdev $1 & - [ -f ${lock[swapf]} ] || manage_swapf $1 & - [ -f ${lock[zswap]} ] || manage_zswap $1 & - [ -f ${lock[vramswap]} ] || manage_vramswap $1 & - ;; - stop) - [ -f ${lock[zram]} ] && manage_zram $1 & - [ -f ${lock[dev]} ] && manage_swapdev $1 & - [ -f ${lock[swapf]} ] && manage_swapf $1 & - [ -f ${lock[zswap]} ] && manage_zswap $1 & - [ -f ${lock[vramswap]} ] && manage_vramswap $1 & - ;; -esac -wait diff --git a/vramswap.sh b/vramswap.sh deleted file mode 100755 index 66a37b5..0000000 --- a/vramswap.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/bash -e -FILE_TMP="$(mktemp)" -lspci | grep VGA > $FILE_TMP -VGA_COUNT="$(cat -n $FILE_TMP | wc -l)" - -read_line(){ - FILE=$1 NUM=$2 - head -n $NUM $FILE | tail -n 1 -} - -for a in $(seq 1 $VGA_COUNT); do - LINE=$(read_line $FILE_TMP $a) - echo $LINE - PCI_SLOT=$(echo $LINE | awk '{print $1}') - FILE_REGIONS_TMP="$(mktemp)" - lspci -v -s $PCI_SLOT | grep '(64-bit, prefetchable)' > $FILE_REGIONS_TMP - REGION_COUNT="$(cat -n $FILE_REGIONS_TMP | tail -n 1 | awk '{print $1}')" - for b in $(seq 1 $REGION_COUNT); do - LINE_2=$(read_line $FILE_REGIONS_TMP $b) - REGION_START=$( echo $LINE_2 | awk '{print $3}' ) - REGION_START_BYTE="$((16#$REGION_START))" - echo Region $b start: $REGION_START \= ${REGION_START_BYTE}B \= $[${REGION_START_BYTE}/1024/1024]MB - REGION_LENGHT=$( echo $LINE_2 | awk '{print $6}' | cut -d'=' -f2 | tr -d ']' ) - if echo $REGION_LENGHT | grep -q M; then - REGION_LENGHT_MB="$(echo $REGION_LENGHT | tr -d 'M')" - REGION_LENGHT_BYTE=$[$REGION_LENGHT_MB*1024*1024] - REGION_LENGHT_HEX="$(printf "%x" $REGION_LENGHT_BYTE)" - echo Region $b size: $REGION_LENGHT_HEX \= ${REGION_LENGHT_BYTE}B \= ${REGION_LENGHT_MB}MB - else - echo "Can't compute VRAM Region size" - fi - done -done - -rm $FILE_TMP