recuperation de tout les scripts root

This commit is contained in:
max/sooulix 2025-02-03 02:51:04 +01:00
commit d5f702c613
33 changed files with 373 additions and 0 deletions

1
du_root Executable file
View File

@ -0,0 +1 @@
du -hs /{bin,etc,home,lib,lib64,opt,root,sbin,usr,var}|sort -h

3
iptables/del_rule Normal file
View File

@ -0,0 +1,3 @@
# check line number of the rule you want to delete with
iptables -L --line-numbers [-t nat]
iptables -D $CHAIN $LINE

3
iptables/fw_port Executable file
View File

@ -0,0 +1,3 @@
# input-port destination:port interface
iptables -t nat -I PREROUTING -p tcp -i $3 --dport $1 -j DNAT --to-destination $2
#iptables -A FORWARD -p tcp -i $3 -d $2 --dport $1 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

1
iptables/new_iptables Executable file
View File

@ -0,0 +1 @@
cp iptables iptables.$(date "+%y%m%d-%H:%M")

1
iptables/restore_iptables Executable file
View File

@ -0,0 +1 @@
cat /etc/network/iptables | iptables-restore

25
libvirt/clone_vm.sh Executable file
View File

@ -0,0 +1,25 @@
#!/bin/bash
BASE=debian-base
BASE_VG=vg0
BASE_ROOTFS=debian-base
BASE_XML=/root/libvirt/domains/debian-base.xml
BASE_DEV=/dev/mapper/vg0-rootfs--www
NEW=merles
NEW_VG=vg0
NEW_ROOTFS=rootfs-merles
NEW_ROOTFS_SZ=15G
DEV=/dev/$NEW_VG/$NEW_ROOTFS
lvcreate -L $NEW_ROOTFS_SZ -n $NEW_ROOTFS $NEW_VG
#dd if=$BASE_DEV of=/dev/$NEW_VG/$NEW_ROOTFS bs=4M
cp $BASE_XML /tmp/${NEW}.xml
sed -i "s/<name>.*<\/name>/<name>${NEW}<\/name>/" /tmp/${NEW}.xml
sed -i "s/<title>.*<\/title>/<title>${NEW}<\/title>/" /tmp/${NEW}.xml
sed -i "s/volume=.devuan-base./volume='${NEW_ROOTFS}'/" /tmp/${NEW}.xml
vim /tmp/${NEW}.xml
virsh pool-refresh $NEW_VG
virsh define --validate /tmp/${NEW}.xml
virsh start $NEW

6
libvirt/destroy_domains.sh Executable file
View File

@ -0,0 +1,6 @@
#!/bin/sh
# list vnc display ports for all active VM
virsh list --state-running --name | \
while read vm; \
do [ -n "$vm" ] && virsh destroy $vm && echo "Destroyed : $vm"; \
done | column -t

1
libvirt/get_unbound_ips Executable file
View File

@ -0,0 +1 @@
eval $(ssh-agent) && ssh-add ~/.ssh/id_rsa_genautobahn && ssh unbound.freepoteries.fr cat /etc/unbound/unbound.conf | grep "local-data-ptr:" | grep -oE '[1-9\.]{3,}[^"]+'

6
libvirt/init_domains.sh Executable file
View File

@ -0,0 +1,6 @@
#!/bin/bash
while read -r domain;
do
echo "Will start $domain";
virsh start $domain && sleep 10;
done < ../domains_start_order

1
libvirt/kernel-qemu Symbolic link
View File

@ -0,0 +1 @@
../kernel/kernel-qemu-4.19.50-buster

6
libvirt/list_domdisplay.sh Executable file
View File

@ -0,0 +1,6 @@
#!/bin/sh
# list vnc display ports for all active VM
virsh list --state-running --name | \
while read vm; \
do [ -n "$vm" ] && echo -e "$vm\t$(virsh domdisplay $vm)"; \
done | column -t

12
libvirt/list_ips.sh Executable file
View File

@ -0,0 +1,12 @@
#!/bin/bash
NSLOOKUP="nslookup %s"
while read -r ip
do
if [ "${1}" == "-d" ];
then
DN=`nslookup ${ip} | head -1 | cut -d'=' -f2 | sed 's/\.$//'`;
echo -e "${ip} ${DN}";
else
echo -e "${ip}";
fi
done < <(nmap -sn 192.168.42.0/24 2>/dev/null |grep "Nmap scan report"| rev |cut -d" " -f1|sed 's/(\|)//g'|rev |sort -t '.' -n -k 4);

40
libvirt/list_ram_usage.sh Executable file
View File

@ -0,0 +1,40 @@
#!/bin/sh
# list vnc display ports for all active VM
conv()
{
awk '{print $1"/1024"}'|bc
}
T_CURRENT=0
T_MAXIMUM=0
T_UNUSED=0
T_AVAILABLE=0
T_USABLE=0
R=`mktemp`
echo -e "VM # CUR # MAX # UNUS # AVAIL # USA" > $R
while read vm;
do
[ -z "$vm" ] && continue
MEMSTAT=`virsh domstats --balloon --raw "$vm"`
CURRENT=`echo "$MEMSTAT"|grep -oP "(?<=balloon.current=).*"|conv`
T_CURRENT=`expr "${CURRENT}" + "${T_CURRENT}"`
MAXIMUM=`echo "$MEMSTAT"|grep -oP "(?<=balloon.maximum=).*"|conv`
T_MAXIMUM=`expr "${MAXIMUM}" + "${T_MAXIMUM}"`
UNUSED=`echo "$MEMSTAT"|grep -oP "(?<=balloon.unused=).*"|conv`
T_UNUSED=`expr "${UNUSED}" + "${T_UNUSED}"`
AVAILABLE=`echo "$MEMSTAT"|grep -oP "(?<=balloon.available=).*"|conv`
T_AVAILABLE=`expr "${AVAILABLE}" + "${T_AVAILABLE}"`
USABLE=`echo "$MEMSTAT"|grep -oP "(?<=balloon.usable=).*"|conv`
T_USABLE=`expr "${USABLE}" + "${T_USABLE}"`
echo -e "$vm # ${CURRENT} # ${MAXIMUM} # ${UNUSED} # ${AVAILABLE} # ${USABLE}" >> $R
done <<< `virsh list --state-running --name`
R_=`mktemp`
cat $R|sort -t '#' -k 3 -n > $R_
echo $T_CURRENT
echo -e "TOTAL # ${T_CURRENT} # ${T_MAXIMUM} # ${T_UNUSED} # ${T_AVAILABLE} # ${T_USABLE}" >> $R_
cat $R_|column -t

View File

@ -0,0 +1,8 @@
#!/bin/sh
# usage : ./net-add_static_lease.sh name 00:00:00:00:00:00 127.0.0.1
[ "$#" -ne 3 ] && echo "usage : net-add_static_lease.sh name 00:00:00:00:00:00 127.0.0.1" && exit 0;
virsh net-update default add ip-dhcp-host \
"<host mac='${2}' \
name='${1}' ip='${3}' />" \
--live --config

10
libvirt/net-change_range.sh Executable file
View File

@ -0,0 +1,10 @@
#!/bin/sh
# usage : ./net-add_static_lease.sh name 00:00:00:00:00:00 127.0.0.1
#[ "$#" -ne 3 ] && echo "usage : net-add_static_lease.sh name 00:00:00:00:00:00 127.0.0.1" && exit 0;
virsh net-update default delete ip-dhcp-range \
"<range start='192.168.42.10' end='192.168.42.150'/>" \
--live --config
cat /etc/network/iptables|iptables-restore

23
libvirt/net-dns_entry.sh Executable file
View File

@ -0,0 +1,23 @@
#!/bin/bash
# Usage :
# ./net-dns_entry.sh [add|del] HOSTNAME IP
NETWORK="default"
CMD=$1
shift
HOSTNAME=$1
shift
IP=$1
shift
if [ $CMD == "add" ]
then
virsh net-update --live --config $NETWORK add dns-host "<host ip='${IP}'><hostname>${HOSTNAME}</hostname></host>";
elif [ $CMD == "del" ]
then
virsh net-update --live --config $NETWORK delete dns-host "<host ip='${IP}'><hostname>${HOSTNAME}</hostname></host>";
else
echo "Command should be 'add' or 'del'" && exit 1;
fi

View File

@ -0,0 +1,8 @@
#!/bin/sh
# usage : ./net-edit_static_lease.sh name 00:00:00:00:00:00 127.0.0.1
[ "$#" -ne 3 ] && echo "usage : net-add_static_lease.sh name 00:00:00:00:00:00 127.0.0.1" && exit 0;
virsh net-update default modify ip-dhcp-host \
"<host mac='${2}' \
name='${1}' ip='${3}' />" \
--live --config

6
libvirt/resume_domains.sh Executable file
View File

@ -0,0 +1,6 @@
#!/bin/sh
# list vnc display ports for all active VM
virsh list --state-paused --name | \
while read vm; \
do [ -n "$vm" ] && virsh resume $vm && echo "Resumed : $vm"; \
done | column -t

6
libvirt/start_domains.sh Executable file
View File

@ -0,0 +1,6 @@
#!/bin/sh
# Start all VMs that are in a shutoff state
virsh list --state-shutoff --name | \
while read vm; \
do [ -n "$vm" ] && virsh start $vm && echo "Resumed : $vm"; \
done | column -t

7
libvirt/start_rpi.sh Normal file
View File

@ -0,0 +1,7 @@
qemu-system-arm -kernel kernel-qemu \
-cpu arm1176 \
-m 256 \
-M versatilepb \
-serial stdio \
-append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" \
-drive "file=/tmp/rpi.img,index=0,media=disk,format=raw"

6
libvirt/suspend_domains.sh Executable file
View File

@ -0,0 +1,6 @@
#!/bin/sh
# list vnc display ports for all active VM
virsh list --state-running --name | \
while read vm; \
do [ -n "$vm" ] && virsh suspend $vm && echo "Suspended : $vm"; \
done | column -t

17
libvirt/switch_boot_device.sh Executable file
View File

@ -0,0 +1,17 @@
#!/bin/sh
# $1 = domain name
XML_DIR=/etc/libvirt/qemu
[[ -f "$XML_DIR/$1.xml" ]] && XML="$XML_DIR/$1.xml" || exit 1
DEV=`grep -o '<boot dev=[^>]*>' "$XML"|cut -d\' -f2`
SED_CMD=
case $DEV in
'hd')
SED_CMD="s/(<boot dev=')($DEV)('\/>)/\1cdrom\3/";;
'cdrom')
SED_CMD="s/(<boot dev=')($DEV)('\/>)/\1hd\3/";;
esac
virsh destroy $1
sed -E "$SED_CMD" $XML > /tmp/new_xml.xml
virsh define --file /tmp/new_xml.xml
virsh start $1
exit 0

15
libvirt/virt_install_rpi.sh Executable file
View File

@ -0,0 +1,15 @@
virt-install \
--name rpi \
--arch armv6l \
--machine versatilepb \
--cpu arm1176 \
--vcpus 1 \
--memory 256 \
--import \
--disk /tmp/rpi.img,format=raw,bus=virtio \
--network user,model=virtio \
--video vga \
--virt-type qemu \
--rng device=/dev/urandom,model=virtio \
--boot 'dtb=/root/libvirt/kernel/versatile-pb.dtb,kernel=/root/libvirt/kernel/kernel-qemu-4.19.50-buster,kernel_args=root=/dev/vda2 panic=1' \
--events on_reboot=destroy

View File

@ -0,0 +1,3 @@
# create-snapshot.sh vg vol snap size
SNAPNAME=${2}-$(date "+%Y%m%dT%H%M%S")
lvcreate --snapshot --name $SNAPNAME --size $3 $1/$2

2
lvm/create-snapshot.sh Executable file
View File

@ -0,0 +1,2 @@
# create-snapshot.sh vg vol snap size
lvcreate --snapshot --name $3 --size $4 $1/$2

1
lvm/crypt_volume.sh Normal file
View File

@ -0,0 +1 @@
cryptsetup -s 512 luksFormat /dev/vg0/pics

35
lvm/lvm-mount Executable file
View File

@ -0,0 +1,35 @@
#!/bin/sh
exitmsg () {
echo $1;
exit 1;
}
LOOP=$(losetup -f)
LV=$1
DST=$2
TMPLOOP=/var/run/rsnapshot.loopdevice
echo $LOOP > $TMPLOOP;
losetup -P $LOOP $1;
NPART=`ls ${LOOP}p* 2> /dev/null | wc -l` || 0
[ -d "$DST" ] || mkdir $DST;
if [[ "$NPART" -eq 0 ]];
then
mount $LV $DST;
else
I=1
while [[ $I -le $NPART ]];
do
PARTDIR=$DST/part$I;
mkdir $PARTDIR || exitmsg "Can't create $PARTDIR";
mount $LOOP"p"$I $PARTDIR 2> /dev/null || rmdir $PARTDIR;
I=`expr $I + 1`;
done;
fi
exit 0;

15
lvm/lvm-umount Executable file
View File

@ -0,0 +1,15 @@
#!/bin/sh
DST=$1
umount $DST/* 2>/dev/null;
if [[ -f /var/run/rsnapshot.loopdevice ]];
then
read LOOP < /var/run/rsnapshot.loopdevice;
losetup -d $LOOP || break;
rm -rf $DST/*;
rm /var/run/rsnapshot.loopdevice;
fi;
umount $DST;
exit 0;

33
lvm/lvmmount Executable file
View File

@ -0,0 +1,33 @@
#!/bin/sh
exitmsg () {
echo $1
exit 1;
}
[ "$#" -lt 3 ] && echo 'Usage: lvmmount VGNAME VOLNAME PARTNUM [LOOPDEVICE]' && exit 2
VG=$1
vgs -qq $VG > /dev/null 2>&1 || exitmsg "The specified VG ($VG) does not exist"
VOL=$2
lvs -qq $VG/$VOL > /dev/null 2>&1 || exitmsg "The specified volume ($VOL) does not exist"
PART=$3
echo $PART | grep -qE "^[[:digit:]]+$" || exitmsg "The specified partition ($PART) must be a number"
DST=${4:-/dev/loop0}
losetup -P $DST /dev/$VG/$VOL || exitmsg "$DST is already used, specify another loop device (or deactivate it with losetup -D $DST"
if [ ! -e ${DST}p${PART} ]
then
losetup -D $DST
exitmsg "The is no partition ${PART} in ${VG}/{$VOL}"
fi
DSTDIR=/tmp/${VG}_${VOL}_${PART}
[ -e $DSTDIR ] && exitmsg "The $DSTDIR directory already exists, please check what you are doing"
mkdir -p $DSTDIR
mount ${DST}p${PART} ${DSTDIR}
echo "Mounted /dev/${VG}/${VOL} part ${PART} in ${DSTDIR}"
exit 0

3
lvm/merge-snapshot.sh Executable file
View File

@ -0,0 +1,3 @@
# $1 : snapshot name
# $2 : vg name (vg0 by default=
lvconvert --merge ${2:-vg0}/$1

7
lvm/mount-lvm-part.sh Executable file
View File

@ -0,0 +1,7 @@
#!/bin/bash
VG=${1:-vg0}
VOL=${2:-devuan-base}
PART=${3:-1}
mkdir /tmp/$VOL
mount -o offset=1048576 /dev/$VG/$VOL /tmp/$VOL

11
lvm/resize_a_partition Normal file
View File

@ -0,0 +1,11 @@
lvextend the device rootfs
change the boot device of the vm
restart the vm and connect to it (vnc or whatever)
fdisk on the device
delete the partition and recreate it fatter
write!
e2fsck -f /dev/wotwotXXX
resize2fs /dev/wotwotXXX newSize
destroy the vm
change the boot device AGAIN to hd
then start the VM and you're done

51
lvm/snapmount_zered.sh Executable file
View File

@ -0,0 +1,51 @@
#!/bin/sh
MOUNTDIR=/media/tmp_snapshot
loop=/dev/loop1
usage() {
echo "Usage : $0 start|stop LV_PATH PART_NUMBER"
}
## @brief mount an lvm snapshot
# @param $1 lvpath (like /dev/vg0/lv0 )
# @param $2 partition number
snap_mount() {
# Cleaning old killed mount or failed backup
umount $MOUNTDIR/* 2>/dev/null
losetup --detach $loop 2>/dev/null
# Proc start
name=$(basename $1)
vgpath=$(dirname $1)
snap="${name}-snap"
lvcreate --snapshot --size 500M --name $vgpath/$snap $1
losetup --partscan $loop $vgpath/$snap
part="${loop}p$2"
mkdir -v -p $MOUNTDIR/$name 2>/dev/null
mount $part $MOUNTDIR/$name
#echo $part mounted on $MOUNTDIR/$name
}
##@brief umount a previously created snapshot and removes it
snap_umount() {
name=$(basename $1)
vgpath=$(dirname $1)
snap="${name}-snap"
umount $MOUNTDIR/$name
losetup --detach $loop
lvremove -y $vgpath/$snap
}
case $1 in
start)
snap_mount $2 $3
;;
stop)
snap_umount $2 $3
;;
*)
usage
exit 1
;;
esac