Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Tuesday, July 7, 2020

Install KiCad 5.1 on xubuntu (no-brainer since the packages are somehow broken)

Big mess. The easiest is to get rid of everything and not rely entirely on the Debian packages. I had to gather some info around and I ended up with the following.

Beware, all existing kicad executable and configuration will be wiped out, as I think that 5.1 does not play well with former versions. Especially, libraries and footprints do not show up after installation. This also makes it annoying to import a former KiCad project in 5.1. I had none so I "simply" started from blank projects, as KiCad is much easier to design new packages and footprint than Eagle CAD I was using so far.


Friday, April 17, 2020

LinuxCNC on an old Dell D430 with a dock

http://www.10k3d.com/2015/06/my-journey-into-world-of-cnc-machining_22.html


''
sudo apt-get install git-core
sudo apt-get install libpci-dev
git clone git://git.kiszka.org/smictrl.git && cd smictrl
git clone git://github.com/zultron/smictrl-deb.git debian
fakeroot dpkg-buildpackage -uc -us
sudo dpkg -i ../smictrl_*.deb
sudo sed -i '/exit 0/ismictrl -s 0' /etc/rc.local
sudo reboot
''

Adding bCNC to the laptop:
''
git clone https://github.com/vlachoudis/bCNC.git
''

How to submit a patch to the linux kernel

How to submit a patch to the linux kernel



On Ubuntu/Debian you probably need ''sudo apt-get install git-email'' first, unless you want to do it entirely manually (uuh).

''
git log
git format-patch HEAD~
scripts/checkpatch.pl 0001-scripts-config-allow-colons-in-option-strings-for-se.patch
scripts/get_maintainer.pl
git send-email 0001-scripts-config-allow-colons-in-option-strings-for-se.patch
''

You could use ''git format-patch --cover-letter HEAD~'' to generate a blank "introductory" email in order to describe your post further than with the sole summary of the patch itself.

Here is a ''~/.gitconfig'' example for git to send emails:
''
[sendemail]
  ; setup for using git send-email; prompts for password
  smtpuser = jf@gmail.com
  smtpserver = smtp.googlemail.com
  smtpencryption = tls
  smtpserverport = 587
''

Note: you might have to create a specific Google "application password" for the provided pseudo "login" prompt that ''git'' will print.

The tool ''./scripts/get_maintainer.pl'' in linux will help you identify the recipients. It does this very nicely, instead of having to guess from the ''MAINTAINERS'' file.

Finally, to submit the real thing to the linux list https://lkml.org/:
''
git send-email --cc-cmd='./scripts/get_maintainer.pl --norolestats 0001-scripts-config-allow-colons-in-option-strings-for-se.patch' 0001-scripts-config-allow-colons-in-option-strings-for-se.patch
''

It will end up, e.g. as https://lkml.org/lkml/2020/4/10/383 

Thursday, February 9, 2017

Use sshfs to upload to a remote www-data apache folder with working user rights

A client of mine is heavily using Samba (workgroup) from their Windows PC. I used to add samba shares or symbolic links within existing shares so they could access the data folder of different webservices hosted on the server. So far so good.

Recently, they purchased a secondary server to off-load part of the existing services, but they would like to keep the convenient file access through Samba.

Now: I hate samba. Configuration sucks. But the user rules. I could have added a secondary samba server (or client) on the secondary PC, then asked them to log on the proper share. But here is a very Unix way to do it:

Wednesday, September 21, 2016

How to find a package in debian / ubuntu

Here are three different command I use to look for a linux package.
  • apt-cache search <regex>
  • apt-show-versions | egrep <regex>
  • dpkg -l | egrep -i <regex>
The first one is super verbose, but it lists all sub-packages.

The second one is a command to install via ''sudo apt-get install apt-show-versions''. I use it often as it also lists whether the package is already installed or not.

The last one I forget all the time. It will show which packages are, or were, installed on your system. It is concise and it lists the architecture (32/64 bits versions).

For example, here are the three output when looking for SDL dev (which stands for "simple direct media layer", a lightweight and cross-platform framework to develop graphics and access to low-level resources).

''
[11:35][jeremie@alpha:~] apt-cache search 'sdl.*-dev'
libsdl1.2-dev - Simple DirectMedia Layer development files
erlang-esdl-dev - Erlang bindings to the SDL (development files)
libalien-sdl-dev-perl - helper  to build  Perl program using SDL libraries
libghc-sdl-dev - Haskell SDL binding for GHC
libghc-sdl-gfx-dev - Haskell SDL gfx binding for GHC
libghc-sdl-image-dev - Haskell SDL Image binding for GHC
libghc-sdl-mixer-dev - Haskell SDL Mixer binding for GHC
libghc-sdl-ttf-dev - Haskell SDL TTF binding for GHC
libsdl-console-dev - Console that can be added to any SDL application, development files
libsdl-gfx1.2-dev - development files for SDL_gfx
libsdl-image1.2-dev - Image loading library for Simple DirectMedia Layer 1.2, development files
libsdl-mixer1.2-dev - Mixer library for Simple DirectMedia Layer 1.2, development files
libsdl-net1.2-dev - Network library for Simple DirectMedia Layer 1.2, development files
libsdl-ocaml-dev - OCaml bindings for SDL - development files
libsdl-pango-dev - text rendering with Pango in SDL applications (development)
libsdl-sge-dev - extension of graphic functions for the SDL multimedia, development files
libsdl-sound1.2-dev - Sound library for Simple DirectMedia Layer 1.2, development files
libsdl-stretch-dev - development files for SDL_stretch library
libsdl-ttf2.0-dev - TrueType Font library for Simple DirectMedia Layer 1.2, development files
libsdl2-dev - Simple DirectMedia Layer development files
libsdl2-gfx-dev - development files for SDL2_gfx
libsdl2-image-dev - Image loading library for Simple DirectMedia Layer 2, development files
libsdl2-mixer-dev - Mixer library for Simple DirectMedia Layer 2, development files
libsdl2-net-dev - Network library for Simple DirectMedia Layer 2, development files
libsdl2-ttf-dev - TrueType Font library for Simple DirectMedia Layer 2, development files
libtaoframework-sdl-cil-dev - Tao CLI binding for SDL - development files

[11:35][jeremie@alpha:~] apt-show-versions |egrep sdl.*-dev
libsdl-image1.2-dev:amd64/xenial 1.2.12-5build2 uptodate
libsdl-image1.2-dev:i386 not installed
libsdl1.2-dev:amd64/xenial 1.2.15+dfsg1-3 uptodate
libsdl1.2-dev:i386 not installed

[11:36][jeremie@alpha:~] dpkg -l | egrep -i 'sdl.*-dev'
ii  libsdl-image1.2-dev:amd64  1.2.12-5build2  amd64  Image loading library for Simple DirectMedia Layer 1.2, development files
ii  libsdl1.2-dev              1.2.15+dfsg1-3  amd64  Simple DirectMedia Layer development files
''
Thge ''ii'' flags mean here "currently installed", i.e. I already have them. You can read about other installation status flags here.

Wednesday, August 31, 2016

Optimize a C program by visually checking the profiling data

You need ''gprof2dot.py'' (specific standalone tool) and the regular package ''dot'' (from graphviz).

''
g++ -pg -g myproc.cpp myproc
myproc myarg1 myarg2
gprof myproc | gprof2dot.py -s | dot -Tpng -o profiler.png
''

Note that the ''-g'' debugging option is required.

Note also that any code optimization flag, like ''-o3'' (aggressive speed optimization), will significantly impact the code layout, and it will be much harder to recognize the profiled sections.  It is much easier to read when most optimizations are tuned off (''-o1'' or ''-o0'').

But there is a nasty trade-off... Without optimization, the overall program will certainly behave differently than in production. It will not spend the same time in the same routines. Reciprocally, with full optimization you are closer to the real program you want to probe... but it will be sometimes just impossible to recognize the source code and unrolled functions that the profiler points to.

Sunday, November 29, 2015

Small and useful bash scripts

A growing collection of scripts and headers for your bash scripts.

Check and exit if a script is already running

This is to prevent multiple running instances of the same script:
''
test "$(pidof -x \"$(basename $0)\")" != $$ && exit
# or (bash only and protected against spaces in paths):
test $(pidof -x "$(basename $0)") != $$ && exit
''

Note: ''pgrep -af 'mqtt_watch.sh /solar/watts 5' '' is interesting to match a script with its arguments.

Or with a file (not totally secure, race conditions may still happen though unlikely):
''
LOCKFILE=/var/lock/$(basename $0).lock
[ -f $LOCKFILE ] && exit 0
trap "{ rc=$?; rm -f $LOCKFILE ; exit $rc; }" EXIT
touch $LOCKFILE
...
''

Via a safe, locking mechanism (see ''man flock''):
''
(
  flock -n 9 || exit 1
  # ... commands executed under lock ...
) 9>/var/lock/mylockfile
''

With an additional timeout (lock is overridden after a while):
''
LOCK_FILE="/var/run/$(basename $0).pid"
LOCK_TIMEOUT=10
test $(( $(date +%s) - $(stat -c %Y "$LOCK_FILE" 2>/dev/null||echo 0) )) -ge "$LOCK_TIMEOUT" && rm -f "$LOCK_FILE"  # remove stale lock
( set -o noclobber; echo "$$" > "$LOCK_FILE") 2> /dev/null || exit 1
trap "{ rc=\$?; rm -f '$LOCK_FILE'; exit \$rc; }" EXIT
''
In case you would better wait for the lock than exit the script, you can loop on lock creation: ''while ! ( set -o noclobber; echo "$$" > "$LOCK_FILE") 2> /dev/null; do echo '(waiting for the lock)'; sleep 0.1; done'' (note that this would not enforce the timeout).

Get the full directory name of the script

This works no matter where it is being called from:
''
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
''

Re-run a script as root if it was not called with sudo

At the start of the script, just add:
''
[ "$UID" -ne 0 ] && exec sudo "$0" "$@"
''

Automatically manage cronjob additions and removal of commands

Super quick backup of a file thanks to bash
''
cp /etc/rssh.{conf,original}
''

Automatically manage cronjob additions and removal of commands

Use command lines to add or remove scheduled jobs in your ''crontab'':
''
croncmd="/home/me/myfunction start 2> /home/me/myfunction/cron_errors < /dev/null"
cronjob="0 0 * * * $croncmd"
# Add
( crontab -l | grep -v "$croncmd" ; echo "$cronjob" ) | crontab -
# Remove
( crontab -l | grep -v "$croncmd" ) | crontab -
''

Use pv to get a progress bar with dd

''pv'' can be used in many contexts to provide a progress bar:

''
dd bs=4M if=image.dd | pv | dd of=/dev/sdz
''

Re-index a set of files with leadging zeroes (e.g. to build a timelapse)

This works by extracting the number in their name (bashism ''${f//[!0-9]/}'' removes all non-digit), then add non-significant leading zeros to them:
''
for f in * .jpg; do mv "$f" /tmp/$(printf "%05d" ${f//[!0-9]/}).jpeg; done
''

Bash process substitution

This usual case:
''
sort file1 | uniq >tmp1
sort file2 | uniq >tmp2
diff tmp1 tmp2
''

Is better done with  process substitution (no temporary file, no subshell, not even pipes):
''
diff <(sort file1|uniq) <(sort file2|uniq)
''

How it works is ''<(...)'' is replaced by a ''/dev/fd'' file descriptor as if it was a file (ref.)

Ubuntu: find the biggest installed packages

''
dpkg-query --show --showformat='${Package;-50}\t${Installed-Size}\n' | sort -k 2 -n | grep -v deinstall | awk '{printf "%.3f MB \t %s\n", $2/(1024), $1}' | tail -50
''
Then after removing it, ''apt-get clean'' will also purge the package cache (a good thing to free some harddrive space)

Find your serial port attached to USB

This returns devices like ttyUSB (or ttyACM):
''
find /sys/bus/usb/devices/usb*/ -name tty|sed 's|.*\(tty[^/]\+[0-9]\).*|\1|'
''


Check ethernet bandwith

''
sudo apt install speedtest-cli
speedtest-cli
''

Compute date difference with flexible parametrization (very bashic!)

Here is ''date_diff.sh'':
''
#!/bin/bash
set -- "${1:-$(</dev/stdin)}" "${@:2}"
[[ -f "$1" ]] && set -- "$(<$1)" "${@:2}"
echo $( date -d@$(( ( $(date +'%s') - $(date -d "$1" '+%s') ) )) +%s ) 3600 / p | dc
''

Then:
''
root:~# ./date_diff.sh "2016-08-09 06:03"
27
''
This is twenty-seven hours from now to the recorded date.

The second and third very bash-specific line in the script makes it possible to pass the date via an argument as above, or through a pipe as below. Say you stored a timestamp with ''date +'%Y-%m-%d %H:%M' > last.txt''. Then:

''
root:~# cat last.txt | ./date_diff.sh 
27
root:~# ./date_diff.sh last.txt 
27
''

Add a line to a reverse, while keeping under 500 lines

''
line="Something to log"
sed -ni "1s/^/$log\n/;500q;p" "$lf"
''

Robust parsing of a key = value ini file:

We want to set variables from a two-column file, without the risk that bash interprets the value (so the value can really be anything)

Say you have this file /tmp/test.ini to parse:
''
# Some comment
AA=da fook
BB=ya ' laa
CC=zarma $(ls)
DD_EE=0
EE=
''

You can parse it safely with this script to generate the respective variables

''
#!/bin/bash
prefix=SAFE_
while read -r line; do
k="$(echo $line|cut -d= -f1)"
v="$(echo $line|cut -d= -f2-)"
[[ $k =~ ^[A-Z_]+$ ]] && export $prefix$k="$v"
done < <(cat /tmp/test.ini)

echo BB=$SAFE_BB
echo CC=$SAFE_CC

# Result is:
#    BB=ya ' laa
#    CC=zarma $(ls)
''

Without ''$prefix'' set, make sure you do not allow variables in the left colum like PATH or LD...

Filter out non printable charcacters of a stream

Eg. when ''syslog'' gets corrupted with binary characters and many tools claim the file is binary:

''
tr -cd '\11\12\15\40-\176'
''



Switch my wifi router off when not in use (WR703N TP-link)

Automatic wifi shutdown when idle on a small WRT router

The script below runs on my OpenWRT wifi router (a cheap but effective $20 TP-Link TL-WR703N, which I flashed with OpenWRT -- see the how to here).



It is configured as a wifi router, when switched on.

But when no one connects to it, it will blink its led for a while before it shuts down by itself. So it does not waste useless energy, it is more secure at home and we live in one less wifi network.


Thursday, October 15, 2015

SSH tunneling: handling your mom's linux box, remotely and anywhere.

I want to be able to log on the linux box of my mom, wherever she is.

This helps both me and her, so I can administrate, debug or backup her computer without having to tell her bad words. Here is what I did, thanks to an automatic SSH tunneling from her to me.

The fact that she is here and there all the times makes it impractical to configure the different internet boxes, routers and NATs. I want to be able to reach here even when she is in a cyber-café (not her kind, but this is out of topic).



Thursday, October 8, 2015

Linux log files: discard verbose useless lines

Your linux log files are "spammed" by verbose programs or activities that you would like to silence? Here is a short how to to filter some specific lines.