Difference between revisions of "Suspend"

From LinuxMCE
Jump to: navigation, search
(removed unnecessary stuff to stop devices, script now does this)
Line 163: Line 163:
 
Edit the /etc/hibernate/blacklisted-modules, and comment the line:
 
Edit the /etc/hibernate/blacklisted-modules, and comment the line:
 
  #nvidia
 
  #nvidia
 
 
== Enable wake on lan ==
 
 
To enable wake on lan, to let the core wake up the MD, you have to run a command to enable this before going to sleep. The normal shutdown process does this, but this is not run when suspending the computer. Hibernate provides a way of including our own scripts before suspending.
 
 
Add a file (i.e. ''enableWOL'') to the /etc/hibernate/scriptlets.d/ folder:
 
AddSuspendHook 15 EnableWOL
 
 
EnableWOL() {
 
    ethtool -s eth0 wol g
 
}
 
  
  
Line 199: Line 187:
 
     /etc/init.d/lirc start
 
     /etc/init.d/lirc start
 
  }
 
  }
 
== Current problems ==
 
'''This can be solved using the resume and suspend scripts described in pm-utils setup above'''
 
 
The devices running on the resumed MD will not receive any messages from the DCERouter. This has to do with the TCP connections being broken after a period of suspension, or if the core has been restarted or the router reloaded.
 
 
To "fix" this, it is possible to restart the devices on the MD at resume. Add a file to the /etc/hibernate/scriptlets.d/ folder. The content should look something like this:
 
 
AddResumeHook 15 RestartDevices
 
 
RestartDevices() {
 
    killall -s SIGKILL App_Server
 
    killall -s SIGKILL Xine_Player
 
    killall -s SIGKILL MythTV_Player
 
    killall -s SIGKILL LIRC_DCE
 
# possible also add these
 
    killall -s SIGKILL External_Media_Identifier
 
    killall -s SIGKILL Mplayer_Player
 
    killall -s SIGKILL SimplePhone
 
    killall -s SIGKILL Disk_Drive
 
    killall -s SIGKILL HAL
 
}
 
 
Add a new line for other devices that you might have running on your MD.
 
  
 
== Suspend to RAM ==
 
== Suspend to RAM ==

Revision as of 19:08, 13 March 2009


This article describes how to suspend your MD to RAM (standby) or to disk (hibernation).

Please note that this functionality is not yet integrated into LinuxMCE, and will thus need some editing of Linux configuration files. Integration of the suspend functions has not been reported in the new issue-tracking system. The old Mantis-ID is 3822.

Ubuntu 710 removed the s2ram utility used to suspend-to-ram. But suspend to disk may still work, depending on your hardware and drivers. It might also be possible to use the s2both command, which is supposed to suspend to RAM and at the same time write the image to disk, to have a backup in case of power-out.

Ubuntu 810 uses pm-utils as the standard suspend/resume framework. Using this is recommended. It will also work with 710. Description of how to use pm-utils are below.

Motivation

  • Saving power
  • Quick boot (usefull if your MD is noisy and you turn it off when not in use)

Setup using PM-Utils

PM-utils are the new package in Ubuntu 810 that are responsible for suspending and resuming your computer. It uses the HAL database to find information about what special tricks it need to pull off to get your particular hardware to successfully resume.

The setup for pm-utils was tested after completing the hibernate setup below, so you may have to include some parts of that to get this to work. In particular the NVidia part may be required.

Install pm-utils

apt-get install pm-utils

Create a file /etc/pm/sleep.d/01lmce

#!/bin/bash

. /usr/lib/pm-utils/functions

RETVAL=0
case "$1" in
        hibernate|suspend)
                /usr/pluto/bin/suspend.sh
                RETVAL=$?
                ;;
        thaw|resume)
                /usr/pluto/bin/resume.sh
                ;;
        *)
                ;;
esac

exit $RETVAL

Create a file /usr/pluto/bin/resume.sh

#!/bin/bash

/usr/bin/screen -d -m -S "LMCE Launch Manager" /usr/pluto/bin/lmce_launch_manager.sh

Create a file /usr/pluto/bin/suspend.sh

#!/bin/bash

. /usr/pluto/bin/Config_Ops.sh
. /usr/pluto/bin/SQL_Ops.sh
. /usr/pluto/bin/pluto.func

echo "LMCE Suspend Process Started"

## Turn on WOL

/usr/pluto/bin/enable_wol.sh

## Find local IP address

LocalIP=$(ip addr show dev eth0|grep "inet "|cut -f6 -d" "|cut -f1 -d/)

## Use local IP address to find MD device number

FindMDDeviceQ="SELECT PK_Device FROM Device
		WHERE IPAddress='$LocalIP';
"

DeviceID=$(RunSQL "$FindMDDeviceQ")

## Identify immediate children DCE devices that are currently Registered
	
FindChildrenQ="SELECT Device.PK_Device
		FROM Device
		JOIN DeviceTemplate ON Device.FK_DeviceTemplate=DeviceTemplate.PK_DeviceTemplate
		LEFT JOIN Device AS Device_Parent on Device.FK_Device_ControlledVia=Device_Parent.PK_Device
		LEFT JOIN DeviceTemplate AS DeviceTemplate_Parent
		ON Device_Parent.FK_DeviceTemplate=DeviceTemplate_Parent.PK_DeviceTemplate
		WHERE (Device.FK_Device_ControlledVia=$DeviceID
		OR (Device_Parent.FK_Device_ControlledVia=$DeviceID AND DeviceTemplate_Parent.FK_DeviceCategory IN (6,7,8) ) )
		AND DeviceTemplate.FK_DeviceCategory <> 1
		AND DeviceTemplate.ImplementsDCE=1
		AND Device.Registered=1;
"

DeviceList=$(RunSQL "$FindChildrenQ")

## Send SYSCOMMAND_0 (device shutdown) message to all immediate children. These devices will relay the message to all decendents

for Device in $DeviceList; do

	/usr/pluto/bin/MessageSend "$DCERouter" 0 "$Device" 7 0 163 "start_local_devices"

done

## Send SYSCOMMAND_0 message to MD device itself
/usr/pluto/bin/MessageSend dcerouter 0 $DeviceID  7 0 163 "start_local_devices"


## Wait for DCE devices to complete their shutdown

MaxLoopCount=50
for ((i = 0; i < MaxLoopCount; i++)); do
	Devices=$(cat /usr/pluto/locks/pluto_spawned_local_devices.txt | grep -v '^$' | tr '\n' ',')
	Devices="${Devices%,}"
	 if [[ -z "$Devices" ]]; then
		break
	fi
	RegCount=0
	Q="SELECT COUNT(*) FROM Device WHERE PK_Device IN ($Devices) AND Registered=1"
	RegCount=$(RunSQL "$Q")
	if [[ "$RegCount" -eq 0 ]]; then 
		break
	fi
	echo "Waiting for $RegCount devices to shutdown ($Devices)"
	sleep 1
done
                                                                                                                       
echo "Done waiting"

## Kill the LM processes
                                                                                                                        
killall -q -s SIGKILL -r lmce_launch_manager\*

Make sure all three scripts are executable

chmod +x <script>


That's it. The setup is complete. You can try to suspend by typing pm-suspend. The suspend can take some time as LMCE is stopping the devices on your MD. If your hardware is supported, it will resume and restart lmce.

Setup using hibernate

It is now recommended to use the pm-utils way of suspending. This way may still work though.

On the MD in question, make sure you have the uswsusp and hibernate packages installed.

apt-get install uswsusp hibernate

Check your BIOS setting to make sure that suspend to ram is enabled.

Using NVidia graphics card

Edit your xorg.conf file and add this line to the "Device" section:

Option "NvAGP" "1"

Also make sure no other vendor AGP modules are loaded, like intel_agp, sis_agp etc. The agpgart module is fine, though. Use

lspci | grep agp

to list all loaded agp modules.

If you see any agp modules besides agpgart, add a line to /etc/modprobe.d/blacklist to blacklist the module.

Edit the /etc/hibernate/ram.conf file, and comment the two lines

#EnableVbeTool yes
#VbetoolPost yes

Edit the /etc/hibernate/blacklisted-modules, and comment the line:

#nvidia


Lirc module unloading and reloading

Not needed for 710

Some lirc modules (at least lirc_serial) needs reloading after suspend. Like for enabling wol, we add a file in the /etc/hibernate/scriptlets.d/ folder, with a descriptive name lirc.

# Stops and unloads the current lirc module before suspending, and
# restarts lirc when resuming

AddSuspendHook 30 UnloadLirc
AddResumeHook 30 StartLirc

UnloadLirc() {
    /etc/init.d/lirc stop
    if [ -f /etc/lirc/hardware.conf ];then
        . /etc/lirc/hardware.conf
        rmmod $MODULES
    fi
}

StartLirc() {
    /etc/init.d/lirc start
}

Suspend to RAM

Run

hibernate-ram

to suspends the MD to ram. The script first checks if it recognizes your hardware, to take the correct actions before suspending. If you get a message that your machine is unknown, edit the /etc/hibernate/ususpend-ram.conf file, and un-comment the

USuspendRamForce yes

line, to force it to suspend anyway.

Suspend to disk / Hibernation

Before trying to set up suspend to disk, I recommend that you first get suspend to ram working. All setup needed for suspend to RAM is also required for suspend to disk.

To enable suspend to disk, you will need to set up a swap partition on the MD in question. This is because the suspend implementation in the current kernel can only save the memory to the swap area. To achieve this on a diskless MD, you might use a CF card and a CF/IDE adapter. A CF card with 1 or 2 GB should be sufficient. However, with a CF card, you should make sure your MD has enough physical ram to avoid using swap, because CF cards may have lower performance than a normal IDE disk.


Edit the /etc/fstab file for the MD and add an entry for the swap partition

/dev/sda1 none            swap    sw              0       0

Adjust the partition to match your swap partition. Make sure you select the right one, or you WILL destroy your data. In the case of a diskless MD, you will probably only have one partition (a CF card for instance). You might need to do partitioning on the disk if it is a empty disk. This is outside the scope of this article.


Copy two files from /usr/share/initramfs-tools/scripts/local-premount/ (resume, uswsusp) into /etc/initramfs-tools/scripts/nfs-premount/ Not sure if both files are required, but it doesn't hurt.

These scripts will run before any file systems have been mounted, and after any disk drivers has been loaded. Both are important: mounting any file systems before resuming can corrupt your data, according to the documentation. And we need to have the drivers loaded to access the suspended image.


After copying the two files, run

update-initramfs -u

to update the initramfs image, which includes the scripts in the boot up process.


Now reboot your MD. When it has started up again, you can type

hibernate-disk

to initiate suspend to disk.

References

The hibernate script uses uswsusp by default when trying to suspend to ram. For more information about the s2ram utility used, read the S2ram howto

The NVidia suspend howto