-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersistence-File-Build
More file actions
174 lines (150 loc) · 5.61 KB
/
Persistence-File-Build
File metadata and controls
174 lines (150 loc) · 5.61 KB
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
#!/bin/bash
# ╔═════════════════════════════════════════╗
# ║ gLiTcH Linux - Persistence File Creator ║
# ║ Creates labeled persistence images for ║
# ║ Debian live-boot (file-based overlay) ║
# ╚═════════════════════════════════════════╝
set -e
# Colors
R='\033[1;31m'
G='\033[1;32m'
Y='\033[1;33m'
C='\033[1;36m'
W='\033[1;37m'
N='\033[0m'
header() {
echo -e "${C}"
echo "╔═════════════════════════════════╗"
echo "║ gLiTcH Persistence File Creator ║"
echo "╚═════════════════════════════════╝"
echo -e "${N}"
}
cleanup() {
echo -e "${Y}[*] Cleaning up...${N}"
[ -n "$MAPPED" ] && cryptsetup luksClose "$MAP_NAME" 2>/dev/null
[ -n "$LOOPED" ] && losetup -d "$LOOP_DEV" 2>/dev/null
[ -d "$MNT" ] && umount "$MNT" 2>/dev/null && rmdir "$MNT" 2>/dev/null
}
trap cleanup EXIT
# Root check
if [ "$(id -u)" -ne 0 ]; then
echo -e "${R}[!] This script must be run as root.${N}"
exit 1
fi
header
# ── 1. Size ──
echo -e "${W}[1/3] Persistence file size${N}"
echo -e " Range: ${G}500${N} MB - ${G}20000${N} MB (20 GB)"
while true; do
read -rp " Size in MB [default: 2048]: " SIZE_MB
SIZE_MB="${SIZE_MB:-2048}"
if [[ "$SIZE_MB" =~ ^[0-9]+$ ]] && [ "$SIZE_MB" -ge 500 ] && [ "$SIZE_MB" -le 20000 ]; then
echo -e " ${G}✓${N} Size: ${SIZE_MB} MB"
break
else
echo -e " ${R}✗ Enter a number between 500 and 20000${N}"
fi
done
echo
# ── 2. Encryption ──
echo -e "${W}[2/3] Encryption${N}"
echo -e " 1) No encryption (default)"
echo -e " 2) LUKS encrypted"
while true; do
read -rp " Choice [1]: " ENC_CHOICE
ENC_CHOICE="${ENC_CHOICE:-1}"
case "$ENC_CHOICE" in
1) ENCRYPT=false; echo -e " ${G}✓${N} Unencrypted persistence"; break ;;
2) ENCRYPT=true; echo -e " ${G}✓${N} LUKS encrypted persistence"; break ;;
*) echo -e " ${R}✗ Enter 1 or 2${N}" ;;
esac
done
echo
# ── 3. Save path ──
echo -e "${W}[3/3] Save location${N}"
read -rp " Directory [default: $(pwd)]: " SAVE_DIR
SAVE_DIR="${SAVE_DIR:-$(pwd)}"
SAVE_DIR="${SAVE_DIR%/}"
if [ ! -d "$SAVE_DIR" ]; then
echo -e " ${R}✗ Directory does not exist: ${SAVE_DIR}${N}"
exit 1
fi
OUTFILE="${SAVE_DIR}/persistence"
if [ -f "$OUTFILE" ]; then
echo -e " ${Y}[!] File already exists: ${OUTFILE}${N}"
read -rp " Overwrite? [y/N]: " OW
[[ "$OW" =~ ^[Yy]$ ]] || exit 0
rm -f "$OUTFILE"
fi
echo -e " ${G}✓${N} Output: ${OUTFILE}"
echo
# ── Summary ──
echo -e "${C}━━━━━━━━━━━━━━━━━━━━━━━━━━━${N}"
echo -e " Size: ${W}${SIZE_MB} MB${N}"
echo -e " Encrypted: ${W}$([ "$ENCRYPT" = true ] && echo "Yes (LUKS)" || echo "No")${N}"
echo -e " Output: ${W}${OUTFILE}${N}"
echo -e "${C}━━━━━━━━━━━━━━━━━━━━━━━━━━━━${N}"
read -rp " Proceed? [Y/n]: " CONFIRM
[[ "$CONFIRM" =~ ^[Nn]$ ]] && exit 0
echo
# ── Create the image file ──
echo -e "${Y}[*] Allocating ${SIZE_MB} MB image...${N}"
dd if=/dev/zero of="$OUTFILE" bs=1M count="$SIZE_MB" status=progress
echo -e "${G}[✓] Image created${N}"
# ── Setup loop device ──
LOOP_DEV=$(losetup -f --show "$OUTFILE")
LOOPED=1
echo -e "${G}[✓] Loop device: ${LOOP_DEV}${N}"
MAP_NAME="persistence-setup"
if [ "$ENCRYPT" = true ]; then
# ── LUKS setup ──
echo -e "${Y}[*] Formatting LUKS container...${N}"
cryptsetup luksFormat --batch-mode --verify-passphrase "$LOOP_DEV"
echo -e "${Y}[*] Opening LUKS container...${N}"
cryptsetup luksOpen "$LOOP_DEV" "$MAP_NAME"
MAPPED=1
FORMAT_DEV="/dev/mapper/${MAP_NAME}"
else
FORMAT_DEV="$LOOP_DEV"
fi
# ── Format ext4 with label ──
echo -e "${Y}[*] Formatting ext4 with label 'persistence'...${N}"
mkfs.ext4 -L persistence "$FORMAT_DEV"
echo -e "${G}[✓] Filesystem created with label: persistence${N}"
# ── Mount and write persistence.conf ──
MNT=$(mktemp -d)
mount "$FORMAT_DEV" "$MNT"
echo "/ union" > "${MNT}/persistence.conf"
echo -e "${G}[✓] persistence.conf written: / union${N}"
# ── Verify ──
echo
echo -e "${C}━━━━━━━━━━━ Verification ━━━━━━━━━━━━━━━━━${N}"
echo -e " Label: ${W}$(e2label "$FORMAT_DEV")${N}"
echo -e " persistence.conf: ${W}$(cat "${MNT}/persistence.conf")${N}"
echo -e " File size: ${W}$(du -h "$OUTFILE" | awk '{print $1}')${N}"
if [ "$ENCRYPT" = true ]; then
echo -e " Encryption: ${W}LUKS$(cryptsetup status "$MAP_NAME" | grep -oP 'version:\s+\K\d')${N}"
fi
echo -e "${C}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${N}"
# ── Cleanup (handled by trap, but unmount first) ──
umount "$MNT"
rmdir "$MNT"
MNT=""
if [ "$ENCRYPT" = true ]; then
cryptsetup luksClose "$MAP_NAME"
MAPPED=""
fi
losetup -d "$LOOP_DEV"
LOOPED=""
echo
echo -e "${G}[✓] Done! Persistence file ready: ${OUTFILE}${N}"
echo
echo -e "${W}Usage:${N}"
echo -e " Copy '${OUTFILE}' to the root of your live USB drive."
echo
if [ "$ENCRYPT" = true ]; then
echo -e " Boot with: ${C}persistence persistence-path=/ persistence-encryption=luks${N}"
else
echo -e " Boot with: ${C}persistence persistence-path=/${N}"
fi
echo