2015-04-16

Regular apt-get update

A canonical way to schedule regular apt cache updates is:
cat >> /etc/apt/apt.conf.d/02periodic <<EOF
APT::Periodic::Enable "1";
APT::Periodic::Update-Package-Lists "1";
EOF
Update-Package-Lists is an update interval in days. This works through /etc/cron.daily/apt

2012-08-19

Delete all Windows update backups

@echo off
for /f "delims=" %%a in ('dir/ad/b %systemroot%\$NtUninstall*') do (
echo %%a
rd /s /q "%systemroot%\%%a"
)
pause

2012-07-02

Run tcpdump with user privileges

There is no need to acquire superuser privileges each time you need to perform some low level tasks. On my system I tend to allow programs like tcpdump, iftop, and some other "safe" programs to have access to required low level features by setting appropriate capability set on their executables.

The following command allows any non-root user to run tcpdump:

 sudo apt-get install libcap2-bin
 sudo setcap CAP_NET_RAW+ep /usr/sbin/tcpdump

  • CAP_NET_RAW - low-level network access capability
  • +ep - enable given capability as Effective an Permitted

User can exploit those programs to:

  • access sensitive data,
  • perform DOS attacks
  • perform man in the middle attacks
For example tcpdump could be exploited to inject alien packets in network traffic.

2011-11-22

ROOT package for debian

ROOT Debian packaging is often late or broken. Users are trying their best themselves. I've decided to join and remake ROOT Debian packaging from scratch.
Debian ROOT packaging system is published on github.
Currently it builds only two packages: libroot and libroot-dev which contain whole ROOT installation. xrootd is not included due to the bug in it's build system. Many more components and plugins are not built to keep dependencies at minimum. Fine grained packaging and useful dependency tags are subject for further improvement.
Debian wiki page watches the progress of main official and unofficial ROOT repositories.

2011-11-09

How to build a DKMS Debian package

Dynamic Kernel Module Support is a very simple concept allowing to always keep your kernel and modules up to date. DKMS builds module source on each change of active kernel. All you need is to install source files and register them with one-line command. On the other hand packaging system of Linux distributions makes those tasks even easier managing versioning, conflicts resolution and automated registration. Suppose you have "mydriver" kernel module. Following is annotated process of packaging for it step by step.
sudo apt-get install debhelper
debhelper is a set of perl scripts for automated Debian packaging. It helps creating a minimal debian package example in your source tree:
dh_make --single --packagename mydriver-1.2 --native --copyright gpl2
  • --single - creates a single deb package template
  • --native - tells dh_make to consider current directory as the root of source tree
  • --copyright - should be gpl2 (defaults to gpl3) to be compatible with current Linux kernel license
  • --packagename - should take argument of form mydriver-version
dh_make will create a subfolder debian with a bunch of template files. Fortunately most of them are optional and can be deleted:
rm debian/*.ex debian/*.EX debian/README* install
This leaves following files and directories: changelog compat control copyright README rules source substvars
  • changelog, copyright, README files are to be (optionally) fixed with your name and comments
  • compat and source are file and directory controlling Debian helper behaviour and are to be left as they are
  • control file contains your package metainformation. If you are sure that your driver will compile and work on any architecture change Architecture: field to "all".
  • substrvars contains variable values that are used in control file. It can be ignored.
The most important rules file should look like:
#!/usr/bin/make -f

VERSION=$(shell dpkg-parsechangelog |grep ^Version:|cut -d ' ' -f 2)

%:
 dh $@ --with dkms

override_dh_install:
 dh_install Makefile mydriver.c usr/src/mydriver-$(VERSION)/

override_dh_dkms:
 dh_dkms -V $(VERSION)
Rules file does almost everything related to Debian packaging. Follows the explanation of its content.
  • VERSION of your package is determined by changelog and Debian policies require changelog to always be up to date. dpkg-parsechangelog parses debian/changelog and outputs information as key: value pairs, one per line. We are selecting "Version" key with grep and obtain its value with cut.
  • % target redirects every unknown target to dh perl program provided by debhelper. dh implements all required targets for debian standard rules Makefile, but it can't guess everything and provide "override" mechanism to configure every aspect of packaging, if rules define override_dh_* target it replaces default action that dh usually does.
  • override_dh_install installs our sources to the required target location. Actually dh has default dh_install step, which may do this for you (using debian/install configuration file), but it is limited to constant path only, which renders it unusable for dkms installation (requiring module version to be present in target folder name).
  • override_dh_dkms it needed for the very same purpose - to avoid updating packet version inside you DKMS configuration file (see below).
Finally create debian/mydriver.dkms file with the following content:
PACKAGE_VERSION="#MODULE_VERSION#"
PACKAGE_NAME="mydriver"
CLEAN="make clean"
BUILT_MODULE_NAME[0]="mydriver"
BUILT_MODULE_LOCATION[0]="./"
DEST_MODULE_LOCATION[0]="/kernel/net"
MAKE[1]="make"
AUTOINSTALL="yes"
It will be copied to usr/src/mydriver-$(VERSION)/ by dh_dkms. #MODULE_VERSION# will be replaced by your module version (note that this won't happen if we omit -V key in dh_dkms invocation). All required files are ready now, we can proceed to building our package with dpkg-buildpackage -us -uc -b It will (list nesting shows call nesting):
  • clean source tree
    • "debian/rules clean"
      • "dh clean"
        • "make clean"
  • build source tree
    • "debian/rules build"
      • "dh build"
        • "make" which is probably doing nothing as your module actually using Kbuild mechanism leaving Makefile in your source without targets.
  • build package
    • "debian/rules binary"
      • install files to a temporary path with
        • "dh_dkms" which uses override_dh_dkms, patching dkms configuration and installing it in an appropriate temporary folder
        • "make override_dh_install" that installs our sources in the very same temporary folder
      • "dh_builddeb" that packages temporary folder into deb package.
You can now look for your ../mydriver_1.2_all.deb file.
Futher reading: Debian packaging

2010-10-23

A while ago I wrote a python library for parse and statistical analysis of network usage logs generated by net-acct.

A python was used for that implementation.

Later I began to learn Scala in order to make it closer to functional programming world. It seemed that writing simple log parser was an adequate task to take a feeling of language without experiencing extra difficulties.

A nice timing bonus makes me develop scala version futher and drop the python one.

python
real 2m32.052s
user 2m24.957s
sys 0m3.840s

scala
real 0m21.942s
user 0m15.653s
sys 0m0.456s

Tests were done on 6 Mb of gzipped logs with 1833.650 Mhz Athlon (3667.30 bogomips).

P.S. Improvements of configuration subsystem are now making Scala version useful for non-programmers. Documentation is still lacking, but I'm publishing it nevertheless.

2010-07-26

A shortest complete list of packages that should be installed on a Debian system to repeat current configuration can be obtained by the following command:

aptitude search '?installed?not(?automatic)?not(?reverse-depends(?installed))'


To use output in scripting add a key: -F %p

This package list can be passed as an argument list an apt-get install program on your new system.

2010-07-20

How to register your private key on the remote SSH server running on Linux

I've forgot which program from SSH toolchain does this (ssh-copy-id), and replaced it with my own method.
ssh-keygen -y -f ~/.ssh/id_dsa | ssh remote.org tee -a ~/.ssh/authorized_keys


~/.ssh/id_dsa should be replaced with your private key file.
remote.org - SSH sever address

2010-05-17

sudo in Debian squeeze

Debian squeeze creates an advanced config file for sudo package. File contains the following lines:

%sudo ALL=(ALL) ALL
#includedir /etc/sudoers.d


The first one allows all users from sudo group to do everything they want and the second one demonstrates a way to create a flexible sudo configuration.

2010-05-12

Unattended shutdown

My notebook seems to have a hardware defect. It might self power on after being powered off for some time.
This bug quickly degrades the battery making me recharge it too often. To prevent the system from discharging the battery I've wrote the following startup script:

#!/bin/sh

### BEGIN INIT INFO
# Provides: haltIfNousers
# Required-Start:
# Required-Stop:
# Should-Start: gdm
# Default-Start: 2 3
# Default-Stop: 4 5
# Short-Description: Execute the halt command.
# Description:
### END INIT INFO

#Powers off the system if no user is logged in in 30 seconds

BLOCK_FILE=/tmp/haltIfNousers

case "$1" in
start)
$0 wait &
;;
wait)
touch "$BLOCK_FILE"
sleep 30
[ -e "$BLOCK_FILE" ] || exit 0
if ! who|grep '.*' -q ; then
echo $0: No users are logged in. Halting now.
shutdown -h now
exit 0
fi
rm -f "$BLOCK_FILE"
;;
restart|reload|force-reload)
echo "Error: argument '$1' not supported" >&2
exit 3
;;
stop)
rm -f "$BLOCK_FILE"
;;
*)
echo "Usage: $0 start|stop" >&2
exit 3
;;
esac


This script checks if a user is logged on in 30 seconds after system boot and if he is not, shuts the system down.

Commented lines at the top of the script are Debian LSB header that drives its new dependecy based boot system. This header requests starting the script after gdb making measured time interval for user to login more precise.

2010-04-12

Multiple make

To clean multiple folders at once:

echo base_path/{subfolder1,subfolder2} | xargs -n 1 make clean -C

2010-03-26

RPM atomated building

If your are providing sources or binaries of your software as RPMs you should be aware of a smart way of automating RPM creation.
To create RPM building environment:

  1. create ~/.rpmmacros with the following content:

    %_topdir .../rpm #Arbitrary
    %_tmppath /tmp/rpm #Optional

    Three dots here denote your favorite RPM building directory infrastructure root. (/home/username will do)
  2. use the following script:

    #!/bin/sh
    for d in _topdir _sourcedir _rpmdir _srcrpmdir _specdir _builddir _tmppath; do
    mkdir -p `rpm --eval %{$d}`
    done


In your RPM generation script use constructions like

# Doesn't override previously set value. See Parameter Expansion
SOURCES=${SOURCES:-`rpm --eval %{_sourcedir}`}
#Assumes your sources are in $PROJECTS/$PROJECT directory
tar --wildcards --exclude \*\.svn\* --exclude \*\~ \ -C "$PROJECTS" -zcf "$SOURCES/$PROJECT.tar.gz" $PROJECT
rpmbuild -ba "$PROJECTS/$PROJECT/$PROJECT.spec"


Following this recommendation you will never need to force your packagers to use a harcoded SOURCES folder or to do configuration of your package with their RPM settings.

Again, thanks to comrade Buzykaev, for his tar command idea

2010-02-12

Bash versioning

Here is the bash command:

cp -i aaa/bbb/ccc/ddd/xxxx_{6,7}.tpl


This expands to
cp -i aaa/bbb/ccc/ddd/xxxx_6.tpl aaa/bbb/ccc/ddd/xxxx_7.tpl


This feature of bash is called Brace Expansion and can ease versioning of files based on filesystem. If you have a sequence of files with versions assigned to them as incremented part of the name, you can easily create one more with the given command:

> ls
aaa/bbb/ccc/ddd/xxxx_4.tpl
aaa/bbb/ccc/ddd/xxxx_5.tpl
aaa/bbb/ccc/ddd/xxxx_6.tpl
> cp -i aaa/bbb/ccc/ddd/xxxx_{6,7}.tpl
> ls
aaa/bbb/ccc/ddd/xxxx_4.tpl
aaa/bbb/ccc/ddd/xxxx_5.tpl
aaa/bbb/ccc/ddd/xxxx_6.tpl
aaa/bbb/ccc/ddd/xxxx_7.tpl


Even nicer usage:

cp filename{,_`date '+%F'`}

2009-12-15

A floating point and C++ templates

Find a bug in the following line:
double sum = std::accumulate(V.begin(), V.end(), 0);


Think more...


This bug can't be easiliy avoided as the connection between internal accumulator variable, return value and third argument types is not obvious and is very well incapsulated by implementation.

The right answer is:
double sum = std::accumulate(V.begin(), V.end(), double(0)); //0.L gives off the same effect

Here is GNU implementation of template function in question:

/**
* @brief Accumulate values in a range.
*
* Accumulates the values in the range [first,last) using operator+(). The
* initial value is @a init. The values are processed in order.
*
* @param first Start of range.
* @param last End of range.
* @param init Starting value to add other values to.
* @return The final sum.
*/
template
inline _Tp
accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
{
// concept requirements
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
__glibcxx_requires_valid_range(__first, __last);

for (; __first != __last; ++__first)
__init = __init + *__first;
return __init;
}

2009-11-12

Empty structures in C and C++

//test.c
#include <stdio.h>

struct Temp0{};
struct Temp1{char a;};

int main() {
printf("Temp0 size: %d\n", sizeof(struct Temp0));
printf("Temp1 size: %d\n", sizeof(struct Temp1));
return 0;
}


This code being compiled with GNU toolchain gives the following results:

$ gcc test.c -o test && ./test
Temp0 size: 0
Temp1 size: 1



$ g++ test.c -o test && ./test
Temp0 size: 1
Temp1 size: 1


The reasons explained

I wonder are other toolchains give the same results...

2009-07-10

Gnome-terminal and gconf

Recently there was a usual problem with gnome-terminal in Debian testing. As gconf is updated, gnome-terminal won't start gconf by itself.
Manual update of gnome-terminal (to version 2.26) from unstable release solves the problem.