forked from muqiuhan/zthfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuninstall.sh
More file actions
executable file
·287 lines (249 loc) · 9.2 KB
/
uninstall.sh
File metadata and controls
executable file
·287 lines (249 loc) · 9.2 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/bin/bash
# ZTHFS Uninstallation Script
# This script removes ZTHFS from the system while preserving data by default
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
# Configuration
CONFIG_FILE="${CONFIG_FILE:-/etc/zthfs/config.json}"
MOUNT_POINT="${MOUNT_POINT:-/mnt/zthfs}"
DATA_DIR="/var/lib/zthfs/data"
# Check if running as root
if [[ $EUID -ne 0 ]]; then
log_error "This script must be run as root (sudo)"
exit 1
fi
# Display warning banner
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo -e "${RED}ZTHFS Uninstallation${NC}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo -e "${YELLOW}WARNING: This will remove ZTHFS from your system.${NC}"
echo ""
echo "Before proceeding, ensure you have:"
echo " 1. Backed up your encryption keys ($CONFIG_FILE)"
echo " 2. Backed up any important data in $MOUNT_POINT"
echo " 3. Unmounted the filesystem (or let this script do it)"
echo ""
read -p "Do you want to continue? (yes/no): " -r
echo ""
if [[ ! "$REPLY" =~ ^[Yy][Ee][Ss]$ ]]; then
log_info "Uninstallation cancelled."
exit 0
fi
# Get the invoking user
if [[ -n "$SUDO_USER" ]]; then
REAL_USER="$SUDO_USER"
else
REAL_USER="${USER:-root}"
fi
# Track what was preserved
PRESERVED_ITEMS=()
# ============================================================================
# Step 1: Stop and disable services
# ============================================================================
log_info "Step 1/7: Stopping ZTHFS services..."
if systemctl is-active --quiet zthfs.service 2>/dev/null; then
systemctl stop zthfs.service
log_success "Stopped zthfs.service"
elif systemctl is-active --quiet zthfs 2>/dev/null; then
systemctl stop zthfs
log_success "Stopped zthfs"
else
log_info "Service not running"
fi
if systemctl is-enabled --quiet zthfs.service 2>/dev/null; then
systemctl disable zthfs.service
log_success "Disabled zthfs.service"
fi
if systemctl is-enabled --quiet zthfs-unmount.service 2>/dev/null; then
systemctl disable zthfs-unmount.service
log_success "Disabled zthfs-unmount.service"
fi
# ============================================================================
# Step 2: Unmount filesystem
# ============================================================================
log_info "Step 2/7: Unmounting ZTHFS..."
MOUNTED=false
if mountpoint -q "$MOUNT_POINT" 2>/dev/null; then
MOUNTED=true
/usr/local/bin/zthfs unmount "$MOUNT_POINT" 2>/dev/null || \
fusermount -u "$MOUNT_POINT" 2>/dev/null || \
umount "$MOUNT_POINT" 2>/dev/null || {
log_warn "Could not unmount $MOUNT_POINT"
log_info "You may need to manually: umount $MOUNT_POINT"
}
log_success "Filesystem unmounted"
else
log_info "Filesystem not mounted at $MOUNT_POINT"
fi
# Check for any other ZTHFS mounts
for mp in $(mount | grep -E "type.*fuse.*zthfs|zthfs on" | awk '{print $3}'); do
log_info "Unmounting $mp..."
umount "$mp" 2>/dev/null || fusermount -u "$mp" 2>/dev/null || true
done
# ============================================================================
# Step 3: Remove systemd services
# ============================================================================
log_info "Step 3/7: Removing systemd service files..."
for svc in zthfs.service zthfs-unmount.service; do
if [[ -f "/etc/systemd/system/$svc" ]]; then
rm -f "/etc/systemd/system/$svc"
log_success "Removed $svc"
fi
done
# Remove override directories
for dir in /etc/systemd/system/zthfs.service.d /etc/systemd/system/zthfs-unmount.service.d; do
if [[ -d "$dir" ]]; then
rm -rf "$dir"
log_success "Removed $dir"
fi
done
systemctl daemon-reload
systemctl reset-failed 2>/dev/null || true
# ============================================================================
# Step 4: Remove binary
# ============================================================================
log_info "Step 4/7: Removing ZTHFS binary..."
if [[ -f "/usr/local/bin/zthfs" ]]; then
rm -f /usr/local/bin/zthfs
log_success "Binary removed"
else
log_info "Binary not found"
fi
# ============================================================================
# Step 5: Handle data directory
# ============================================================================
log_info "Step 5/7: Handling data directory..."
echo ""
echo -ne "Remove data directory ($DATA_DIR)? "
echo -ne "${YELLOW}This will delete ALL encrypted data.${NC}"
echo -n " (yes/no): "
read -r
if [[ "$REPLY" =~ ^[Yy][Ee][Ss]$ ]]; then
if [[ -d "/var/lib/zthfs" ]]; then
rm -rf /var/lib/zthfs
log_success "Data directory removed"
fi
else
log_info "Preserving data directory: /var/lib/zthfs"
PRESERVED_ITEMS+=("Data directory: /var/lib/zthfs")
fi
# ============================================================================
# Step 6: Handle configuration
# ============================================================================
log_info "Step 6/7: Handling configuration..."
echo ""
echo -ne "Remove configuration file ($CONFIG_FILE)? "
echo -ne "${YELLOW}This contains your encryption keys!${NC}"
echo -n " (yes/no): "
read -r
if [[ "$REPLY" =~ ^[Yy][Ee][Ss]$ ]]; then
if [[ -d "/etc/zthfs" ]]; then
rm -rf /etc/zthfs
log_success "Configuration removed"
fi
else
log_info "Preserving configuration: $CONFIG_FILE"
PRESERVED_ITEMS+=("Configuration: $CONFIG_FILE")
# Create a backup with timestamp
if [[ -f "$CONFIG_FILE" ]]; then
BACKUP_CONFIG="${CONFIG_FILE}.uninstalled_$(date +%Y%m%d_%H%M%S)"
cp "$CONFIG_FILE" "$BACKUP_CONFIG"
log_success "Configuration backed up to: $BACKUP_CONFIG"
fi
fi
# ============================================================================
# Step 7: Clean up remaining items
# ============================================================================
log_info "Step 7/7: Cleaning up remaining items..."
# Remove log directory
if [[ -d "/var/log/zthfs" ]]; then
echo ""
echo -n "Remove log directory (/var/log/zthfs)? (yes/no): "
read -r
if [[ "$REPLY" =~ ^[Yy][Ee][Ss]$ ]]; then
rm -rf /var/log/zthfs
log_success "Log directory removed"
else
PRESERVED_ITEMS+=("Log directory: /var/log/zthfs")
fi
fi
# Remove system user
echo ""
echo -n "Remove zthfs system user? (yes/no): "
read -r
if [[ "$REPLY" =~ ^[Yy][Ee][Ss]$ ]]; then
if id -u zthfs &>/dev/null; then
userdel zthfs 2>/dev/null || true
log_success "System user removed"
else
log_info "System user does not exist"
fi
else
PRESERVED_ITEMS+=("System user: zthfs")
fi
# Remove mount point (only if empty)
if [[ -d "$MOUNT_POINT" ]]; then
# Check if directory is empty (only lost+found or nothing)
CONTENTS=$(ls -A "$MOUNT_POINT" 2>/dev/null)
if [[ -z "$CONTENTS" ]] || [[ "$CONTENTS" == "lost+found" ]]; then
echo ""
echo -n "Remove empty mount point ($MOUNT_POINT)? (yes/no): "
read -r
if [[ "$REPLY" =~ ^[Yy][Ee][Ss]$ ]]; then
rmdir "$MOUNT_POINT" 2>/dev/null || rm -rf "$MOUNT_POINT" 2>/dev/null || true
log_success "Mount point removed"
else
PRESERVED_ITEMS+=("Mount point: $MOUNT_POINT")
fi
else
log_info "Mount point not empty - preserving"
PRESERVED_ITEMS+=("Mount point: $MOUNT_POINT (contains data)")
fi
fi
# Remove from fuse group
if getent group fuse > /dev/null 2>&1; then
if getent group fuse | grep -q "\bzthfs\b"; then
gpasswd -d zthfs fuse 2>/dev/null || true
log_info "Removed zthfs from fuse group"
fi
fi
# ============================================================================
# Summary
# ============================================================================
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
log_success "ZTHFS uninstallation completed!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
if [[ ${#PRESERVED_ITEMS[@]} -gt 0 ]]; then
echo -e "${YELLOW}Preserved items:${NC}"
for item in "${PRESERVED_ITEMS[@]}"; do
echo " • $item"
done
echo ""
fi
echo -e "${BLUE}To completely remove all remaining items:${NC}"
echo " sudo rm -rf /var/lib/zthfs /etc/zthfs /var/log/zthfs '$MOUNT_POINT'"
echo " sudo userdel zthfs"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"