Apply ZFS driver settings without reboot

Apply your modprobe.d values without rebooting:

egrep -v '^#|^\s*$' zfs.conf \
| while read L; do
   M=($L)
   N=${M[2]}
   P=(${N/=/ })
   echo "${P[1]}" > /sys/module/zfs/parameters/${P[0]}
done

#zfs #linux #bash

Advertisements

Applying patches?

You know me, I love using bash.

for f in ~/Documents/jbr_patches/*diff  ; do \
   echo $f; patch -p1 < $f || exit 1; \
sleep 1 ; done

Right.

Keep that Laptop from Racing thru Battery

Linux and Laptops: a long, old story. However, one that with a bit homework, helps get the most out of your battery. Using powertop, you can see your energy usage profile and your device power settings. The downside to powertop is that you cannot "export as shell script." (Seriously, y u no export?) To get these power savings on boot, I wrote my own script.

  1 #!/bin/bash
  2 /usr/sbin/rfkill block 0 #bt
  3 /usr/sbin/rfkill block 1 #wifi
  4 /usr/sbin/rfkill block 2 #bt
  5 for i in /sys/class/scsi_host/host*/link_power_management_policy
  6 do
  7     echo "min_power" > $i
  8 done
  9 echo 0 > /proc/sys/kernel/nmi_watchdog
 10 echo 1 > /sys/module/snd_hda_intel/parameters/power_save
 11 echo 60000 > /proc/sys/vm/dirty_writeback_centisecs
 12 
 13 find /sys/devices/ \
 14 | grep 'power/control' \
 15 | while read i
 16 do
 17     echo -n "$i@"
 18     cat $i 2>/dev/nulll
 19 done \
 20 | grep -v auto \
 21 | awk -F@ '{print $1}' > /tmp/sleepy.tmp
 22 
 23 for i in `cat /tmp/sleepy.tmp`
 24 do
 25     echo "auto" > $i
 26 done
 27 
 28 for i in `find /sys -name autosuspend -exec echo {} \;`
 29 do
 30     echo "1" > $i
 31 done
 32 
 33 find /sys/devices/pci0000:00 -type f -iname autosuspend_delay_ms \
 34    -exec echo "1000" > {} \;
 35 
 36 for i in /sys/bus/usb/devices/usb*/power/autosuspend_delay_ms
 37 do
 38     echo "1000" > $i
 39 done
 40 
 41 for i in /sys/bus/usb/devices/usb*/power/autosuspend
 42 do
 43     echo "1" > $i
 44 done
 45 
 46 for i in /sys/bus/usb/devices/*/power/autosuspend
 47 do
 48     echo "1" > $i
 49 done
 50 
 51 webcamDev=$( lsusb | grep -i webcam | awk '{print $2 "-" $4 }' | tr -d ':0' )
 52 if [ ! -z "$webcamDev" ]
 53 then
 54     echo 1 > /sys/bus/usb/devices/$webcamDev/power/autosuspend
 55     echo auto > /sys/bus/usb/devices/$webcamDev/power/level
 56     echo 1000 > /sys/bus/usb/devices/$webcamDev/power/autosuspend_delay_ms
 57 fi
 58 
 59 # eof

The best way to run this script is to open two windows, one running powertop, the other for this script. After adjusting this script, type r in the powertop window to refresh the device stats. I was able to take my T60 from 30.1W down to somewhere between 19.8 – 16.1W. Unfortunately, I haven’t seen that 16.1W reading since.

I hope this serves as a good starting point for your own laptop power savings! And not just laptops, but this script can also be used for home NAS or media center or other small servers where you need thermal management, too.

Checking for your ssh-agent on login – updated

The first thing my .bash_aliases file does on login is to check if I’m running ssh-agent and if so, stick that into my shell environment. If not, kick it up, and update a reminder to it. This morning I found a flaw in that, so I believe this is the fix.

  4 export SSH_RECENT="$HOME/.ssh/recent"
  5 [ -f "$SSH_RECENT" ] && eval `cat $SSH_RECENT`
  6 RUNNING_AGENTS=0
  7 if [ ! -z "$SSH_AGENT_PID" ]
  8 then
  9    RUNNING_AGENTS=`pgrep -u $(id -u) ssh-agent | grep $SSH_AGENT_PID | wc -l`
 10 fi
 11 if [ $RUNNING_AGENTS -lt 1 -a $UID -ne 0 ]
 12 then
 13    eval `ssh-agent`
 14    echo "export SSH_AGENT_PID=$SSH_AGENT_PID" >    $SSH_RECENT
 15    echo "export SSH_AUTH_SOCK=$SSH_AUTH_SOCK" >>   $SSH_RECENT
 16 fi
 17 [ `ssh-add -l | fgrep -v ' no ' | wc -l` -eq 0 ] && ssh-add

ZFS, Bash, and Piped Loops

Fellow Linux Nutjobs! Here is your evening lesson in how to reclaim space from a ZFS partition by destroying snapshots. In my setup, my remote backup has a subset of the snapshots I’m automatically creating from my backup scripts. To reclaim space, I’m deleting those snapshots that have been backed up off-site.


Assume alias Zfs="sudo zfs" in the following example:

Zfs list -rHt snap tank/pictures \
| while read L ; do 
   [[ $L == "tank/pictures@20131008-191254"* ]] && break; 
   echo $L;
done \
| cut -d' ' -f1 \
| while read M ; do 
    Zfs destroy $M; 
done

Pop quiz. What other command could I use besides cut?

And guess what? I typed that command in almost one go, not pasted in from an editor. I’ve formatted it all fancy-like for your precious sanity.

Example of how Jed works his mojo.

Example of how Jed works his mojo.

Make, classpaths and environment variables

Make, Ant and probably any other build language (and/or toolkit, since Ant is not much in the way of a language) are tricky bastards. I’ve been maintaining a parallel set of build scripts for a Java and C++ project for a few years now, and this has been great practice in getting my Make chops snappier. Just today, I found I needed to build the -Xbootclasspath argument for my makefile. I ended up re-learning an important lesson that applies to bash. Not expectedly so, since Make and bash are not of common ancestry…but that lesson was “spaces.” One needs to put at least one space after Makes conditionals if, ifeq or ifneq. Just like one has to put space after bash’s if, while, and for.

  • Bash:
    if [ -z "$classpath" ] ; then do
       classpath="./*jar"
    done
  • Make:
    ifeq (,$(classpath))
       classpath="./*jar"
    endif
      Both those statements are checking for zero length strings.

However, I’m going to stop comparing the two. You just look at where I’ve put those spaces, yung’un.
My real goal was to evaluate a series of conditions (is there an JAVA6_HOME defined? Is there a $JAVA_HOME/../jdk6 directory? Is there /usr/local/jdk6 directory? If so create a BOOTCLASSPATH variable:

This is the trick:

  4 comma:= ,
  5 colon:= :
  6 empty:=
  7 space:= $(empty) $(empty)
  ...
 56 ifneq (,$(JAVA6_HOME))
 57    ifneq (,$(wildcard  $(JAVA6_HOME)/.))
 58       JAVA6 = ${JAVA6_HOME}
 59    endif
 60 endif
 61 ifeq (,$(JAVA6))
 62    ifneq (,$(wildcard $(JAVA_HOME)/../jdk6/.))
 63       JAVA6 = $(JAVA_HOME)/../jdk6
 64    endif
 65 endif
 66 ifeq (,$(JAVA6))
 67    ifneq (,$(wildcard /usr/local/jdk6/.))
 68       JAVA6 = "/usr/local/jdk6"
 69    endif
 70 endif
 71 ifneq (,$(JAVA6))
 72    BOOTCLASSPATH := $(wildcard $(JAVA6)/lib/*.jar)
 73    JAVATARGET = -target 1.6 \
 74                -source 1.6 \
 75                -Xbootclasspath/p:$(subst $(space),$(colon),$(BOOTCLASSPATH))
 76    $(info BOOTCLASSPATH is $(BOOTCLASSPATH))                                                                     
 77    $(info JAVATARGET    is $(JAVATARGET))
 78 endif

And the quiz for the reader is, why is line 75 important?