[dev] Remount rootfs sync on impending battery depletion.

2010-12-04 Thread Antoni Grzymala
Hi,

just wanted to share a simple idea here – a little feature implemented
in my statusbar script: remount the rootfs sync, when the battery
level is below some predefined threshold and falling.

I somehow find it less distracting than warnings popping up and less
annoying then a forced poweroff upon reaching some level.

Here's my implementation (full of bash, gnu tools and other stuff you
love), feel free to pick ideas if you find any of it useful:

#!/bin/bash

while test 1; do


# Battery charge.

DO_BATTERY=""
BATTERY=BAT0 ### <- Battery ID

grep ' charged' /proc/acpi/battery/${BATTERY}/state 2>&1 >/dev/null && 
TIME_LEFT="" || DO_BATTERY=1

if [[ -n ${DO_BATTERY} ]]; then
  TIME_LEFT=`acpi | head -n1 | sed -r \
  's/^.*Battery//; s/discharging, /-/i; s/charging, /+/i; s/,//g; s/ 
([0-9][0-9]:[0-9][0-9]):[0-9][0-9] .*$/ \1 /'`

  if [[ "${TIME_LEFT}" =~ ^-[0-5]% ]]; then
TIME_LEFT="[fs SYNC] ${TIME_LEFT}"
grep '^/dev/root.*sync,' /proc/mounts || sudo mount / -o remount -o sync
  else
grep '^/dev/root.*sync,' /proc/mounts && sudo mount / -o remount
  fi

  TIME_LEFT="${TIME_LEFT} · "
fi

### [...much other code including some sleep...]

STATUS="${TKABBER}${UNREADMAIL}${TIME_LEFT}${PPPD}${WIRELESS}${FETCHMAIL}${UPTIME}${DATE}"
xsetroot -name "${STATUS}"

done

-- 
[a]



Re: [dev] Remount rootfs sync on impending battery depletion.

2010-12-04 Thread Gene Auyeung
Hi,

Just curious, how would the script get permission to mount? You can't
type the password for sudo because it's not interactive. And you can't
rely on the limited time that sudo grants access if you run the script
with sudo, because presumably the battery lasts much longer than that?

Thanks,
Gene

On Sat, Dec 4, 2010 at 11:16 AM, Antoni Grzymala  wrote:
> Hi,
>
> just wanted to share a simple idea here – a little feature implemented
> in my statusbar script: remount the rootfs sync, when the battery
> level is below some predefined threshold and falling.
>
> I somehow find it less distracting than warnings popping up and less
> annoying then a forced poweroff upon reaching some level.
>
> Here's my implementation (full of bash, gnu tools and other stuff you
> love), feel free to pick ideas if you find any of it useful:
>
> #!/bin/bash
>
> while test 1; do
>
> 
> # Battery charge.
>
> DO_BATTERY=""
> BATTERY=BAT0 ### <- Battery ID
>
> grep ' charged' /proc/acpi/battery/${BATTERY}/state 2>&1 >/dev/null && 
> TIME_LEFT="" || DO_BATTERY=1
>
> if [[ -n ${DO_BATTERY} ]]; then
>  TIME_LEFT=`acpi | head -n1 | sed -r \
>  's/^.*Battery//; s/discharging, /-/i; s/charging, /+/i; s/,//g; s/ 
> ([0-9][0-9]:[0-9][0-9]):[0-9][0-9] .*$/ \1 /'`
>
>  if [[ "${TIME_LEFT}" =~ ^-[0-5]% ]]; then
>    TIME_LEFT="[fs SYNC] ${TIME_LEFT}"
>    grep '^/dev/root.*sync,' /proc/mounts || sudo mount / -o remount -o sync
>  else
>    grep '^/dev/root.*sync,' /proc/mounts && sudo mount / -o remount
>  fi
>
>  TIME_LEFT="${TIME_LEFT} · "
> fi
>
> ### [...much other code including some sleep...]
>
> STATUS="${TKABBER}${UNREADMAIL}${TIME_LEFT}${PPPD}${WIRELESS}${FETCHMAIL}${UPTIME}${DATE}"
> xsetroot -name "${STATUS}"
>
> done
>
> --
> [a]
>
>



Re: [dev] Remount rootfs sync on impending battery depletion.

2010-12-04 Thread Troels Henriksen
Gene Auyeung  writes:

> Hi,
>
> Just curious, how would the script get permission to mount? You can't
> type the password for sudo because it's not interactive. And you can't
> rely on the limited time that sudo grants access if you run the script
> with sudo, because presumably the battery lasts much longer than that?

sudo can be configured to permit certain users (such as your user
account) to run specific commands (such as mount) without prompting for
a password.  It's a rather flexible tool.

-- 
\  Troels
/\ Henriksen



Re: [dev] Remount rootfs sync on impending battery depletion.

2010-12-04 Thread Antoni Grzymala
Gene Auyeung dixit (2010-12-04, 11:23):

> Just curious, how would the script get permission to mount? You can't
> type the password for sudo because it's not interactive. And you can't
> rely on the limited time that sudo grants access if you run the script
> with sudo, because presumably the battery lasts much longer than
> that?

Simplest way:

  ALL=(ALL) NOPASSWD: ALL

The safer way would obviously be if you limit this to /bin/mount.

Regards,

-- 
[a]



Re: [dev] Remount rootfs sync on impending battery depletion.

2010-12-04 Thread Gene Auyeung
On Sat, Dec 4, 2010 at 11:29 AM, Antoni Grzymala  wrote:

> Simplest way:
>
>   ALL=(ALL) NOPASSWD: ALL
>
> The safer way would obviously be if you limit this to /bin/mount.

Ah yes I forgot about that. It's more configuration, but I guess
there's no getting around that.

Thanks,
Gene