-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlinfreeze
executable file
·530 lines (513 loc) · 14.7 KB
/
linfreeze
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#!/bin/bash -e
#
# linfreeze - Utility to prevent permanent file changes.
#
# Copyright (C) 2010 Ryan Peters <sloshy45 [--at--] sbcglobal [--dot--] net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Additional permission under GNU GPL version 3 section 7
#
# If you modify this Program, or any covered work, by linking or
# combining it with [name of library] (or a modified version of that library),
# containing parts covered by the terms of [name of library's license],
# the licensors of this Program grant you additional permission to convey
# the resulting work. {Corresponding Source for a non-source form of such
# a combination shall include the source code for the parts of [name of library]
# used as well as that of the covered work.}
#
# Constants
TEXTDOMAINDIR=/usr/local/share/locale
TEXTDOMAIN=linfreeze
PROGNAME=linfreeze
NAMEPROPER=LinFreeze
VERSION=0.9.4
FROZNAME="/etc/.dynamic" # File listing the dynamic folders for frozen dirs.
STATNAME="/etc/.static" # File listing the static folders for frozen users/dirs.
LISTNAME="/etc/.frozdirs" # File listing the frozen users/dirs.
SPECDNAME="/etc/.spec-dy" # File listing users'/dirs' dynamic directories to be removed.
SPECSNAME="/etc/.spec-stat" # File listing users'/dirs' static directories to be removed.
# Functions
usage() {
echo $"$NAMEPROPER $VERSION -- Utility to prevent permanent file changes."
echo ""
echo $"WARNING: While in use, this program makes changes to /etc/fstab."
echo $"Do not edit these files while a system is frozen, only before and after."
echo ""
echo $"Usage:"
echo $"$PROGNAME [option] [directory or user to freeze or unfreeze]"
echo
echo $"Options:"
echo $"-h Display this help message and exit."
echo $"-v Display version information and exit."
echo ""
echo $"-d Freeze a directory to prevent permanent file changes."
echo $"-f Freeze a user account to prevent permanent file changes."
echo $"-u Unfreeze all directories and users."
echo $"-s Unfreeze a specific user or directory."
echo $"-c Remove the temporary directories for user accounts and/or directories."
echo $"-l Lists the frozen users and directories."
}
ifroot() { # Checks if the current user is 'root', which is needed for this utility.
if [ "$(whoami)" != "root" ];
then
echo $"You must be the 'root' user to use this utility."
exit 1
fi
}
checkcmdline() { #Checks if aufs=tmpfs parameter is up. If not, probably the system can't mount the dir/partition.
if ! [ `cat /proc/cmdline | grep "aufs=tmpfs"` ];
then
echo $"Remember to set aufs=tmpfs on kernel parameters to AVOID PROBLEMS. Example: ... ro quiet splash aufs=tmpfs ..."
fi
}
checkfiles() { # Checks if /etc/.frozen exists and renames it to /etc/.dynamic.
ifroot
if [ -f /etc/.frozen ];
then
cp /etc/.frozen $FROZNAME
rm /etc/.frozen
fi
}
directory() { # Recursively freezes an individual folder
ifroot
DIRF=`echo "$OPTARG" | sed -e "s/\/*$//" ` # Removes the trailing slash of $OPTARG
if [ ! -d $DIRF ];
then
echo $"Directory $DIRF does not exist!"
echo $"Please enter an existing directory or create it with 'mkdir $DIRF'."
exit 1
fi
if [ -d $DIRF.dynamic ];
then
echo $"This directory is already frozen! Unfreeze it with $PROGNAME -u."
echo $"If $DIRF is not frozen, use $PROGNAME -c to remove the temporary directory."
exit 1
fi
checkfiles
if [ ! -f $FROZNAME ];
then
touch $FROZNAME
fi
if [ ! -f /etc/fstab.thaw ];
then
cp /etc/fstab /etc/fstab.thaw
fi
mkdir $DIRF.dynamic
echo $"$DIRF.dynamic" >> $FROZNAME
echo $"Making dynamic directory at $OPTARG.dynamic for temporary files..."
echo $DIRF >> $LISTNAME
echo tmpfs $DIRF.dynamic tmpfs defaults,noatime,uid=1000,gid=1000 0 0 >> /etc/fstab
if [ ! -d $DIRF.static ]
then
echo $"Would you like to create a directory for read-only files to be laid"
echo -n $"over the original directory at $DIRF.static? [Y/n]: "
read STATYES
echo ""
fi
if [ -d $DIRF.static ];
then
touch $STATNAME
echo $DIRF.static >> $STATNAME
echo none $DIRF aufs br=$DIRF.dynamic=rw:$DIRF.static=ro:$DIRF=ro 0 0 >> /etc/fstab
echo $"Folder $DIRF is now frozen. Reboot your computer to not make any changes."
echo ""
echo $"If you wish to put temporary files and/or folders in the 'static' directory, please do so now by putting them in $DIRF.static."
STATYES=N
fi
if [ "$STATYES" = "Y" -o "$STATYES" = "y" ]
then
mkdir $DIRF.static
touch $STATNAME
echo $DIRF.static >> $STATNAME
echo none $DIRF aufs br=$DIRF.dynamic=rw:$DIRF.static=ro:$DIRF=ro 0 0 >> /etc/fstab
echo $"Folder $DIRF is now frozen. Reboot your computer to not make any permanent changes."
echo ""
echo $"If you wish to put temporary files and/or folders in the static directory, please do so now by putting them in $DIRF.static."
echo ""
echo $"If you would like to 'freeze' other directories or users, you may do so."
echo ""
else
echo none $DIRF aufs br=$DIRF.dynamic=rw:$DIRF=ro 0 0 >> /etc/fstab
echo $"Folder $DIRF is now 'frozen'. Reboot your computer to not make any permanent changes."
echo ""
echo "If you would like to 'freeze' other directories or users, you may do so."
echo ""
fi
chowndir
if [ -d /home/$CHOWNUSER ];
then
chown $CHOWNUSER $DIRF.dynamic
[ -d $DIRF.static ] && chown $CHOWNUSER $DIRF.static
RETRY=N
echo $"Directory permissions changed successfully."
else
echo -n "User $CHOWNUSER does not exist!"
echo $"Use 'chown [user] $DIRF.dynamic' and 'chown [user] $DIRF.static' to set permissions."
echo $"Exiting..."
fi
checkcmdline
}
chowndir() { # Changes ownership of temporary directories
ifroot
echo $"Would you like to set the ownership of the temporary directories used for 'freezing'?"
echo -n $"This is necessary if the directory frozen was inside of a home directory. [Y/n]: "
read CHOWNYES
echo ""
if [ "$CHOWNYES" = "Y" -o "$CHOWNYES" = "y" ];
then
echo -n $"Which user would you like to own the temporary directories?"
read CHOWNUSER
echo ""
else
echo "Exiting..."
exit 0
fi
}
user() { # This freezes the user account if the proper directories are in place.
ifroot
if [ ! -d /home/$OPTARG ];
then
echo $"User account $OPTARG does not exist! Create it first."
exit 1
fi
if [ ! -d /home/$OPTARG.dynamic ];
then
prep;
else
echo $"User account $OPTARG is already frozen!"
echo "Exiting..."
exit 1
fi
if [ ! -d "/home/$OPTARG.static" -a "$STATNO" = "Y" ];
then
echo "Would you like to create a directory for read-only files to be laid"
echo -n $"over the original user directory at /home/$OPTARG.static? [Y/n]: "
read STATIC
echo ""
fi
if [ "$STATIC" = "Y" -o "$STATIC" = "y" ];
then
prep;
fi
if [ ! -f /etc/fstab.thaw ];
then
cp /etc/fstab /etc/fstab.thaw
fi
echo tmpfs /home/$OPTARG.dynamic tmpfs defaults,noatime,uid=1000,gid=1000 0 0 >> /etc/fstab
if [ -d /home/$OPTARG.static ];
then
echo none /home/$OPTARG aufs br=/home/$OPTARG.dynamic=rw:/home/$OPTARG.static=ro:/home/$OPTARG=ro 0 0 >> /etc/fstab
echo $"User account $OPTARG is now 'frozen'. Reboot this computer and log in as $OPTARG to not make any permanent changes."
echo ""
echo "If you wish to put temporary files and/or folders in the static directory, do so now by putting them in /home/$OPTARG.static."
echo ""
echo $"If you would like to 'freeze' other directories or users, you may do so."
else
echo none /home/$OPTARG aufs br=/home/$OPTARG.dynamic=rw:/home/$OPTARG=ro 0 0 >> /etc/fstab
echo "User account $OPTARG is now 'frozen'. Reboot this computer and log in as $OPTARG to not make any permanent changes."
echo ""
echo "If you would like to 'freeze' other directories or users, you may do so."
fi
checkcmdline
}
prep() { # This prepares a user account for freezing.
ifroot
if [ ! -d /home/$OPTARG.dynamic ];
then
echo $"Making dynamic directory at /home/$OPTARG.dynamic for temporary files..."
echo ""
mkdir /home/$OPTARG.dynamic
chown $OPTARG /home/$OPTARG.dynamic
fi
if [ ! -d /home/$OPTARG.static ];
then
echo $"Would you like to create a directory for read-only files"
echo -n $"to be laid over the user directory at /home/$OPTARG.static? [Y/n]: "
read STATYES
echo ""
else
echo $"Static directory already created. Continuing..."
echo ""
fi
if [ "$STATYES" = "Y" -o "$STATYES" = "y" ];
then
mkdir /home/$OPTARG.static
chown $OPTARG /home/$OPTARG.static
STATNO=Y
fi
echo /home/$OPTARG >> $LISTNAME
echo $"Preparations complete!"
echo ""
}
unfreeze() { # This unfreezes all user accounts and directories that are frozen.
ifroot
if [ -f /tmp/.linreboot ];
then
echo $"There are users and/or directories waiting to be un-frozen."
echo $"Please reboot this computer and execute $PROGNAME -c"
echo ""
fi
if [ -f /etc/fstab.thaw ];
then
echo -n $"Would you like to 'unfreeze' all user accounts and directories? [Y/n]: "
read UNFREEZE
echo ""
else
echo $"There are no 'frozen' users or directories."
echo $"Freeze them with $PROGNAME [-f user] [-d directory]."
exit 1
fi
if [ "$UNFREEZE" = "Y" -o "$UNFREEZE" = "y" ];
then
rm /etc/fstab
mv /etc/fstab.thaw /etc/fstab
else
echo $"Leaving system as-is."
exit 0
fi
checkfiles
echo $"All user accounts and/or directories have been 'unfrozen'."
echo ""
echo $"First reboot your computer for the temporary 'dynamic' directories to be safely unmounted."
echo ""
echo $"Then run $PROGNAME -c after rebooting to remove the temporary directories."
echo ""
echo $"If you put any files in a 'static' directory, you may wish to move them elsewhere now."
echo ""
echo $"To remove the 'static' directories, if any, use $PROGNAME -c [user or directory] after rebooting."
touch /tmp/.linreboot
if [ -f $LISTNAME ];
then
rm $LISTNAME
fi
}
specific() { # Unfreezes a specific user or directory
ifroot
SPEC=`echo "$OPTARG" | sed -e "s/\/*$//" ` # Removes the trailing slash of $OPTARG
if [ ! -f /etc/fstab.thaw ];
then
echo $"There are no users or directories 'frozen'. Exiting..."
exit 0
fi
echo -n $"Would you like to 'unfreeze' $SPEC? [Y/n]: "
read SPECYES
if [ "$SPECYES" != "Y" -a "$SPECYES" != "y" ];
then
echo "Exiting..."
exit 0
fi
if [ ! -f $SPECNAME ];
then
touch $SPECNAME
fi
if [ -d /home/$SPEC.dynamic ];
then
sed -i '\:/home/'$SPEC': d' /etc/fstab
echo "/home/$SPEC.dynamic" >> $SPECDNAME
touch /tmp/.linreboot
echo $"User $SPEC has been 'unfrozen'."
echo "First reboot your computer for the temporary 'dynamic' directories to be safely unmounted."
echo ""
echo "Then run $PROGNAME -c after rebooting to remove the temporary directories."
fi
if [ -d /home/$SPEC.static ];
then
touch /tmp/.linreboot
echo "/home/$SPEC.static" >> $SPECSNAME
echo ""
echo "If you put any files in a 'static' directory, you may wish to move them elsewhere now."
echo ""
echo $"To remove the 'static' directory, use $PROGNAME -c after rebooting."
fi
if [ -d $SPEC.dynamic ];
then
sed -i '\:'$SPEC': d' /etc/fstab
echo "$SPEC.dynamic" >> $SPECDNAME
touch /tmp/.linreboot
echo ""
echo $"Directory $SPEC has been 'unfrozen'."
echo "First reboot your computer for the temporary 'dynamic' directories to be safely unmounted."
echo ""
echo "Then run $PROGNAME -c after rebooting to remove the temporary directories."
echo ""
fi
if [ -d $SPEC.static ];
then
touch /tmp/.linreboot
echo "$SPEC.static" >> $SPECSNAME
echo ""
echo "If you put any files in a 'static' directory, you may wish to move them elsewhere now."
echo ""
echo "To remove the 'static' directory, use $PROGNAME -c after rebooting."
fi
if [ ! -d "/home/$SPEC.dynamic" -a ! -d "$SPEC.dynamic" ];
then
echo $"User or directory $SPEC is not frozen or does not exist. Exiting..."
exit 1
fi
}
clean() { # Removes dynamic and static directories for unfrozen users and directories.
ifroot
checkfiles
if [ -f /tmp/.linreboot ];
then
echo $"This system needs to be rebooted before this function can be used."
exit 1
fi
if [ -f "$SPECDNAME" -a -f "/etc/fstab.thaw" ];
then
echo -n $"Remove temporary directories for 'unfrozen' users/directories? [Y/n]: "
read SPECDYN
echo ""
else
SPECDYN=N
fi
if [ "$SPECDYN" = "Y" -o "$SPECDYN" = "y" ];
then
while read SPECDYNDIR; do
rm -rf $SPECDYNDIR
echo $"Removed $SPECDYNDIR"
done < $SPECDNAME
rm $SPECDNAME
fi
if [ -f "$SPECSNAME" -a -f "/etc/fstab.thaw" ];
then
echo $"Remove 'static directories for 'unfrozen' users/directories?"
echo $"If you have files in any static directories, move them elsewhere."
echo -n "[Y/n]: "
read SPECSTAT
echo ""
else
SPECSTAT=N
fi
if [ "$SPECSTAT" = "Y" -o "$SPECSTAT" = "y" ];
then
while read SPECSTATDIR; do
rm -rf $SPECSTATDIR
echo $"Removed $SPECSTATDIR"
done < $SPECSNAME
rm $SPECSNAME
fi
if [ ! -f /etc/fstab.thaw ];
then
echo -n $"Remove temporary directories for all users and folders? [Y/n]: "
read REMDYN
echo ""
else
REMDYN=N
fi
if [ "$REMDYN" = "Y" -o "$REMDYN" = "y" ];
then
rm -rf /home/*.dynamic
echo $"'Dynamic' directories for users removed."
DIRZ=Y
else
DIRZ=N
fi
if [ "$DIRZ" = "Y" -a -f "$FROZNAME" ];
then
while read FROZDIR; do
rm -rf $FROZDIR
echo $"Removed $FROZDIR"
done < $FROZNAME
rm $FROZNAME
fi
if [ ! -f /etc/fstab.thaw ];
then
echo -n $"Remove 'static' directories for all users? [Y/n]: "
read HERP
echo ""
else
HERP=N
fi
if [ "$HERP" = "Y" -o "$HERP" = "y" ];
then
rm -rf /home/*.static
echo $"'Static' directories for users removed."
echo ""
fi
if [ -f $STATNAME ];
then
echo -n $"Remove 'static' directories for all directories? [Y/n]: "
read LAWL
echo ""
else
LAWL=N
fi
if [ "$LAWL" = "Y" -o "$LAWL" = y ];
then
while read STATDIR; do
rm -rf $STATDIR
echo $"Removed $STATDIR."
done < $STATNAME
rm $STATNAME
fi
if [ -f $SPECDNAME ];
then
rm $SPECDNAME
fi
if [ -f $SPECSNAME ];
then
rm $SPECSNAME
fi
echo $"Cleanup complete!"
}
listfrozen() { # Lists frozen users and directories.
if [ -f $LISTNAME ];
then
echo $"The following directories are 'frozen':"
cat $LISTNAME
else
echo $"There appear to be no 'frozen' directories or users on this system."
echo "Exiting..."
exit 1
fi
}
# Parse arguments
while getopts hvd:f:us:cl OPT; do
case "$OPT" in
h) usage;
exit 0
;;
v) echo $"$NAMEPROPER version $VERSION"
exit 0
;;
d) directory;
exit 0
;;
f) user;
exit 0
;;
u) unfreeze;
exit 0
;;
s) specific;
exit 0
;;
c) clean;
exit 0
;;
l) listfrozen;
exit 0
;;
*) usage;
exit 1
;;
esac
done
if [ $# -le 1 ]; then
usage;
exit 0;
fi