Schlagworte: snippet
15.03.11
Script to double/halve OpenVZ resources
The following script allows you to easily double or halve resources in an OpenVZ container.
You would install this script as "/usr/local/bin/vz-double-resources" and also create a symlink named "vz-half-resources" to it (probably also in /u/l/b).
This then allows you to just call "vz-double-resources 123 shmpages" in case you've been notified that there have been shmpages beancounter failures (resource limit has been hit).
This outputs the command to double the bean counter limit, and allows for easy execution by just forwarding the output to "sh", as in "vz-double-resources 123 shmpages | sh -".
Here's the script (available and maintained as/at Gist):
Code:
# cat =vz-double-resources | |
#!/bin/bash | |
| |
VZ="$1" | |
RESOURCE="$2" | |
| |
if [ -z $VZ ] || [ -z $RESOURCE ]; then | |
echo "Usage: $0 VZ RESOURCE" | |
exit 1 | |
fi | |
| |
case $0 in | |
*-double-*) OPERATION="*2" ;; | |
*-half-*) OPERATION="/2" ;; | |
*) echo "Invalid: $0" ; exit 1;; | |
esac | |
| |
echo "# Operation: $OPERATION" | |
| |
# get failures: | |
# awk '$NF ~ /[0-9]*[1-9]$/' /proc/bc/*/resources | |
| |
if [ -f /proc/bc/$VZ/resources ] ; then | |
# uid resource held maxheld barrier limit failcnt | |
| |
resource=$(echo $RESOURCE | tr '[:upper:]' '[:lower:]') | |
awk /$resource/ /proc/bc/$VZ/resources | { | |
read resource held maxheld barrier limit failcnt | |
if [ -n "$resource" ]; then | |
cmd="vzctl set $VZ --$resource $(($barrier $OPERATION)):$(($limit $OPERATION)) --save" | |
echo $cmd | |
exit | |
fi | |
} | |
fi | |
[[ -n "$cmd" ]] && exit | |
if [ -f /etc/vz/conf/$VZ.conf ] ; then | |
value=$(grep -i "^$RESOURCE=" /etc/vz/conf/$VZ.conf) | |
eval $value | |
RESOURCE=$(echo $RESOURCE | tr '[:lower:]' '[:upper:]') | |
resource=$(echo $RESOURCE | tr '[:upper:]' '[:lower:]') | |
value=$(eval echo \$${RESOURCE}) | |
echo $value | { | |
IFS=: read barrier limit | |
cmd="vzctl set $VZ --$resource $(($barrier $OPERATION)):$(($limit $OPERATION)) --save" | |
echo $cmd | |
} | |
else | |
echo "ERROR: container $VZ not found." | |
exit 1 | |
fi |
This is a script I've been using since quite a while (otherwise it would use zsh instead of bash), and have not looked into after creating it.
I just noticed that it is quite useful in general and therefore wanted to make it public.
08.11.10
Get container ID from inside an OpenVZ container
The following snippet will get you the OpenVZ container, when you're in a container. I have added this to my zsh prompt, but this might be useful in other places, too.
Code:
# Get OpenVZ container ID (/proc/bc is only on the host): | |
if [[ -f /proc/user_beancounters && ! -d /proc/bc ]]; then | |
CTID=$(sed -n 3p /proc/user_beancounters | cut -f1 -d: | tr -d '[:space:]') | |
fi |
21.05.10
Useful wrappers for apt-get, apt-source and apt-file
Link: https://github.com/blueyed/oh-my-zsh/blob/master/plugins/apt/apt.plugin.zsh
I've finally started to manage the setup of my dotfiles (configuration files) for shell, editor etc.
It is based on the popular dotfiles repository of ryanb and my fork can be found at github:blueyed/dotfiles.
While I'm still in the process of setting this up, I've just added the apt-* helpers I wrote some years ago:
They provide neat things like asrc -g hardy hello to get the version of the "hello" package from hardy (via "apt-get source" and the version number grepped from "apt-cache madison" - so you need to have it in your apt sources list, of course).
Also, ashow -g testing hello will show the package from Debian testing.
Apart from that these are mostly aliases, like "aup" for "sudo apt-get update" and some of them support shell completion (of package names) for e.g. "ainst" ("sudo apt-get install"). Shell completion works in both zsh and bash (at least).
You can get the file (to be sourced in a shell) from:
https://github.com/blueyed/oh-my-zsh/blob/master/plugins/apt/apt.plugin.zsh.
Feedback is very welcome and I am sure some of this is in packages like debian-goodies already (actually, there's nothing like that in _that_ package, but..).
Does it make sense to add (parts of) it to some package for easy installation across Debian/Ubuntu?
14.01.10
ftplicity/duply/duplicity: Get list of files in backup sorted by size
To get a list of files in your current backup, sorted by (current) file size, you can use the following snippet, which will create a file backup-size.list with the result.
Code:
for i in $(duply $BACKUPNAME list | tail -n +10 | head -n -1 | cut -b26-); do | |
FILE="/$i"; | |
test -f $FILE && du $FILE; | |
done | sort -n -r > backup-size.list |
I've used this to find the biggest files in the current backup which are not required to get backed up and excluded them via the exclude list feature of ftplicity/duply/duplicity (ftplicity or duply are frontends for duplicity).
16.02.08
Launch a webserver in the current directory
Use case: you want to start a webserver in a given directory, to serve some files from there temporarily.
How many lines of code do you need (including editing config files) to start a webserver in the current directory?
The best answer is probably "one":
python -c "import SimpleHTTPServer; SimpleHTTPServer.test()"
(via comment in "15 Line HTTP server").
Python (with batteries, like web server interfaces, included) is just awesome ![]()
20.12.07
Binäres Debian-Paket manipulieren
Ich hatte gerade den Fall, wo die Abhängigkeiten/Meta-Informationen zu einem Debian-Paket nicht wirklich passten (aufgrund eines Bugs).
Eine Möglichkeit, dies zu beheben wäre gewesen, sich das Quellpaket für das Paket zu holen, die Anpassungen vorzunehmen und es neu zu bauen.
Unglücklicherweise handelt es sich aber um ein ziemlich grosses Quellpaket (linux-restricted-modules-2.6.24, etwa 100MB) und es würden beim Bauen auch alle anderen binären Pakete gebaut (nicht nur nvidia-glx-new).
Also hab ich stattdessen das binäre Paket direkt editiert. Und das ging so:
dpkg-deb -x foo.deb foo-new
dpkg-deb -e foo.deb foo-new/DEBIAN
[...edit...]
dpkg-deb -b foo-new
Zuerst wird foo.deb (bzw nvidia-glx-new_100.14.19+2.6.24.2-2.7_i386.deb) ins Verzeichnis foo-new entpackt. Dann die Steuer-Informationsdateien dazu ("dpkg-deb -e"), ins Unterverzeichnis DEBIAN.
Nun habe ich foo-new/DEBIAN/control angepasst und dann ein neues Debianpaket mittels "dpkg-deb -b foo-new" erstellt und dann mittels "dpkg-deb -i foo-new.deb" installiert.
Ging insgesamt (inklusive Bloggen^WDokumentation) weitaus schneller, als alle Pakete in linux-restricted-modules neu zu bauen und dann nur das gefixte zu installieren.
05.11.07
Einmalige Eingabe der SSH-Passphrase bei Verwendung
Vor einiger Zeit habe ich ein gutes Alias für "ssh" gefunden, welches es erlaubt, eine SSH-Key-Passphrase nur einmal einzugeben - und zwar genau dann, wenn sie benötigt wird (und nicht bei jedem Login).
Setup (z.B. in ~/.bash_profile):
alias ssh='if ! ssh-add -l >/dev/null 2>&1; then ssh-add; fi; ssh'
keychain
. ~/.keychain/$HOSTNAME-sh
. ~/.keychain/$HOSTNAME-sh-gpg
keychain ist in jeder guten Distribution enthalten. Stattdessen kann man aber natürlich auch direkt ssh-agent/gpg-agent verwenden/laden.
