pátek 8. března 2024

add text to the first page of a PDF file

 1. crop pdf files 

pdfcrop --margins '-30 -30 -10 -20' input.pdf output.odf

margins: left, top, right, bottom


2.

string="this is a string"
tmpfile=$(mktemp) && echo $string | \
enscript -M a4 --margins=::0: -B -f Courier-Bold8 -o- | \
ps2pdf - "$tmpfile" && \
qpdf input.pdf --overlay "$tmpfile" -- output.pdf && \
rm -f  "$tmpfile" 

enscript --margins=left:right:top:bottom

úterý 7. listopadu 2023

remote logging to rsyslog

 

#enable remote access (mind also the firewall)

# provides UDP syslog reception
  module(load="imudp")
  input(type="imudp" port="514")
  
 # provides TCP syslog reception
  module(load="imtcp")
  input(type="imtcp" port="514")

#redirect all expernal traffic to dedicated file

$template remote-incoming-logs,"/var/log/remote/%HOSTNAME%.log"
  :inputname, isequal, "imudp" ?remote-incoming-logs
  :fromhost-ip , !isequal , "127.0.0.1" stop

 

Further reading: here

úterý 10. října 2023

LVM - replace 2-disk LV by a large single drive

The original LV named data/bigdata consists of two separate drives: /dev/sdc1 (6T) and /dev/sdd1 (5T) .

I would like to replace these two disks by the 12T single drive (/dev/sde).

The standard way is using the pvmove commnad. However, I opted to use mirroring approach.

  •  Create mirror:

    pvcreate /dev/sde1
    vgextend data /dev/sde1
    lvconvert --type mirror  -m 1 /dev/data/bigdata 

    lvs -a -o name,copy_percent,devices data

    lvs --all --segments -o +devices

  • remove old disks from the mirror
    lvconvert -m0 data/bigdata /dev/sdc1 /dev/sdd1

Now there is 1PE free space between two segments on /dev/sde1

[root@server ~]# pvs --segments -v
  PV         VG     Fmt  Attr PSize    PFree   Start   SSize   LV      Start   Type   PE Ranges
/dev/sdc1  data   lvm2 a--    <5.46t   <5.46t       0 1430791             0 free
/dev/sdd1  data   lvm2 a--    <4.55t   <4.55t       0 1192326             0 free
/dev/sde1  data   lvm2 a--    10.91t  <5.46t       0 1430791 bigdata      0 linear /dev/sde1:0-1430790
/dev/sde1  data   lvm2 a--    10.91t  <5.46t 1430791 1                    0 free
/dev/sde1  data   lvm2 a--    10.91t  <5.46t 1430792 1192326 bigdata      0 linear /dev/sde1:1430792-2623117


to remove the free space, move  the second segment from /dev/sde1 to the old drive, and than back to /dev/sde1

pvmove /dev/sde1:1430792-2623117 /dev/sdc1
pvmove /dev/sdc1 /dev/sde1

[root@server ~]# pvs --segments -v
  PV         VG     Fmt  Attr PSize    PFree    Start   SSize   LV      Start Type   PE Ranges
/dev/sdc1  data   lvm2 a--    <5.46t   <5.46t       0 1430791             0 free
/dev/sdd1  data   lvm2 a--    <4.55t   <4.55t       0 1192326             0 free
/dev/sde1  data   lvm2 a--    10.91t <929.45g       0 2623117 bigdata     0 linear /dev/sde1:0-2623116
/dev/sde1  data   lvm2 a--    10.91t <929.45g 2623117  237938             0 free

This way the two segments join without further conditions.

pvdisplay -m /dev/sde1
lvs --all --segments -o +devices data
lvs -a -o name,copy_percent,devices datapvs --segments -v

pondělí 31. července 2023

in-place replacement failing drive from md array

failing drive sdd1:

/dev/sdd
 Number  Start (sector)    End (sector)  Size       Code  Name
    1            2048     11721045134   5.5 TiB     FD00  Linux RAID

create temporary replacement virtual drive:
truncate -s $(((11721045134-2048)*512)) fake-sdd1-file
losetup -f fake-sdd1-file
replace the failing drive with the virtual spare space
mdadm --manage /dev/md0 --add /dev/loop0
mdadm /dev/md0 --replace /dev/sdd1 --with /dev/loop0
wait for data replication (see /proc/mdstat), then remove the failed drive
mdadm /dev/md0 --remove /dev/sdd1
eventually, destroy all md data from the drive and delete the partition
mdadm --zero-superblock /dev/sdd1
gdisk /dev/sdd
or use wipefs

úterý 21. března 2023

Rescan sata port (hot-swap)

Short:

First: identify and delete the old drive (identify HCTL, which is Host:Channel:Target:Lun (viz ))

$ lsscsi
[0:0:0:0]    cd/dvd  HL-DT-ST BD-RE  BH10LS30  1.02  /dev/sr0
[1:0:0:0]    disk    ATA      APPLE SSD TS256C 0201  /dev/sdd
[2:0:0:0]    disk    ATA      WDC WD6002FRYZ-0 1M03  /dev/sdb
[3:0:0:0]    disk    ATA      WDC WD5001F9YZ-0 1E03  /dev/sdc
[N:0:1:1]    disk    WDC PC SN520 SDAPNUW-512G-1002__1          /dev/nvme0n1
$ lsscsi -g
[0:0:0:0]    cd/dvd  HL-DT-ST BD-RE  BH10LS30  1.02  /dev/sr0   /dev/sg0
[1:0:0:0]    disk    ATA      APPLE SSD TS256C 0201  /dev/sdd   /dev/sg1
[2:0:0:0]    disk    ATA      WDC WD6002FRYZ-0 1M03  /dev/sdb   /dev/sg2
[3:0:0:0]    disk    ATA      WDC WD5001F9YZ-0 1E03  /dev/sdc   /dev/sg3
[N:0:1:1]    disk    WDC PC SN520 SDAPNUW-512G-1002__1          /dev/nvme0n1  -  
$ lsblk -o NAME,MODEL,HCTL
NAME             MODEL                          HCTL
sdb              WDC WD6002FRYZ-01WD5B1         2:0:0:0
└─sdb1                                          
  └─data-bigdata                                
sdc              WDC WD5001F9YZ-09YUWL1         3:0:0:0
└─sdc1                                          
  └─data-bigdata                                
sdd              APPLE SSD TS256C               1:0:0:0
sr0              HL-DT-ST BD-RE BH10LS30        0:0:0:0

and also:

readlink /sys/block/sdX

To find the physical SATA port number, add one to the given host number.

2:0:0:0 -> H=2, sdb == ata3
 

Or use this command:

# find -L /sys/bus/pci/devices/*/ata*/host*/target* -maxdepth 3 -name "sd*" 2>/dev/null |\
egrep block |egrep --colour '(ata[0-9]*)|(sd.*)'
/sys/bus/pci/devices/0000:00:18.0/ata1/host0/target0:0:0/0:0:0:0/block/sda
/sys/bus/pci/devices/0000:00:18.0/ata2/host1/target1:0:0/1:0:0:0/block/sdb
/sys/bus/pci/devices/0000:00:18.0/ata3/host2/target2:0:0/2:0:0:0/block/sdc
  ...   

As a first step, the old drive has to be deleted (see), and (see):

echo 1 > /sys/block/sda/device/delete

Then rescan the host bus:

echo "- - -" >/sys/class/scsi_host/host<n>/scan

or

for i in /sys/class/scsi_host/host* ; do echo "- - -" > $i/scan ; done

also here.

pátek 3. března 2023

Debian 11

Staticka IP adresa

(pred  systemctl restart networking je obcas nutne rucne smazat IP adresy zarizeni, jinak se zmeny v /etc/network/interfaces neprojevi)


Aktivace rc.local

(podle https://blog.wijman.net/enable-rc-local-in-debian-bullseye/ )

Create a file /etc/rc.local

#!/bin/sh
  # ...
  # By default this script does nothing.
  /etc/rc.firewall
  touch /var/lock/subsys/local
  exit 0
  
chmod +x /etc/rc.local

Then we need to reload the systemd manager configuration:

systemctl daemon-reload

Then we start the rc-local daemon:

systemctl start rc-local

And then we check the status of rc-local to confirm it ran OK:


systemctl status rc-local 

Pokud definice sluzby chybi: (podle https://www.cyberciti.biz/faq/how-to-enable-rc-local-shell-script-on-systemd-while-booting-linux-system/

[Unit]
Description=/etc/rc.local
ConditionPathExists=/etc/rc.local
 
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
 
[Install]
WantedBy=multi-user.target

anebo delsi a ukecanejsi soubor (ale musi mit sekci [Install]):

  
# /etc/systemd/system/rc-local.service
#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
 
# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target
 
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no
StandardOutput=journal+console
StandardError=journal+console
[Install]
WantedBy=multi-user.target
 [Install]
WantedBy=multi-user.target

Iptables firewall

iptables negate: (https://www.cyberciti.biz/faq/iptables-invert-ip-or-protocol-with/) 

Narozdil od starsi implementace, negace se dava pred "option" (viz man iptables): --option ! this is deprecated in favour of ! --option this

--------------