Executing scripts that read from most recent USB udev plugged in device - linux

As of right now, I've got my udev setup to execute scripts when a USB flash drive is plugged in or removed, but I'm stuck trying to figure out if there is some way that I can execute a script to read a file from the most recent USB device plugged in.
I am using usbmount to automatically mount all of my flash drives, and they are mounted according to this scheme:
/dev/sdb1 15G 8.0K 15G 1% /media/usb0
/dev/sdc1 15G 8.0K 15G 1% /media/usb1
/dev/sdd1 15G 8.0K 15G 1% /media/usb2
/dev/sde1 15G 8.0K 15G 1% /media/usb3
So for example, when I plug in USB flash drive #5, it gets automounted to /media/usb4, then I would like to say execute 'cat /media/usb4/data.txt > /tmp/output.txt' and only that drive that was just plugged in. Ideally I would like this to work no matter the number assigned to /media/usbx, so that if I replug in device 2, it would execute the script just for that device and not the rest.
Any ideas of how this can be done through bash scripting preferably but open to other ideas.
Thank you for your time.
------------- EDIT
I figured out a way although it's definitely not the prettiest or maybe even the most reliable:
$ sudo tail -n2 /var/log/syslog
Oct 4 14:40:58 development usbmount[32250]: executing command: mount -tvfat -osync,noexec,nodev,noatime,nodiratime /dev/sda1 /media/usb0
Oct 4 14:40:58 development usbmount[32250]: executing command: run-parts /etc/usbmount/mount.d
$
OK, so now to cut that down to just the media mount point,
$ sudo tail -n2 /var/log/syslog |grep media | awk '{print $12}'
/media/usb0
$
With this assuming no other errors or anything filling the last two spots on the syslog, I can execute scripts using something like:
#!/bin/bash
device=`sudo tail -n2 /var/log/syslog |grep media | awk '{print $12}'`
cat $device/data.txt > /tmp/output.txt

The run-parts bit is a tip-off...you can create a file /etc/usbmount/mount.d/50_copydata
Something like this:
#!/bin/bash
set -u
[[ -f "${UM_MOUNTPOINT}/data.txt" ]] && cat "${UM_MOUNTPOINT}/data.txt" > /tmp/output.txt
usbmount will set $UM_MOUNTPOINT to i.e. /media/usb0. I use set -u to make sure it only executes if $UM_MOUNTPOINT is set.
I assume you are going to filter the data - if you are only going to cat the file you might as well use cp.
Remember to make the file executable:
chmod +x /etc/usbmount/mount.d/50_copydata
Unplug and re-plug your device(s) to test. Hope this helps!

Related

Fails to `mkdir /mnt/vzsnap0` for Container Backups with Permission Denied

This is all done as the root user.
The script for backups at /usr/share/perl5/PVE/VZDump/LXC.pm sets a default mount point
my $default_mount_point = "/mnt/vzsnap0";
But regardless of whether I use the GUI or the command line I get the following error:
ERROR: Backup of VM 103 failed - mkdir /mnt/vzsnap0:
Permission denied at /usr/share/perl5/PVE/VZDump/LXC.pm line 161.
And lines 160 - 161 in that script is:
my $rootdir = $default_mount_point;
mkpath $rootdir;
After the installation before I created any images or did any backups I setup two things.
(1) SSHFS mount for /mnt/backups
(2) Added all other drives as Linux LVM
What I did for the drive addition is as simple as:
pvcreate /dev/sdb1
pvcreate /dev/sdc1
pvcreate /dev/sdd1
pvcreate /dev/sde1
vgextend pve /dev/sdb1
vgextend pve /dev/sdc1
vgextend pve /dev/sdd1
vgextend pve /dev/sde1
lvextend pve/data /dev/sdb1
lvextend pve/data /dev/sdc1
lvextend pve/data /dev/sdd1
lvextend pve/data /dev/sde1
For the SSHFS instructions see my blog post on it: https://6ftdan.com/allyourdev/2018/02/04/proxmox-a-vm-server-for-your-home/
Here are filesystem directory permission related files and details.
cat /etc/fstab
# <file system> <mount point> <type> <options> <dump> <pass>
/dev/pve/root / ext4 errors=remount-ro 0 1
/dev/pve/swap none swap sw 0 0
proc /proc proc defaults 0 0
df -h
Filesystem Size Used Avail Use% Mounted on
udev 7.8G 0 7.8G 0% /dev
tmpfs 1.6G 9.0M 1.6G 1% /run
/dev/mapper/pve-root 37G 8.0G 27G 24% /
tmpfs 7.9G 43M 7.8G 1% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 7.9G 0 7.9G 0% /sys/fs/cgroup
/dev/fuse 30M 20K 30M 1% /etc/pve
sshfs#10.0.0.10:/mnt/raid/proxmox_backup 1.4T 725G 672G 52% /mnt/backups
tmpfs 1.6G 0 1.6G 0% /run/user/0
ls -dla /mnt
drwxr-xr-x 3 root root 0 Aug 12 20:10 /mnt
ls /mnt
backups
ls -dla /mnt/backups
drwxr-xr-x 1 1001 1002 80 Aug 12 20:40 /mnt/backups
The command that I desire to succeed is:
vzdump 103 --compress lzo --node ProxMox --storage backup --remove 0 --mode snapshot
For the record the container image is only 8GB in size.
Cloning containers does work and snapshots work.
Q & A
Q) How are you running the perl script?
A) Through the GUI you click on Backup now, then select your storage (I have backups and local and the both produce this error), then select the state of the container (Snapshot, Suspend, Stop each produce the same error), then compression type (none, LZO, and gzip each produce the same error). Once all that is set you click Backup and get the following output.
INFO: starting new backup job: vzdump 103 --node ProxMox --mode snapshot --compress lzo --storage backups --remove 0
INFO: Starting Backup of VM 103 (lxc)
INFO: Backup started at 2019-08-18 16:21:11
INFO: status = stopped
INFO: backup mode: stop
INFO: ionice priority: 7
INFO: CT Name: Passport
ERROR: Backup of VM 103 failed - mkdir /mnt/vzsnap0: Permission denied at /usr/share/perl5/PVE/VZDump/LXC.pm line 161.
INFO: Failed at 2019-08-18 16:21:11
INFO: Backup job finished with errors
TASK ERROR: job errors
From this you can see that the command is vzdump 103 --node ProxMox --mode snapshot --compress lzo --storage backups --remove 0 . I've also tried logging in with a SSH shell and running this command and get the same error.
Q) It could be that the directory's "immutable" attribute is set. Try lsattr / and see if /mnt has the lower-case "i" attribute set to it.
A) root#ProxMox:~# lsattr /
--------------e---- /tmp
--------------e---- /opt
--------------e---- /boot
lsattr: Inappropriate ioctl for device While reading flags on /sys
--------------e---- /lost+found
lsattr: Operation not supported While reading flags on /sbin
--------------e---- /media
--------------e---- /etc
--------------e---- /srv
--------------e---- /usr
lsattr: Operation not supported While reading flags on /libx32
lsattr: Operation not supported While reading flags on /bin
lsattr: Operation not supported While reading flags on /lib
lsattr: Inappropriate ioctl for device While reading flags on /proc
--------------e---- /root
--------------e---- /var
--------------e---- /home
lsattr: Inappropriate ioctl for device While reading flags on /dev
lsattr: Inappropriate ioctl for device While reading flags on /mnt
lsattr: Operation not supported While reading flags on /lib32
lsattr: Operation not supported While reading flags on /lib64
lsattr: Inappropriate ioctl for device While reading flags on /run
Q) Can you manually created /mnt/vzsnap0 without any issues?
A) root#ProxMox:~# mkdir /mnt/vzsnap0
mkdir: cannot create directory ‘/mnt/vzsnap0’: Permission denied
Q) Can you replicate it in a clean VM ?
A) I don't know. I don't have an extra system to try it on and I need the container's I have on it. Trying it within a VM in ProxMox… I'm not sure. I suppose I could try but I'd really rather not have to just yet. Maybe if all else fails.
Q) If you look at drwxr-xr-x 1 1001 1002 80 Aug 12 20:40 /mnt/backups, it looks like there are is a user with id 1001 which has access to the backups, so not even root will be able to write. You need to check why it is 1001 and which group is represented by 1002. Then you can add your root as well as the user under which the GUI runs to the group with id 1002.
A) I have no problem writing to the /mnt/backups directory. Just now did a cd /mnt/backups; mkdir test and that was successful.
From the message
mkdir /mnt/vzsnap0: Permission denied
it is obvious the problem is the permissions for /mnt directory.
It could be that the directory `s "immutable" attribute is set.
Try lsattr / and see if /mnt has the lower-case "i" attribute set to it.
As a reference:
The lower-case i in lsattr output indicates that the file or directory is set as immutable: even root must clear this attribute first before making any changes to it. With root access, you should be able to remove this with chattr -i /mnt, but there is probably a reason why this was done in the first place; you should find out what the reason was and whether or not it's still applicable before removing it. There may be security implications.
So, if this is the case, try:
chattr -i /mnt
to remove it.
References
lsattr output
According to inode flags—attributes manual page:
FS_IMMUTABLE_FL 'i':
The file is immutable: no changes are permitted to the file
contents or metadata (permissions, timestamps, ownership, link
count and so on). (This restriction applies even to the supe‐
ruser.) Only a privileged process (CAP_LINUX_IMMUTABLE) can
set or clear this attribute.
As long as the bounty is still up I'll give it to a legitimate answer that fixes the problem described here.
What I'm writing here for you all is a work around I've thought of which works. Note, it is very slow.
Since I am able to write to the /mnt/backups directory, which exists on another system on the network, I went ahead and changed the Perl script to point to /mnt/backups/vzsnap0 instead of /mnt/vzsnap0.
Bounty remains for anyone who can get the /mnt directory to work for the mount path to successfully mount vzsnap0 for the backup script..
1)
Perhaps your "/mnt/vzsnap0" is mounted as read only?
It may tell from your:
/dev/pve/root / ext4 errors=remount-ro 0 1
'errors=remount-ro' means in case of mistake remounting the partition like readonly. Perhaps this setting applies for your mounted filesystem as well.
Can you try remounting the drive as in the following link? https://askubuntu.com/questions/175739/how-do-i-remount-a-filesystem-as-read-write
And if that succeeds, manually create the directory afterwards?
2) If that didn't help:
https://www.linuxquestions.org/questions/linux-security-4/mkdir-throws-permission-denied-error-in-a-directoy-even-with-root-ownership-and-777-permission-4175424944/
There, someone remarked:
What is the filesystem for the partition that contains the directory.[?]
Double check the permissions of the directory, or whether it's a
symbolic link to another directory. If the directory is an NFS mount,
rootsquash can prevent writing by root.
Check for attributes (lsattr). Check for ACLs (getfacl). Check for
selinux restrictions. (ls -Z)
If the filesystem is corrupt, it might be initially mounted RW but
when you try to write to a bad area, change to RO.
Great, turns out this is a pretty long-standing issue with Ubuntu Make which is faced by many people.
I saw a workaround mentioned by an Ubuntu Developer in the above link.
Just follow the below steps:
sudo -s
unset SUDO_UID
unset SUDO_GID
Then run umake to install your application as normal.
you should now be able to install to any directory you want. Works flawlessly for me.
try ls laZ /mnt to review the security context, in case SE Linux is enabled. relabeling might be required then. errors=remount-ro should also be investigated (however, it is rather unlikely lsattr would fail, unless the /mnt inode itself is corrupted). Creating a new directory inode for these mount-points might be worth a try; if it works, one can swap them.
Just change /mnt/backups to /mnt/sshfs/backups
And the vzdump will work.

sd card data broken after umount

I wrote an SD host-controller Linux driver for a totally customized board, using IP TE4395.
With the driver I wrote,
I can mount an SD card, read and write.
when mounted, once I umount, the next mount will fail.
command mount -o remount /dev/mmcblk0p1 worked normally....
# ls /mnt/
aaa.txt lost+found
# mount -o remount /dev/mmcblk0p1
EXT4-fs (mmcblk0p1): re-mounted. Opts: block_validity,delalloc,barrier,user_xattr,acl
# echo adfaf >/mnt/bbb.txt
# ls /mnt/
aaa.txt bbb.txt lost+found
# cat /mnt/bbb.txt
adfaf
# sync
# ls /mnt/
aaa.txt bbb.txt lost+found
# umount /mnt/
# mount /dev/mmcblk0p1 /mnt
mount: mounting /dev/mmcblk0p1 on /mnt failed: Invalid argument
I know the reason.
the driver has a problem. I added some code for debug, that read register when driver write registers.
And the Data Port register is special, when access the register the buffer pointer inside the controller will be incremented automatically.
The debug code read before and after write to the register, so accessed the register 3 times, but expected just 1 time access(write).

missing superblock on encrypted filesystem

I have a hard drive with ubuntu 14 installed. The whole disk is encrypted. My default users home directory is encrypted as well. Lately, after a system crash, I am presented with a busybox (initramfs) on startup. When I chose to start in recovery mode, I can grasp several error messages like " ... Failed to read block at offset xyz ...".
I searched and found this Q&A: Boot drops to a (initramfs) prompts/busybox
I booted from a CD and followed the instructions. However I am only able to do ...
sudo dumpe2fs /dev/sda1
... and then continue to check and repair superblocks on /dev/sda1 .
If I try ...
sudo dumpe2fs /dev/sda2
... i get the following error message:
dumpe2fs: Attempted to read block from filesystem resulted
in short read while trying to open /dev/sda2
Couldn't find valid filesystem superblock.
gparted shows the partitioning and file systems of the drive as follows:
partition file system size used unused flags
-------------------------------------------------------------
/dev/sda1 ext2 243M 210M 32M boot
/dev/sda2 extended 465G - - -
/dev/sda5!!crypt-luks 465G - - -
unallocated unallocated 1M - - -
The warning (!!) at sda5 says "Linux Unified Key Setup encryption is not yet supported".
If I try ...
sudo dumpe2fs /dev/sda5
... it returns this error message:
dumpe2fs: Bad magic number in super-block while trying to open /dev/sda5
Couldn't find valid filesystem superblock.
Mounting and rw-accessing sda1 works without error.
Any clues what is the cause and how i can repair, mount and decrypt the filesystem to boot normaly or at least to recover the data?
The given solution has missed some commands that you need to decrypt the file system and access it. Here's the full solution
Boot from Ubuntu USB
cryptsetup luksOpen /dev/rawdevice somename
sck /dev/mapper/somename
Get backup superblock:
sudo dumpe2fs /dev/mapper/ubuntu--vg-root | grep superblock
Fix:
sudo fsck -b 32768 /dev/mapper/ubuntu--vg-root -y
Verify:
mkdir /a
sudo mount /dev/mapper/ubuntu--vg-root /a
This worked for me:
Boot from Ubuntu USB
get backup superblock:
sudo dumpe2fs /dev/mapper/ubuntu--vg-root | grep superblock
fix:
sudo fsck -b 32768 /dev/mapper/ubuntu--vg-root -y
verify
mkdir /a
sudo mount /dev/mapper/ubuntu--vg-root /a
I used following links as source:
https://askubuntu.com/questions/137655/boot-drops-to-a-initramfs-prompts-busybox
https://serverfault.com/questions/375090/using-fsck-to-check-and-repair-luks-encrypted-disk

Can I change devices permissions with chmod?

Before read: I don't want to use any udev rule.
I need to change (programmatically) the permissions of some device. But first in order to underestand what I have to do (in code) I want to make the job using just chmod command.
So far, I've tried this:
root# ls -l /dev/sdb
root# brw-rw-rw- 1 root disk 8, 16 Apr 7 05:27 /dev/sdb
root# chmod 0600 /dev/sdb
root# ls -l /dev/sdb
root# brw------- 1 root disk 8, 16 Apr 7 05:27 /dev/sdb
as you can see, /dev/sdb has read and write permissions only for the owner (wich is root). But I'm still able to create new files and read files from my connected flash drive.
What I'm missing. How can I use chmod to prevent users to write to some device?
Can be done what I'm asking for?
Thank in advance.

Get details of RAID configuration on Linux

How to get the details of RAID configuration in Linux ?
mdadm -D /dev/mdxx will give you detail of raid configuration.
cat /proc/mdstat will give detail about raid algorithm,level and chunk size etc .
This is real if this RAID is sofware....
In case of RAID hardware, you could type this command :
lspci -vv | grep -i raid
01:00.0 RAID bus controller: LSI Logic / Symbios Logic MegaRAID SAS 2208 [Thunderbolt] (rev 01)
Kernel driver in use: megaraid_sas
Kernel modules: megaraid_sas
If you're talking about a running array:
cat /proc/mdstat
If you're talking about the mdadm config file, it's usually in /etc or /etc/mdadm depending on the distribution you're running on. The following command should find it in any event:
find /etc -name '*mdadm*'
ETA: Also, I would strongly recommend that you carefully study the mdadm man page so that you are very familiar with that utility. Knowing that utility well will save your bacon at some point.
mdadm --detail /dev/md0
(or whatever /dev/mdXXX you are using)

Resources