-
Notifications
You must be signed in to change notification settings - Fork 1
/
_swap.sh
203 lines (162 loc) · 6.16 KB
/
_swap.sh
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/bash
# Amin 's Crypted SWAP helper script. v. 2022-11-07
# This script is old legacy; Consider native https://wiki.archlinux.org/title/Dm-crypt/Swap_encryption
DEPENDS='cryptsetup losetup realpath blkid lsblk';
# Check dependencies in OS
deps=( $DEPENDS )
for dep in "${deps[@]}"
do
if ! command -v $dep &> /dev/null
then
echo "No tool [$dep] - install before using.";
exit 11;
fi
done
if [ -e "$1" ] || [ "$2" == "create" ]
then SWPFILE=$1;
else
echo "Usage: $0 <Path to Dm-Crypt Swap> [start|stop|list|create|make_loops] [cipher]
Example: $0 ~/myswap.bin create
$0 /dev/sda13 start
create - make new swap. Existing files don't touch for prevent data loss
make_loops - create new loop-devices in /dev";
exit 1;
fi
if [ -n "$3" ]
then CIPHER=$3;
else CIPHER="aes-xts-essiv:sha256";
fi
SWPDEV=`basename "$SWPFILE"`; # filename
RLPATH=`realpath "$SWPFILE"`; # full-path
## Start safety checks - mounted paritions, RAID, LVM, ZFS
## ACHTUNG CHECKS !!!
if [ -e "$SWPFILE" ] && [ `mount -f | cut -d ' ' -f 1 | grep '/dev/' | grep "$RLPATH" | wc -l` -gt 0 ] && [ "$2" != "stop" ]
then echo "Device $SWPFILE mounted. Unmount first. BE CARE!"; exit 69;
elif [ -e "$SWPFILE" ] && [ `swapon -s | grep '/dev/' | cut -d ' ' -f 1 | grep "$RLPATH" | wc -l` -gt 0 ]
then echo "Device $SWPFILE is active non-crypted SWAP. Unmount first. Stop."; exit 71;
elif [ -e "$SWPFILE" ] && [ `mdadm -D /dev/md* 2>/dev/null | grep 'active' | grep "$RLPATH" | wc -l` -gt 0 ]
then echo "Device $SWPFILE in RAID-array. Stop. BE CARE!"; exit 72;
elif [ -e "$SWPFILE" ] && [ `pvscan -s 2>/dev/null | grep '/dev/' | grep "$RLPATH" | wc -l` -gt 0 ]
then echo "Device $SWPFILE in LVM. Stop. BE CARE!"; exit 73;
elif [ -e "$SWPFILE" ] && [ `blkid -s TYPE | grep ' TYPE="zfs_member"' | cut -d ':' -f 1 | grep "$RLPATH" | wc -l` -gt 0 ]
then echo "Device $SWPFILE contain ZFS. Stop. BE CARE!"; exit 74;
elif [ ! -e "$SWPFILE" ] && [ `echo "$RLPATH" | grep -E "^/(dev|sys|proc)/"` ]
then echo "Path $RLPATH in system area. Stop. Use new regular file or free block-device."; exit 75;
elif [ `lsblk $RLPATH -n -o MOUNTPOINT 2> /dev/null | grep -v '^$' | wc -l` -gt 0 ]
then echo "Block device $RLPATH has active MOUNTPOINT."; exit 77;
fi
## End safety checks - mounted paritions, RAID, LVM, ZFS
start() {
if [ `losetup -a | grep "$RLPATH" | wc -l` -gt 0 ]
then echo "This device already loop-mapped ! Stop it first."; exit 62;
fi
FILEHEADER=`dd if=$SWPFILE bs=8 count=1` 2>/dev/null;
if [ "$FILEHEADER" == 'DMC-SWAP' ]
then
LOOPD=`/sbin/losetup -f`;
/sbin/losetup -o 512 $LOOPD $SWPFILE;
/sbin/cryptsetup --key-file=/dev/urandom --key-size 512 -c $CIPHER create $SWPDEV $LOOPD;
/sbin/mkswap -L $SWPDEV -f /dev/mapper/$SWPDEV >/dev/null 2>&1;
/sbin/swapon /dev/mapper/$SWPDEV;
echo '===== CryptoSWAP Start for ['$SWPFILE'] --> ['$LOOPD'] --> ['$SWPDEV'] =====';
else
echo "Sorry, this storage NOT LABELED as 'DMC-SWAP' ";
fi
}
stop() {
LOOPD=`/sbin/losetup -a | grep $RLPATH | cut -d ':' -f 1`;
/sbin/swapoff /dev/mapper/$SWPDEV;
/sbin/cryptsetup remove $SWPDEV;
/sbin/losetup -d $LOOPD;
echo '===== CryptoSWAP Stop for ['$SWPFILE'] --> ['$LOOPD'] --> ['$SWPDEV'] =====';
}
create() {
echo '----- CREATE NEW Crypto-SWAP File ---------------------';
if [ -f "$SWPFILE" ]
then echo "file $SWPFILE exist, usage existing files denied."; exit 61;
elif [ `losetup -a | grep "$RLPATH" | wc -l` -gt 0 ]
then echo "This device already loop-mapped ! Stop it first."; exit 62;
else
if [ `echo "$RLPATH" | grep -E "^/dev/"` ]
then echo "!! ATTENTION !! Your Crypto-SWAP on PHYSICAL block device !
!! Current content of $RLPATH will be ERASED! Be carefully!";
fi
OLDFS=`blkid "$RLPATH"`
if [ ! "$OLDFS" == '' ]
then echo "!! Device $SWPFILE contain filesystem:
$OLDFS"
fi
echo -n "You start to CREATE dm-crypt SWAP. Continue (Yes/No)? "
read CONFIRM
if [ ! -n "$CONFIRM" ] || [ ! $CONFIRM == 'Yes' ]
then echo 'No confirmation!'; exit 60;
else echo "OK, continue...";
fi
if [ ! `echo "$RLPATH" | grep -E "^/dev/"` ]
then
while [ "$NEWSIZE" = "" ]
do
echo -n "Enter file size (1048576, 1024K, 100M, 2G): "
read NEWSIZE
NEWSIZE=`echo "$NEWSIZE" | grep -Ex '[0-9KMGTPEZY]+'`
done
else
NEWSIZE=`lsblk -bdno SIZE "$RLPATH" | tr -d ' '`;
echo "Block device :: Full detected size [$NEWSIZE] used"
fi
echo "Fast fill crypted swap"
dd if=/dev/null of="$SWPFILE" bs=1 seek="$NEWSIZE"
if [ $? -ne 0 ]
then
echo "DD error; Size too big, read-only storage, etc ?"
exit 7;
elif [ ! `mkswap "$RLPATH"` ]
then
echo "Can't create initial swap on device. Too small size or DOS-extended ??!"
exit 8;
fi
touch $SWPFILE;
LOOPD=`/sbin/losetup -f`;
dd if=/dev/null of=$SWPFILE bs=1 seek=$NEWSIZE
losetup -o 0 --sizelimit 512 $LOOPD $SWPFILE
echo "DMC-SWAP : Edit only 5 comment strings ! [ Header: 512b ] */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* 1. */
/* 2. */
/* 3. */
/* 4. */
/* 5. */
/*___________________________________________________________*/
"> $LOOPD
sync
losetup -d $LOOPD
LOOPD=`/sbin/losetup -f`;
losetup -o 512 $LOOPD $SWPFILE
dd if=/dev/urandom of=$LOOPD
sync
losetup -d $LOOPD
fi
}
make_loops() {
for i in $(seq 100 220); do
mknod -m0660 /dev/loop$i b 7 $i;
chown root.disk /dev/loop$i;
done;
}
list() { /sbin/swapon -s; }
case "$2" in
start)
start;;
stop)
stop;;
create)
create;;
make_loops)
make_loops ;;
list)
list;;
*)
SWPLINE=`/sbin/losetup -a | grep $SWPFILE`;
if [ -n "$SWPLINE" ]; then stop; else start; fi
esac
exit 0