power.sh should allow laptop_mode to do it's thing

Bug #74394 reported by Roy Badami
12
Affects Status Importance Assigned to Milestone
acpi-support (Ubuntu)
Fix Released
Undecided
Unassigned

Bug Description

Binary package hint: acpi-support

In 6.06.1 LTS, /etc/acpi/power.sh explicitly calls

    laptop_mode start

and

    laptop_mode stop

This means that the preferences specified in /etc/laptop-mode/laptop-mode.conf are not honoured.

Instead, power.sh should simply call

    laptop_mode auto

in both cases. That way the user's preferences as to when laptop mode will be used will be honoured.

The specific bug that was hitting me was that laptop mode was being disabled when AC power was reconnected, despite having set ENABLE_LAPTOP_MODE_ON_AC to 1 in laptop-mode.conf.

Also, power.sh is explicitly changing the hard drive spindown and power management behaviour by calling hdparm (with fixed, uncofigurable values). It should leave this to laptop_mode, which already has completely configurable functionality to do this.

Patch against 6.06.1 LTS:

--- power.sh.orig 2006-12-04 14:54:44.000000000 +0000
+++ power.sh 2006-12-04 15:33:17.000000000 +0000
@@ -3,36 +3,6 @@
 . /etc/default/acpi-support
 . /usr/share/acpi-support/power-funcs

-function laptop_mode_enable {
- $LAPTOP_MODE start
-
- for x in /sys/bus/ide/devices/*/block; do
- drive=$(basename $(readlink $x));
- $HDPARM -S 12 /dev/$drive 2>/dev/null
- $HDPARM -B 1 /dev/$drive 2>/dev/null
- done
-
- for x in /sys/bus/scsi/devices/*/block; do
- drive=$(basename $(readlink $x));
- $HDPARM -S 12 /dev/$drive 2>/dev/null
- $HDPARM -B 1 /dev/$drive 2>/dev/null
- done
-}
-
-function laptop_mode_disable {
- for x in /sys/bus/ide/devices/*/block; do
- drive=$(basename $(readlink $x));
- $HDPARM -S 0 /dev/$drive 2>/dev/null
- $HDPARM -B 255 /dev/$drive 2>/dev/null
- done
- for x in /sys/bus/scsi/devices/*/block; do
- drive=$(basename $(readlink $x));
- $HDPARM -S 0 /dev/$drive 2>/dev/null
- $HDPARM -B 255 /dev/$drive 2>/dev/null
- done
- $LAPTOP_MODE stop
-}
-
 getState;

 checkStateChanged;
@@ -46,15 +16,12 @@
        for SCRIPT in /etc/acpi/battery.d/*.sh; do
            . $SCRIPT
        done
- if [ x$ENABLE_LAPTOP_MODE = xtrue ]; then
- (sleep 5 && laptop_mode_enable)&
- fi
     else
        for SCRIPT in /etc/acpi/ac.d/*.sh; do
            . $SCRIPT
        done
- if [ x$ENABLE_LAPTOP_MODE = xtrue ]; then
- (sleep 5 && laptop_mode_disable)&
- fi
     fi
-done
\ No newline at end of file
+ if [ x$ENABLE_LAPTOP_MODE = xtrue ]; then
+ (sleep 5 && $LAPTOP_MODE auto)&
+ fi
+done

Revision history for this message
James Stembridge (jstembridge) wrote :

Both of these issues still exist in Feisty. Is there anything blocking the supplied path being applied?

Revision history for this message
Jason (jasonxh) wrote :

Indeed! I was just about to report this bug and found this one. Current power.sh does not respect configurations in laptop-mode.conf on: when to enable, hdparm -B value, and hdparm -S value. Why no just leave decisions to laptop-mode.conf and make things NOT that ambiguous!

Revision history for this message
Jason (jasonxh) wrote :

Forgot to mention that I'm using Feisty.

Revision history for this message
Tormod Volden (tormodvolden) wrote :

AFAICS, this is the same in Hardy. I am not sure how much power.sh is in use, but it looks wrong as it is.

description: updated
Changed in acpi-support:
status: New → Confirmed
Revision history for this message
chourave (gaston72) wrote :

I agree with Roy description.

Everything worked fine under Gutsy with kernel 2-6-22-14, I have just upgraded my kernel : installing 2-6-24-16.
It fixes my resume problem, but now laptop-mode is broken.

1. In order to control everything using laptop-mode.conf, I used to enable laptop-mode both on AC and battery. Now it's impossible.
2. after resume, the hdparm settings are overwritten and the hard disk problem comes back (Load_Cycle_Count issue)

I'm becoming crazy with the behaviour of power.sh :
- the laptop-mode setting are overwritten by power.sh because of the definition of laptop_mode_enable or laptop_mode_disable functions.

- if you use SCRIPT in /etc/acpi/ac.d/ or /etc/acpi/battery.d/ :
The loop execute them, but their action will be cancelled by the call to laptop_mode_enable or laptop_mode_disable...

For the resume problem, the solution proposed here :
https://bugs.launchpad.net/ubuntu/+source/acpi-support/+bug/89269/comments/18
doesn't work because you always call power.sh, which sets wrong parameter values

As far as I understand, the solution proposed by Roy is the good one :
modifying power.sh : i.e. replacing the "laptop_mode_enable" or "laptop_mode_disable" functions by simple calls to laptop-mode auto (/usr/sbin/laptop_mode auto)

It could be implemented as follow :
- First : testing if laptop-mode is installed, and then applying parameter

getState;
checkStateChanged;

if laptop-mode is installed :
    laptop-mode auto
    if ac
        loop on the script .../ac.d
    elseif battery
        loop on the script .../battery.d
    end

else : laptop mode isn't installed : power.sh imposes the settings
    if ac
        ac default hdparm values
        loop on the script .../ac.d
    elseif battery
        battery default hdparm values
        loop on the script .../battery.d
    end
end..

Could it be right ? Does somebody knows how to do it safely and properly ?

See also (to be continued..)
https://bugs.launchpad.net/ubuntu/+source/acpi-support/+bug/59695/comments/273
https://bugs.launchpad.net/ubuntu/+source/acpi-support/+bug/229693/comments/1
https://bugs.launchpad.net/ubuntu/+source/laptop-mode-tools/+bug/80331/comments/1
https://bugs.launchpad.net/ubuntu/+source/laptop-mode-tools/+bug/172282/comments/1
....

Revision history for this message
Tormod Volden (tormodvolden) wrote :

Yes, this is horribly broken in Ubuntu 8.04. I guess it would help if power.sh does its settings _before_ it calls laptop_mode, so that laptop_mode's settings will prevail. Please test the patch and report whether it helps for you.

Revision history for this message
chourave (gaston72) wrote :

Sorry Tormod, your script seems not be complete...

In fact power.sh doen't really call laptop mode, it call two functions defined at the beginning of the script: laptop_mode_enable and laptop_mode_disable

Revision history for this message
chourave (gaston72) wrote : Fixing hdparm parameter value and laptop-mode issue when switching power supply (AC/battery)

Fixing hdparm parameter value and laptop-mode issue when switching power supply (AC/battery)

Bug feature : When plugging on and off the ac adapter the laptop-mode parameters are not taken into account.
- Even if the command "cat /proc/sys/vm/laptop_mode" give a correct value (2 or more) the hdparm parameter are not set in agreement with /etc/laptop-mode/laptop-mode.conf settings.

Consequence :
- the Load_Cycle_Count is not controled anymore by user settings (laptop-mode.conf)
- setting "ENABLE_LAPTOP_MODE = true" in "/etc/acpi/acpi-support" corresponds to a laptop mode controled by the power.sh script (during supply switching, the boot seems to be ok), not by laptop-mode-tools.
- the setting "ENABLE_LAPTOP_MODE_ON_AC=1" in "laptop-mode.conf" is not taken into account when switching AC/battery

Cause : The /etc/acpi/power.sh script is called at each power event notification (acpi event).
The hdparm parameters are overwritten by this script, which doesn't reload laptop-mode.conf settings.

Solution : Modification of power.sh : the solution 2 is the more efficient, it works for me

solution 1 :
- set the good values of hdparm -S and -B in power.sh, without any other modification
BUT : the boot values seems to be always controled by laptop-mode.conf and ENABLE_LAPTOP_MODE_ON_AC=1" is not taken into account after switching.

solution 2 : see the first attachment power.sh
- "ENABLE_LAPTOP_MODE = true" call systematically laptop-mode : "/usr/sbin/laptop_mode auto"
- "ENABLE_LAPTOP_MODE = false" means laptop-mode on battery and no laptop-mode on AC with the hdparm parameters are controled by power.sh. (it is OK for a laptop, but there is no way shutdown completely laptop mode)

solution 3 : not tested
If laptop-mode-tools are installed by default, the best solution could be to load laptop-mode.conf setting and to test ENABLE_LAPTOP_MODE_ON_BATTERY and ENABLE_LAPTOP_MODE_ON_AC in power.sh

thus, "ENABLE_LAPTOP_MODE=true" could recover it's real meaning (?) :

1. if "ENABLE_LAPTOP_MODE=true"
 - by default laptop-mode is disabled on AC, and enabled on battery, using default setting present in power.sh
 - if laptop-mode tools are intalled : power.sh reloads laptop-mode each time occurs an acpi event
2. if "ENABLE_LAPTOP_MODE=false"
 - just executing the script in /etc/acpi/ac.d or /etc/acpi/battery.d,

BUT :
- I don't know if laptop-mode package is installed by default
- It would simpler to add hdparm settings in acpi-support...
- the solution 2 works well, it's enougth for me, I have no more time to do this...

Useful command : testing current hdparm setting
sudo hdparm -I /dev/sda | grep 'Advanced'

Revision history for this message
chourave (gaston72) wrote : Fixing hdparm parameter value and laptop-mode issue on resume

Fixing hdparm parameter value and laptop-mode issue on resume

Bug : when resuming from suspend laptop-mode.conf setting are not respected

Consequences : the same as in the previous case : laptop-mode.conf settings are not applied

Cause : nothing is done concerning laptop-mode during resume.

False solution ?? : I'm using pm-utils, and when I add SERVICES="laptop-mode" in "/etc/acpi/acpi-support", it doesnt't help, I don't understand why (laptop-mode should be restarted on resume).
But, It seem to work (SERVICES="laptop-mode") when using "/etc/apci/sleep.sh force" (so It may work using default suspend function of gnome?...)

-------------------

Real solutions :
With solution 1 and 2, you have to be carreful to the power.sh script, which has also to be modified.
With solution 3, nothing has to be done to power.sh (but see previous post...)

--------------------

Solution 1 : when using pm-utils, the following script can be added :
(proposed by Valentin : https://bugs.launchpad.net/ubuntu/+source/acpi-support/+bug/89269/comments/18)

2. cd /usr/lib/pm-utils/sleep.d
3. sudo gedit 99-power-state-fix.sh
4. paste this:

#!/bin/bash
/usr/bin/on_ac_power
if [ "$?" -eq 0 ]; then
 echo "BATTERY" > /var/lib/acpi-support/powerstate
 /etc/acpi/power.sh ac_adapter
else
 if [ "$?" -eq 1 ]; then
  echo "AC" > /var/lib/acpi-support/powerstate
  /etc/acpi/power.sh battery
 fi
fi

5. save & close
6. sudo chmod 755 99-power-state-fix.sh

-------------------

Solution 2 : using the default suspend of gnome (script /etc/apci/sleep.sh ?) :
Adding the same script in : /etc/acpi/resume.d/ (and maybe /etc/acpi/suspend.d/)

-------------------

Solution 3 : using the default suspend of gnome (script /etc/apci/sleep.sh ?) :
Adding a script called (for example) "99-hdd-spin-fix.sh" in the following directories:
/etc/acpi/start.d/
/etc/acpi/suspend.d/
/etc/acpi/resume.d/
which simply contains hdparm setting :
#!/bin/sh
hdparm -B 255 /dev/sda

BUT : with this solution no difference is done between AC and battery on resume (concerning hdparm setting)
-------------------

Solution 1 work fine for me combined with the power.sh script I proposed in the previous post.

Revision history for this message
Tormod Volden (tormodvolden) wrote :

chourave, I didn't attach a new script, but a patch for it. A patch only includes the changes, and you have to apply it with:
 sudo patch -i power.sh.patch /etc/apci/power.sh"
(or just hand-edit the power.ch yourself, and move "$LAPTOP_MODE start" to the bottom of the laptop_mode_enable function)

Revision history for this message
chourave (gaston72) wrote :

ok Tormod, thank you for patching my linux knowledge !!

Your solution is simpler than mine : I have an additionnal logical condition evaluation to distinguish between the laptop-mode script and the laptop_mode_enable function

if [ x$ENABLE_LAPTOP_MODE = xtrue ]; then
     (sleep 5 && /usr/sbin/laptop_mode auto)&
else
     (sleep 5 && laptop_mode_enable)&
fi

But , with your patch, the hdparm settings are applied two times :
- first, the default setting :
 $HDPARM -S $SPINDOWN_TIME /dev/$drive 2>/dev/null
 $HDPARM -B 1 /dev/$drive 2>/dev/null
- and then, with the the call to $LAPTOP_MODE start, which use the laptop-mode.conf parameters

Revision history for this message
Tormod Volden (tormodvolden) wrote :

Yes, they will be applied twice, but that shouldn't be a problem. If we want to have this changed in Hardy (StableReleaseUpdate) then the change has to be minimal. These things have been changed again upstream and will look different in Intrepid. Which reminds me that I already have installed the Intrepid version (because of bug #226069), so my above analysis and patch applies to that version, oops! I will have to verify again if it's the same in the Hardy version.

Revision history for this message
Tormod Volden (tormodvolden) wrote :

Never mind, it was pm-utils I had upgraded, and not acpi-support. Please also see bug #89269, I wonder if this bug is a duplicate of that one.

Revision history for this message
Tormod Volden (tormodvolden) wrote :

Daniel Hahler, you worked on this AFAICS: Doesn't it make sense to call $LAPTOP_MODE start _after_ the hdparm settings in /etc/acpi/power.sh?

Revision history for this message
Roy Badami (roy-gnomon) wrote :

AFAICS you should *never* call $LAPTOP_MODE start (or stop). You should always use $LAPTOP_MODE auto (which will then honour the config file preferences).

-roy

Revision history for this message
Tormod Volden (tormodvolden) wrote :

Yes, upstream went away from using start/stop to only use "auto" some time ago. But a patch was kept in the Ubuntu version to retain the start/stop options, probably because of interactions with acpi-support. I am not sure if this patch is needed any longer. Testing would be welcome, as well as comments from for instance Matthew who I think was pretty involved in these changes.

Revision history for this message
Jason (jasonxh) wrote :

Seems this old bug has regained a lot of attention these days. I'm just wondering why the original patch by Roy is kept back, for so long. The logic is crystal clear and It works perfectly well. And this bug still doesn't have an assignee?

Revision history for this message
Alexey Borzenkov (snaury) wrote :

I was applying a similar patch for a while now and didn't have any problems (of course you'd have to apply my patch from bug #239419). I was going to finally report it, but found this bug. It is shocking that this wasn't fixed for 1.5 years. O.o

Oh well, if developers don't care I guess we'll have to continue to patch it ourselves.

Revision history for this message
Joss Winn (josswinn) wrote :

Running Hardy on a Dell D430.

I've installed the laptop-mode-tools upstream deb package from the maintainer's website on his suggestion. It's seems to be widely acknowledged that good laptop power management in Ubuntu is broken.

With the upstream Debian package, things appear to be working but I'm unsure what power.sh should now look like when using laptop-mode for power management. Most people suggest commenting out/removing the hdparm commands and changing $LAPTOP_MODE to auto.

Can anyone confirm that all that is needed is:

function laptop_mode_enable {
    $LAPTOP_MODE auto
}

Thank you
Joss

Revision history for this message
Tormod Volden (tormodvolden) wrote :

acpi-support (0.110) intrepid; urgency=low

  * Let laptop-mode-tools run properly (closes LP: #250938)
    - power.sh: remove laptop-mode invocations, should be handled by pm-utils
    - acpi-support: remove SPINDOWN_TIME, should be controlled by laptop-mode

Changed in acpi-support:
status: Confirmed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.