From 6146d1904aad28f0bacbb6986205c28bb7020356 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 3 Dec 2015 16:03:29 +0000 Subject: [PATCH] ramdisk: Avoid copying data if no ramdisk is used Signed-off-by: Michael Tremer --- src/initscripts/init.d/collectd | 24 ++++------------ src/initscripts/init.d/functions | 48 +++++++++++++++++++++----------- src/initscripts/init.d/vnstat | 25 ++++------------- 3 files changed, 43 insertions(+), 54 deletions(-) diff --git a/src/initscripts/init.d/collectd b/src/initscripts/init.d/collectd index 24187d215..e5c35950d 100644 --- a/src/initscripts/init.d/collectd +++ b/src/initscripts/init.d/collectd @@ -13,20 +13,9 @@ fi case "$1" in start) if use_ramdisk; then - #mount ramdisk - if ! mountpoint $RRDLOG &>/dev/null; then - boot_mesg "Mount rrd Ramdisk..." - if [ $(find "$RRDLOG" | wc -l) -ne 1 ]; then - backup_ramdisk "$RRDLOG" - fi - mount_ramdisk "$RRDLOG" - evaluate_retval - - #restore old values to ramdisk if exist - restore_ramdisk "$RRDLOG" - fi - else - restore_ramdisk "$RRDLOG" + boot_mesg "Mounting RRD ramdisk..." + mount_ramdisk "${RRDLOG}" + evaluate_retval fi # If run from init and collectd alrady started then exit silent @@ -122,10 +111,9 @@ case "$1" in boot_mesg "Stopping Collection daemon..." killproc /usr/sbin/collectd evaluate_retval - backup_ramdisk "$RRDLOG" - umount_ramdisk "$RRDLOG" - # sync after backup... - sync + + # Umount the ramdisk (if any) + umount_ramdisk "${RRDLOG}" ;; restart) ${0} stop diff --git a/src/initscripts/init.d/functions b/src/initscripts/init.d/functions index bd2c946e0..fc4d8a4b9 100644 --- a/src/initscripts/init.d/functions +++ b/src/initscripts/init.d/functions @@ -743,29 +743,45 @@ use_ramdisk() { } mount_ramdisk() { - mount -t tmpfs none $1 -} + local path="${1}" + local path_tmpfs="${path}.tmpfs" -umount_ramdisk() { - if mountpoint $1 &>/dev/null; then - umount $1 + # Check if the ramdisk is already mounted + if mountpoint "${path}" &>/dev/null; then + return 0 fi -} -backup_ramdisk() { - if [ ! -e $1.bak ]; then - mkdir -p $1.bak - fi + # Create ramdisk + mkdir -p "${path_tmpfs}" + mount -t tmpfs none "${path_tmpfs}" + + # Restore ramdisk content + cp -pR "${path}/*" "${path_tmpfs}" - cp -pR $1/* $1.bak/ - rm -rf $1/* + # Move ramdisk to final destination + mount --move "${path_tmpfs}" "${path}" + rm -f "${path_tmpfs}" } -restore_ramdisk() { - if [ -e $1.bak ];then - cp -pR $1.bak/* $1/ - rm -rf $1.bak/* +umount_ramdisk() { + local path="${1}" + local path_tmpfs="${path}.tmpfs" + + # Check if a ramdisk is actually mounted + if ! mountpoint "${path}" &>/dev/null; then + return 0 fi + + # Move the ramdisk + mkdir -p "${path_tmpfs}" + mount --move "${path}" "${path_tmpfs}" + + # Backup ramdisk content + cp -pR "${path_tmpfs}/*" "${path}" + + # Destroy the ramdisk + umount "${path_tmpfs}" + rm -f "${path_tmpfs}" } # End $rc_base/init.d/functions diff --git a/src/initscripts/init.d/vnstat b/src/initscripts/init.d/vnstat index 2593afe4d..05c35eefc 100755 --- a/src/initscripts/init.d/vnstat +++ b/src/initscripts/init.d/vnstat @@ -13,31 +13,16 @@ fi case "$1" in start) if use_ramdisk; then - if ! mountpoint $VNSTATLOG &>/dev/null; then - boot_mesg "Mount vnstat ramdisk..." - if [ $(find "$VNSTATLOG" | wc -l) -ne 1 ]; then - backup_ramdisk "$VNSTATLOG" - fi - mount_ramdisk "$VNSTATLOG" - evaluate_retval - $0 restore - fi - else - restore_ramdisk "$VNSTATLOG" + boot_mesg "Mounting vnstat ramdisk..." + mount_ramdisk "${VNSTATLOG}" + evaluate_retval fi ;; stop) - $0 backup - umount_ramdisk "$VNSTATLOG" - ;; - backup) - backup_ramdisk "$VNSTATLOG" - ;; - restore) - restore_ramdisk "$VNSTATLOG" + umount_ramdisk "${VNSTATLOG}" ;; *) - echo "Usage: $0 {start|stop|backup}" + echo "Usage: $0 {start|stop}" exit 1 ;; esac -- 2.39.2