commit d5f702c61355945f049f32f2ecb33679c3318001 Author: max/sooulix Date: Mon Feb 3 02:51:04 2025 +0100 recuperation de tout les scripts root diff --git a/du_root b/du_root new file mode 100755 index 0000000..5a7f26c --- /dev/null +++ b/du_root @@ -0,0 +1 @@ +du -hs /{bin,etc,home,lib,lib64,opt,root,sbin,usr,var}|sort -h diff --git a/iptables/del_rule b/iptables/del_rule new file mode 100644 index 0000000..006a8a2 --- /dev/null +++ b/iptables/del_rule @@ -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 diff --git a/iptables/fw_port b/iptables/fw_port new file mode 100755 index 0000000..0636cb6 --- /dev/null +++ b/iptables/fw_port @@ -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 diff --git a/iptables/new_iptables b/iptables/new_iptables new file mode 100755 index 0000000..91fcf55 --- /dev/null +++ b/iptables/new_iptables @@ -0,0 +1 @@ +cp iptables iptables.$(date "+%y%m%d-%H:%M") diff --git a/iptables/restore_iptables b/iptables/restore_iptables new file mode 100755 index 0000000..9ac9c6c --- /dev/null +++ b/iptables/restore_iptables @@ -0,0 +1 @@ +cat /etc/network/iptables | iptables-restore diff --git a/libvirt/clone_vm.sh b/libvirt/clone_vm.sh new file mode 100755 index 0000000..b7aa730 --- /dev/null +++ b/libvirt/clone_vm.sh @@ -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>/${NEW}<\/name>/" /tmp/${NEW}.xml +sed -i "s/.*<\/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 + diff --git a/libvirt/destroy_domains.sh b/libvirt/destroy_domains.sh new file mode 100755 index 0000000..b7ef2e4 --- /dev/null +++ b/libvirt/destroy_domains.sh @@ -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 diff --git a/libvirt/get_unbound_ips b/libvirt/get_unbound_ips new file mode 100755 index 0000000..df99f5a --- /dev/null +++ b/libvirt/get_unbound_ips @@ -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,}[^"]+' diff --git a/libvirt/init_domains.sh b/libvirt/init_domains.sh new file mode 100755 index 0000000..3e5c7ef --- /dev/null +++ b/libvirt/init_domains.sh @@ -0,0 +1,6 @@ +#!/bin/bash +while read -r domain; +do + echo "Will start $domain"; + virsh start $domain && sleep 10; +done < ../domains_start_order diff --git a/libvirt/kernel-qemu b/libvirt/kernel-qemu new file mode 120000 index 0000000..0750cdd --- /dev/null +++ b/libvirt/kernel-qemu @@ -0,0 +1 @@ +../kernel/kernel-qemu-4.19.50-buster \ No newline at end of file diff --git a/libvirt/list_domdisplay.sh b/libvirt/list_domdisplay.sh new file mode 100755 index 0000000..a30d947 --- /dev/null +++ b/libvirt/list_domdisplay.sh @@ -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 diff --git a/libvirt/list_ips.sh b/libvirt/list_ips.sh new file mode 100755 index 0000000..21a80a7 --- /dev/null +++ b/libvirt/list_ips.sh @@ -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); diff --git a/libvirt/list_ram_usage.sh b/libvirt/list_ram_usage.sh new file mode 100755 index 0000000..39fecfc --- /dev/null +++ b/libvirt/list_ram_usage.sh @@ -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 diff --git a/libvirt/net-add_static_lease.sh b/libvirt/net-add_static_lease.sh new file mode 100755 index 0000000..f5e23d4 --- /dev/null +++ b/libvirt/net-add_static_lease.sh @@ -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 diff --git a/libvirt/net-change_range.sh b/libvirt/net-change_range.sh new file mode 100755 index 0000000..f1d12c2 --- /dev/null +++ b/libvirt/net-change_range.sh @@ -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 + diff --git a/libvirt/net-dns_entry.sh b/libvirt/net-dns_entry.sh new file mode 100755 index 0000000..1894148 --- /dev/null +++ b/libvirt/net-dns_entry.sh @@ -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 + diff --git a/libvirt/net-edit_static_lease.sh b/libvirt/net-edit_static_lease.sh new file mode 100755 index 0000000..d1263bf --- /dev/null +++ b/libvirt/net-edit_static_lease.sh @@ -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 diff --git a/libvirt/resume_domains.sh b/libvirt/resume_domains.sh new file mode 100755 index 0000000..7720967 --- /dev/null +++ b/libvirt/resume_domains.sh @@ -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 diff --git a/libvirt/start_domains.sh b/libvirt/start_domains.sh new file mode 100755 index 0000000..5636d42 --- /dev/null +++ b/libvirt/start_domains.sh @@ -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 diff --git a/libvirt/start_rpi.sh b/libvirt/start_rpi.sh new file mode 100644 index 0000000..1e4b219 --- /dev/null +++ b/libvirt/start_rpi.sh @@ -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" diff --git a/libvirt/suspend_domains.sh b/libvirt/suspend_domains.sh new file mode 100755 index 0000000..5a45093 --- /dev/null +++ b/libvirt/suspend_domains.sh @@ -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 diff --git a/libvirt/switch_boot_device.sh b/libvirt/switch_boot_device.sh new file mode 100755 index 0000000..a2dab12 --- /dev/null +++ b/libvirt/switch_boot_device.sh @@ -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 diff --git a/libvirt/virt_install_rpi.sh b/libvirt/virt_install_rpi.sh new file mode 100755 index 0000000..9e3eadc --- /dev/null +++ b/libvirt/virt_install_rpi.sh @@ -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 diff --git a/lvm/create-snapshot-timestamp.sh b/lvm/create-snapshot-timestamp.sh new file mode 100755 index 0000000..b2839a7 --- /dev/null +++ b/lvm/create-snapshot-timestamp.sh @@ -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 diff --git a/lvm/create-snapshot.sh b/lvm/create-snapshot.sh new file mode 100755 index 0000000..d3446aa --- /dev/null +++ b/lvm/create-snapshot.sh @@ -0,0 +1,2 @@ +# create-snapshot.sh vg vol snap size +lvcreate --snapshot --name $3 --size $4 $1/$2 diff --git a/lvm/crypt_volume.sh b/lvm/crypt_volume.sh new file mode 100644 index 0000000..df635ef --- /dev/null +++ b/lvm/crypt_volume.sh @@ -0,0 +1 @@ +cryptsetup -s 512 luksFormat /dev/vg0/pics diff --git a/lvm/lvm-mount b/lvm/lvm-mount new file mode 100755 index 0000000..703a9ce --- /dev/null +++ b/lvm/lvm-mount @@ -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; diff --git a/lvm/lvm-umount b/lvm/lvm-umount new file mode 100755 index 0000000..672e213 --- /dev/null +++ b/lvm/lvm-umount @@ -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; diff --git a/lvm/lvmmount b/lvm/lvmmount new file mode 100755 index 0000000..d3d09ea --- /dev/null +++ b/lvm/lvmmount @@ -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 diff --git a/lvm/merge-snapshot.sh b/lvm/merge-snapshot.sh new file mode 100755 index 0000000..4dabb17 --- /dev/null +++ b/lvm/merge-snapshot.sh @@ -0,0 +1,3 @@ +# $1 : snapshot name +# $2 : vg name (vg0 by default= +lvconvert --merge ${2:-vg0}/$1 diff --git a/lvm/mount-lvm-part.sh b/lvm/mount-lvm-part.sh new file mode 100755 index 0000000..1adc5f7 --- /dev/null +++ b/lvm/mount-lvm-part.sh @@ -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 diff --git a/lvm/resize_a_partition b/lvm/resize_a_partition new file mode 100644 index 0000000..47d8af7 --- /dev/null +++ b/lvm/resize_a_partition @@ -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 diff --git a/lvm/snapmount_zered.sh b/lvm/snapmount_zered.sh new file mode 100755 index 0000000..4cb79d0 --- /dev/null +++ b/lvm/snapmount_zered.sh @@ -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