Categories
Linux

use mount in udev rule with systemd-udevd

If you write your own scripts called by udevd, for example after plugin a specific USB stick, you maybe want to mount a partition, which could fail. I got often return code 32 from mount, which means “mount failure” but without any additional error message.

This happens, because systemd-udevd is started in its own namespace for mounting. So the filesystem of udevd differs from the main system. You should be aware, that running udevd in an own namespace increases security. But if you want to disable this to run your scripts, do the following steps.

First verify the current MountFlags settings of udevd service. It should be “slave”.

systemctl show systemd-udevd | grep -F MountFlags

Now edit the udevd service

systemctl edit systemd-udevd

and set MountFlags to shared:

[Service]
MountFlags=shared

At last you just have to reload systemd settings and restart the udevd daemon:

systemctl daemon-reload
systemctl restart systemd-udevd

You then also can verify that udevd didn’t use the namespace anymore and list all current mount namespaces in use:

lsns -t mnt
Categories
Linux Virtualization

analyze / mount virtual images (VDI) from VirtualBox

Sometimes you would like to look into a virtual image (VDI file) of VirtualBox without starting the VM. One solution could be to setup another VM and attach the VDI to the helper VM. An easier solution would be the analysis with standard Linux tools.

You can use the QEMU tools to mount your virtual disks. First you have to install these tools if not already present (here for Ubuntu):

apt install qemu-utils

Then you should create a snapshot of your virtual image, so that modifications are not written back to your image. If you need to manipulate a virtual image you can skip this step.

qemu-img create -f qcow2 -b VIRTUALDISK.vdi snapshot.qcow2

Then you will use qemu-nbd to create a block device of your virtual image. To use qemu-nbd you need to have to load the kernel module nbd.

sudo modprobe nbd
lsmod | grep -Fi nbd 

The last command should output something like:

nbd                    40960  0

Then you will connect the blockdevice /dev/nbd0 with your virtual image:

qemu-nbd -c /dev/nbd0 snapshot.qcow2

Then you can use it like every other disc. For example with fdisk or lsblk or you can mount a partition of the virtual image.

fdisk -lu /dev/nbd0
lsblk -i /dev/nbd0
mount /dev/nbd0p1 /mnt

After you finished your analysis, you have to disconnect the virtual image:

qemu-nbd -d /dev/nbd0

If you created a snapshot you can easily remove this file.

Categories
Linux Networking

Optimize CIFS mounts for slow connections in Linux

With the default settings on mounting CIFS I had the problem while uploading a large file (200 – 300 MB) that my system frozen for some seconds. Additionally progress bars in Midnight Commander for example filled up to 100 % in some milliseconds and then it took 5 till 10 minutes until the copy job was finished.

The reason for this was that the dirty block cache of the Linux Kernel was filled by the copy job and sometimes was also full and could only cleared slowly, because of the slow connection to the server.

You can watch the current state of the dirty_cache with:

watch -n 2 "cat /proc/meminfo | egrep -i 'dirty|write'"

Also you can adjust the dirty_background_bytes and dirty_bytes of the kernel, but these settings are system wide and not only for a mountpoint. So you need more cache for local storage to get things fast and small cache for slow remote operations. But you should check the following settings of your kernel:

  • vm.dirty_background_bytes
  • vm.dirty_bytes
  • vm.dirty_expire_centisecs
  • vm.dirty_writeback_centisecs

There are many different “best values” found on the internet, but it depends. So I will not add more here.

Sometimes I also get some errors (Error 4 and Error 5), I assume due to timeouts or thatever, after the copy job was finished. Also in kernellog the following entries was reported:

CIFS VFS: No writable handles for inode<br>CIFS VFS: cifs_invalidate_mapping: could not invalidate inode

To solve the problem with the cifs mount, one solution is to disable the cache of this mount. You can do it with the mount option cache=none of mount.cifs.

The upload is a little bit slower, but it is stable now.