Skip to content

Commit

Permalink
now works with encryption, plus a bunch of syntax improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
arcmags committed Jan 31, 2018
1 parent 821ce17 commit 6cf6877
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 213 deletions.
3 changes: 1 addition & 2 deletions README.html
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,6 @@ <h1>Issues / Future Implementations</h1>
automatically if installed)</li>
<li>option to create a unique hostname for each RAM boot instance</li>
<li>improve security features<ul>
<li>test compatibility with full disk encryption</li>
<li>option to require removal of boot media before starting userspace</li>
<li>option to fail boot unless loaded to RAM (requires <em>chroot</em> to
make changes; advanced users)</li>
Expand All @@ -688,7 +687,7 @@ <h1>Credits</h1>
<tbody valign="top">
<tr class="field"><th class="field-name">Author:</th><td class="field-body">Chris Magyar</td>
</tr>
<tr class="field"><th class="field-name">Version:</th><td class="field-body">1.1.7</td>
<tr class="field"><th class="field-name">Version:</th><td class="field-body">1.1.8</td>
</tr>
<tr class="field"><th class="field-name">License:</th><td class="field-body">GPL 3.0</td>
</tr>
Expand Down
4 changes: 1 addition & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ Issues / Future Implementations

* improve security features

+ test compatibility with full disk encryption

+ option to require removal of boot media before starting userspace

+ option to fail boot unless loaded to RAM (requires *chroot* to
Expand All @@ -183,7 +181,7 @@ by several inquisitive `forum posts`_.
Chris Magyar

:Version:
1.1.7
1.1.8

:License:
GPL 3.0
Expand Down
96 changes: 47 additions & 49 deletions lib/hooks/ramroot
Original file line number Diff line number Diff line change
Expand Up @@ -4,91 +4,90 @@

run_hook() {
# root (and boot) UUID
bootUUID=''
rootUUID=''
UUID_BOOT=''
UUID_ROOT=''
# ramroot prompt:
promptDefault='yes'
promptTimeout=15
LOAD_DEFAULT='yes'
LOAD_TIMEOUT=15
# options:
fstabMount='false'
issueRAM='false'
FLAG_FSTAB='false'
FLAG_ISSUE='false'

# wait for output from other init processes:
sleep 4

# detect available RAM:
ramM=`free --mega | awk '/Mem/ {print int($2)}'`
ramG=$(( $ramM / 1000 ))
RAM_M=`free --mega | awk '/Mem/ {print int($2)}'`
RAM_G=$(( $RAM_M / 1000 ))

# mount root partition to /local_root:
poll_device "/dev/disk/by-uuid/${rootUUID}" 20
mount -U "${rootUUID}" /local_root
poll_device "/dev/disk/by-uuid/$UUID_ROOT" 20
mount -U "$UUID_ROOT" /local_root
# get size of /local_root:
rootM=`df -BM /local_root/ | awk 'FNR==2 {print int($3)}'`
ROOT_M=`df -BM /local_root/ | awk 'FNR==2 {print int($3)}'`

# mount boot partition (if exists) to /local_boot:
if [ -n "$bootUUID" ]; then
poll_device "/dev/disk/by-uuid/${bootUUID}" 20
mount -U "${bootUUID}" /local_boot
if [ -n "$UUID_BOOT" ]; then
poll_device "/dev/disk/by-uuid/$UUID_BOOT" 20
mount -U "$UUID_BOOT" /local_boot
# add size of /local_boot:
rootM=$(( $rootM + `df -BM /local_boot/ | \
ROOT_M=$(( $ROOT_M + `df -BM /local_boot/ | \
awk 'FNR==2 {print int($3)}'` ))
fi

# check and print available memory:
echo ":: Total RAM available : ${ramM}M"
echo ":: Root filesystem size : ${rootM}M"
diffM=$(( $ramM - $rootM ))
if [ $diffM -lt 500 ]; then
echo ":: Total RAM available : ${RAM_M}M"
echo ":: Root filesystem size : ${ROOT_M}M"
RAM_EXTRA_M=$(( $RAM_M - $ROOT_M ))
if [ $RAM_EXTRA_M -lt 500 ]; then
echo ":! Not enough RAM available."
inputYN='n'
INPUT='n'

# prompt [Y/n] to load filesystem to RAM:
elif [ "$promptDefault" = 'yes' ]; then
elif [ "$LOAD_DEFAULT" = 'yes' ]; then
echo -n ">: Load root filesystem to RAM? [Y/n] "
inputYN='y'
read -s -r -t $promptTimeout -n 1 inputYN
if [ "${inputYN}" = 'n' ] || [ "${inputYN}" = 'N' ] || \
[ "${inputYN}" = 'q' ] || [ "${inputYN}" = 'Q' ]; then
inputYN='n'
INPUT='y'
read -s -r -t $LOAD_TIMEOUT -n 1 INPUT
if [ "$INPUT" = 'n' ] || [ "$INPUT" = 'N' ] ||
[ "$INPUT" = 'q' ] || [ "INPUT" = 'Q' ]; then
INPUT='n'
else
inputYN='y'
INPUT='y'
fi
# prompt [y/N] to load filesystem to RAM:
else
echo -n ">: Load root filesystem to RAM? [y/N] "
inputYN='n'
read -s -r -t $promptTimeout -n 1 inputYN
if [ "${inputYN}" = 'y' ] || [ "${inputYN}" = 'Y' ]; then
inputYN='y'
INPUT='n'
read -s -r -t $LOAD_TIMEOUT -n 1 INPUT
if [ "$INPUT" = 'y' ] || [ "INPUT" = 'Y' ] ; then
INPUT='y'
else
inputYN='n'
INPUT='n'
fi
fi

# copy root filesystem to RAM:
if [ "${inputYN}" = 'y' ]; then
if [ "$INPUT" = 'y' ]; then
echo "yes"
echo ":: Copying root filesystem to RAM..."

# enable zRAM devices:
if [ ! -b "/dev/zram0" ]; then
modprobe zram num_devices=$(nproc)
fi

# make zRAM partition using half of available free RAM (max +6G)
addM=$(( $diffM / 2 ))
if [ $addM -gt 6000 ]; then
addM=6000
else
addM=$(( $diffM / 2 ))
ROOT_ADD_M=$(( $RAM_EXTRA_M / 2 ))
if [ $ROOT_ADD_M -gt 6000 ]; then
ROOT_ADD_M=6000
fi
zramM=$(( $rootM + $addM ))
zdevice=$(zramctl -f -s "${zramM}M" -a lzo -t $(nproc))
mkfs.ext4 -q "$zdevice"
ZRAM_M=$(( $ROOT_M + $ROOT_ADD_M ))
ZRAM_DEVICE=$(zramctl -f -s "${ZRAM_M}M" -a lzo -t $(nproc))
mkfs.ext4 -q "$ZRAM_DEVICE"

# mount zRAM partition to /zram_root:
mount "$zdevice" /zram_root
export zdevice
mount "$ZRAM_DEVICE" /zram_root
export ZRAM_DEVICE
# copy files from local root to zRAM root:
cp -a /local_root/* /zram_root/

Expand All @@ -97,30 +96,29 @@ run_hook() {
mv /zram_root/etc/fstab /zram_root/etc/fstab~
fi
# use other mount points from /etc/fstab normally:
if [ "$fstabMount" = 'true' ] && [ -f /zram_root/etc/fstab~ ]; then
if [ "$FLAG_FSTAB" = 'true' ] && [ -f /zram_root/etc/fstab~ ]; then
sed 's@\(.* /boot .*\)@#\1@g; s@\(.* / .*\)@#\1@g;' \
/zram_root/etc/fstab~ > /zram_root/etc/fstab
fi
# use custom /etc/issue file:
if [ "$issueRAM" = 'true' ] &&
if [ "$FLAG_ISSUE" = 'true' ] &&
[ -f /zram_root/usr/lib/ramroot/etc/issue ]; then
if [ -f /zram_root/etc/issue ]; then
mv /zram_root/etc/issue /zram_root/etc/issue~
fi
cp /zram_root/usr/lib/ramroot/etc/issue /zram_root/etc/issue
chmod +x /zram_root/etc/issue
fi

# copy files from local boot partition (if exists) to zRAM boot:
if [ -n "$bootUUID" ]; then
if [ -n "$UUID_BOOT" ]; then
mkdir -p /zram_root/boot
cp -a /local_boot/* /zram_root/boot/
fi
# unmount zRAM root:
umount /zram_root
# set new mount_handler to use zRAM device:
oroot_mount() {
mount "$zdevice" "$1"
mount "$ZRAM_DEVICE" "$1"
}
# don't perform filesystem check on new zRAM partition:
fsck_root() {
Expand All @@ -136,7 +134,7 @@ run_hook() {

# unmount /local_root and /local_boot:
umount /local_root
if [ -n "$bootUUID" ]; then
if [ -n "$UUID_BOOT" ]; then
umount /local_boot
fi
}
2 changes: 1 addition & 1 deletion lib/man/ramroot.8
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH RAMROOT 8 "December 2017" "ramroot 1.1.7" "Ramroot Manual"
.TH RAMROOT 8 "January 2018" "ramroot 1.1.8" "Ramroot Manual"
.SH NAME
ramroot \- load root filesystem entirely to RAM during boot
.SH SYNOPSIS
Expand Down
Loading

0 comments on commit 6cf6877

Please sign in to comment.