rendered paste body#!/bin/sh
# discover video card's ID
vID=`lspci | sed -e '/VGA/!d' -e 's/ .*//' -e 's@0000:@@' -e 's@:@/@' -eq`
aID=`lspci | sed -e '/audio/!d' -e 's/ .*//' -e 's@0000:@@' -e 's@:@/@' -eq`
# securely create a temporary file
vTMP_FILE=`mktemp /var/tmp/video_state.XXXXXX`
trap 'rm -f $vTMP_FILE' 0 1 15
aTMP_FILE=`mktemp /var/tmp/audio_state.XXXXXX`
trap 'rm -f $aTMP_FILE' 0 1 15
# switch to virtual terminal 1 to avoid graphics
# corruption in X
chvt 1
# synchronize system clock with hardware
/sbin/hwclock --directisa --localtime --systohc
# write all unwritten data (just in case)
sync
# dump current data from the video card to the
# temporary file
cat /proc/bus/pci/$vID > $vTMP_FILE
cat /proc/bus/pci/$aID > $aTMP_FILE
# suspend or hibernate
case "$1" in
suspend) /usr/sbin/s2ram ;;
hibernate) /usr/sbin/s2disk ;;
both) /usr/sbin/s2both ;;
esac
# restore video card data from the temporary file
# on resume
cat $vTMP_FILE > /proc/bus/pci/$vID
cat $aTMP_FILE > /proc/bus/pci/$aID
# synchronize hardware clock with system
/sbin/hwclock --directisa --localtime --hctosys
# switch back to virtual terminal 7 (running X)
chvt 5
# remove temporary file
rm -f $vTMP_FILE
rm -f $aTMP_FILE