-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path0.0.9-unified_diff.patch
159 lines (159 loc) · 4.37 KB
/
0.0.9-unified_diff.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
--- /dev/null
+++ /usr/bin/zram_swap_resize.sh
@@ -0,0 +1,156 @@
+#/bin/bash
+#
+# (C) 2023, Roberto A. Foglietta <roberto.foglietta@gmail.com>
+# Released under MIT license for SailFish OS 4.5.19
+#
+################################################################################
+
+if [ "$(whoami)" != "root" ]; then
+ echo "This script should be executed by root"
+ echo "Please, insert the password or CTRL-C"
+ devel-su /bin/bash -c "$0 $1"
+ exit $?
+fi
+
+if echo "${1:-help}" | grep -q "h"; then
+ echo -e "\nUSAGE: $(basename $0) [ size(MB) | offload ]\n"
+ exit 0
+fi
+
+### GLOBAL VARIABLES INITIALISATION ############################################
+
+mtavail=no
+swapuse=off
+pwsvenagain=0
+pwsvstate=enabled
+blockname=/dev/block/zram0
+filename=$(ls -1 /vendor/etc/fstab.pdx20?)
+
+### GLOBAL FUNTCIONS DEFINITIONS ###############################################
+
+power_saving_state() {
+ pwsvstate=$(mcetool | sed -ne \
+ "s,^Power saving mode: *\([endisabl]*\) .*,\\1,p")
+ echo "Power saving mode: $pwsvstate"
+}
+
+power_saving_toggle() {
+ if [ "$1" = "disabled" ]; then
+ echo "Enabling power state..."
+ mcetool --set-power-saving-mode=enabled
+ power_saving_state
+ elif [ "$1" = "enabled" ]; then
+ echo "Disabling power state..."
+ mcetool --set-power-saving-mode=disabled
+ power_saving_state
+ else
+ echo "USAGE: power_saving_toggle enable|disable"
+ return 1
+ fi
+ return 0
+}
+
+mcetool_check() {
+ if ! which mcetool >/dev/null; then
+ echo -e "\nThis script whish to have mce-tools installed"
+ echo "because swapoff will fail with power saving enabled."
+ echo "You can disable power saving manually or you can"
+ echo "accept to reboot the device to complete the resize"
+ echo "or you can install with pkcon install -y mce-tools"
+ echo -e "\nPress ENTER to continue or CTRL-C to abort."
+ read
+ fi
+ mtavail=yes
+}
+
+zram_swap_change() {
+ echo -e "\nDisabling and resizing the zRAM swap..." |\
+ awk '{print} END {fflush()}'
+ echo 1 > /proc/sys/vm/drop_caches
+ if [ "$swapuse" != "off" ]; then
+ if ! swapoff -v $blockname; then
+ echo -e "\nWARNING: the on-line resize failed, reboot required"
+ return 1
+ fi
+ fi
+ swapusage
+ zramctl -s $1 $blockname
+ mkswap $blockname
+ return 0
+}
+
+zram_swap_offload() {
+ if [ "$swapuse" = "off" ]; then
+ echo -e "\nWARNING: the swap is not active, nothing to do\n"
+ return 1
+ fi
+ echo "Offloading the zRAM swap..."
+ sleep 0.25; echo 1 > /proc/sys/vm/drop_caches
+ if ! swapoff -v $blockname; then
+ echo -e "\nWARNING: swapoff failed, aborting\n"
+ return 1
+ fi
+ swapusage
+ if ! swapon -v /dev/zram0; then
+ echo -e "\nWARNING: swapon failed, aborting\n"
+ return 1
+ fi
+ swapusage
+}
+
+swapusage() {
+ free -m | grep -i swap | tr -s ' ' | sed "s,0 0 0,off,"
+}
+
+zram_swap_resize() {
+ mb=$((${1:-1024} + 0))
+ zramsize=$((mb*1024*1024))
+
+ zram_swap_change $zramsize || resized=no
+
+ echo -e "\nThe zram size at the next boot is set in $filename by this line"
+ sed -i "s|\(^"$blockname".*size\)=[0-9]*,max|\\1="$((mb*1024*1024))",max|" \
+ $filename
+ grep zram $filename | tr -s ' '
+
+ if [ "$resized" != "no" ]; then
+ echo -e "\nEnabling the zRAM swap..."
+ swapusage
+ if ! swapon -v /dev/zram0; then
+ echo -e "\nWARNING: swapon failed, aborting\n"
+ return 1
+ fi
+ swapusage
+ fi
+ echo
+}
+
+sysmon_update() {
+ return 0 # disabled because it does not work as expected
+ kill $(pgrep harbour-systemmonitor) 2>/dev/null # wake-up/update
+}
+
+### MAIN SCRIPT SECTION ########################################################
+
+swapuse=$(swapusage | awk '{ print $2 }')
+printf "\nCurrent $(basename $blockname) swap size: %s Mb" $swapuse
+printf "\nCurrent swapiness index: %d%%\n\n" $(cat /proc/sys/vm/swappiness)
+
+mcetool_check
+power_saving_state
+if [ "$pwsvstate" = "enabled" ]; then
+ power_saving_toggle enabled
+ pwsvenagain=1
+fi
+
+if [ "$1" = "offload" ]; then
+ zram_swap_offload
+else
+ zram_swap_resize $1
+fi
+sysmon_update
+
+if [ "$pwsvenagain" = "1" ]; then
+ power_saving_toggle disabled
+fi
+echo