Categories
Linux

setup custom .desktop-file

Gnome Desktop defines a file which describes an application. These description results in the icons and starters of applications in Gnome Desktop.

So if you want to add an custom application or an script as own application icon you have to write a custom .desktop file.

Your user specific .desktop-files are stored in ~/.local/share/applications.

Using gnome-desktop-item-edit

Gnome Desktop delivers a simple tool to create a desktop file called “gnome-desktop-item-edit”. You can use it from console:

mkdir -p ~/.local/share/applications
gnome-desktop-item-edit --create-new ~/.local/share/applications/myapp.desktop

Writing by hand

If you have special requirements, you will create your file by hand. Just open your favorite text editor and create a new .desktop file with for example the following content:

#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Type=Application
Terminal=true
Exec=/home/user/vpn.sh
Name=VPN
Icon=network-vpn 

This will create an application icon which starts a custom script (/home/user/vpn.sh) in an own terminal window.

Icons can be found in directory /usr/share/icons or you can reference a full path to any icon file.

If you use Terminal=true, you will see that the running terminal window is matched to the normal terminal windows and not to your application icon. To solve this you can use a custom Window Manager class and start your script with gnome-terminal and your custom class.

#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Type=Application
Exec=gnome-terminal --class=VPNConnection --name=VPN -- /home/user/vpn.sh
Name=VPN
Icon=network-vpn
StartupWMClass=VPNConnection

More options and also a description of the here used ones can be found in the Desktop Entry Specification.

Categories
Linux

disable automount for specific USB devices in Linux

I had the problem that I don’t want to automount a specific USB device in Linux. I could switch whole automount off, but this was not what I wanted.

To achieve this, you can define a specific udev rule. Udev is the widely used daemon for handling devices of Linux kernels in user space.

First you need to know how to identify your device. So plug your USB stick in and view the device specific parameters. The following example assumes, that your USB stick is added as /dev/sdb.

sudo udevadm info -a -n /dev/sdb

You will see everything which is related to this USB device, so also USB hub, etc. So be careful to choose the correct attributes for further filtering.

Now you have to add a udev rule for your device. You should create a new rules file, for example in /etc/udev/rules.d/10-myspecialdevice.rules with the following content:

ATTRS{idProduct}=="449a", ATTRS{idVendor}=="0815", ATTRS{serial}=="foobar", ENV{UDISKS_AUTO}="0"

The first 3 ATTRS parts are your extracted attributes from the previous command. The last sets an environment variable UDISKS_AUTO which handles the automount behavior of Gnome. There are more variables, for example UDISKS_IGNORE=1 for hiding the device in Nautilus. The following overview can be found in the man pages via man udisks:

UDISKS_SYSTEM
If set, this overrides the value of the HintSystem property.

UDISKS_IGNORE
If set, this overrides the value of the HintIgnore property.

UDISKS_AUTO
If set, this overrides the value of the HintAuto property.

UDISKS_CAN_POWER_OFF
If set, this overrides the value of the CanPowerOff property.

UDISKS_NAME
The name to use for the device when presenting it in an user interface. This corresponds to the HintName property.

UDISKS_ICON_NAME
The icon to use for the device when presenting it in an user interface. If set, the name must adhere to the freedesktop.org icon theme specification[4]. This corresponds to the HintIconName property.

UDISKS_SYMBOLIC_ICON_NAME
The icon to use for the device when presenting it in an user interface using a symbolic icon. If set, the name must adhere to the freedesktop.org icon theme specification[4]. This corresponds to the HintSymbolicIconName property.

UDISKS_FILESYSTEM_SHARED
If set to 1, the filesystem on the device will be mounted in a shared directory (e.g. /media/VolumeName) instead of a private directory (e.g. /run/media/$USER/VolumeName) when the Filesystem.Mount() method is handled.

ID_SEAT
The physical seat the device is attached to. If unset or set to the empty string, “seat0” (the first seat) is assumed.

After you created your file you have to reload your rules:

sudo udevadm control --reload
Categories
Java

calculate date difference in JPA / JPQL query using PostgreSQL and QueryDSL

Unfortunately JPA or JQL currently did not support a date difference function. So you have to use native database functions. In PostgreSQL you would use date_part like:

SELECT DATE_PART('day', date2 - date1) FROM mytable

JPA only supports to call functions with simple parameters, but not the calculation of two dates. So you have the problem to subtract one date or timestamp from another. I used a custom function to solve this problem:

CREATE OR REPLACE FUNCTION datediff(a TIMESTAMP, b TIMESTAMP) RETURNS integer AS $$
BEGIN
    RETURN DATE_PART('day', a - b);
END;
$$ LANGUAGE plpgsql

So you can use this function with two simple parameters.

But how to use this function in QueryDSL? This is quite easy. You just have to define a template, especially a number template in this example. You can use the static methods in class Expressions to create such templates. This template is then easily usable in the next query.

QMyTable myTable = QMyTable.myTable;
final NumberTemplate<Integer> template = Expressions.numberTemplate(Integer.class, "function('datediff', {0}, {1})", myTable.date1, myTable.date2);
JPAQuery<MyTable> query = new JPAQuery<>(getEntityManager()).select(myTable).from(myTable).where(template.goe(5));
return query.fetch();

Categories
Linux

cleanup Btrfs volumes

Sometimes your Btrfs disk can reported to be full or nearly full but you haven’t that amount of data saved on that volume.

To solve this you have to rebalance parts of your Btrfs volume. This is something like defragmentation. I had different parts, which needed rebalanced.

You can show currently used space with (/ is here mount path of your Btrfs volume):

btrfs filesystem df /

You will get something similar to this output:

Data, single: total=437.00GiB, used=388.76GiB<br>System, single: total=4.00MiB, used=64.00KiB<br>Metadata, single: total=6.01GiB, used=4.17GiB<br>GlobalReserve, single: total=512.00MiB, used=0.00B

The difference between total and used can be freed. For metadata you have to call:

btrfs balance start -m /

And for Data you have to call:

btrfs balance start -dusage=90 -v /

The option -dusage=90 filters all data blocks which are maximum filled by 90 %. Pay attention that a full rebalance could took some time and uses many IO resources, because everything would be rewritten. So you should start with lower values and rerun with large values if there was not freed enough space.

You can also combine -m and -dusage=90 into one task.

A long running rebalancing can be monitored with:

btrfs balance status /
Categories
Java

days between two dates with Java 8

A common task in programming is to calculate a difference between two dates / timestamps. Java 8 introduced a new Date/Time API for solving such problems in an easy way.

To use this API you have to use LocalDate or LocalTime. So if you have an “old school” java.util.Date instance, you must convert it..

LocalDate localDate = new Date().toInstant().atZone(ZoneId.systemDefault());

Now you can calculate the difference between two values:

//now
LocalDate localDate1 = LocalDate.now();
//a converted future date 
LocalDate localDate2 = dateInFuture.toInstant().atZone(ZoneId.systemDefault());
int differenceInDays = localDate1.until(localDate2, ChronoUnit.DAYS)

A second variant is to use ChronoUnit method between:

//now
LocalDate localDate1 = LocalDate.now();
//a converted future date 
LocalDate localDate2 = dateInFuture.toInstant().atZone(ZoneId.systemDefault());
int differenceInDays = ChronoUnit.DAYS.between(localDate1, localDate2)

Categories
Linux

limit or pause resync of mdadm

If you have a running resync job of mdadm this job can impact your normal operations a lot under different circumstances. You can limit the resync throughput with two kernel variables:

  • /proc/sys/dev/raid/speed_limit_min
  • /proc/sys/dev/raid/speed_limit_max

You can set here the minimum and maximum bandwidth in kibibytes per second(1024 Bytes).

Unfortunately you cannot set 0 to pause the resync. But you can set it to 1 kibibyte per seconds. This is nearly zero and is enough to be nearly pause.

But pay attention! If you raid is not sync you risk data loss.

Categories
Linux Networking

hash known_hosts in Linux

When using SSH to connect to other hosts a file containing the accepted public keys is saved in your home directory, especially ~/.ssh/known_hosts. This file contains beside the public key the IP / Hostname of the connected servers. These informations can be critical, if any other program or user will read the known_hosts file. One way to protect these informations is to hash the IP / Hostname part of the file.

To activate SSH to do this, is to add the following config entry into your ssh config. If you cannot add it system wide you can use your local ssh config file: ~/.ssh/config

HashKnownHosts yes

You can use the following command to achieve this.

echo "HashKnownHosts yes" >> ~/.ssh/config

Now SSH client will hash newly generated entries automatically. To update all given entries you should run:

ssh-keygen -H -f ~/.ssh/known_hosts

Then check if the conversion was successful and after that delete the old file:

rm ~/.ssh/known_hosts.old

Categories
Linux

convert Markdown files to PDF in Ubuntu

I love it writing short and medium notes in Markdown. For printing or sharing with others I often use the PDF format.

To convert Markdown files into other formats, like PDF, HTML, etc., you can use pandoc.

Using Latex

Installation in Ubuntu

For installation just install package pandoc. For PDF conversion you need some latex packages too.

sudo apt install texlive-latex-base texlive-fonts-recommended texlive-fonts-extra texlive-latex-extra pandoc

Conversion to PDF

To convert your markdown file test.md into test.pdf you need to run the following command:

pandoc -t latex -o test.pdf test.md

Sometimes I had some problems with “complex” markdown files. Especially if i use nested ordered / unordered lists. One solution is to use the html5 engine of pandoc.

Using HTML

Currently this is my preferred way for conversation, because it is more stable. You can also setup some styling with a simple CSS file.

Installation in Ubuntu

For installation just install package pandoc and the rendered wkhtmltopdf.

sudo apt install wkhtmltopdf pandoc

Conversion to PDF

Now it is like the usage of latex, but with a small different parameter (example is with paper size A4):

pandoc -t html5 -V papersize=a4 -o test.pdf test.md

Further variables can be read in the manual of pandoc.

Categories
Linux

Modify screen resolution or dpi in display manager like gdm or lightdm

If you have a screen with a large resolution (for example 4k) or you have a high DPI (HiDPI) screen, you will know that the display manager uses a lower resolution or a wrong DPI setting.

The solution is very easy. As gdm or lightdm also have a normal user account with normal home directory. So you can just copy your correct monitors.xml to the home directory of your display manager. For gdm this would be:

sudo mkdir -p ~gdm/.config
sudo chown gdm:gdm ~gdm/.config
sudo cp -a  ~/.config/monitors.xml ~gdm/.config/
sudo chown gdm:gdm ~gdm/.config/monitors.xml

After reboot you will see the correct screen resolution.

This is especially useful on HiDPI changes, because GNOME Shell in Manjaro (ArchLinux) makes all windows small on screen lock, so that you have to resize them after relogin.