Skip to content

Commit 33beaae

Browse files
committed
Add swap file support
1 parent 1e946a7 commit 33beaae

File tree

5 files changed

+56
-2
lines changed

5 files changed

+56
-2
lines changed

gen.menu.json.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ const kterm_scripts=
1313
["Shell",[
1414
["USER=kindle","./chroot.shell.user.sh"],
1515
["USER=root","./chroot.shell.sh"]]],
16+
["Add/Resize swap file","./make.swap.sh"],
17+
["Remove swap file",
18+
[["Yes,remoce swap file","./remove.swap.sh"],
19+
["No",null]]],
1620
["Umount","./umount.sh"],
1721
["Resize rootfs","./resize.sh"],
1822
["Remove rootfs",
19-
[["Yes,Remove rootfs","./remove.sh"],
23+
[["Yes,remove rootfs","./remove.sh"],
2024
["No",null]]]]]]]
2125

2226
function translate_kterm_scripts(name,priority,script){

lib.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ cd "$(dirname "$0")" || fail
3535

3636
BIN="$(pwd)"
3737
ROOTFS_DIR="$(pwd)/rootfs"
38+
SWAP_IMG="$(pwd)/swap"
3839
ROOTFS_IMG="$(pwd)/rootfs.img"
40+
SWAP_LOCK="/tmp/kUaL_lInUx_sWaP_mOuNtEd"
3941
ROOTFS_LOCK="/tmp/kUaL_lInUx_mOuNtEd"
4042
INNER_TMP="/tmp/kUaL_lInUx"
4143
ROOTFS_TYPE=unknown
@@ -49,11 +51,22 @@ else
4951
fail "This kernel doesn't support ext4 and ext3."
5052
fi
5153

54+
baseus(){
55+
echo "$*" | sed 's|^/mnt/us/|/mnt/base-us/|'
56+
}
57+
5258
copy_etc_files(){
5359
[ -f "$ROOTFS_LOCK" ] || fail "rootfs is not mounted."
5460
cp /etc/hostname /etc/hostname /etc/hosts /etc/resolv.conf "$ROOTFS_DIR/etc"
5561
}
5662

63+
mount_swap(){
64+
if [ ! -f "$SWAP_LOCK" ] && [ -f "$SWAP_IMG" ]; then
65+
touch "$SWAP_LOCK" || fail
66+
mkswap "$(baseus "$SWAP_IMG")" || fail "cannot mount swap."
67+
swapon "$(baseus "$SWAP_IMG")" || fail "cannot mount swap."
68+
fi
69+
}
5770
mount_rootfs_base(){
5871
[ -f "$ROOTFS_LOCK" ] && fail "rootfs mounted."
5972
touch "$ROOTFS_LOCK" || fail
@@ -70,20 +83,43 @@ mount_rootfs_all(){
7083
for d in /dev /dev/pts /proc /sys; do
7184
mount -o bind "/$d" "$ROOTFS_DIR/$d" || fail "cannot bind $d"
7285
done
86+
mount_swap
7387
}
7488

89+
umount_swap(){
90+
if [ -f "$SWAP_LOCK" ] ; then
91+
swapoff "$(baseus "$SWAP_IMG")" || fail "cannot unmount swap."
92+
rm "$SWAP_LOCK" || fail
93+
fi
94+
}
7595
umount_rootfs_all(){
7696
[ -f "$ROOTFS_LOCK" ] || fail "rootfs is not mounted."
7797
for d in /dev/pts /dev /proc /sys /tmp; do
7898
umount "$ROOTFS_DIR/$d" 2>/dev/null
7999
done
80100
umount "$ROOTFS_DIR" || fail "cannot unmount rootfs."
81101
rm "$ROOTFS_LOCK" || fail
102+
umount_swap
82103
}
83104

105+
remove_swap(){
106+
[ -f "$SWAP_LOCK" ] && umount_swap
107+
rm -f "$SWAP_IMG" || fail
108+
}
109+
make_swap_interactive(){
110+
remove_swap
111+
cp rootfs."$ROOTFS_TYPE".base "$SWAP_IMG" || fail
112+
echo "Please enter the size of swap file and press Enter (e.g. 1000M):"
113+
local SWAP_SIZE
114+
read SWAP_SIZE || fail "cannot read input."
115+
"$BIN"/resize2fs "$SWAP_IMG" "$SWAP_SIZE" || fail "cannot resize."
116+
mkswap "$SWAP_IMG"
117+
mount_swap
118+
}
84119
resize_rootfs_interactive(){
85120
[ -f "$ROOTFS_LOCK" ] && fail "rootfs mounted."
86121
echo "Please enter the rootfs size and press Enter (e.g. 1000M):"
122+
local ROOTFS_SIZE
87123
read ROOTFS_SIZE || fail "cannot read input."
88124
"$BIN"/resize2fs "$ROOTFS_IMG" "$ROOTFS_SIZE" || fail "cannot resize."
89125
}

make.swap.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
. "$(dirname "$0")"/lib.sh
4+
5+
make_swap_interactive
6+
7+
quit

remove.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
[ -f "$ROOTFS_LOCK" ] && umount_rootfs_all
66

7-
rm -fr "$ROOTFS_IMG" || fail
7+
rm -f "$ROOTFS_IMG" || fail
88

99
quit

remove.swap.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
. "$(dirname "$0")"/lib.sh
4+
5+
remove_swap
6+
7+
quit

0 commit comments

Comments
 (0)