Skip to content

Commit 7f2a8c2

Browse files
committed
added --dryrun, --no, and --timeout OPTIONS
1 parent a41594e commit 7f2a8c2

File tree

5 files changed

+127
-49
lines changed

5 files changed

+127
-49
lines changed

README.html

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,15 +612,22 @@ <h1>Actions</h1>
612612
<h1>Options</h1>
613613
<p>All are optional (although <em>--root</em> may be required as noted below).</p>
614614
<dl class="docutils">
615-
<dt><tt class="docutils literal"><span class="pre">-b,</span> <span class="pre">--boot</span> [UUID]</tt></dt>
615+
<dt><tt class="docutils literal"><span class="pre">-b,</span> <span class="pre">--boot</span> &lt;UUID&gt;</tt></dt>
616616
<dd>Specify <em>/boot</em> partition UUID to use when building hooks.</dd>
617+
<dt><tt class="docutils literal"><span class="pre">--dryrun</span></tt></dt>
618+
<dd>Execute action without making any changes. Useful for debugging
619+
or viewing changes in <em>~/.cache/ramroot</em> before enabling.</dd>
617620
<dt><tt class="docutils literal"><span class="pre">-H,</span> <span class="pre">--help</span></tt></dt>
618621
<dd>Display help text and exit.</dd>
619622
<dt><tt class="docutils literal"><span class="pre">-K,</span> <span class="pre">--keep</span></tt></dt>
620623
<dd>Keep copies of new build and runtime hooks in <em>~/.cache/ramroot</em>.</dd>
621-
<dt><tt class="docutils literal"><span class="pre">-r,</span> <span class="pre">--root</span> [UUID]</tt></dt>
624+
<dt><tt class="docutils literal"><span class="pre">-N,</span> <span class="pre">--no</span></tt></dt>
625+
<dd>Change startup prompt default to not load filesystem to RAM.</dd>
626+
<dt><tt class="docutils literal"><span class="pre">-r,</span> <span class="pre">--root</span> &lt;UUID&gt;</tt></dt>
622627
<dd>Specify root partition UUID to use when building hooks;
623628
required if unable to detect UUID via lsblk or <em>/etc/fstab</em>.</dd>
629+
<dt><tt class="docutils literal"><span class="pre">-t,</span> <span class="pre">--timeout</span> &lt;SECONDS&gt;</tt></dt>
630+
<dd>Set RAM boot prompt timeout (default=15).</dd>
624631
</dl>
625632
</div>
626633
<div class="section" id="notes">
@@ -658,7 +665,7 @@ <h1>Credits</h1>
658665
<tbody valign="top">
659666
<tr class="field"><th class="field-name">Author:</th><td class="field-body">Chris Magyar</td>
660667
</tr>
661-
<tr class="field"><th class="field-name">Version:</th><td class="field-body">1.1.2</td>
668+
<tr class="field"><th class="field-name">Version:</th><td class="field-body">1.1.3</td>
662669
</tr>
663670
<tr class="field"><th class="field-name">License:</th><td class="field-body">GPL 3.0</td>
664671
</tr>

README.rst

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,29 @@ Options
8686

8787
All are optional (although *--root* may be required as noted below).
8888

89-
``-b, --boot [UUID]``
89+
``-b, --boot <UUID>``
9090
Specify */boot* partition UUID to use when building hooks.
9191

92+
``--dryrun``
93+
Execute action without making any changes. Useful for debugging
94+
or viewing changes in *~/.cache/ramroot* before enabling.
95+
9296
``-H, --help``
9397
Display help text and exit.
9498

9599
``-K, --keep``
96100
Keep copies of new build and runtime hooks in *~/.cache/ramroot*.
97101

98-
``-r, --root [UUID]``
102+
``-N, --no``
103+
Change startup prompt default to not load filesystem to RAM.
104+
105+
``-r, --root <UUID>``
99106
Specify root partition UUID to use when building hooks;
100107
required if unable to detect UUID via lsblk or */etc/fstab*.
101108

109+
``-t, --timeout <SECONDS>``
110+
Set RAM boot prompt timeout (default=15).
111+
102112

103113
Notes
104114
=====
@@ -144,7 +154,7 @@ by several inquisitive `forum posts`_.
144154
Chris Magyar
145155

146156
:Version:
147-
1.1.2
157+
1.1.3
148158

149159
:License:
150160
GPL 3.0

lib/hooks/ramroot

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ run_hook() {
66
# root (and boot) UUID
77
bootUUID=''
88
rootUUID=''
9+
# ramroot prompt:
10+
promptDefault='yes'
11+
promptTimeout=15
912

1013
# wait for output from other init processes:
1114
sleep 4
@@ -35,24 +38,34 @@ run_hook() {
3538
diffM=$(( $ramM - $rootM ))
3639
if [ $diffM -lt 500 ]; then
3740
echo ":! Not enough RAM available."
38-
inputYN="n"
41+
inputYN='n'
3942

40-
# prompt [y/n] to load filesystem to RAM:
43+
# prompt [Y/n] to load filesystem to RAM:
44+
elif [ "$promptDefault" = 'yes' ]; then
45+
echo -n ">: Load root filesystem to RAM? [Y/n] "
46+
inputYN='y'
47+
read -s -r -t $promptTimeout -n 1 inputYN
48+
if [ "${inputYN}" = 'n' ] || [ "${inputYN}" = 'N' ] || \
49+
[ "${inputYN}" = 'q' ] || [ "${inputYN}" = 'Q' ]; then
50+
inputYN='n'
51+
else
52+
inputYN='y'
53+
fi
54+
# prompt [y/N] to load filesystem to RAM:
4155
else
42-
echo -n ">: Load root filesystem to RAM? "
43-
echo -n "[Y/n] "
44-
inputYN="y"
45-
read -s -r -t 15 -n 1 inputYN
46-
if [ "${inputYN}" = "n" ] || [ "${inputYN}" = "N" ] || \
47-
[ "${inputYN}" = "q" ] || [ "${inputYN}" = "Q" ]; then
48-
inputYN="n"
56+
echo -n ">: Load root filesystem to RAM? [y/N] "
57+
inputYN='n'
58+
read -s -r -t $promptTimeout -n 1 inputYN
59+
if [ "${inputYN}" = 'y' ] || [ "${inputYN}" = 'Y' ]; then
60+
inputYN='y'
4961
else
50-
inputYN="y"
62+
inputYN='n'
5163
fi
64+
5265
fi
5366

5467
# copy root filesystem to RAM:
55-
if [ "${inputYN}" = "y" ]; then
68+
if [ "${inputYN}" = 'y' ]; then
5669
echo "yes"
5770
echo ":: Copying root filesystem to RAM..."
5871
# enable zRAM devices:

lib/man/ramroot.8

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.TH RAMROOT 8 "November 2017" "ramroot 1.1.2" "Ramroot Manual"
1+
.TH RAMROOT 8 "November 2017" "ramroot 1.1.3" "Ramroot Manual"
22
.SH NAME
33
ramroot \- load root filesystem entirely to RAM during boot
44
.SH SYNOPSIS
@@ -45,15 +45,25 @@ Return exit status 2 if not enabled.
4545
\fB\-b\fR, \fB\-\-boot\fR \fIUUID\fR
4646
Specify /boot partition UUID to use when building hooks.
4747
.TP
48+
\fB\-\-dryrun\fR
49+
Execute ACTION without making any changes. Useful for debugging
50+
or viewing changes in ~/.cache/ramroot before enabling.
51+
.TP
4852
\fB\-H\fR, \fB\-\-help\fR
4953
Display help text and exit.
5054
.TP
5155
\fB\-K\fR, \fB\-\-keep\fR
5256
Keep copies of new build and runtime hooks in ~/.cache/ramroot.
5357
.TP
58+
\fB\-N\fR, \fB\-\-no\fR
59+
Change startup prompt default to not load filesystem to RAM.
60+
.TP
5461
\fB\-r\fR, \fB\-\-root\fR \fIUUID\fR
5562
Specify root partions UUID to use when building hooks; required if
5663
unable to detect UUID via lsblk or /etc/fstab.
64+
.TP
65+
\fB\-t\fR, \fB\-\-timeout\fR \fISECONDS\fR
66+
Set RAM boot prompt timeout (default=15).
5767
.SH NOTES
5868
The filesystem transfer to RAM can take several minutes. As soon as
5969
the boot process is complete, the boot media can be safely removed.

ramroot

Lines changed: 69 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
##============================== ramroot ===============================##
33
# Copyright (C) 2018 Chris Magyar GNU General Public License v3 #
44
##========================================================================##
5-
version="1.1.2-2"
5+
version="1.1.3"
66

77
print_help() {
88
cat <<'HELPDOC'
@@ -36,14 +36,23 @@ OPTIONS
3636
-b, --boot UUID
3737
Specify /boot partition UUID to use when building hooks.
3838
39+
--dryrun
40+
Execute ACTION without making any changes.
41+
3942
-H, --help
4043
Display help text and exit.
4144
4245
-K, --keep
4346
Keep copies of new build and runtime hooks in ~/.cache/ramroot.
4447
48+
-N, --no
49+
Change startup prompt default to not load filesystem to RAM.
50+
4551
-r, --root UUID
4652
Specify root partition UUID to use when building hooks.
53+
54+
-t, --timeout
55+
Set RAM boot prompt timeout (default=15).
4756
HELPDOC
4857
return 0
4958
}
@@ -54,6 +63,7 @@ return 0
5463
dirCache="$HOME/.cache/ramroot"
5564
dirBuild='/tmp/ramroot'
5665
keepBuildFiles='false'
66+
promptTimeoutDefault=15
5767
# script variables (do not change):
5868
rootUUID=
5969
bootUUID=
@@ -67,6 +77,9 @@ hookRuntime="$dirLib/hooks/ramroot"
6777
initMODULES=
6878
initHOOKS=
6979
exitStatus=0
80+
dryRun='false'
81+
promptTimeout=$promptTimeoutDefault
82+
promptDefault='yes'
7083

7184

7285
##============================= FUNCTIONS ==============================##
@@ -143,7 +156,9 @@ ramroot_enable() {
143156
# make runtime hook:
144157
mkdir -p hooks
145158
sed "s@rootUUID=.*@rootUUID=\'$rootUUID\'@g; \
146-
s@bootUUID=.*@bootUUID=\'$bootUUID\'@g;" \
159+
s@bootUUID=.*@bootUUID=\'$bootUUID\'@g; \
160+
s@promptDefault=.*@promptDefault=\'$promptDefault\'@g; \
161+
s@promptTimeout=.*@promptTimeout=\'$promptTimeout\'@g;" \
147162
"$hookRuntime" > hooks/ramroot
148163
# FAIL: ramroot hooks not built:
149164
if [ ! -f hooks/ramroot ] || [ ! -f install/ramroot ]; then
@@ -180,7 +195,7 @@ ramroot_enable() {
180195
fi
181196

182197
# if changes need to be made:
183-
if [ "$mkinitChange" = 'true' ]; then
198+
if [ "$mkinitChange" = 'true' ] || [ "$dryRun" = 'true' ]; then
184199
# build new mkinitcpio.conf:
185200
sed "s@^MODULES=.*@$initMODULES@g; \
186201
s@^HOOKS=.*@$initHOOKS@g;" \
@@ -192,13 +207,17 @@ ramroot_enable() {
192207
fi
193208
# keep build files in cache:
194209
ramroot_cache
195-
# copy mkinitcpio.conf to /etc and hooks to /usr/lib/initcpio:
196-
printf ":: Enabling ramroot...\n"
197-
sudo cp hooks/ramroot "$dirHooks/ramroot" &&
198-
sudo cp install/ramroot "$dirInstall/ramroot" &&
199-
sudo cp mkinitcpio.conf /etc/mkinitcpio.conf &&
200-
sudo mkinitcpio -p linux &&
201-
printf ":: ramroot enabled.\n"
210+
if [ "$dryRun" = 'false' ]; then
211+
# copy mkinitcpio.conf to /etc and hooks to /usr/lib/initcpio:
212+
printf ":: Enabling ramroot...\n"
213+
sudo cp hooks/ramroot "$dirHooks/ramroot" &&
214+
sudo cp install/ramroot "$dirInstall/ramroot" &&
215+
sudo cp mkinitcpio.conf /etc/mkinitcpio.conf &&
216+
sudo mkinitcpio -p linux &&
217+
printf ":: ramroot enabled.\n"
218+
else
219+
printf ":: ramroot enable dryrun successful.\n"
220+
fi
202221
else
203222
printf ":: ramroot already enabled.\n"
204223
fi
@@ -235,7 +254,7 @@ ramroot_disable() {
235254
fi
236255

237256
# if changes need to be made:
238-
if [ "$mkinitChange" = 'true' ]; then
257+
if [ "$mkinitChange" = 'true' ] || [ "$dryRun" = 'true' ]; then
239258
# keep copy of old mkinitcpio.conf:
240259
if [ "$keepBuildFiles" = 'true' ]; then
241260
mkdir -p "$dirCache"
@@ -253,10 +272,14 @@ ramroot_disable() {
253272
# keep build files in cache:
254273
ramroot_cache
255274
# copy mkinitcpio.conf to /etc and rebuild linux cpio image:
256-
printf ":: Disabling ramroot...\n"
257-
sudo cp mkinitcpio.conf /etc/mkinitcpio.conf &&
258-
sudo mkinitcpio -p linux &&
259-
printf ":: ramroot disabled.\n"
275+
if [ "$dryRun" = 'false' ]; then
276+
printf ":: Disabling ramroot...\n"
277+
sudo cp mkinitcpio.conf /etc/mkinitcpio.conf &&
278+
sudo mkinitcpio -p linux &&
279+
printf ":: ramroot disabled.\n"
280+
else
281+
printf ":: ramroot disable dryrun successful.\n"
282+
fi
260283
fi
261284

262285
return 0
@@ -267,7 +290,8 @@ ramroot_disable() {
267290
ramroot_remove() {
268291
# check for installed hooks:
269292
if [ -f /usr/lib/initcpio/hooks/ramroot ] ||
270-
[ -f /usr/lib/initcpio/install/ramroot ]; then
293+
[ -f /usr/lib/initcpio/install/ramroot ] ||
294+
[ "$dryRun" = 'true' ]; then
271295
# keep copy of old hooks:
272296
if [ "$keepBuildFiles" = 'true' ]; then
273297
if [ -f /usr/lib/initcpio/hooks/ramroot ]; then
@@ -282,12 +306,16 @@ ramroot_remove() {
282306
fi
283307
fi
284308
# remove ramroot hooks:
285-
printf ":: Removing ramroot hooks...\n"
286-
if [ -f /usr/lib/initcpio/hooks/ramroot ]; then
287-
sudo rm /usr/lib/initcpio/hooks/ramroot
288-
fi
289-
if [ -f /usr/lib/initcpio/install/ramroot ]; then
290-
sudo rm /usr/lib/initcpio/install/ramroot
309+
if [ "$dryRun" = 'false' ]; then
310+
printf ":: Removing ramroot hooks...\n"
311+
if [ -f /usr/lib/initcpio/hooks/ramroot ]; then
312+
sudo rm /usr/lib/initcpio/hooks/ramroot
313+
fi
314+
if [ -f /usr/lib/initcpio/install/ramroot ]; then
315+
sudo rm /usr/lib/initcpio/install/ramroot
316+
fi
317+
else
318+
printf ":: ramroot remove dryrun successful.\n"
291319
fi
292320
fi
293321

@@ -309,7 +337,6 @@ ramroot_status() {
309337
fi
310338
}
311339

312-
313340
##======================== helper functions ========================##
314341
ramroot_cache() {
315342
if [ "$keepBuildFiles" = 'true' ]; then
@@ -328,14 +355,17 @@ dirScript="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
328355

329356
# parse command line arguments:
330357
for arg in "$@"; do case $arg in
331-
-b|--boot) shift; bootUUID="$1"; shift;;
332-
-D|--disable|disable) ramrootAction='disable'; shift;;
333-
-E|--enable|enable) ramrootAction='enable'; shift;;
334-
-H|-h|--help) ramrootAction='help'; shift;;
335-
-K|--keep) keepBuildFiles='true'; shift;;
336-
-r|--root) shift; rootUUID="$1"; shift;;
337-
-R|--remove|remove) ramrootAction='remove'; shift;;
338-
-S|--status|status) ramrootAction='status'; shift;;
358+
-b|--boot) shift; bootUUID="$1"; shift;;
359+
-D|--disable|disable) ramrootAction='disable'; shift;;
360+
--dryrun) dryRun='true'; shift;;
361+
-E|--enable|enable) ramrootAction='enable'; shift;;
362+
-H|-h|--help) ramrootAction='help'; shift;;
363+
-K|--keep) keepBuildFiles='true'; shift;;
364+
-N|--no) promptDefault='no'; shift;;
365+
-R|--remove|remove) ramrootAction='remove'; shift;;
366+
-r|--root) shift; rootUUID="$1"; shift;;
367+
-S|--status|status) ramrootAction='status'; shift;;
368+
-t|--timeout) shift; promptTimeout="$1"; shift;;
339369
esac; done
340370

341371
# print help:
@@ -356,6 +386,14 @@ if [ "$ramrootAction" = 'none' ]; then
356386
exit 1
357387
fi
358388

389+
# WARN: invalid prompt timeout:
390+
regex='^[1-9][0-9]*$'
391+
if [[ ! "$promptTimeout" =~ $regex ]]; then
392+
printf ":! Invalid prompt timeout: $promptTimeout\n"
393+
printf ":: Using default timeout: $promptTimeoutDefault\n"
394+
promptTimeout=promptTimeoutDefault
395+
fi
396+
359397
# prepare build:
360398
mkdir -p "$dirBuild"
361399
cd "$dirBuild"

0 commit comments

Comments
 (0)