From 3f71e5e47055550d187eb1b60d889f82bb1de28f Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sun, 14 Feb 2021 18:59:26 +0100 Subject: update --- bin/open | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100755 bin/open (limited to 'bin') diff --git a/bin/open b/bin/open deleted file mode 100755 index 0fa97a7..0000000 --- a/bin/open +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh - -exec >/tmp/open.out 2>&1 -set -x - -cmd=xdg-open -case $1 in -(*.mkv|*.mp4) cmd=vlc;; -esac - -exec $cmd "$@" -- cgit v1.2.3 From 17b6d57450341896743f31ae67921d2abd8551bc Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Mon, 22 Feb 2021 17:10:51 +0100 Subject: updated --- bin/gauth | 4 +++- bin/p4a | 3 +++ bin/xt | 3 ++- 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100755 bin/p4a (limited to 'bin') diff --git a/bin/gauth b/bin/gauth index 575d6fc..699cd84 100755 --- a/bin/gauth +++ b/bin/gauth @@ -1,7 +1,9 @@ #!/bin/sh # Use backup from andOTP -gpg -qd ~/.otp_accounts.json.gpg | +#gpg -qd ~/otp_accounts.json.gpg.pgp | +#gpg -qd ~/.otp_accounts.json.gpg | +cat ~/otp_accounts.json | jq -r '.[] | "\(.label) \(.secret)"' | while read -r l s; do echo "$l $(oathtool --totp -b "$s")" diff --git a/bin/p4a b/bin/p4a new file mode 100755 index 0000000..30e54ff --- /dev/null +++ b/bin/p4a @@ -0,0 +1,3 @@ +#!/bin/sh +adb forward tcp:8222 tcp:8022 +ssh ssh://u0_a250@localhost:8222 diff --git a/bin/xt b/bin/xt index 71b92d3..9395b3e 100755 --- a/bin/xt +++ b/bin/xt @@ -1,4 +1,5 @@ #!/bin/sh -exec xterm -sl 500 -j -cr red "$@" & +#exec xterm -sl 500 -j -cr red "$@" & +exec xterm "$@" & #cmd="urxvtc ${@:--T $HOSTNAME}" #eval "$cmd" || { [ $? = 2 ] && urxvtd -q -o -f && eval "$cmd"; } -- cgit v1.2.3 From 45ecd3877ab52d73f4bec998ad456aa270ffddac Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Tue, 23 Feb 2021 14:52:10 +0100 Subject: update --- bin/update_hosts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100755 bin/update_hosts (limited to 'bin') diff --git a/bin/update_hosts b/bin/update_hosts new file mode 100755 index 0000000..6444550 --- /dev/null +++ b/bin/update_hosts @@ -0,0 +1,16 @@ +#!/bin/sh -ex + +# Update /etc/hosts with a well curated blacklist of malware, ads, porn, etc. +# Custom hosts are preserved. + +[ "$USER" = root ] || exec sudo "$0" "$@" +cd /etc +cp -p hosts hosts.old +hosts=$(awk '/^# Custom host /, /^# End of custom host /' hosts.old) +curl -s https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts | +awk 'BEGIN { hosts = ARGV[1]; ARGV[1] = "" } +/^# Custom host / { print hosts; next } +/^# End of custom host / { next } +{print}' "$hosts" > hosts.new +mv hosts.new hosts +[ "$(uname -s)" != Darwin ] || dscacheutil -flushcache -- cgit v1.2.3 From 9793ed6f2c0a89728d19a8b06d084c3c3fb102e5 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Wed, 24 Feb 2021 22:28:41 +0100 Subject: update --- bin/start_godoc | 3 +++ 1 file changed, 3 insertions(+) create mode 100755 bin/start_godoc (limited to 'bin') diff --git a/bin/start_godoc b/bin/start_godoc new file mode 100755 index 0000000..15bb70a --- /dev/null +++ b/bin/start_godoc @@ -0,0 +1,3 @@ +#!/bin/sh + +/Users/marc/go/bin/godoc >/tmp/godoc.out 2>&1 & -- cgit v1.2.3 From 9e34d600e3d8f8e50b8b87779c76c62924f41d2f Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sun, 28 Feb 2021 14:05:43 +0100 Subject: update --- bin/vm | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100755 bin/vm (limited to 'bin') diff --git a/bin/vm b/bin/vm new file mode 100755 index 0000000..22886d8 --- /dev/null +++ b/bin/vm @@ -0,0 +1,65 @@ +#!/bin/sh + +# vm is a tool to manage virtual machines. + +vm_version='vm-0.1' +unset CDPATH +export LC_ALL=C IFS=' +' + +add() { + usage 'add name' 'Add a new VM' && return + echo 'not implemented yet' +} + +del() { + usage 'del name' 'Delete a VM' && return + echo 'not implemented yet' +} + +die() { echo "$0: fatal: $@" >&2; exit 1; } + +help() { + usage 'help' 'Print this help text' && return + printf "$vm_version\nUsage: vm command [options] [args]\n" + Opth=1; for c in $Cmdlist; do $c; done +} + +info() { + usage 'info name' 'Print informations on a VM' && return + echo 'not implemented yet' +} + +list() { + usage 'list' 'list VMs' && return + echo 'not implemented yet' +} + +start() { + usage 'start name' 'Start a VM' && return + echo 'not implemented yet' +} + +stop() { + usage 'stop name' 'Stop a VM' && return + echo 'not implemented yet' +} + +usage() { [ "$Opth" ] && printf " %-34s %s\n" "$1" "$2"; } + +version() { + usage 'version' 'Print version' && return + echo "$vm_version" +} + +# Main starts here. +Cmdlist='add del info help list start stop version' +[ "$1" ] && C=$1 && shift 1 || { help; exit 1; } +for c in $Cmdlist +do + case $c in + ($C) cmd=$c; break;; + ($C*) [ "$cmd" ] && die "ambiguous command $C" || cmd=$c;; + esac +done +[ "$cmd" ] || die "no command \"$C\"" && $cmd "$@" -- cgit v1.2.3 From 737e83162766162be992f76e068386736abea71d Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sun, 28 Feb 2021 22:10:26 +0100 Subject: update --- bin/rdate | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100755 bin/rdate (limited to 'bin') diff --git a/bin/rdate b/bin/rdate new file mode 100755 index 0000000..a69d137 --- /dev/null +++ b/bin/rdate @@ -0,0 +1,11 @@ +#!/bin/sh + +# Set date on remote machine from local one. This version works +# from MacOS to AlpineLinux. It is intended to re-sync clock of a +# virtual machine after host wakeup. + +# TODO: option to display the time difference between host and remote. +# hint: use 'date +%s' to get timestamp in seconds since epoch. + +ssh "$1" "sudo date $(date +%m%d%H%M%Y.%S)" + -- cgit v1.2.3 From 9aa9c25a4d2ced1ea41d1d981b3fad9fe28a17a6 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sat, 6 Mar 2021 16:58:38 +0100 Subject: update --- bin/vm | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'bin') diff --git a/bin/vm b/bin/vm index 22886d8..4b0e960 100755 --- a/bin/vm +++ b/bin/vm @@ -21,7 +21,9 @@ die() { echo "$0: fatal: $@" >&2; exit 1; } help() { usage 'help' 'Print this help text' && return - printf "$vm_version\nUsage: vm command [options] [args]\n" + echo "$vm_version +Manage virtual machines +Usage: vm command [options] [args]" Opth=1; for c in $Cmdlist; do $c; done } -- cgit v1.2.3 From 050afa8def0739b459122b9d4c7fcf854ae1ed2a Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Fri, 12 Mar 2021 12:31:20 +0100 Subject: added wol. Updated vm --- bin/vm | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ bin/wol | 6 ++++++ 2 files changed, 68 insertions(+), 6 deletions(-) create mode 100755 bin/wol (limited to 'bin') diff --git a/bin/vm b/bin/vm index 4b0e960..5a8ea8d 100755 --- a/bin/vm +++ b/bin/vm @@ -9,7 +9,10 @@ export LC_ALL=C IFS=' add() { usage 'add name' 'Add a new VM' && return - echo 'not implemented yet' + [ -d "$idir/$1" ] && die "vm $1 already exists in $idir/$1" + init_alpine || die "could not init alpine iso" + mkdir -p "$idir/$1" && cd "$idir/$1" || die "add $1 failed" + pwd } del() { @@ -32,9 +35,54 @@ info() { echo 'not implemented yet' } -list() { - usage 'list' 'list VMs' && return - echo 'not implemented yet' +init() { + : +} + +init_alpine() { + mkdir -p "$sdir/alpine" && cd "$sdir/alpine" || die "init alpine failed" + [ -f Makefile ] || cat << \EOT > Makefile +# Do no edit. This file is generated by vm. +# Install a run a native AlpineLinux VM in Apple silicon MacOS + +# Check https://alpinelinux.org/downloads for possible upgrades +iso_url = https://dl-cdn.alpinelinux.org/alpine/v3.13/releases/aarch64/alpine-virt-3.13.2-aarch64.iso +iso = $(notdir $(iso_url)) +isoinfo ?= /opt/homebrew/bin/isoinfo + +all: initrd vmlinux + +runiso: initrd vmlinux $(iso) + vftool -k vmlinux -i initrd -d hdd.img -c $(iso) + +initrd: $(iso) $(isoinfo) + isoinfo -i $(iso) -J -x /boot/initramfs-virt > $@ + +vmlinux: $(iso) $(isoinfo) + isoinfo -i $(iso) -J -x /boot/vmlinuz-virt | gunzip > $@ + +$(iso): + curl -LO $(iso_url) || { rm -f $@; false; } + +$(isoinfo): + brew install cdrtools +EOT + + make -s all +} + +ls() { + usage 'ls' 'list VMs' && return + [ -d "$idir" ] && cd "$idir" || return + for i in *; do + echo "$i" + done +} + +runiso() { + usage 'runiso [isoname]' 'run a cdrom iso' && return + init_alpine + make runiso } start() { @@ -55,7 +103,15 @@ version() { } # Main starts here. -Cmdlist='add del info help list start stop version' +pdir="$HOME/.vm" +sdir="$pdir/iso" +edir="$pdir/engine" +idir="$pdir/img" + +engines="vftool" # Todo: add qemu (when supported on m1) +isos="alpine" # Todo: add freebsd, openbsd, archlinux, debian, ubuntu, macOS, win10, etc... + +Cmdlist='add del info help ls runiso start stop version' [ "$1" ] && C=$1 && shift 1 || { help; exit 1; } for c in $Cmdlist do @@ -64,4 +120,4 @@ do ($C*) [ "$cmd" ] && die "ambiguous command $C" || cmd=$c;; esac done -[ "$cmd" ] || die "no command \"$C\"" && $cmd "$@" +[ "$cmd" ] || { help; exit 1; } && $cmd "$@" diff --git a/bin/wol b/bin/wol new file mode 100755 index 0000000..39bce0d --- /dev/null +++ b/bin/wol @@ -0,0 +1,6 @@ +#!/bin/sh -x +ip_plex=192.168.1.90 mac_plex=b0:83:fe:61:a7:aa +eval "mac=\$mac_$1 ip=\$ip_$1" +wakeonlan "$mac" +ping -o "$ip" +ssh "root@$ip" -- cgit v1.2.3 From 556754fb516ff84965a510d0205f3c1d6cf64505 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Wed, 17 Mar 2021 16:17:42 +0100 Subject: improved vm --- bin/vm | 133 +++++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 88 insertions(+), 45 deletions(-) (limited to 'bin') diff --git a/bin/vm b/bin/vm index 5a8ea8d..e8f7684 100755 --- a/bin/vm +++ b/bin/vm @@ -1,32 +1,50 @@ #!/bin/sh -# vm is a tool to manage virtual machines. +# vm is a tool to manage and operate virtual machines. vm_version='vm-0.1' + +# TODO: +# - DONE: fetch, build and install vftool in .vm/vftool +# - setup a config file per VM +# - creation of hdd image +# - script install from CDROM iso: +# - setup system, including static IP address +# - setup user account from host +# - setup ssh from host +# - time synchronization from host +# + unset CDPATH export LC_ALL=C IFS=' ' add() { usage 'add name' 'Add a new VM' && return - [ -d "$idir/$1" ] && die "vm $1 already exists in $idir/$1" + [ -d "$dir/$1" ] && die "vm $1 already exists in $dir/$1" init_alpine || die "could not init alpine iso" - mkdir -p "$idir/$1" && cd "$idir/$1" || die "add $1 failed" + mkdir -p "$dir/$1" && cd "$dir/$1" || die "add $1 failed" pwd } +console() { + usage 'console name' 'Attach a console to a VM' && return + [ "$1" ] || die 'console: name is missing' + cd "$dir/$1" || die 'console $1 failed' + [ -f vftool.pid ] || die "vm $1 is not active" + screen -r "$1" +} + del() { usage 'del name' 'Delete a VM' && return echo 'not implemented yet' } -die() { echo "$0: fatal: $@" >&2; exit 1; } +die() { [ "$1" ] && echo "$@" >&2; exit 1; } help() { usage 'help' 'Print this help text' && return - echo "$vm_version -Manage virtual machines -Usage: vm command [options] [args]" + echo "$vm_version\nManage virtual machines\nUsage: vm command [options] [args]" Opth=1; for c in $Cmdlist; do $c; done } @@ -35,16 +53,9 @@ info() { echo 'not implemented yet' } -init() { - : -} - -init_alpine() { - mkdir -p "$sdir/alpine" && cd "$sdir/alpine" || die "init alpine failed" - [ -f Makefile ] || cat << \EOT > Makefile -# Do no edit. This file is generated by vm. -# Install a run a native AlpineLinux VM in Apple silicon MacOS +# CAUTION: be careful to preserve tabs in the following Makefile template string. +alpine_makefile='# Generated by "vm". DO NOT EDIT. # Check https://alpinelinux.org/downloads for possible upgrades iso_url = https://dl-cdn.alpinelinux.org/alpine/v3.13/releases/aarch64/alpine-virt-3.13.2-aarch64.iso iso = $(notdir $(iso_url)) @@ -52,9 +63,6 @@ isoinfo ?= /opt/homebrew/bin/isoinfo all: initrd vmlinux -runiso: initrd vmlinux $(iso) - vftool -k vmlinux -i initrd -d hdd.img -c $(iso) - initrd: $(iso) $(isoinfo) isoinfo -i $(iso) -J -x /boot/initramfs-virt > $@ @@ -63,36 +71,79 @@ vmlinux: $(iso) $(isoinfo) $(iso): curl -LO $(iso_url) || { rm -f $@; false; } + ln -sf $(iso) iso $(isoinfo): brew install cdrtools -EOT +' +init_alpine() { + mkdir -p "$dir/alpine-iso" && cd "$dir/alpine-iso" || die 'init alpine failed' + [ -f 'Makefile' ] || echo "$alpine_makefile" > Makefile make -s all } +init_vftool() { + [ -x "$dir/vftool" ] && return + cd '/tmp' && + git clone --depth=1 'https://github.com/evansm7/vftool' && + cd 'vftool' && + make && + cp 'build/vftool' "$dir/vftool" + rm -rf '/tmp/vftool' +} + +log() { + usage 'log name' 'print logs of a VM' && return + [ "$1" ] || die "log failed: name missing" + cd "$dir/$1" || die "log $1 failed" + cat vftool.log.old vftool.log 2>/dev/null +} + ls() { usage 'ls' 'list VMs' && return - [ -d "$idir" ] && cd "$idir" || return - for i in *; do - echo "$i" + [ -d "$dir" ] && cd "$dir" || return + for i in */; do + i=${i%/} + [ -f "$i/vftool.pid" ] && state=active || state=stopped + printf "%-20s %s\n" "$i" "$state" done } -runiso() { - usage 'runiso [isoname]' 'run a cdrom iso' && return - init_alpine - make runiso -} - start() { - usage 'start name' 'Start a VM' && return - echo 'not implemented yet' + usage 'start [-a][-c vm] name' 'Start a VM' && return + while getopts :ac: opt; do + case $opt in + (a) attach=1 ;; + (c) from=$OPTARG ;; + (*) Opth=1 start; exit;; + esac + done + shift $((OPTIND - 1)) + + [ "$1" ] || die "start failed: name missing" + init_vftool + cd "$dir/$1" || die "start $1 failed" + [ -f vftool.pid ] && die "Error: process $(cat vftool.pid) is active or $PWD/vftool.pid should be removed" + start_vm & } +start_vm() ( + trap 'rm -f vftool.pid' EXIT + [ -f vftool.log ] && cat vftool.log >> vftool.log.old + "$dir/vftool" -k vmlinux -i initrd -c iso >vftool.log 2>&1 & sleep 1 + exec 1>>vftool.log 2>&1 + echo "$!" >vftool.pid + screen -S "${PWD##*/}" -d -m "$(grep -om 1 '\/dev\/tty.*' vftool.log)" + wait +) + stop() { usage 'stop name' 'Stop a VM' && return - echo 'not implemented yet' + [ "$1" ] || die 'stop: name missing' + cd "$dir/$1" || die 'stop $1 failed' + [ -f vftool.pid ] || die "stop: vm $1 is not active" + kill $(cat vftool.pid) } usage() { [ "$Opth" ] && printf " %-34s %s\n" "$1" "$2"; } @@ -103,21 +154,13 @@ version() { } # Main starts here. -pdir="$HOME/.vm" -sdir="$pdir/iso" -edir="$pdir/engine" -idir="$pdir/img" - -engines="vftool" # Todo: add qemu (when supported on m1) -isos="alpine" # Todo: add freebsd, openbsd, archlinux, debian, ubuntu, macOS, win10, etc... - -Cmdlist='add del info help ls runiso start stop version' +dir="$HOME/.vm" +Cmdlist='add console del info help ls log start stop version' [ "$1" ] && C=$1 && shift 1 || { help; exit 1; } -for c in $Cmdlist -do +for c in $Cmdlist; do case $c in - ($C) cmd=$c; break;; - ($C*) [ "$cmd" ] && die "ambiguous command $C" || cmd=$c;; + ($C) cmd=$c; break ;; + ($C*) [ "$cmd" ] && die "ambiguous command $C" || cmd=$c ;; esac done [ "$cmd" ] || { help; exit 1; } && $cmd "$@" -- cgit v1.2.3 From d86227be78743b71c713a7ce6267cfcea73f6582 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Thu, 18 Mar 2021 16:28:30 +0100 Subject: update --- bin/vm | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'bin') diff --git a/bin/vm b/bin/vm index e8f7684..83ce0ea 100755 --- a/bin/vm +++ b/bin/vm @@ -1,12 +1,12 @@ #!/bin/sh -# vm is a tool to manage and operate virtual machines. +# vm is a command line tool to manage and operate virtual machines. vm_version='vm-0.1' # TODO: # - DONE: fetch, build and install vftool in .vm/vftool -# - setup a config file per VM +# - DONE: setup a config file per VM # - creation of hdd image # - script install from CDROM iso: # - setup system, including static IP address @@ -61,7 +61,7 @@ iso_url = https://dl-cdn.alpinelinux.org/alpine/v3.13/releases/aarch64/alpine-vi iso = $(notdir $(iso_url)) isoinfo ?= /opt/homebrew/bin/isoinfo -all: initrd vmlinux +all: initrd vmlinux config initrd: $(iso) $(isoinfo) isoinfo -i $(iso) -J -x /boot/initramfs-virt > $@ @@ -69,9 +69,11 @@ initrd: $(iso) $(isoinfo) vmlinux: $(iso) $(isoinfo) isoinfo -i $(iso) -J -x /boot/vmlinuz-virt | gunzip > $@ +config: $(iso) + @echo "iso=$(iso)" >config + $(iso): curl -LO $(iso_url) || { rm -f $@; false; } - ln -sf $(iso) iso $(isoinfo): brew install cdrtools @@ -79,7 +81,7 @@ $(isoinfo): init_alpine() { mkdir -p "$dir/alpine-iso" && cd "$dir/alpine-iso" || die 'init alpine failed' - [ -f 'Makefile' ] || echo "$alpine_makefile" > Makefile + [ -f 'Makefile' ] || printf "%s" "$alpine_makefile" > Makefile make -s all } @@ -129,10 +131,21 @@ start() { } start_vm() ( - trap 'rm -f vftool.pid' EXIT [ -f vftool.log ] && cat vftool.log >> vftool.log.old - "$dir/vftool" -k vmlinux -i initrd -c iso >vftool.log 2>&1 & sleep 1 - exec 1>>vftool.log 2>&1 + exec 1>vftool.log 2>&1 + . config || die "vm: could not source $PWD/config" + trap 'rm -f vftool.pid' EXIT + + "$dir/vftool" \ + -k vmlinux \ + -i initrd \ + ${iso+-c "$iso"} \ + ${hdd+-d "$hdd"} \ + ${cpu+-p "$cpu"} \ + ${ram+-m "$ram"} \ + ${arg+-m "$arg"} \ + >>vftool.log 2>&1 & sleep 1 + echo "$!" >vftool.pid screen -S "${PWD##*/}" -d -m "$(grep -om 1 '\/dev\/tty.*' vftool.log)" wait -- cgit v1.2.3 From a8e3586e9b691c2a25b5f2e50a2761d5fcad18f8 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Fri, 19 Mar 2021 22:27:11 +0100 Subject: update --- bin/vm | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'bin') diff --git a/bin/vm b/bin/vm index 83ce0ea..0eaa921 100755 --- a/bin/vm +++ b/bin/vm @@ -8,6 +8,7 @@ vm_version='vm-0.1' # - DONE: fetch, build and install vftool in .vm/vftool # - DONE: setup a config file per VM # - creation of hdd image +# - import iso, kernel, initrd from existing vm # - script install from CDROM iso: # - setup system, including static IP address # - setup user account from host @@ -20,13 +21,31 @@ export LC_ALL=C IFS=' ' add() { - usage 'add name' 'Add a new VM' && return + usage 'add [Options] name' 'Add a new VM' && return + while getopts :c:p:s: opt; do + case $opt in + c|k|p|s) eval "$opt=$OPTARG";; + *) Opth=1 add; exit ;; + esac + done + shift $((OPTIND - 1)) + [ -d "$dir/$1" ] && die "vm $1 already exists in $dir/$1" init_alpine || die "could not init alpine iso" mkdir -p "$dir/$1" && cd "$dir/$1" || die "add $1 failed" - pwd + hdd=${p-$1.raw} + [ -f "$hdd" ] || dd if=/dev/zero of=$hdd bs=1 count=0 seek=${s-32g} 2>/dev/null + echo "hdd=\"$hdd\"" > config + [ "$c" ] && { + [ -d "../$c" ] || die "invalid directory: $dir/$c" + iso=$(getconf "$c" iso) + [ "$iso" ] && echo "iso=\"../$c/$iso\"" >> config + cp "../$c/vmlinux" "../$c/initrd" . + } } +getconf() { awk -F '=' -v k="$2" '$1 == k {print $2}' "$dir/$1/config"; } + console() { usage 'console name' 'Attach a console to a VM' && return [ "$1" ] || die 'console: name is missing' @@ -128,6 +147,7 @@ start() { cd "$dir/$1" || die "start $1 failed" [ -f vftool.pid ] && die "Error: process $(cat vftool.pid) is active or $PWD/vftool.pid should be removed" start_vm & + [ "$attach" ] && sleep 1 && vm console $1 } start_vm() ( @@ -139,8 +159,8 @@ start_vm() ( "$dir/vftool" \ -k vmlinux \ -i initrd \ - ${iso+-c "$iso"} \ ${hdd+-d "$hdd"} \ + ${iso+-c "$iso"} \ ${cpu+-p "$cpu"} \ ${ram+-m "$ram"} \ ${arg+-m "$arg"} \ -- cgit v1.2.3 From 262eb286cb7c8d7412160cacfb022247b1f09f00 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Mon, 22 Mar 2021 08:50:26 +0100 Subject: update --- bin/vm | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 17 deletions(-) (limited to 'bin') diff --git a/bin/vm b/bin/vm index 0eaa921..11fed61 100755 --- a/bin/vm +++ b/bin/vm @@ -7,9 +7,12 @@ vm_version='vm-0.1' # TODO: # - DONE: fetch, build and install vftool in .vm/vftool # - DONE: setup a config file per VM -# - creation of hdd image -# - import iso, kernel, initrd from existing vm +# - DONE: creation of hdd image +# - DONE: delete a VM +# - DONE: import iso, kernel, initrd from existing vm +# - when no vm is specified, apply command to last one # - script install from CDROM iso: +# - patch alpine setup-disk # - setup system, including static IP address # - setup user account from host # - setup ssh from host @@ -22,9 +25,9 @@ export LC_ALL=C IFS=' add() { usage 'add [Options] name' 'Add a new VM' && return - while getopts :c:p:s: opt; do + while getopts :c:i:k:p:s: opt; do case $opt in - c|k|p|s) eval "$opt=$OPTARG";; + [cikps]) eval "$opt=$OPTARG";; *) Opth=1 add; exit ;; esac done @@ -32,6 +35,7 @@ add() { [ -d "$dir/$1" ] && die "vm $1 already exists in $dir/$1" init_alpine || die "could not init alpine iso" + [ "$1" = 'alpine-iso' ] && return mkdir -p "$dir/$1" && cd "$dir/$1" || die "add $1 failed" hdd=${p-$1.raw} [ -f "$hdd" ] || dd if=/dev/zero of=$hdd bs=1 count=0 seek=${s-32g} 2>/dev/null @@ -41,11 +45,11 @@ add() { iso=$(getconf "$c" iso) [ "$iso" ] && echo "iso=\"../$c/$iso\"" >> config cp "../$c/vmlinux" "../$c/initrd" . + echo "kernel=${k-vmlinux}" >> config + echo "initrd=initrd" >> config } } -getconf() { awk -F '=' -v k="$2" '$1 == k {print $2}' "$dir/$1/config"; } - console() { usage 'console name' 'Attach a console to a VM' && return [ "$1" ] || die 'console: name is missing' @@ -56,10 +60,39 @@ console() { del() { usage 'del name' 'Delete a VM' && return - echo 'not implemented yet' + case $1 in ''|*/*|.*) die "invalid VM name: $1" ;; esac + [ -d "$dir/$1" ] || die "$dir/$1 not found" + cd "$dir" && rm -r "$1" +} + +die() { [ "$1" ] && echo "$0: $@" >&2; exit 1; } + +edit() { + usage 'edit name' 'Edit a VM configuration' && return + [ -f "$dir/$1/config" ] || die "$dir/$1/config not found" + ${EDITOR-vi} "$dir/$1/config" +} + +exp() { + usage 'exp name' 'experiment on a VM' && return + cd "$dir/$1" || die "invalid VM: $1" + [ -f vftool.pid ] || die "vm $1 is not active" + + screen -X stuff 'root +' + screen -X stuff 'sed -i "s/die..Bootloader/;; #/" /sbin/setup-disk +' + screen -X stuff 'grep BOOTLOADER /sbin/setup-disk +' + } -die() { [ "$1" ] && echo "$@" >&2; exit 1; } +finalize() { + tty=$(vftool_tty) + echo "root\nuname -a" >> "$tty" +} + +getconf() { awk -F '=' -v k="$2" '$1 == k {print $2}' "$dir/$1/config"; } help() { usage 'help' 'Print this help text' && return @@ -90,6 +123,11 @@ vmlinux: $(iso) $(isoinfo) config: $(iso) @echo "iso=$(iso)" >config + @echo "initrd=initrd" >>config + @echo "kernel=vmlinux" >>config + @echo "cpu=1" >> config + @echo "ram=512" >> config + @echo "arg=\"console=hvc0\"" >> config $(iso): curl -LO $(iso_url) || { rm -f $@; false; } @@ -126,17 +164,19 @@ ls() { [ -d "$dir" ] && cd "$dir" || return for i in */; do i=${i%/} + [ "$i" = '*' ] && continue [ -f "$i/vftool.pid" ] && state=active || state=stopped printf "%-20s %s\n" "$i" "$state" done } start() { - usage 'start [-a][-c vm] name' 'Start a VM' && return - while getopts :ac: opt; do + usage 'start [-af][-c vm] name' 'Start a VM' && return + while getopts :ac:f opt; do case $opt in - (a) attach=1 ;; + (a) a=1 ;; (c) from=$OPTARG ;; + (f) f=1 ;; (*) Opth=1 start; exit;; esac done @@ -146,8 +186,9 @@ start() { init_vftool cd "$dir/$1" || die "start $1 failed" [ -f vftool.pid ] && die "Error: process $(cat vftool.pid) is active or $PWD/vftool.pid should be removed" - start_vm & - [ "$attach" ] && sleep 1 && vm console $1 + start_vm & sleep 2 + [ "$f" ] && finalize + [ "$a" ] && vm console $1 } start_vm() ( @@ -157,8 +198,8 @@ start_vm() ( trap 'rm -f vftool.pid' EXIT "$dir/vftool" \ - -k vmlinux \ - -i initrd \ + ${kernel+-k "$kernel"} \ + ${initrd+-i "$initrd"} \ ${hdd+-d "$hdd"} \ ${iso+-c "$iso"} \ ${cpu+-p "$cpu"} \ @@ -167,7 +208,7 @@ start_vm() ( >>vftool.log 2>&1 & sleep 1 echo "$!" >vftool.pid - screen -S "${PWD##*/}" -d -m "$(grep -om 1 '\/dev\/tty.*' vftool.log)" + screen -S "${PWD##*/}" -d -m "$(vftool_tty)" wait ) @@ -186,9 +227,11 @@ version() { echo "$vm_version" } +vftool_tty() { grep -om 1 '\/dev\/tty.*' 'vftool.log'; } + # Main starts here. dir="$HOME/.vm" -Cmdlist='add console del info help ls log start stop version' +Cmdlist='add console del edit exp info help ls log start stop version' [ "$1" ] && C=$1 && shift 1 || { help; exit 1; } for c in $Cmdlist; do case $c in -- cgit v1.2.3 From 73aec290b0f95b7be7ef952b042359bb95b18645 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Fri, 26 Mar 2021 10:38:56 +0100 Subject: improve update_hosts --- bin/update_hosts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'bin') diff --git a/bin/update_hosts b/bin/update_hosts index 6444550..793344c 100755 --- a/bin/update_hosts +++ b/bin/update_hosts @@ -1,9 +1,10 @@ -#!/bin/sh -ex +#!/bin/sh -e # Update /etc/hosts with a well curated blacklist of malware, ads, porn, etc. # Custom hosts are preserved. [ "$USER" = root ] || exec sudo "$0" "$@" +echo "last update: $(date -r /etc/hosts)" cd /etc cp -p hosts hosts.old hosts=$(awk '/^# Custom host /, /^# End of custom host /' hosts.old) @@ -14,3 +15,4 @@ awk 'BEGIN { hosts = ARGV[1]; ARGV[1] = "" } {print}' "$hosts" > hosts.new mv hosts.new hosts [ "$(uname -s)" != Darwin ] || dscacheutil -flushcache +diff -u /etc/hosts.old /etc/hosts | diffstat -- cgit v1.2.3 From 9734e798057795f89a7bd0a0d933745f0d249b2f Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Fri, 26 Mar 2021 12:01:52 +0100 Subject: improve update_hosts --- bin/update_hosts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'bin') diff --git a/bin/update_hosts b/bin/update_hosts index 793344c..8d148a6 100755 --- a/bin/update_hosts +++ b/bin/update_hosts @@ -3,8 +3,18 @@ # Update /etc/hosts with a well curated blacklist of malware, ads, porn, etc. # Custom hosts are preserved. + [ "$USER" = root ] || exec sudo "$0" "$@" -echo "last update: $(date -r /etc/hosts)" + +echo "Check from https://github.com/StevenBlack/hosts" +lsd=$(curl -s "https://api.github.com/repos/StevenBlack/hosts/commits?path=hosts&page=1&per_page=1"| jq -r '.[0].commit.committer.date') +echo "last source update: $(date -j -f "%FT%TZ" "$lsd")" +echo "last local update: $(date -r /etc/hosts)" +if [ $(date -j -f "%FT%TZ" "$lsd" +%s) -lt $(date -r /etc/hosts +%s) ]; then + echo "Nothing to do" + exit +fi + cd /etc cp -p hosts hosts.old hosts=$(awk '/^# Custom host /, /^# End of custom host /' hosts.old) -- cgit v1.2.3 From a8243acb9f5cabd19cc9df16a15018129e0b225e Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Fri, 26 Mar 2021 13:50:43 +0100 Subject: improve update_hosts --- bin/update_hosts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'bin') diff --git a/bin/update_hosts b/bin/update_hosts index 8d148a6..4427cd2 100755 --- a/bin/update_hosts +++ b/bin/update_hosts @@ -10,19 +10,18 @@ echo "Check from https://github.com/StevenBlack/hosts" lsd=$(curl -s "https://api.github.com/repos/StevenBlack/hosts/commits?path=hosts&page=1&per_page=1"| jq -r '.[0].commit.committer.date') echo "last source update: $(date -j -f "%FT%TZ" "$lsd")" echo "last local update: $(date -r /etc/hosts)" -if [ $(date -j -f "%FT%TZ" "$lsd" +%s) -lt $(date -r /etc/hosts +%s) ]; then - echo "Nothing to do" - exit -fi +[ $(date -j -f "%FT%TZ" "$lsd" +%s) -lt $(date -r /etc/hosts +%s) ] && echo "Nothing to do" && exit cd /etc cp -p hosts hosts.old hosts=$(awk '/^# Custom host /, /^# End of custom host /' hosts.old) -curl -s https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts | -awk 'BEGIN { hosts = ARGV[1]; ARGV[1] = "" } -/^# Custom host / { print hosts; next } -/^# End of custom host / { next } -{print}' "$hosts" > hosts.new +curl -s 'https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts' | awk ' + BEGIN { hosts = ARGV[1]; ARGV[1] = "" } + /^# Custom host / { print hosts; next } + /^# End of custom host / { next } + { print } +' "$hosts" > hosts.new + mv hosts.new hosts [ "$(uname -s)" != Darwin ] || dscacheutil -flushcache diff -u /etc/hosts.old /etc/hosts | diffstat -- cgit v1.2.3 From ae04cc260d63765fc20a2d7bb21d121986f71445 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Mon, 29 Mar 2021 10:26:13 +0200 Subject: update --- bin/icat | 2 ++ bin/vm | 50 +++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 45 insertions(+), 7 deletions(-) (limited to 'bin') diff --git a/bin/icat b/bin/icat index 0170513..d363e09 100755 --- a/bin/icat +++ b/bin/icat @@ -2,6 +2,8 @@ # Display images directly in terminal. # Tested with xterm v361. Depends on imagemagick convert(1). +[ "$LC_TERMINAL" = iTerm2 ] && [ -x "$HOME/.iterm2/imgcat" ] && exec "$HOME/.iterm2/imgcat" "$@" + # maxsize prints the geomtry size of terminal window, with # a maximum value of 1000 pixels for width and height, or # 640x480 if terminal size can not be probed. diff --git a/bin/vm b/bin/vm index 11fed61..818d6f8 100755 --- a/bin/vm +++ b/bin/vm @@ -62,7 +62,8 @@ del() { usage 'del name' 'Delete a VM' && return case $1 in ''|*/*|.*) die "invalid VM name: $1" ;; esac [ -d "$dir/$1" ] || die "$dir/$1 not found" - cd "$dir" && rm -r "$1" + [ -f "$dir/$1/vftool.pid" ] && die "vm $1 is still active, stop it first" + rm -rf "$dir/$1" } die() { [ "$1" ] && echo "$0: $@" >&2; exit 1; } @@ -78,13 +79,48 @@ exp() { cd "$dir/$1" || die "invalid VM: $1" [ -f vftool.pid ] || die "vm $1 is not active" - screen -X stuff 'root + sleep 1 && screen -X stuff 'root ' - screen -X stuff 'sed -i "s/die..Bootloader/;; #/" /sbin/setup-disk + sleep 1 && screen -X stuff 'sed -i.bak "s/die..Bootloader/;; # die \"Bootloader/" /sbin/setup-disk ' - screen -X stuff 'grep BOOTLOADER /sbin/setup-disk + sleep 1 && screen -X stuff 'setup-alpine -e +' + sleep 1 && screen -X stuff 'none +' + sleep 1 && screen -X stuff "$1 +" + sleep 1 && screen -X stuff 'eth0 +' + sleep 1 && screen -X stuff '192.168.64.2 +' + sleep 1 && screen -X stuff '255.255.255.0 +' + sleep 1 && screen -X stuff '192.168.64.1 +' + sleep 1 && screen -X stuff 'n +' + sleep 3 && screen -X stuff ' +' + sleep 1 && screen -X stuff '192.168.64.1 +' + sleep 1 && screen -X stuff 'Europe/Paris +' + sleep 5 && screen -X stuff 'none +' + sleep 1 && screen -X stuff 'chrony +' + sleep 5 && screen -X stuff '1 +' + sleep 1 && screen -X stuff 'openssh +' + sleep 5 && screen -X stuff 'vda +' + sleep 3 && screen -X stuff 'sys +' + sleep 1 && screen -X stuff 'y +' + sleep 20 && screen -X stuff 'cat /etc/fstab ' - } finalize() { @@ -109,7 +145,7 @@ info() { alpine_makefile='# Generated by "vm". DO NOT EDIT. # Check https://alpinelinux.org/downloads for possible upgrades -iso_url = https://dl-cdn.alpinelinux.org/alpine/v3.13/releases/aarch64/alpine-virt-3.13.2-aarch64.iso +iso_url = https://dl-cdn.alpinelinux.org/alpine/v3.13/releases/aarch64/alpine-virt-3.13.3-aarch64.iso iso = $(notdir $(iso_url)) isoinfo ?= /opt/homebrew/bin/isoinfo @@ -204,7 +240,7 @@ start_vm() ( ${iso+-c "$iso"} \ ${cpu+-p "$cpu"} \ ${ram+-m "$ram"} \ - ${arg+-m "$arg"} \ + -a "${arg-console=hvc0}" \ >>vftool.log 2>&1 & sleep 1 echo "$!" >vftool.pid -- cgit v1.2.3 From 3f9cb5afb3b6ee2d4234d5e4d3e5398cf022bc41 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sat, 10 Apr 2021 14:15:57 +0200 Subject: update vm --- bin/vm | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'bin') diff --git a/bin/vm b/bin/vm index 818d6f8..19255d2 100755 --- a/bin/vm +++ b/bin/vm @@ -47,6 +47,9 @@ add() { cp "../$c/vmlinux" "../$c/initrd" . echo "kernel=${k-vmlinux}" >> config echo "initrd=initrd" >> config + echo "cpu=1" >> config + echo "ram=512" >> config + echo "arg=\"console=hvc0\"" >> config } } @@ -119,7 +122,7 @@ exp() { ' sleep 1 && screen -X stuff 'y ' - sleep 20 && screen -X stuff 'cat /etc/fstab + sleep 20 && screen -X stuff 'blkid /dev/vda3 ' } @@ -253,7 +256,7 @@ stop() { [ "$1" ] || die 'stop: name missing' cd "$dir/$1" || die 'stop $1 failed' [ -f vftool.pid ] || die "stop: vm $1 is not active" - kill $(cat vftool.pid) + kill $(cat vftool.pid) || rm -f vftool.pid } usage() { [ "$Opth" ] && printf " %-34s %s\n" "$1" "$2"; } -- cgit v1.2.3 From 6962b31552fbf1a2516b0777cc4a52d99a76f6f2 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Wed, 5 May 2021 20:32:05 +0200 Subject: update --- bin/update_hosts | 1 - 1 file changed, 1 deletion(-) (limited to 'bin') diff --git a/bin/update_hosts b/bin/update_hosts index 4427cd2..75c41d1 100755 --- a/bin/update_hosts +++ b/bin/update_hosts @@ -3,7 +3,6 @@ # Update /etc/hosts with a well curated blacklist of malware, ads, porn, etc. # Custom hosts are preserved. - [ "$USER" = root ] || exec sudo "$0" "$@" echo "Check from https://github.com/StevenBlack/hosts" -- cgit v1.2.3 From ebfa4aaf6afe44ffcc15261d8333c861c62ae27d Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sun, 9 May 2021 16:59:33 +0200 Subject: add gemini --- bin/gemini | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100755 bin/gemini (limited to 'bin') diff --git a/bin/gemini b/bin/gemini new file mode 100755 index 0000000..1bc3d57 --- /dev/null +++ b/bin/gemini @@ -0,0 +1,132 @@ +#!/usr/bin/env yaegi +package main + +import ( + "bufio" + "crypto/tls" + "fmt" + "io/ioutil" + "net/url" + "os" + "strconv" + "strings" +) + +func main() { + stdinReader := bufio.NewReader(os.Stdin) + var u string // URL + links := make([]string, 0, 100) + history := make([]string, 0, 100) + for { + fmt.Print("> ") + cmd, _ := stdinReader.ReadString('\n') + cmd = strings.TrimSpace(cmd) + // Command dispatch + switch strings.ToLower(cmd) { + case "": // Nothing + continue + case "q": // Quit + fmt.Println("Bye!") + os.Exit(0) + case "b": // Back + if len(history) < 2 { + fmt.Println("No history yet!") + continue + } + u = history[len(history)-2] + history = history[0 : len(history)-2] + default: + index, err := strconv.Atoi(cmd) + if err != nil { + // Treat this as a URL + u = cmd + if !strings.HasPrefix(u, "gemini://") { + u = "gemini://" + u + } + } else { + // Treat this as a menu lookup + u = links[index-1] + } + } + // Parse URL + parsed, err := url.Parse(u) + if err != nil { + fmt.Println("Error parsing URL!") + continue + } + // Connect to server + conn, err := tls.Dial("tcp", parsed.Host+":1965", &tls.Config{InsecureSkipVerify: true}) + if err != nil { + fmt.Println("Failed to connect: " + err.Error()) + continue + } + defer conn.Close() + // Send request + conn.Write([]byte(u + "\r\n")) + // Receive and parse response header + reader := bufio.NewReader(conn) + responseHeader, err := reader.ReadString('\n') + parts := strings.Fields(responseHeader) + status, err := strconv.Atoi(parts[0][0:1]) + meta := parts[1] + // Switch on status code + switch status { + case 1, 3, 6: + // No input, redirects or client certs + fmt.Println("Unsupported feature!") + case 2: + // Successful transaction + // text/* content only + if !strings.HasPrefix(meta, "text/") { + fmt.Println("Unsupported type " + meta) + continue + } + // Read everything + bodyBytes, err := ioutil.ReadAll(reader) + if err != nil { + fmt.Println("Error reading body") + continue + } + body := string(bodyBytes) + if meta == "text/gemini" { + // Handle Gemini map + links = make([]string, 0, 100) + preformatted := false + for _, line := range strings.Split(body, "\n") { + if strings.HasPrefix(line, "```") { + preformatted = !preformatted + } else if preformatted { + fmt.Println(line) + } else if strings.HasPrefix(line, "=>") { + line = line[2:] + bits := strings.Fields(line) + parsedLink, err := url.Parse(bits[0]) + if err != nil { + continue + } + link := parsed.ResolveReference(parsedLink).String() + var label string + if len(bits) == 1 { + label = link + } else { + label = strings.Join(bits[1:], " ") + } + links = append(links, link) + fmt.Printf("[%d] %s\n", len(links), label) + } else { + // This should really be wrapped, but there's + // no easy support for this in Go's standard + // library + fmt.Println(line) + } + } + } else { + // Just print any other kind of text + fmt.Print(body) + } + history = append(history, u) + case 4, 5: + fmt.Println("ERROR: " + meta) + } + } +} -- cgit v1.2.3 From 547f1550666700a087acb13f1cc3ef010bbb78fc Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sat, 15 May 2021 18:11:23 +0200 Subject: fvwm is back --- bin/wol | 6 ++++++ bin/xt | 1 + 2 files changed, 7 insertions(+) create mode 100755 bin/wol (limited to 'bin') diff --git a/bin/wol b/bin/wol new file mode 100755 index 0000000..146e46a --- /dev/null +++ b/bin/wol @@ -0,0 +1,6 @@ +#!/bin/sh -x +ip_plex=192.168.1.90 mac_plex=b0:83:fe:61:a7:aa +eval "mac=\$mac_$1 ip=\$ip_$1" +wakeonlan "$mac" +#ping -o "$ip" +ssh "root@$ip" diff --git a/bin/xt b/bin/xt index 71b92d3..f14954c 100755 --- a/bin/xt +++ b/bin/xt @@ -1,4 +1,5 @@ #!/bin/sh exec xterm -sl 500 -j -cr red "$@" & #cmd="urxvtc ${@:--T $HOSTNAME}" +#cmd="urxvtc $@" #eval "$cmd" || { [ $? = 2 ] && urxvtd -q -o -f && eval "$cmd"; } -- cgit v1.2.3 From 67efb5857e720ebbe323ea7284793c43f27b0f67 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sat, 29 May 2021 18:16:42 +0200 Subject: update --- bin/md | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100755 bin/md (limited to 'bin') diff --git a/bin/md b/bin/md new file mode 100755 index 0000000..ca08090 --- /dev/null +++ b/bin/md @@ -0,0 +1,136 @@ +#!/bin/sh + +# See https://spec.commonmark.org for reference +md2html() { + awk ' + function newblock(nb) { + if (text) print "<" block ">" text "" + text = "" + block = nb ? nb : "p" + } + + function subinline(tgl, inl) { + while (match($0, tgl)) { + if (inline[ni] == inl) + ni -= sub(tgl, "") + else if (sub(tgl, "<" inl ">")) + inline[++ni] = inl + } + } + + function dolink(href, lnk) { + # Undo escaped html in uris + gsub(/&/, "\\&", href) + gsub(/</, "<", href) + gsub(/>/, ">", href) + # & can be tricky, and not standard: + gsub(/&/, "\\\\\\&", href) + gsub(/&/, "\\\\\\&", lnk) + return "" lnk "" + } + + BEGIN { ni = 0; nl = 0; text = ""; block = "p" } + + # Escape html + esc != "false" { gsub("&", "\\&"); gsub("<", "\\<"); gsub(">", "\\>") } + + # Horizontal rules (_ is not in markdown) + /^[ ]*([-*_] ?)+[ ]*$/ && text == "" { print "
"; next } + + # Tables (not in markdown) + # Syntax: + # Right Align| Center Align |Left Align + /([ ]\|)|(\|[ ])/ { + if (block != "table") newblock("table") + nc = split($0, cells, "|") + $0 = "\n" + for (i = 1; i <= nc; i++) { + align = "left" + if (sub(/^[ ]+/, "", cells[i])) { + if (sub(/[ ]+$/, "", cells[i])) + align = "center" + else + align = "right" + } + sub(/[ ]+$/, "", cells[i]) + $0 = $0 "" cells[i] "\n" + } + $0 = $0 "" + } + + # Ordered and unordered (possibly nested) lists + /^[ ]*([*+-]|(([0-9]+[\.-]?)+))[ ]/ { + newblock("li") + nnl = 1 + while (match($0, /^[ ]/)) { sub(/^[ ]/,""); nnl++ } + while (nl > nnl) print "" + while (nl < nnl) { + list[++nl] = "ol" + if (match($0, /^[*+-]/)) list[nl] = "ul" + print "<" list[nl] ">" + } + sub(/^([*+-]|(([0-9]+[\.-]?)+))[ ]/,"") + } + + # Multi line list items + block == "li" { sub(/^( *)|( *)/,"") } + + # Code blocks + /^( | )/ { + if (block != "code") newblock("code") + sub(/^( | )/, "") + text = text $0 "\n" + next + } + + # Paragraph + /^$/ { newblock(); while(nl > 0) print "" } + + # Setex-style Headers (plus h3 with underscores) + /^=+$/ { block = "h" 1; next } + /^-+$/ { block = "h" 2; next } + /^_+$/ { block = "h" 3; next } + + # Atx-style headers + /^#/ { + newblock() + match($0, /#+/) + n = RLENGTH + if (n > 6) n = 6 + sub(/# */, "#") + text = substr($0, RLENGTH + 1) + block = "h" n + next + } + + { + # Images + while (match($0, /!\[[^\]]+\]\([^\)]+\)/)) { + split(substr($0, RSTART, RLENGTH), a, /(!\[)|\)|(\]\()/) + sub(/!\[[^\]]+\]\([^\)]+\)/, "\""") + } + # Links + while (match($0, /\[[^\]]+\]\([^\)]+\)/)) { + split(substr($0, RSTART, RLENGTH), a, /[\[\)]|(\]\()/) + sub(/\[[^\]]+\]\([^\)]+\)/, dolink(a[3], a[2])) + } + # Auto links (uri matching is poor) + na = split($0, a, /(^\()|[ ]|([,\.\)]([ ]|$))/) + for (i = 1; i <= na; i++) + if (match(a[i], /^(((https?|ftp|file|news|irc):\/\/)|(mailto:)).+$/)) + sub(a[i], dolink(a[i], a[i])) + # Inline + subinline("(\\*\\*)|(__)", "strong") + subinline("\\*", "em") + subinline("`", "code") + text = text (text ? " " : "") $0 + } + + END { + while(ni > 0) text = text "" + newblock() + while(nl > 0) print "" + }' "$1" +} + +md2html "$1" -- cgit v1.2.3 From 2dc5563129877c1aad571e2023a7e9cc8eec5d04 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sun, 30 May 2021 19:01:16 +0200 Subject: update --- bin/vm | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) (limited to 'bin') diff --git a/bin/vm b/bin/vm index 19255d2..b0e1303 100755 --- a/bin/vm +++ b/bin/vm @@ -28,7 +28,7 @@ add() { while getopts :c:i:k:p:s: opt; do case $opt in [cikps]) eval "$opt=$OPTARG";; - *) Opth=1 add; exit ;; + *) Opth=1 add "$1"; exit ;; esac done shift $((OPTIND - 1)) @@ -38,25 +38,27 @@ add() { [ "$1" = 'alpine-iso' ] && return mkdir -p "$dir/$1" && cd "$dir/$1" || die "add $1 failed" hdd=${p-$1.raw} - [ -f "$hdd" ] || dd if=/dev/zero of=$hdd bs=1 count=0 seek=${s-32g} 2>/dev/null + [ -f "$hdd" ] || dd if=/dev/zero of="$hdd" bs=1 count=0 seek="${s-32g}" 2>/dev/null echo "hdd=\"$hdd\"" > config [ "$c" ] && { [ -d "../$c" ] || die "invalid directory: $dir/$c" iso=$(getconf "$c" iso) - [ "$iso" ] && echo "iso=\"../$c/$iso\"" >> config cp "../$c/vmlinux" "../$c/initrd" . - echo "kernel=${k-vmlinux}" >> config - echo "initrd=initrd" >> config - echo "cpu=1" >> config - echo "ram=512" >> config - echo "arg=\"console=hvc0\"" >> config + [ "$iso" ] && echo "iso=\"../$c/$iso\"" >> config + cat <<- EOT >> config + kernel=${k-vmlinux} + initrd=initrd + cpu=1 + ram=512 + arg="console=hvc0" + EOT } } console() { usage 'console name' 'Attach a console to a VM' && return [ "$1" ] || die 'console: name is missing' - cd "$dir/$1" || die 'console $1 failed' + cd "$dir/$1" || die "console $1 failed" [ -f vftool.pid ] || die "vm $1 is not active" screen -r "$1" } @@ -66,10 +68,10 @@ del() { case $1 in ''|*/*|.*) die "invalid VM name: $1" ;; esac [ -d "$dir/$1" ] || die "$dir/$1 not found" [ -f "$dir/$1/vftool.pid" ] && die "vm $1 is still active, stop it first" - rm -rf "$dir/$1" + rm -rf "${dir:?}/$1" } -die() { [ "$1" ] && echo "$0: $@" >&2; exit 1; } +die() { [ "$1" ] && echo "$0: $*" >&2; exit 1; } edit() { usage 'edit name' 'Edit a VM configuration' && return @@ -128,14 +130,14 @@ exp() { finalize() { tty=$(vftool_tty) - echo "root\nuname -a" >> "$tty" + printf 'root\nuname -a\n' >> "$tty" } getconf() { awk -F '=' -v k="$2" '$1 == k {print $2}' "$dir/$1/config"; } help() { usage 'help' 'Print this help text' && return - echo "$vm_version\nManage virtual machines\nUsage: vm command [options] [args]" + printf '%s\n' "$vm_version\nManage virtual machines\nUsage: vm command [options] [args]" Opth=1; for c in $Cmdlist; do $c; done } @@ -210,13 +212,12 @@ ls() { } start() { - usage 'start [-af][-c vm] name' 'Start a VM' && return - while getopts :ac:f opt; do + usage 'start [-af] name' 'Start a VM' && return + while getopts :af opt; do case $opt in (a) a=1 ;; - (c) from=$OPTARG ;; (f) f=1 ;; - (*) Opth=1 start; exit;; + (*) Opth=1 start "$1"; exit;; esac done shift $((OPTIND - 1)) @@ -227,7 +228,7 @@ start() { [ -f vftool.pid ] && die "Error: process $(cat vftool.pid) is active or $PWD/vftool.pid should be removed" start_vm & sleep 2 [ "$f" ] && finalize - [ "$a" ] && vm console $1 + [ "$a" ] && vm console "$1" } start_vm() ( @@ -254,9 +255,9 @@ start_vm() ( stop() { usage 'stop name' 'Stop a VM' && return [ "$1" ] || die 'stop: name missing' - cd "$dir/$1" || die 'stop $1 failed' + cd "$dir/$1" || die "stop $1 failed" [ -f vftool.pid ] || die "stop: vm $1 is not active" - kill $(cat vftool.pid) || rm -f vftool.pid + kill "$(cat vftool.pid)" || rm -f vftool.pid } usage() { [ "$Opth" ] && printf " %-34s %s\n" "$1" "$2"; } @@ -274,8 +275,8 @@ Cmdlist='add console del edit exp info help ls log start stop version' [ "$1" ] && C=$1 && shift 1 || { help; exit 1; } for c in $Cmdlist; do case $c in - ($C) cmd=$c; break ;; - ($C*) [ "$cmd" ] && die "ambiguous command $C" || cmd=$c ;; + ("$C") cmd=$c; break ;; + ("$C"*) [ "$cmd" ] && die "ambiguous command $C" || cmd=$c ;; esac done [ "$cmd" ] || { help; exit 1; } && $cmd "$@" -- cgit v1.2.3 From 87b63654a7a881afefddaeb6c3dbbf32105070bb Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Fri, 4 Jun 2021 16:43:08 +0200 Subject: update, add fv --- bin/fv | 6 ++++++ bin/update_hosts | 6 +++--- bin/vm | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) create mode 100755 bin/fv (limited to 'bin') diff --git a/bin/fv b/bin/fv new file mode 100755 index 0000000..5662ed3 --- /dev/null +++ b/bin/fv @@ -0,0 +1,6 @@ +#!/bin/sh +# quick file viewer +#exec fzf --multi --preview-window=right:66% --preview 'cat {1}' +exec fzf --ansi --multi --preview-window=right:75% \ + --bind=left:preview-page-up --bind=right:preview-page-down \ + --preview 'cat {1}' diff --git a/bin/update_hosts b/bin/update_hosts index 75c41d1..e7321bf 100755 --- a/bin/update_hosts +++ b/bin/update_hosts @@ -5,7 +5,7 @@ [ "$USER" = root ] || exec sudo "$0" "$@" -echo "Check from https://github.com/StevenBlack/hosts" +echo "Checking from https://github.com/StevenBlack/hosts:" lsd=$(curl -s "https://api.github.com/repos/StevenBlack/hosts/commits?path=hosts&page=1&per_page=1"| jq -r '.[0].commit.committer.date') echo "last source update: $(date -j -f "%FT%TZ" "$lsd")" echo "last local update: $(date -r /etc/hosts)" @@ -14,7 +14,7 @@ echo "last local update: $(date -r /etc/hosts)" cd /etc cp -p hosts hosts.old hosts=$(awk '/^# Custom host /, /^# End of custom host /' hosts.old) -curl -s 'https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts' | awk ' +curl -s 'https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts' | awk ' BEGIN { hosts = ARGV[1]; ARGV[1] = "" } /^# Custom host / { print hosts; next } /^# End of custom host / { next } @@ -22,5 +22,5 @@ curl -s 'https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts' | aw ' "$hosts" > hosts.new mv hosts.new hosts -[ "$(uname -s)" != Darwin ] || dscacheutil -flushcache +[ "$(uname -s)" != Darwin ] || { dscacheutil -flushcache; killall -HUP mDNSResponder; } diff -u /etc/hosts.old /etc/hosts | diffstat diff --git a/bin/vm b/bin/vm index b0e1303..732ba49 100755 --- a/bin/vm +++ b/bin/vm @@ -179,7 +179,7 @@ $(isoinfo): init_alpine() { mkdir -p "$dir/alpine-iso" && cd "$dir/alpine-iso" || die 'init alpine failed' - [ -f 'Makefile' ] || printf "%s" "$alpine_makefile" > Makefile + [ -f 'Makefile' ] || printf '%s' "$alpine_makefile" > Makefile make -s all } -- cgit v1.2.3 From 6462de909492c58a8b1061633cec1c325539b3b6 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Fri, 11 Jun 2021 00:33:09 +0200 Subject: added migrate_vimki --- bin/migrate_vimki | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100755 bin/migrate_vimki (limited to 'bin') diff --git a/bin/migrate_vimki b/bin/migrate_vimki new file mode 100755 index 0000000..15242e6 --- /dev/null +++ b/bin/migrate_vimki @@ -0,0 +1,58 @@ +#!/bin/sh + +# Migrate Wiki content from vimki to vimki2 format. + +fixfile() { + awk ' + { + for (i = 1; i <= NR; i++) { + if (match($i, /^[A-Z][A-Z_0-9]*[a-z][a-z0-9]*[_A-Z]/) == 0) continue + s = $i + l = "" + while (match(s, /^[A-Z][A-Z_0-9]*[a-z]+/) != 0) { + l = l substr(s, 1, RLENGTH) " " + s = substr(s, RLENGTH+1) + } + p = match(s, /[^A-Za-z0-9_]/) + if (p != 0) { + s2 = substr(s, 1, p-1) "]" substr(s, p) + s = s2 + } else s = s "]" + l = "[" l s + gsub(/[ \t_]+/, " ", l) + sub(/ *]/, "]", l) + $i = tolower(l) + } + print + } + ' "${1:-HomePage}" +} + +fixname() { + echo "$1" | awk '{ + s = $0 + l = "" + while (match(s, /^[A-Z][A-Z_0-9]*[a-z]+/) != 0) { + l = l substr(s, 1, RLENGTH) " " + s = substr(s, RLENGTH+1) + } + p = match(s, /[^A-Za-z0-9_]/) + if (p != 0) { + s2 = substr(s, 1, p-1) "]" substr(s, p) + s = s2 + } + l = l s + gsub(/[ \t_]+/, " ", l) + sub(/ *$/, "", l) + print tolower(l) + + }' +} + +mkdir -p $HOME/Wiki2 +cd $HOME/Wiki +for file in *; do + new=$(fixname "$file") + echo "$new" + fixfile "$file" > "$HOME/Wiki2/$new" +done -- cgit v1.2.3 From 2621ec5405f52343cf253d02e307c5b160cf7f04 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sun, 13 Jun 2021 14:49:53 +0200 Subject: update --- bin/update_hosts | 8 ++++---- bin/vimki | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) create mode 100755 bin/vimki (limited to 'bin') diff --git a/bin/update_hosts b/bin/update_hosts index e7321bf..8550ec3 100755 --- a/bin/update_hosts +++ b/bin/update_hosts @@ -13,11 +13,11 @@ echo "last local update: $(date -r /etc/hosts)" cd /etc cp -p hosts hosts.old -hosts=$(awk '/^# Custom host /, /^# End of custom host /' hosts.old) +hosts=$(awk '/^# Custom host/, /^# End of custom host/' hosts.old) curl -s 'https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts' | awk ' - BEGIN { hosts = ARGV[1]; ARGV[1] = "" } - /^# Custom host / { print hosts; next } - /^# End of custom host / { next } + BEGIN { hosts = ARGV[1]; ARGV[1] = "" } + /^# Custom host/ { print hosts; next } + /^# End of custom host/ { next } { print } ' "$hosts" > hosts.new diff --git a/bin/vimki b/bin/vimki new file mode 100755 index 0000000..50ab6ea --- /dev/null +++ b/bin/vimki @@ -0,0 +1,29 @@ +#!/bin/sh + +# Batch operations on vimki + +# rename changes the link name and propagates the change in the wiki +rename1() { + gawk -v old="$1" -v new="$2" -v IGNORECASE=1 ' + { + gsub("[[]" old "[]]", "[" new "]") + print + } + ' "$3" +} + +linkfile() { + echo "$1" + #echo "$1" | tr '[:upper:] àçéèêëîïôèùûü' '[:lower:]_aceeeeiiouuu' +} + +rename() { + for f in *; do + rename1 "$1" "$2" "$f" > "$f.$$" && mv "$f.$$" "$f" + done + oldf="$(linkfile "$1")" + [ -f "$oldf" ] && mv "$oldf" "$(linkfile "$2")" +} + +cd ~/Wiki +"$@" -- cgit v1.2.3 From 53569210e7967c6d891affb027c1ea202194f9c0 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sat, 19 Jun 2021 13:00:21 +0200 Subject: vimki: rename handle accents and punctuation --- bin/vimki | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'bin') diff --git a/bin/vimki b/bin/vimki index 50ab6ea..f2ba45b 100755 --- a/bin/vimki +++ b/bin/vimki @@ -5,25 +5,46 @@ # rename changes the link name and propagates the change in the wiki rename1() { gawk -v old="$1" -v new="$2" -v IGNORECASE=1 ' + BEGIN { + rx = old + gsub(/_/, "\\W+", rx) + gsub(/a/, "[aåäáàâã]", rx) + gsub(/c/, "[cç]", rx) + gsub(/e/, "[eéèêë]", rx) + gsub(/i/, "[iîíìï]", rx) + gsub(/o/, "[oôöóõò]", rx) + gsub(/u/, "[uùûüú]", rx) + gsub(/y/, "[yÿ]", rx) + gsub(/n/, "[nñ]", rx) + } { - gsub("[[]" old "[]]", "[" new "]") + gsub("[[]" rx "[]]", "[" new "]") print } ' "$3" } linkfile() { - echo "$1" - #echo "$1" | tr '[:upper:] àçéèêëîïôèùûü' '[:lower:]_aceeeeiiouuu' + echo "$1" | + tr '[:upper:]' '[:lower:]' | + tr 'åäáàâãçéèêëîíìïôöóõòùûüúÿñ' 'aaaaaaceeeeiiiiooooouuuuyn' | + awk '{gsub(/\W+/, "_"); print}' } rename() { - for f in *; do + for f in *.md; do + [ "$(head -c 8 "$f")" = VimCrypt ] && continue rename1 "$1" "$2" "$f" > "$f.$$" && mv "$f.$$" "$f" done oldf="$(linkfile "$1")" [ -f "$oldf" ] && mv "$oldf" "$(linkfile "$2")" } +fixfiles() { + for f in *; do + mv "$f" "$(linkfile "$f")" + done +} + cd ~/Wiki "$@" -- cgit v1.2.3 From 6f40c5ba8549a66bd2b9ab449f57a574f0c2e905 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sat, 19 Jun 2021 15:59:06 +0200 Subject: vimki: fix rename --- bin/vimki | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'bin') diff --git a/bin/vimki b/bin/vimki index f2ba45b..3c6626c 100755 --- a/bin/vimki +++ b/bin/vimki @@ -24,6 +24,8 @@ rename1() { ' "$3" } +#shellcheck disable=SC2020 + linkfile() { echo "$1" | tr '[:upper:]' '[:lower:]' | @@ -36,8 +38,8 @@ rename() { [ "$(head -c 8 "$f")" = VimCrypt ] && continue rename1 "$1" "$2" "$f" > "$f.$$" && mv "$f.$$" "$f" done - oldf="$(linkfile "$1")" - [ -f "$oldf" ] && mv "$oldf" "$(linkfile "$2")" + oldf="$(linkfile "$1").md" + [ -f "$oldf" ] && mv "$oldf" "$(linkfile "$2").md" } fixfiles() { @@ -46,5 +48,5 @@ fixfiles() { done } -cd ~/Wiki +cd ~/Wiki || exit "$@" -- cgit v1.2.3 From 1433c15716d0da97d54d39f7397b80654ce10415 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sun, 20 Jun 2021 14:33:05 +0200 Subject: vimki: fix rename --- bin/vimki | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'bin') diff --git a/bin/vimki b/bin/vimki index 3c6626c..2f64162 100755 --- a/bin/vimki +++ b/bin/vimki @@ -5,8 +5,16 @@ # rename changes the link name and propagates the change in the wiki rename1() { gawk -v old="$1" -v new="$2" -v IGNORECASE=1 ' + { + gsub("[[]" old "[]]", "[" new "]") + print + } + ' "$3" +} + +link2rx() { + gawk -v rx="$1" -v IGNORECASE=1 ' BEGIN { - rx = old gsub(/_/, "\\W+", rx) gsub(/a/, "[aåäáàâã]", rx) gsub(/c/, "[cç]", rx) @@ -16,12 +24,8 @@ rename1() { gsub(/u/, "[uùûüú]", rx) gsub(/y/, "[yÿ]", rx) gsub(/n/, "[nñ]", rx) - } - { - gsub("[[]" rx "[]]", "[" new "]") - print - } - ' "$3" + print rx + }' } #shellcheck disable=SC2020 @@ -34,9 +38,10 @@ linkfile() { } rename() { + rx=$(link2rx "$1") for f in *.md; do [ "$(head -c 8 "$f")" = VimCrypt ] && continue - rename1 "$1" "$2" "$f" > "$f.$$" && mv "$f.$$" "$f" + rename1 "$rx" "$2" "$f" > "$f.$$" && mv "$f.$$" "$f" done oldf="$(linkfile "$1").md" [ -f "$oldf" ] && mv "$oldf" "$(linkfile "$2").md" -- cgit v1.2.3 From 0ad6087640b7c6f9bee6e954f0736d9c183f6a5c Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sun, 20 Jun 2021 15:36:38 +0200 Subject: vimki: parallelize rename --- bin/vimki | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'bin') diff --git a/bin/vimki b/bin/vimki index 2f64162..e3e0f4d 100755 --- a/bin/vimki +++ b/bin/vimki @@ -4,12 +4,13 @@ # rename changes the link name and propagates the change in the wiki rename1() { + [ "$(head -c 8 "$3")" = VimCrypt ] && return gawk -v old="$1" -v new="$2" -v IGNORECASE=1 ' { gsub("[[]" old "[]]", "[" new "]") print } - ' "$3" + ' "$3" > "$3.$$" && mv "$3.$$" "$3" } link2rx() { @@ -39,10 +40,8 @@ linkfile() { rename() { rx=$(link2rx "$1") - for f in *.md; do - [ "$(head -c 8 "$f")" = VimCrypt ] && continue - rename1 "$rx" "$2" "$f" > "$f.$$" && mv "$f.$$" "$f" - done + # Parallelize renaming + echo *.md | xargs -n 1 -P 8 "$0" rename1 "$rx" "$2" oldf="$(linkfile "$1").md" [ -f "$oldf" ] && mv "$oldf" "$(linkfile "$2").md" } -- cgit v1.2.3 From e70107cb6a2f1fe17b848c43774b4e946749ea23 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sun, 15 Aug 2021 11:26:14 +0200 Subject: update --- bin/update_kernel_host | 8 ++++++++ bin/vm | 10 ++++++---- bin/wol | 4 ++-- 3 files changed, 16 insertions(+), 6 deletions(-) create mode 100755 bin/update_kernel_host (limited to 'bin') diff --git a/bin/update_kernel_host b/bin/update_kernel_host new file mode 100755 index 0000000..6180921 --- /dev/null +++ b/bin/update_kernel_host @@ -0,0 +1,8 @@ +#!/bin/sh -ex + +# After 'apk upgrade', update kernel and initrd on VM host + +sudo cp /boot/initramfs-virt /tmp +sudo chmod a+r /tmp/initramfs-virt +gunzip < /boot/vmlinuz-virt > /tmp/vmlinux +scp /tmp/initramfs-virt /tmp/vmlinux marc@m1:.vm/vm1/ diff --git a/bin/vm b/bin/vm index 732ba49..97e3521 100755 --- a/bin/vm +++ b/bin/vm @@ -212,11 +212,12 @@ ls() { } start() { - usage 'start [-af] name' 'Start a VM' && return - while getopts :af opt; do + usage 'start [-afs] name' 'Start a VM' && return + while getopts :afs opt; do case $opt in (a) a=1 ;; (f) f=1 ;; + (s) s=1 ;; (*) Opth=1 start "$1"; exit;; esac done @@ -227,8 +228,9 @@ start() { cd "$dir/$1" || die "start $1 failed" [ -f vftool.pid ] && die "Error: process $(cat vftool.pid) is active or $PWD/vftool.pid should be removed" start_vm & sleep 2 - [ "$f" ] && finalize - [ "$a" ] && vm console "$1" + ! [ "$f" ] || finalize + ! [ "$a" ] || vm console "$1" + ! [ "$s" ] || exec ssh "$1" } start_vm() ( diff --git a/bin/wol b/bin/wol index 39bce0d..7975df9 100755 --- a/bin/wol +++ b/bin/wol @@ -1,6 +1,6 @@ -#!/bin/sh -x +#!/bin/sh ip_plex=192.168.1.90 mac_plex=b0:83:fe:61:a7:aa eval "mac=\$mac_$1 ip=\$ip_$1" wakeonlan "$mac" -ping -o "$ip" +while ! nc -zw 2 "$ip" 22; do printf .; sleep 2; done ssh "root@$ip" -- cgit v1.2.3 From 0b89577b1e7c46986bed003033d677f2d1ebf9cb Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sun, 15 Aug 2021 11:43:21 +0200 Subject: update --- bin/gauth | 3 ++- bin/wol | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'bin') diff --git a/bin/gauth b/bin/gauth index 575d6fc..53ddf30 100755 --- a/bin/gauth +++ b/bin/gauth @@ -1,7 +1,8 @@ #!/bin/sh # Use backup from andOTP -gpg -qd ~/.otp_accounts.json.gpg | +# gpg -qd ~/.otp_accounts.json.gpg | +cat ~/otp_accounts.json | jq -r '.[] | "\(.label) \(.secret)"' | while read -r l s; do echo "$l $(oathtool --totp -b "$s")" diff --git a/bin/wol b/bin/wol index 146e46a..7975df9 100755 --- a/bin/wol +++ b/bin/wol @@ -1,6 +1,6 @@ -#!/bin/sh -x +#!/bin/sh ip_plex=192.168.1.90 mac_plex=b0:83:fe:61:a7:aa eval "mac=\$mac_$1 ip=\$ip_$1" wakeonlan "$mac" -#ping -o "$ip" +while ! nc -zw 2 "$ip" 22; do printf .; sleep 2; done ssh "root@$ip" -- cgit v1.2.3 From 7a36b02b5f3ae6560030c97513b8c15d9abd16d8 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sat, 27 Nov 2021 11:04:34 +0100 Subject: update --- bin/update_hosts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'bin') diff --git a/bin/update_hosts b/bin/update_hosts index 8550ec3..40ec470 100755 --- a/bin/update_hosts +++ b/bin/update_hosts @@ -5,11 +5,11 @@ [ "$USER" = root ] || exec sudo "$0" "$@" -echo "Checking from https://github.com/StevenBlack/hosts:" -lsd=$(curl -s "https://api.github.com/repos/StevenBlack/hosts/commits?path=hosts&page=1&per_page=1"| jq -r '.[0].commit.committer.date') +echo 'Checking from https://github.com/StevenBlack/hosts:' +lsd=$(curl -s 'https://api.github.com/repos/StevenBlack/hosts/commits?path=hosts&page=1&per_page=1' | jq -r '.[0].commit.committer.date') echo "last source update: $(date -j -f "%FT%TZ" "$lsd")" echo "last local update: $(date -r /etc/hosts)" -[ $(date -j -f "%FT%TZ" "$lsd" +%s) -lt $(date -r /etc/hosts +%s) ] && echo "Nothing to do" && exit +[ $(date -j -f "%FT%TZ" "$lsd" +%s) -lt $(date -r /etc/hosts +%s) ] && echo 'Nothing to do' && exit cd /etc cp -p hosts hosts.old -- cgit v1.2.3 From b9df08e08dc7eba9352bc68a4d1bb55c8d27529f Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sun, 28 Nov 2021 15:45:27 +0100 Subject: update --- bin/backup | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'bin') diff --git a/bin/backup b/bin/backup index 72c69c3..adce78d 100755 --- a/bin/backup +++ b/bin/backup @@ -1,8 +1,6 @@ #!/bin/sh # Incremental backup using rsync(1). -[ "$USER" = root ] || exec sudo "$0" "$@" - backup() { date=$(date +%Y%m%d_%H%M%S) last=$(rsync --list-only "$dest/" 2>/dev/null | cut -b 47- | tail -1) @@ -12,25 +10,29 @@ backup() { (*) opt_link=;; esac - rsync -HSxa$optv --exclude-from=$ignore $opt_link / /boot "$dest/$date" + rsync -HSxa$optv --exclude-from=$ignore $opt_link $volumes "$dest/$date" } dest=/.history -ignore=/etc/backup/ignore +ignore=/etc/backupignore +volumes='/ /boot' -while getopts :d:i:nv opt; do +while getopts :d:i:nuv opt; do case $opt in (d) dest="$OPTARG" ;; (i) ignore="$OPTARG" ;; (n|v) optv="$opt$optv" ;; - (*) echo "Usage: $0 [-nv] [-d [host:]dir] [clean|diff]"; exit 1 ;; + (u) optu=1 volumes="$HOME" ignore="$HOME/.backupignore" ;; + (*) echo "Usage: $0 [-nuv] [-d [[user@]host:]dir] [clean|diff]"; exit 1 ;; esac done shift $((OPTIND - 1)) +[ "$optu" ] || [ "$USER" = root ] || exec sudo "$0" "$@" + [ "$1" ] && cmd=$1 && shift || cmd="" case $cmd in (""|save) backup ;; -(clean) exec backup-clean ${optv+-$optv} "$dest";; +(clean) exec backup-clean ${optv+-$optv} "$@" "$dest";; (diff) exec diffdir "$@";; esac -- cgit v1.2.3 From ea345acc22fbb3a0ac36a7d1ea43de3dae0f51a1 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Thu, 2 Dec 2021 15:26:02 +0100 Subject: update --- bin/backup | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bin') diff --git a/bin/backup b/bin/backup index adce78d..9de097f 100755 --- a/bin/backup +++ b/bin/backup @@ -3,7 +3,7 @@ backup() { date=$(date +%Y%m%d_%H%M%S) - last=$(rsync --list-only "$dest/" 2>/dev/null | cut -b 47- | tail -1) + last=$(rsync --list-only "$dest/" 2>/dev/null | awk '{r=$NF} END {print r}') case $last in ([12]*) opt_link=--link-dest=../$last;; -- cgit v1.2.3 From e589eff80a403bfe7831c040fe55a6bcf5ba038b Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Mon, 6 Dec 2021 15:02:31 +0100 Subject: update --- bin/backup | 55 ++++++++++++++---------- bin/backup-clean | 127 ++++++++++++++++++++++--------------------------------- bin/byo | 4 +- bin/gauth | 6 +-- bin/p4a | 3 ++ 5 files changed, 91 insertions(+), 104 deletions(-) create mode 100755 bin/p4a (limited to 'bin') diff --git a/bin/backup b/bin/backup index 72c69c3..3964927 100755 --- a/bin/backup +++ b/bin/backup @@ -1,36 +1,47 @@ #!/bin/sh -# Incremental backup using rsync(1). -[ "$USER" = root ] || exec sudo "$0" "$@" +usage() { + echo "Usage: $0 [-nv] [[[user@]host]:dir] -backup() { - date=$(date +%Y%m%d_%H%M%S) - last=$(rsync --list-only "$dest/" 2>/dev/null | cut -b 47- | tail -1) +Incremental backup using rsync(1). If run as root, a full system +backup is performed. Otherwise, the user's home directory is backed. - case $last in - ([12]*) opt_link=--link-dest=../$last;; - (*) opt_link=;; - esac +Options: +-n dry-run +-v verbose - rsync -HSxa$optv --exclude-from=$ignore $opt_link / /boot "$dest/$date" -} +Files: +- $HOME/.backupignore exclude files from user backup (non root) +- /etc/backupignore exclude files from system backup (root) -dest=/.history -ignore=/etc/backup/ignore +Environment: +- BACKUP backup directory" +} -while getopts :d:i:nv opt; do +while getopts :nv opt; do case $opt in - (d) dest="$OPTARG" ;; - (i) ignore="$OPTARG" ;; (n|v) optv="$opt$optv" ;; - (*) echo "Usage: $0 [-nv] [-d [host:]dir] [clean|diff]"; exit 1 ;; + (*) usage; exit 1 ;; esac done shift $((OPTIND - 1)) -[ "$1" ] && cmd=$1 && shift || cmd="" -case $cmd in -(""|save) backup ;; -(clean) exec backup-clean ${optv+-$optv} "$dest";; -(diff) exec diffdir "$@";; +BACKUP=${1:-$BACKUP} +[ "$BACKUP" ] || { usage; exit 1; } + +[ "$USER" = root ] && + ignore=/etc/backupignore volumes='/ /boot' || + ignore="$HOME/.backupignore" volumes="$HOME" +[ -f "$ignore" ] && ignore="--exclude-from=$ignore" || ignore= + +last=$(rsync --list-only "$BACKUP/" 2>/dev/null | awk '{r=$NF} END {print r}') +case $last in +([12]*) opt_link=--link-dest=../$last;; +(*) opt_link=;; esac + +date=$(date +%Y-%m-%d-%H%M%S) + +[ "$optv" ] && echo "# Backup $volumes to $BACKUP/$date" + +exec rsync -HSxa$optv $ignore $opt_link $volumes "$BACKUP/$date" diff --git a/bin/backup-clean b/bin/backup-clean index 30d25db..1c752d9 100755 --- a/bin/backup-clean +++ b/bin/backup-clean @@ -1,89 +1,62 @@ #!/bin/sh -# backup garbage collector -[ "$USER" = root ] || exec sudo "$0" "$@" -# Durations, in number of seconds. -hd=3600 # hour duration: 60s * 60 -h12=43200 # 12h: hd * 12 -dd=86400 # day duration: hd * 24 -wd=604800 # week duration: dd * 7 -md=2592000 # month duration: dd * 30 -yd=31557600 # year duration: dd * 365.25 +usage() { + echo "Usage: $0 [-nv] [[[user@]host:]dir] -now=$(date +'%Y%m%d_%H%M%S') +$0 removes old backups and keeps: +- backups for the current day +- 1 backup per past day for the current month +- 1 backup per past month for the current year +- 1 backup per past year -# The following works only for busybox date, not coreutils -# Kept for reference only. -#date2ts() { d=${1%_*} t=${1#*_}; date -d "$d${t%??}" +%s; } +Options: +-n dry run +-v verbose -# Convert date to timestamp, using date from GNU coreutils, -# works also with busybox date. -date2ts() { - t=$1; r=${t#????}; Y=${t%$r} # Year (with century) - t=$r; r=${t#??}; m=${t%$r} # Month - t=$r; r=${t#??}; d=${t%$r} # Day - t=${r#_}; r=${t#??}; H=${t%$r} # Hour - t=$r; r=${t#??}; M=${t%$r} # Minute - S=${t#??} # Second - date -d "$Y-$m-$d $H:$M:$S" +%s - #date -jf "%Y-%m-%d %H:%M:%S" "$Y-$m-$d $H:$M:$S" +%s # BSD, MacOS (not tested) +Environment variables: +- BACKUP - backup directory" } -ts2date() { date -d "@$1" +'%Y%m%d_%H%M%S'; } - -tsn=$(date2ts "$now") - -# Minimal retention delay in seconds, according to backup age, -# implemented using POSIX shell arithmetic. -retention_delay() { - d=$((tsn - $1)) - if [ $((d < h12)) = 1 ]; then - r=0 # keep all backups in the last 12 hours - elif [ $((d < dd)) = 1 ]; then - r=$hd # keep 1 backup per hour in the last day - elif [ $((d < wd)) = 1 ]; then - r=$dd # keep 1 backup per day in the last week - elif [ $((d < md)) = 1 ]; then - r=$wd # keep 1 backup per week in the last month - elif [ $((d < yd)) = 1 ]; then - r=$md # keep 1 backup per month in the last year - else - r=$yd # keep 1 backup per year in the previous years - fi - echo $r -} - -dest=/.history -while getopts :d:nv opt; do +while getopts :nv opt; do case $opt in - (d) tsn=$(date2ts "$OPTARG") ;; - (n) optn=1 ;; - (v) optv=1 ;; - (*) echo "Usage: $0 [-nv] [dir]"; exit 1 ;; + (n) optn=echo ;; + (v) optv=-t ;; + (*) usage; exit 1 ;; esac done -shift $((OPTIND - 1)) -[ "$1" ] && dest=$1 +shift $((OPTIND -1)) -# Sorted list of backups, most recent first. -lbu=$(ls -rv "$dest") -lasy=${lbu##* +BACKUP=${1:-$BACKUP} +[ "$BACKUP" ] || { usage; exit 1; } + +host=${BACKUP%:*} dir=${BACKUP#*:} +ls='ls -r' rm="xargs -r $optv $optn rm -rf" +[ "$host" = "$dir" ] || ls="ssh $host $ls" rm="ssh $host $rm" + +[ "$optv" ] && echo "# Cleaning backups on $BACKUP" + +$ls "$dir" | +awk -v now=$(date +%Y-%m-%d) -v dir="$dir" ' +BEGIN { + yn = substr(now, 1, 4) # Year now + mn = substr(now, 6, 2) # Month now + dn = substr(now, 9, 2) # Day now } -for d in $lbu; do - tsc=$(date2ts "$d") - if ! [ "$tsp" ]; then - [ "$optv" ] && echo "keep $dest/$d" - tsp=$tsc - continue - fi - mrd=$(retention_delay "$tsp") - dp=$((tsp - tsc)) - #if [ $((dp < mrd)) = 1 ]; then - if [ "$d" != "last" -a $((dp < mrd)) = 1 ]; then - [ "$optv" ] && echo "delete $dest/$d" - [ "$optn" ] || rm -rf "${dest:?}/$d" - else - [ "$optv" ] && echo "keep $dest/$d" - tsp=$tsc - fi -done +{ + yb = substr($0, 1, 4) # Year backup + mb = substr($0, 6, 2) # Month backup + db = substr($0, 9, 2) # Day backup + $0 = dir "/" $0 + + if (yb != yn) { + if (yb in yearly) print; else yearly[yb] = 1 + next + } + if (mb != mn) { + if (mb in monthly) print; else monthly[mb] = 1 + next + } + if (db != dn) { + if (db in dayly) print; else dayly[db] = 1 + } +}' | $rm diff --git a/bin/byo b/bin/byo index bc09fb8..93bb7f0 100755 --- a/bin/byo +++ b/bin/byo @@ -26,8 +26,8 @@ yoda_uuid='8c463221-6bb7-414e-9060-c9570bb3a6bb' dest=/mnt/backup/$(hostname) [ -b /dev/mapper/yoda ] && noclose=1 || cryptsetup open "$dev" yoda findmnt /dev/mapper/yoda /mnt >/dev/null && noumount=1 || mount /dev/mapper/yoda /mnt -time backup -v -d "$dest" -[ ! "$optC" ] || backup -v -d "$dest" clean +time backup -v "$dest" +# [ ! "$optC" ] || backup -v -d "$dest" clean [ ! "$optd" ] || time duperemove -drh --hashfile="$dest/.hashfile" "$dest" df -h / "$dest" ls -v "$dest" diff --git a/bin/gauth b/bin/gauth index 53ddf30..b18f73d 100755 --- a/bin/gauth +++ b/bin/gauth @@ -1,9 +1,9 @@ #!/bin/sh # Use backup from andOTP -# gpg -qd ~/.otp_accounts.json.gpg | -cat ~/otp_accounts.json | +# cat ~/.otp_accounts.json | +gpg -qd ~/.otp_accounts.json.gpg 2>/dev/null | jq -r '.[] | "\(.label) \(.secret)"' | while read -r l s; do echo "$l $(oathtool --totp -b "$s")" -done +done | column -t diff --git a/bin/p4a b/bin/p4a new file mode 100755 index 0000000..30e54ff --- /dev/null +++ b/bin/p4a @@ -0,0 +1,3 @@ +#!/bin/sh +adb forward tcp:8222 tcp:8022 +ssh ssh://u0_a250@localhost:8222 -- cgit v1.2.3 From 14b5c600034a9d52497af2974f30dafc21710282 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Mon, 6 Dec 2021 15:36:55 +0100 Subject: update --- bin/backup | 57 ++++++++++++++---------- bin/backup-clean | 132 +++++++++++++++++++++++-------------------------------- 2 files changed, 88 insertions(+), 101 deletions(-) (limited to 'bin') diff --git a/bin/backup b/bin/backup index 9de097f..3964927 100755 --- a/bin/backup +++ b/bin/backup @@ -1,38 +1,47 @@ #!/bin/sh -# Incremental backup using rsync(1). -backup() { - date=$(date +%Y%m%d_%H%M%S) - last=$(rsync --list-only "$dest/" 2>/dev/null | awk '{r=$NF} END {print r}') +usage() { + echo "Usage: $0 [-nv] [[[user@]host]:dir] - case $last in - ([12]*) opt_link=--link-dest=../$last;; - (*) opt_link=;; - esac +Incremental backup using rsync(1). If run as root, a full system +backup is performed. Otherwise, the user's home directory is backed. - rsync -HSxa$optv --exclude-from=$ignore $opt_link $volumes "$dest/$date" -} +Options: +-n dry-run +-v verbose -dest=/.history -ignore=/etc/backupignore -volumes='/ /boot' +Files: +- $HOME/.backupignore exclude files from user backup (non root) +- /etc/backupignore exclude files from system backup (root) + +Environment: +- BACKUP backup directory" +} -while getopts :d:i:nuv opt; do +while getopts :nv opt; do case $opt in - (d) dest="$OPTARG" ;; - (i) ignore="$OPTARG" ;; (n|v) optv="$opt$optv" ;; - (u) optu=1 volumes="$HOME" ignore="$HOME/.backupignore" ;; - (*) echo "Usage: $0 [-nuv] [-d [[user@]host:]dir] [clean|diff]"; exit 1 ;; + (*) usage; exit 1 ;; esac done shift $((OPTIND - 1)) -[ "$optu" ] || [ "$USER" = root ] || exec sudo "$0" "$@" +BACKUP=${1:-$BACKUP} +[ "$BACKUP" ] || { usage; exit 1; } -[ "$1" ] && cmd=$1 && shift || cmd="" -case $cmd in -(""|save) backup ;; -(clean) exec backup-clean ${optv+-$optv} "$@" "$dest";; -(diff) exec diffdir "$@";; +[ "$USER" = root ] && + ignore=/etc/backupignore volumes='/ /boot' || + ignore="$HOME/.backupignore" volumes="$HOME" +[ -f "$ignore" ] && ignore="--exclude-from=$ignore" || ignore= + +last=$(rsync --list-only "$BACKUP/" 2>/dev/null | awk '{r=$NF} END {print r}') +case $last in +([12]*) opt_link=--link-dest=../$last;; +(*) opt_link=;; esac + +date=$(date +%Y-%m-%d-%H%M%S) + +[ "$optv" ] && echo "# Backup $volumes to $BACKUP/$date" + +exec rsync -HSxa$optv $ignore $opt_link $volumes "$BACKUP/$date" diff --git a/bin/backup-clean b/bin/backup-clean index 30d25db..f5baa29 100755 --- a/bin/backup-clean +++ b/bin/backup-clean @@ -1,89 +1,67 @@ #!/bin/sh -# backup garbage collector -[ "$USER" = root ] || exec sudo "$0" "$@" -# Durations, in number of seconds. -hd=3600 # hour duration: 60s * 60 -h12=43200 # 12h: hd * 12 -dd=86400 # day duration: hd * 24 -wd=604800 # week duration: dd * 7 -md=2592000 # month duration: dd * 30 -yd=31557600 # year duration: dd * 365.25 +usage() { + echo "Usage: $0 [-nv] [[[user@]host:]dir] -now=$(date +'%Y%m%d_%H%M%S') +$0 removes old backups and keeps: +- backups for the current day +- 1 backup per past day for the current month +- 1 backup per past month for the current year +- 1 backup per past year -# The following works only for busybox date, not coreutils -# Kept for reference only. -#date2ts() { d=${1%_*} t=${1#*_}; date -d "$d${t%??}" +%s; } +Options: +-n dry run +-v verbose -# Convert date to timestamp, using date from GNU coreutils, -# works also with busybox date. -date2ts() { - t=$1; r=${t#????}; Y=${t%$r} # Year (with century) - t=$r; r=${t#??}; m=${t%$r} # Month - t=$r; r=${t#??}; d=${t%$r} # Day - t=${r#_}; r=${t#??}; H=${t%$r} # Hour - t=$r; r=${t#??}; M=${t%$r} # Minute - S=${t#??} # Second - date -d "$Y-$m-$d $H:$M:$S" +%s - #date -jf "%Y-%m-%d %H:%M:%S" "$Y-$m-$d $H:$M:$S" +%s # BSD, MacOS (not tested) +Environment variables: +- BACKUP - backup directory" } -ts2date() { date -d "@$1" +'%Y%m%d_%H%M%S'; } - -tsn=$(date2ts "$now") - -# Minimal retention delay in seconds, according to backup age, -# implemented using POSIX shell arithmetic. -retention_delay() { - d=$((tsn - $1)) - if [ $((d < h12)) = 1 ]; then - r=0 # keep all backups in the last 12 hours - elif [ $((d < dd)) = 1 ]; then - r=$hd # keep 1 backup per hour in the last day - elif [ $((d < wd)) = 1 ]; then - r=$dd # keep 1 backup per day in the last week - elif [ $((d < md)) = 1 ]; then - r=$wd # keep 1 backup per week in the last month - elif [ $((d < yd)) = 1 ]; then - r=$md # keep 1 backup per month in the last year - else - r=$yd # keep 1 backup per year in the previous years - fi - echo $r -} - -dest=/.history -while getopts :d:nv opt; do +while getopts :nv opt; do case $opt in - (d) tsn=$(date2ts "$OPTARG") ;; - (n) optn=1 ;; - (v) optv=1 ;; - (*) echo "Usage: $0 [-nv] [dir]"; exit 1 ;; + (n) optn=echo ;; + (v) optv=-t ;; + (*) usage; exit 1 ;; esac done -shift $((OPTIND - 1)) -[ "$1" ] && dest=$1 +shift $((OPTIND -1)) + +BACKUP=${1:-$BACKUP} +[ "$BACKUP" ] || { usage; exit 1; } -# Sorted list of backups, most recent first. -lbu=$(ls -rv "$dest") -lasy=${lbu##* +host=${BACKUP%:*} dir=${BACKUP#*:} +ls='ls -r' rm="xargs -r $optv $optn rm -rf" +[ "$host" = "$dir" ] || ls="ssh $host $ls" rm="ssh $host $rm" + +[ "$optv" ] && echo "# Cleaning backups on $BACKUP" + +$ls "$dir" | +awk -v now=$(date +%Y-%m-%d) -v dir="$dir" ' +BEGIN { + yn = substr(now, 1, 4) # Year now + mn = substr(now, 6, 2) # Month now + dn = substr(now, 9, 2) # Day now } -for d in $lbu; do - tsc=$(date2ts "$d") - if ! [ "$tsp" ]; then - [ "$optv" ] && echo "keep $dest/$d" - tsp=$tsc - continue - fi - mrd=$(retention_delay "$tsp") - dp=$((tsp - tsc)) - #if [ $((dp < mrd)) = 1 ]; then - if [ "$d" != "last" -a $((dp < mrd)) = 1 ]; then - [ "$optv" ] && echo "delete $dest/$d" - [ "$optn" ] || rm -rf "${dest:?}/$d" - else - [ "$optv" ] && echo "keep $dest/$d" - tsp=$tsc - fi -done +{ + yb = substr($0, 1, 4) # Year backup + mb = substr($0, 6, 2) # Month backup + db = substr($0, 9, 2) # Day backup + $0 = dir "/" $0 + + dy = yn - yb + dm = dy * 12 + mn - mb + # if (yb != yn) { + if (dm > 12) + if (yb in yearly) print; else yearly[yb] = 1 + next + } + # if (mb != mn) { + dd = dm * 30 + dn - db + if (dd > 30) { + if (mb in monthly) print; else monthly[mb] = 1 + next + } + if (db != dn) { + if (db in dayly) print; else dayly[db] = 1 + } +}' | $rm -- cgit v1.2.3 From 4233fc9c6bc0f2f136a495efcd497394edd5156b Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Tue, 7 Dec 2021 14:50:57 +0100 Subject: update --- bin/backup | 3 +-- bin/backup-clean | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) (limited to 'bin') diff --git a/bin/backup b/bin/backup index 3964927..d682849 100755 --- a/bin/backup +++ b/bin/backup @@ -42,6 +42,5 @@ esac date=$(date +%Y-%m-%d-%H%M%S) -[ "$optv" ] && echo "# Backup $volumes to $BACKUP/$date" - +echo "# Backup $volumes to $BACKUP/$date" exec rsync -HSxa$optv $ignore $opt_link $volumes "$BACKUP/$date" diff --git a/bin/backup-clean b/bin/backup-clean index f5baa29..34e6d97 100755 --- a/bin/backup-clean +++ b/bin/backup-clean @@ -33,7 +33,7 @@ host=${BACKUP%:*} dir=${BACKUP#*:} ls='ls -r' rm="xargs -r $optv $optn rm -rf" [ "$host" = "$dir" ] || ls="ssh $host $ls" rm="ssh $host $rm" -[ "$optv" ] && echo "# Cleaning backups on $BACKUP" +echo "# Cleaning backups on $BACKUP" $ls "$dir" | awk -v now=$(date +%Y-%m-%d) -v dir="$dir" ' @@ -51,7 +51,7 @@ BEGIN { dy = yn - yb dm = dy * 12 + mn - mb # if (yb != yn) { - if (dm > 12) + if (dm > 12) { if (yb in yearly) print; else yearly[yb] = 1 next } -- cgit v1.2.3 From 6258df971a00f175cd434e7874c6ba5616b50e60 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sat, 18 Dec 2021 11:37:10 +0100 Subject: update --- bin/backup | 3 +-- bin/backup-clean | 11 ++++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) (limited to 'bin') diff --git a/bin/backup b/bin/backup index 3964927..d682849 100755 --- a/bin/backup +++ b/bin/backup @@ -42,6 +42,5 @@ esac date=$(date +%Y-%m-%d-%H%M%S) -[ "$optv" ] && echo "# Backup $volumes to $BACKUP/$date" - +echo "# Backup $volumes to $BACKUP/$date" exec rsync -HSxa$optv $ignore $opt_link $volumes "$BACKUP/$date" diff --git a/bin/backup-clean b/bin/backup-clean index 1c752d9..34e6d97 100755 --- a/bin/backup-clean +++ b/bin/backup-clean @@ -33,7 +33,7 @@ host=${BACKUP%:*} dir=${BACKUP#*:} ls='ls -r' rm="xargs -r $optv $optn rm -rf" [ "$host" = "$dir" ] || ls="ssh $host $ls" rm="ssh $host $rm" -[ "$optv" ] && echo "# Cleaning backups on $BACKUP" +echo "# Cleaning backups on $BACKUP" $ls "$dir" | awk -v now=$(date +%Y-%m-%d) -v dir="$dir" ' @@ -48,11 +48,16 @@ BEGIN { db = substr($0, 9, 2) # Day backup $0 = dir "/" $0 - if (yb != yn) { + dy = yn - yb + dm = dy * 12 + mn - mb + # if (yb != yn) { + if (dm > 12) { if (yb in yearly) print; else yearly[yb] = 1 next } - if (mb != mn) { + # if (mb != mn) { + dd = dm * 30 + dn - db + if (dd > 30) { if (mb in monthly) print; else monthly[mb] = 1 next } -- cgit v1.2.3 From 053556962bb2d29ac76bcca51cabd1df238f80e3 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Fri, 7 Jan 2022 15:24:15 +0100 Subject: update --- bin/hdmi | 8 +- bin/vm | 258 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 262 insertions(+), 4 deletions(-) create mode 100755 bin/vm (limited to 'bin') diff --git a/bin/hdmi b/bin/hdmi index d6f206a..e6e3094 100755 --- a/bin/hdmi +++ b/bin/hdmi @@ -2,8 +2,8 @@ # Toggle auxiliary HDMI screen on/off when connected/disconnected. opt="--auto --left-of eDP1" -#opt="--auto --right-of eDP-1" -#opt="--auto --above eDP-1" +#opt="--auto --right-of eDP1" +#opt="--auto --above eDP1" # Also add the following rule to /etc/udev/rules.d/hdmi.rules # KERNEL=="card0", SUBSYSTEM=="drm", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/marc/.Xauthority", RUN+="/home/marc/bin/hdmi" @@ -11,5 +11,5 @@ opt="--auto --left-of eDP1" # wait for the screen to settle sleep 3 -xrandr | grep -q 'HDMI1 connected' || opt="--off" -xrandr --output HDMI1 $opt +xrandr | grep -q '^DP1 connected' || opt="--off" +xrandr --output DP1 $opt diff --git a/bin/vm b/bin/vm new file mode 100755 index 0000000..8b4da42 --- /dev/null +++ b/bin/vm @@ -0,0 +1,258 @@ +#!/bin/sh + +# Manage virtual machines +# +# Prereq: +# - curl +# - cdrtools (isoinfo) +# - expect +# - qemu +# - screen +# +# TODO: +# - extract bzImage and initrd.gz with isoinfo +# - ssh scripts to finish install (once ssh is ready) +# +# DONE: +# - setup vde and networking +# - setup ssh keys so it is possible to ssh in vm from host +# +unset CDPATH +export LC_ALL=C IFS=' +' + +version='vm-0.1' + +arch=$(uname -m) +sys=$(uname -s) +alpine_version='3.15.0' +pubkey=$HOME/.ssh/id_rsa.pub +dir="${VM_DIR:-$HOME/.vm}" + +console() { + usage 'console name' 'Attach a console to a virtual machine' && return + [ "$1" ] || die "missing argument" + is_running "$1" && screen -r "vm!$1!" +} + +create() { + usage 'create [-s size] name' 'Create an alpinelinux disk image' && return + size=8g + while getopts :s: opt; do + case $opt in + s) size=$OPTARG ;; + *) Opth=2 create_alpine_image; return ;; + esac + done + shift $((OPTIND - 1)) + [ -d "$dir/$1" ] && die "create failed: $dir/$1 already exists" + mkdir "$dir/$1" + cd "$dir/$1" || die "create failed: invalid directory $dir/$1" + qemu-img create "$1.raw" "$size" || die "create failed" + mac=$(new_macaddr) + ip=$(new_ip) + hdd="$1.raw" + echo "hdd=$hdd +mac=$mac +ip=$ip" >> config + + # Before install do not use virtio, as devices may not recognized as bootable + # TODO: alternate way: extract kernel and initrd files and pass them directly to qemu + screen -S "vm!$1!" -d -m qemu-system-$arch -nographic \ + -cdrom ../alpine-iso/alpine-virt-$alpine_version-$arch.iso \ + -hdd "$hdd" -net nic,macaddr=$mac -net vde + setup_alpine "$1" + post_setup_alpine "$1" +} + +die() { [ "$1" ] && echo "$0: $*" >&2; exit 1; } + +help() { + usage 'help' 'Print this help text' && return + printf "$version\n Manage virtual machines\n\nUsage: vm command [options] [args]\n" + Opth=1; for c in $Cmdlist; do $c; done +} + +init() { + mkdir -p "$dir" +} + +init_alpine_iso() { + usage init_alpine_iso '' && return + mkdir -p "$dir/alpine-iso" + iso_url="https://dl-cdn.alpinelinux.org/alpine/v${alpine_version%.*}/releases/$arch" + iso="alpine-virt-$alpine_version-$arch.iso" + cd "$dir/alpine-iso" + [ -f "$iso" ] || curl -LO "$iso_url/$iso" || rm -f "$iso" + echo "iso=$iso +" > config + echo 10 > ../index +} + +is_running() { screen -ls "vm!$1!" >/dev/null 2>&1; } + +ls() { + usage 'ls' 'list virtual machines' && return + init && cd "$dir" || die "could not change dir to $dir" + for i in */; do + i=${i%/} + [ "$i" = '*' ] && continue + is_running "$i" && state=active || state=stopped + printf "%-20s %s\n" "$i" "$state" + done +} + +new_ip() { + read index < $dir/index + index=$((index + 1)) + echo "$index" > $dir/index + echo "10.0.2.$index/24" +} + +new_macaddr() { printf 'de:ad:be:ef:%02x:%02x\n' $((RANDOM % 256)) $((RANDOM % 256)); } + +pidof() { + usage 'pidof name' 'print the PID of a virtual machine' && return + p=$(screen -ls "vm!$1!" | awk 'NR==2 {print substr($1, 1, index($1, ".")-1)}') + [ "$p" ] && pgrep -P $p +} + +post_setup_alpine() { + start "$1" + read proto key id < "$pubkey" + # echo ' + expect -c ' + set timeout -1 + spawn screen -x "vm!'$1'!" + expect { + " login: " { send "root\r"; exp_continue } + "Password: " { send "root\r"; exp_continue } + ":~# " + } + send "mkdir -pm 0700 .ssh\r" + expect ":~# " + send "echo '$proto' '$key' '$id' >.ssh/authorized_keys\r" + expect ":~# " + send "exit\r" + ' +} + +# setup_alpine automates alpine installation from iso to image. +# When done, base image system is ready, with storage and network up. +# No user nor ssh access configured yet. +# TODO: custom network (in case of no dhcp). +setup_alpine() { + usage 'setup_alpine' && return + expect -c ' + set timeout -1 + spawn screen -x "vm!'$1'!" + expect { + "localhost login: " { send "root\r"; exp_continue } + "localhost:~# " { send "SWAP_SIZE=0 setup-alpine\r"; exp_continue } + "Select keyboard layout: " { send "\r"; exp_continue } + "Enter system hostname" { send "'$1'\r"; exp_continue } + "Which one do you want to initialize?" { send "\r"; exp_continue } + "Ip address for eth0?" { send "'$ip'\r"; exp_continue } + "Gateway?" { send "10.0.2.2\r"; exp_continue } + "manual network configuration?" { send "\r"; exp_continue } + "DNS domain name?" { send "lan\r"; exp_continue } + "DNS nameserver(s)?" { send "10.0.2.2 1.1.1.1\r"; exp_continue } + "New password:" { send "root\r"; exp_continue } + "Retype password:" { send "root\r"; exp_continue } + "Which timezone are you in?" { send "\r"; exp_continue } + "HTTP/FTP proxy URL?" { send "\r"; exp_continue } + "Enter mirror number " { send "\r"; exp_continue } + "Which SSH server?" { send "\r"; exp_continue } + "Which disk(s) would you like to use?" { send "sda\r"; exp_continue } + "How would you like to use it?" { send "sys\r"; exp_continue } + "Erase the above disk(s) and continue?" { send "y\r"; exp_continue } + "Installation is complete" { send "poweroff\r" } + } + interact + ' +} + +start_vde() { + usage start_vde && return + sudo sh <<- EOT + vde_switch -tap tap0 -sock /tmp/vde.ctl -daemon -mod 666 + sleep 1 + ip address add 10.0.2.2/24 dev tap0 + ip link set tap0 up + echo 1 > /proc/sys/net/ipv4/ip_forward + iptables -t nat -A POSTROUTING -s 10.0.2.0/24 -o eth0 -j MASQUERADE + iptables -t nat -A POSTROUTING -s 10.0.2.0/24 -o wlan0 -j MASQUERADE + EOT + #slirpvde --dhcp --daemon +} + +stop_vde() { + # killall slirpvde + sudo sh <<- EOT + iptables -t nat -D POSTROUTING -s 10.0.2.0/24 -o eth0 -j MASQUERADE + iptables -t nat -D POSTROUTING -s 10.0.2.0/24 -o wlan0 -j MASQUERADE + ip link set tap0 down + killall vde_switch + EOT +} + +start() { + usage 'start [-acd] name' 'start a virtual machine' && return + while getopts :acd opt; do + case $opt in + a) opta=1 ;; + c) boot=c ;; + d) boot=d ;; + esac + done + shift $((OPTIND -1)) + [ "$1" ] || die 'start failed: name missing' + cd "$dir/$1" || die "start failed: invalid directory $dir/$1" + is_running "$1" || start_qemu "$1" + [ "$opta" ] && console "$1" +} + +start_qemu() ( + opt='-nographic -cpu max' + [ "$sys" = Linux ] && opt="$opt -enable-kvm" + . ./config || die "could not source $PWD/config" + exec 1>>qemu.log 2>&1 + date +%F_%T + set -x + screen -S "vm!$1!" -d -m qemu-system-$arch $opt \ + ${smp+-smp $smp} \ + ${ram+-m $ram} \ + ${mac+-net nic,macaddr=$mac,model=virtio-net-pci} -net vde \ + ${hdd+-drive file="$hdd",if=virtio,media=disk,format=raw} \ + ${iso+-drive file="$iso",if=virtio,media=cdrom,format=raw} \ + ${boot+-boot $boot} +) + +stop() { + usage 'stop name' 'stop a virtual machine' && return + is_running "$1" && kill "$(pidof "$1")" +} + +usage() { + case $Opth in + 1) printf " %-34s %s\n" "$1" "$2" ;; + 2) printf "$0 $1\n\t$2\n" ;; + *) return 1 ;; + esac +} + +version() { + usage 'version' 'Print version' && return + echo "$version" +} + +Cmdlist='console create help init_alpine_iso ls pidof setup_alpine start start_vde stop version' +[ "$1" ] && C=$1 && shift 1 || { help; exit 1; } +#for c in $Cmdlist; do +# case $c in +# ("$C") cmd=$c; break ;; +# ("$C"*) [ "$cmd" ] && die "ambiguous command $C" || cmd=$c ;; +# esac +#done +cmd=$C +[ "$cmd" ] || { help; exit 1; } && $cmd "$@" -- cgit v1.2.3 From e200c8d78ca1d3388c25c1cef8892b1dd015bbf7 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Fri, 28 Jan 2022 12:19:59 +0100 Subject: update --- bin/vm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'bin') diff --git a/bin/vm b/bin/vm index 97e3521..6b03d0e 100755 --- a/bin/vm +++ b/bin/vm @@ -242,7 +242,8 @@ start_vm() ( "$dir/vftool" \ ${kernel+-k "$kernel"} \ ${initrd+-i "$initrd"} \ - ${hdd+-d "$hdd"} \ + ${hda+-d "$hda"} \ + ${hdb+-d "$hdb"} \ ${iso+-c "$iso"} \ ${cpu+-p "$cpu"} \ ${ram+-m "$ram"} \ -- cgit v1.2.3 From a3a3a61742950d45f1573a45a928e9b04c115634 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Thu, 3 Feb 2022 14:43:32 +0100 Subject: update --- bin/hdmi | 10 +++++----- bin/vm | 30 +++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 6 deletions(-) (limited to 'bin') diff --git a/bin/hdmi b/bin/hdmi index e6e3094..0a81716 100755 --- a/bin/hdmi +++ b/bin/hdmi @@ -1,9 +1,9 @@ #!/bin/sh # Toggle auxiliary HDMI screen on/off when connected/disconnected. -opt="--auto --left-of eDP1" -#opt="--auto --right-of eDP1" -#opt="--auto --above eDP1" +opt="--auto --left-of eDP-1" +#opt="--auto --right-of eDP-1" +#opt="--auto --above eDP-1" # Also add the following rule to /etc/udev/rules.d/hdmi.rules # KERNEL=="card0", SUBSYSTEM=="drm", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/marc/.Xauthority", RUN+="/home/marc/bin/hdmi" @@ -11,5 +11,5 @@ opt="--auto --left-of eDP1" # wait for the screen to settle sleep 3 -xrandr | grep -q '^DP1 connected' || opt="--off" -xrandr --output DP1 $opt +xrandr | grep -q '^DP-1 connected' || opt="--off" +xrandr --output DP-1 $opt diff --git a/bin/vm b/bin/vm index 8b4da42..35a107e 100755 --- a/bin/vm +++ b/bin/vm @@ -35,13 +35,41 @@ console() { is_running "$1" && screen -r "vm!$1!" } +create_arch() { + usage 'create_arch [-s size] name' 'Create an archlinux disk image' && return + size=8g + while getopts :s: opt; do + case $opt in + s) size=$OPTARG ;; + *) Opth=2 create_arch; return ;; + esac + done + shift $((OPTIND - 1)) + [ -d "$dir/$1" ] && die "create failed: $dir/$1 already exists" + mkdir "$dir/$1" + cd "$dir/$1" || die "create failed: invalid directory $dir/$1" + qemu-img create "$1.raw" "$size" || die "create failed" + mac=$(new_macaddr) + ip=$(new_ip) + hdd="$1.raw" + echo "hdd=$hdd +mac=$mac +ip=$ip" >> config + + mkfs.ext4 "$1.raw" + mkdir -p mnt + sudo mount "$1.raw" mnt + sudo pacstrap mnt base base-devel + sudo umount mnt +} + create() { usage 'create [-s size] name' 'Create an alpinelinux disk image' && return size=8g while getopts :s: opt; do case $opt in s) size=$OPTARG ;; - *) Opth=2 create_alpine_image; return ;; + *) Opth=2 create_arch; return ;; esac done shift $((OPTIND - 1)) -- cgit v1.2.3 From 8867b0a2d59bb6a2747a54c89a772cc5443308a5 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sat, 5 Feb 2022 16:26:29 +0100 Subject: update --- bin/update_kernel_host | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'bin') diff --git a/bin/update_kernel_host b/bin/update_kernel_host index 6180921..d159417 100755 --- a/bin/update_kernel_host +++ b/bin/update_kernel_host @@ -2,6 +2,11 @@ # After 'apk upgrade', update kernel and initrd on VM host +if [ -f /etc/arch-release ]; then + scp /boot/Image /boot/Image /boot/initramfs-linux.img marc@m1:.vm/a1 + exit +fi + sudo cp /boot/initramfs-virt /tmp sudo chmod a+r /tmp/initramfs-virt gunzip < /boot/vmlinuz-virt > /tmp/vmlinux -- cgit v1.2.3 From 342712955e9b65f2de134fa6542f97095a59e438 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Mon, 7 Feb 2022 09:19:12 +0100 Subject: update --- bin/update_kernel_host | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bin') diff --git a/bin/update_kernel_host b/bin/update_kernel_host index d159417..4f2cbbf 100755 --- a/bin/update_kernel_host +++ b/bin/update_kernel_host @@ -3,7 +3,7 @@ # After 'apk upgrade', update kernel and initrd on VM host if [ -f /etc/arch-release ]; then - scp /boot/Image /boot/Image /boot/initramfs-linux.img marc@m1:.vm/a1 + scp /boot/Image /boot/initramfs-linux.img marc@m1:.vm/a1 exit fi -- cgit v1.2.3 From cba68be4f36e9a1d34bc7a2d88ceda7a08ea0ddd Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Fri, 13 May 2022 13:47:14 +0200 Subject: update --- bin/gauth | 4 ++-- bin/start_godoc | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'bin') diff --git a/bin/gauth b/bin/gauth index b18f73d..169fd3b 100755 --- a/bin/gauth +++ b/bin/gauth @@ -1,8 +1,8 @@ #!/bin/sh # Use backup from andOTP -# cat ~/.otp_accounts.json | -gpg -qd ~/.otp_accounts.json.gpg 2>/dev/null | +#gpg -qd ~/.otp_accounts.json.gpg 2>/dev/null | +cat ~/.otp_accounts.json | jq -r '.[] | "\(.label) \(.secret)"' | while read -r l s; do echo "$l $(oathtool --totp -b "$s")" diff --git a/bin/start_godoc b/bin/start_godoc index 15bb70a..6433377 100755 --- a/bin/start_godoc +++ b/bin/start_godoc @@ -1,3 +1,3 @@ #!/bin/sh -/Users/marc/go/bin/godoc >/tmp/godoc.out 2>&1 & +/home/marc/go/bin/godoc >/tmp/godoc.out 2>&1 & -- cgit v1.2.3 From 96f5dc439ef6f91ba50ae842d4e5cb86c92a3553 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Tue, 14 Jun 2022 17:37:01 +0200 Subject: update --- bin/md2html | 193 +++++++++++++++++++++++++++++------------------------------- 1 file changed, 93 insertions(+), 100 deletions(-) (limited to 'bin') diff --git a/bin/md2html b/bin/md2html index 35e1d72..7be4aaa 100755 --- a/bin/md2html +++ b/bin/md2html @@ -1,43 +1,42 @@ #!/usr/bin/awk -f # md2html.awk -# by: Jesus Galan (yiyus) , May 2009 # Usage: # md2html file.md > file.html # Options: -v esc=false to not escape html -function newblock(nblock){ - if(text) - print "<" block ">" text ""; - text = ""; - block = nblock ? nblock : "p"; +function newblock(nblock) { + if (text) + print "<" block ">" text "" + text = "" + block = nblock ? nblock : "p" } -function subinline(tgl, inl){ - while(match($0, tgl)){ +function subinline(tgl, inl) { + while (match($0, tgl)){ if (inline[ni] == inl) - ni -= sub(tgl, ""); + ni -= sub(tgl, "") else if (sub(tgl, "<" inl ">")) - inline[++ni] = inl; + inline[++ni] = inl } } -function dolink(href, lnk){ +function dolink(href, lnk) { # Undo escaped html in uris - gsub(/&/, "\\&", href); - gsub(/</, "<", href); - gsub(/>/, ">", href); + gsub(/&/, "\\&", href) + gsub(/</, "<", href) + gsub(/>/, ">", href) # & can be tricky, and not standard: - gsub(/&/, "\\\\\\&", href); - gsub(/&/, "\\\\\\&", lnk); - return "" lnk ""; + gsub(/&/, "\\\\\\&", href) + gsub(/&/, "\\\\\\&", lnk) + return "" lnk "" } BEGIN { ni = 0; # inlines nl = 0; # nested lists - text = ""; - block = "p"; + text = "" + block = "p" } # Escape html @@ -49,128 +48,122 @@ esc != "false" { # Horizontal rules (_ is not in markdown) /^[ ]*([-*_] ?)+[ ]*$/ && text == "" { - print "
"; - next; + print "
" + next } # Tables (not in markdown) # Syntax: # Right Align| Center Align |Left Align /([ ]\|)|(\|[ ])/ { - if(block != "table") - newblock("table"); - nc = split($0, cells, "|"); - $0 = "\n"; - for(i = 1; i <= nc; i++){ - align = "left"; - if(sub(/^[ ]+/, "", cells[i])){ - if(sub(/[ ]+$/, "", cells[i])) - align = "center"; + if (block != "table") + newblock("table") + nc = split($0, cells, "|") + $0 = "\n" + for (i = 1; i <= nc; i++){ + align = "left" + if (sub(/^[ ]+/, "", cells[i])){ + if (sub(/[ ]+$/, "", cells[i])) + align = "center" else - align = "right"; + align = "right" } - sub(/[ ]+$/,"", cells[i]); - $0 = $0 "" cells[i] "\n"; + sub(/[ ]+$/,"", cells[i]) + $0 = $0 "" cells[i] "\n" } - $0 = $0 ""; + $0 = $0 "" } # Ordered and unordered (possibly nested) lists /^[ ]*([*+-]|(([0-9]+[\.-]?)+))[ ]/ { - newblock("li"); - nnl = 1; - while(match($0, /^[ ]/)){ - sub(/^[ ]/,""); - nnl++; + newblock("li") + nnl = 1 + while (match($0, /^[ ]/)){ + sub(/^[ ]/,"") + nnl++ } - while(nl > nnl) - print ""; - while(nl < nnl){ - list[++nl] = "ol"; - if(match($0, /^[*+-]/)) - list[nl] = "ul"; - print "<" list[nl] ">"; + while (nl > nnl) + print "" + while (nl < nnl){ + list[++nl] = "ol" + if (match($0, /^[*+-]/)) + list[nl] = "ul" + print "<" list[nl] ">" } - sub(/^([*+-]|(([0-9]+[\.-]?)+))[ ]/,""); + sub(/^([*+-]|(([0-9]+[\.-]?)+))[ ]/,"") } # Multi line list items block == "li" { - sub(/^( *)|( *)/,""); + sub(/^( *)|( *)/,"") } # Code blocks /^( | )/ { - if(block != "code") - newblock("code"); - sub(/^( | )/, ""); - text = text $0 "\n"; - next; + if (block != "code") + newblock("code") + sub(/^( | )/, "") + text = text $0 "\n" + next } -# Paragraph +# Paragraphs /^$/ { - newblock(); - while(nl > 0) - print ""; + newblock() + while (nl > 0) + print "" } -# Setex-style Headers -# (Plus h3 with underscores.) -/^=+$/ { - block = "h" 1; - next; -} - -/^-+$/ { - block = "h" 2; - next; +# Headers +/^#/ { + newblock() + match($0, /#+/) + n = RLENGTH + if (n > 6) + n = 6 + text = substr($0, RLENGTH + 1) + block = "h" n + next } -/^_+$/ { - block = "h" 3; - next; +# Alternate headers (underlined) +/^=+$/ { + block = "h" 1 + next } -# Atx-style headers -/^#/ { - newblock(); - match($0, /#+/); - n = RLENGTH; - if(n > 6) - n = 6; - text = substr($0, RLENGTH + 1); - block = "h" n; - next; +/^-+$/ { + block = "h" 2 + next } -// { +{ # Images - while(match($0, /!\[[^\]]+\]\([^\)]+\)/)){ - split(substr($0, RSTART, RLENGTH), a, /(!\[)|\)|(\]\()/); - sub(/!\[[^\]]+\]\([^\)]+\)/, "\"""); + while (match($0, /!\[[^\]]+\]\([^\)]+\)/)){ + split(substr($0, RSTART, RLENGTH), a, /(!\[)|\)|(\]\()/) + sub(/!\[[^\]]+\]\([^\)]+\)/, "\""") } # Links - while(match($0, /\[[^\]]+\]\([^\)]+\)/)){ - split(substr($0, RSTART, RLENGTH), a, /[\[\)]|(\]\()/); - sub(/\[[^\]]+\]\([^\)]+\)/, dolink(a[3], a[2])); + while (match($0, /\[[^\]]+\]\([^\)]+\)/)){ + split(substr($0, RSTART, RLENGTH), a, /[\[\)]|(\]\()/) + sub(/\[[^\]]+\]\([^\)]+\)/, dolink(a[3], a[2])) } # Auto links (uri matching is poor) - na = split($0, a, /(^\()|[ ]|([,\.\)]([ ]|$))/); - for(i = 1; i <= na; i++) - if(match(a[i], /^(((https?|ftp|file|news|irc):\/\/)|(mailto:)).+$/)) - sub(a[i], dolink(a[i], a[i])); + na = split($0, a, /(^\()|[ ]|([,\.\)]([ ]|$))/) + for (i = 1; i <= na; i++) + if (match(a[i], /^(((https?|ftp|file|news|irc):\/\/)|(mailto:)).+$/)) + sub(a[i], dolink(a[i], a[i])) # Inline - subinline("(\\*\\*)|(__)", "strong"); - subinline("\\*", "em"); - subinline("`", "code"); - text = text (text ? " " : "") $0; + subinline("(\\*\\*)|(__)", "strong") + subinline("\\*", "em") + subinline("`", "code") + text = text (text ? " " : "") $0 } END { - while(ni > 0) - text = text ""; - newblock(); - while(nl > 0) - print ""; + while (ni > 0) + text = text "" + newblock() + while (nl > 0) + print "" } -- cgit v1.2.3 From 21d2bd0a3bebcec2cf6adab854d96196f59463ce Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Mon, 22 Aug 2022 15:04:00 +0200 Subject: update --- bin/start_godoc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'bin') diff --git a/bin/start_godoc b/bin/start_godoc index 6433377..c6a716b 100755 --- a/bin/start_godoc +++ b/bin/start_godoc @@ -1,3 +1,5 @@ #!/bin/sh -/home/marc/go/bin/godoc >/tmp/godoc.out 2>&1 & +# Start a godoc server on port 6060 + +exec /home/marc/go/bin/godoc >/tmp/godoc.out 2>&1 & -- cgit v1.2.3 From 68c376b472b62b72487e8b4ec93a93453e0382c1 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Fri, 26 Aug 2022 20:05:54 +0200 Subject: update --- bin/vcat | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100755 bin/vcat (limited to 'bin') diff --git a/bin/vcat b/bin/vcat new file mode 100755 index 0000000..7c162d4 --- /dev/null +++ b/bin/vcat @@ -0,0 +1,8 @@ +#!/bin/sh + +# Use vim to cat a file. +# Useful if file is vim-encrypted (password silently expected). + +trap "stty '$(stty -g)'" EXIT +stty -echo +vim -es '+%p' '+:q!' "$1" -- cgit v1.2.3 From 8f53ffef1295dabee8fd296a5f5cc9c849935af0 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sat, 27 Aug 2022 15:25:58 +0200 Subject: update --- bin/gauth | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bin') diff --git a/bin/gauth b/bin/gauth index 169fd3b..2da5ee2 100755 --- a/bin/gauth +++ b/bin/gauth @@ -1,8 +1,8 @@ #!/bin/sh # Use backup from andOTP -#gpg -qd ~/.otp_accounts.json.gpg 2>/dev/null | -cat ~/.otp_accounts.json | +#cat ~/.otp_accounts.json | +gpg -qd ~/.otp_accounts.json.gpg 2>/dev/null | jq -r '.[] | "\(.label) \(.secret)"' | while read -r l s; do echo "$l $(oathtool --totp -b "$s")" -- cgit v1.2.3 From b5bfa659d79895d3217377aea14ff14eb31a6319 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Wed, 31 Aug 2022 23:36:21 +0200 Subject: update --- bin/update_hosts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'bin') diff --git a/bin/update_hosts b/bin/update_hosts index 40ec470..74bf25b 100755 --- a/bin/update_hosts +++ b/bin/update_hosts @@ -7,9 +7,11 @@ echo 'Checking from https://github.com/StevenBlack/hosts:' lsd=$(curl -s 'https://api.github.com/repos/StevenBlack/hosts/commits?path=hosts&page=1&per_page=1' | jq -r '.[0].commit.committer.date') -echo "last source update: $(date -j -f "%FT%TZ" "$lsd")" +#echo "last source update: $(date -j -f "%FT%TZ" "$lsd")" +echo "last source update: $(date --date="$lsd")" echo "last local update: $(date -r /etc/hosts)" -[ $(date -j -f "%FT%TZ" "$lsd" +%s) -lt $(date -r /etc/hosts +%s) ] && echo 'Nothing to do' && exit +# [ $(date -j -f "%FT%TZ" "$lsd" +%s) -lt $(date -r /etc/hosts +%s) ] && echo 'Nothing to do' && exit +[ $(date --date="$lsd" +%s) -lt $(date -r /etc/hosts +%s) ] && echo 'Nothing to do' && exit cd /etc cp -p hosts hosts.old -- cgit v1.2.3 From 0a294a82b2cf29a46cc926ceefebd2dcd34a0ab7 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Tue, 27 Sep 2022 15:10:44 +0200 Subject: add bin/w3 --- bin/w3 | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100755 bin/w3 (limited to 'bin') diff --git a/bin/w3 b/bin/w3 new file mode 100755 index 0000000..52a6408 --- /dev/null +++ b/bin/w3 @@ -0,0 +1,6 @@ +#!/bin/sh + +cols=$(stty -a|awk -F "[ ;]" '{print $9; exit}') +stty cols 72 +w3m "${@:-https://ddg.gg/lite/}" +stty cols "$cols" -- cgit v1.2.3 From 88199deb1a7e2ab390d392593d9499173b1190ba Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Wed, 28 Sep 2022 08:49:02 +0200 Subject: update --- bin/w3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bin') diff --git a/bin/w3 b/bin/w3 index 52a6408..90cc29c 100755 --- a/bin/w3 +++ b/bin/w3 @@ -1,6 +1,6 @@ #!/bin/sh cols=$(stty -a|awk -F "[ ;]" '{print $9; exit}') -stty cols 72 +[ $cols -gt 72 ] && stty cols 72 w3m "${@:-https://ddg.gg/lite/}" stty cols "$cols" -- cgit v1.2.3 From abaaf4b6a33c497f233fd797272ba36152eb6961 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Thu, 29 Sep 2022 15:16:15 +0200 Subject: yoda: slight improvements --- bin/byo | 9 ++++++--- bin/yoda | 4 +++- 2 files changed, 9 insertions(+), 4 deletions(-) (limited to 'bin') diff --git a/bin/byo b/bin/byo index 93bb7f0..33ba3b7 100755 --- a/bin/byo +++ b/bin/byo @@ -2,23 +2,26 @@ # Backup to yoda [ "$USER" = root ] || exec sudo "$0" "$@" -usage='Usage: byo [-cdk] [/dev/sda2] +usage='Usage: byo [-cdkx] [/dev/sda2] Backup local disk to yoda external disk. Options: -c close and umount yoda -d dedupe yoda -k do not umount and close yoda + -x enable debug traces ' -while getopts :Ccdk opt; do +while getopts :Ccdkx opt; do case $opt in - (C|c|d|k) eval "opt$opt=$opt" ;; + (C|c|d|k|x) eval "opt$opt=$opt" ;; (*) printf %s "$usage"; exit 1 ;; esac done shift $((OPTIND - 1)) +[ "$optx" ] && set -x + dev=$1 yoda_uuid='8c463221-6bb7-414e-9060-c9570bb3a6bb' [ "$dev" ] || dev=$(blkid --uuid "$yoda_uuid") diff --git a/bin/yoda b/bin/yoda index 8ff9e81..a5d5c5b 100755 --- a/bin/yoda +++ b/bin/yoda @@ -138,12 +138,13 @@ EOT cat > /mnt/etc/locale.gen << \EOT en_US.UTF-8 UTF-8 +en_GB.UTF-8 UTF-8 fr_FR.UTF-8 UTF-8 EOT echo yoda > /mnt/etc/hostname echo 'KEYMAP=fr-latin1' > /mnt/etc/vconsole.conf - echo 'LANG=en_US.UTF-8' > /mnt/etc/locale.conf + echo 'LANG=en_GB.UTF-8' > /mnt/etc/locale.conf ln -sf /usr/share/zoneinfo/Europe/Paris /mnt/etc/localtime cat > /mnt/etc/systemd/network/20-wired.network << \EOT @@ -171,6 +172,7 @@ EOT cat > /mnt/etc/systemd/resolved.conf << \EOT [Resolve] DNSSEC=no +DNSOverTLS=no EOT packages="linux linux-firmware intel-ucode amd-ucode sof-firmware \ -- cgit v1.2.3 From 5b201b3fcd94c8bad917e2b45dbb1105cb060cdb Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sat, 1 Oct 2022 12:41:24 +0200 Subject: update --- bin/backup | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'bin') diff --git a/bin/backup b/bin/backup index d682849..9e73703 100755 --- a/bin/backup +++ b/bin/backup @@ -1,5 +1,7 @@ #!/bin/sh +# A backup a day keeps the doctor away. + usage() { echo "Usage: $0 [-nv] [[[user@]host]:dir] @@ -34,13 +36,19 @@ BACKUP=${1:-$BACKUP} ignore="$HOME/.backupignore" volumes="$HOME" [ -f "$ignore" ] && ignore="--exclude-from=$ignore" || ignore= -last=$(rsync --list-only "$BACKUP/" 2>/dev/null | awk '{r=$NF} END {print r}') -case $last in -([12]*) opt_link=--link-dest=../$last;; -(*) opt_link=;; -esac - -date=$(date +%Y-%m-%d-%H%M%S) +now=$(date +%F) +opt_link='' +past=$(rsync --list-only "$BACKUP/" 2>/dev/null | awk '{print $NF}' | sort -r) +for f in $past +do + case $f in + ([0-9][0-9][0-9][0-9]-*) ;; + (*) continue ;; + esac + [ "$f" = "$now" ] && continue + opt_link="--link-dest=../$f" + break +done -echo "# Backup $volumes to $BACKUP/$date" -exec rsync -HSxa$optv $ignore $opt_link $volumes "$BACKUP/$date" +echo "# Backup $volumes to $BACKUP/$now" +exec rsync -HSXxa$optv $ignore $opt_link $volumes "$BACKUP/$now" -- cgit v1.2.3 From 6aa36aad93b61538befff7b8c1454e73b93de8c0 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Mon, 17 Oct 2022 18:24:04 +0200 Subject: update --- bin/fv | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bin') diff --git a/bin/fv b/bin/fv index 5662ed3..1f54bdc 100755 --- a/bin/fv +++ b/bin/fv @@ -3,4 +3,4 @@ #exec fzf --multi --preview-window=right:66% --preview 'cat {1}' exec fzf --ansi --multi --preview-window=right:75% \ --bind=left:preview-page-up --bind=right:preview-page-down \ - --preview 'cat {1}' + --preview 'bat --color always {1}' -- cgit v1.2.3 From a13224ab0c00d6524710f8477d9efdf661296ca3 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Tue, 1 Nov 2022 12:08:28 +0100 Subject: update --- bin/wag | 291 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 291 insertions(+) create mode 100755 bin/wag (limited to 'bin') diff --git a/bin/wag b/bin/wag new file mode 100755 index 0000000..4e0c802 --- /dev/null +++ b/bin/wag @@ -0,0 +1,291 @@ +#!/bin/sh + +## wag is a tool to generate static web sites + +unset CDPATH +export LC_ALL=C IFS=' +' + +cmd=$(command -v "$0") + +## help prints this program documentation +help() { awk '/^## / {print substr($0, 4)}' "$cmd"; } + +lipsum='Lorem ipsum dolor sit amet, consectetur adipiscing elit, +sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris +nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in +reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla +pariatur. Excepteur sint occaecat cupidatat non proident, sunt in +culpa qui officia deserunt mollit anim id est laborum.' + +# An overly simplified http request parser for static web sites. +http_request() { + read -r cmd uri proto + case $uri in */) uri="${uri}index.html" ;; esac + while true; do + read -r line || break + [ ${#line} = 0 ] && break + done + printf 'HTTP/1.1 200 OK\n\n' && cat "${uri#/}" +} + +md2h() { want="$2" got=$(echo "$1" | md2html -); } + +md2html() { + tmp=$(mktemp -u) + trap "rm -f '$tmp'" EXIT + { + cat "${1:--}" | tee "$tmp" | awk '/^[ ]*\[[^]]+\]:/' + cat "$tmp" + } | + awk ' + function newblock(nblock) { + if (text) + print "<" block ">" text "" + text = "" + out = 1 + block = nblock ? nblock : "p" + } + + function subinline(tgl, inl) { + while (match($0, tgl)) { + if (inline[ni] == inl) + ni -= sub(tgl, "") + else if (sub(tgl, "<" inl ">")) + inline[++ni] = inl + } + } + + function dolink(href, lnk) { + # Undo escaped html in uris + gsub(/&/, "\\&", href) + gsub(/</, "<", href) + gsub(/>/, ">", href) + # & can be tricky, and not standard: + gsub(/&/, "\\\\\\&", href) + gsub(/&/, "\\\\\\&", lnk) + return "" lnk "" + } + + BEGIN { + ni = 0 # inlines + nl = 0 # nested lists + out = 0 # 0 if no output so far + text = "" + block = "p" + } + + # Skip front matter. + out == 0 && $0 == "---" { + do + getline + while ($0 != "---") + next + } + + # Escape HTML. + esc != "false" { + gsub("&", "\\&") + gsub("<", "\\<") + gsub(">", "\\>") + } + + # Internal references. + match($0, /^[ ]*\[[^]]+\]:/) > 0 { + k = substr($0, RSTART+1, RLENGTH-3) + v = substr($0, RLENGTH+1) + sub(/^[ ]/, "", v) + sub(/[ ]$/, "", v) + ref[substr($0, RSTART+1, RLENGTH-3)] = v + next + } + + # Horizontal rules. + /^[ ]*([-*_] ?)+[ ]*$/ && text == "" { + print "
" + next + } + + # Tables. Syntax: + # Right Align| Center Align |Left Align + /([ ]\|)|(\|[ ])/ { + if (block != "table") + newblock("table") + nc = split($0, cells, "|") + $0 = "\n" + for (i = 1; i <= nc; i++) { + align = "left" + if (sub(/^[ ]+/, "", cells[i])) { + if (sub(/[ ]+$/, "", cells[i])) + align = "center" + else + align = "right" + } + sub(/[ ]+$/, "", cells[i]) + $0 = $0 "" cells[i] "\n" + } + $0 = $0 "" + } + + # Ordered and unordered (possibly nested) lists. + /^[ ]*([*+-]|(([0-9]+[.-]?)+))[ ]/ { + newblock("li") + nnl = 1 + while (match($0, /^[ ]/)) { + sub(/^[ ]/, "") + nnl++ + } + while (nl > nnl) + print "" + while (nl < nnl) { + list[++nl] = "ol" + if (match($0, /^[*+-]/)) + list[nl] = "ul" + print "<" list[nl] ">" + } + sub(/^([*+-]|(([0-9]+[.-]?)+))[ ]/, "") + } + + # Multi line list items. + block == "li" { + sub(/^( *)|( *)/, "") + } + + # Code blocks. + /^( | )/ { + if (block != "code") + newblock("code") + sub(/^( | )/, "") + text = text $0 "\n" + next + } + + # Paragraphs. + /^$/ { + newblock() + while (nl > 0) + print "" + } + + # Headers. + /^#+ / { + newblock() + match($0, /#+/) + n = RLENGTH + if (n > 6) + n = 6 + text = substr($0, RLENGTH + 1) + sub(/^ */, "", text) + block = "h" n + next + } + + # Alternate headers (underlined). + /^=+$/ { + block = "h" 1 + next + } + + /^-+$/ { + block = "h" 2 + next + } + + { + # Images. + while (match($0, /!\[[^]]+\]\([^)]+\)/)) { + split(substr($0, RSTART, RLENGTH), a, /(!\[)|\)|(\]\()/) + sub(/!\[[^]]+\]\([^)]+\)/, "\""") + } + # Links. + while (match($0, /\[[^]]+\]\([^)]+\)/)) { + split(substr($0, RSTART, RLENGTH), a, /[[)]|(\]\()/) + sub(/\[[^]]+\]\([^)]+\)/, dolink(a[3], a[2])) + } + # Internal references. + while (match($0, /\[[^]]+\]/)) { + k = substr($0, RSTART+1, RLENGTH-2) + sub(/\[[^]]+\]/, dolink(ref[k], k)) + } + # Auto links (uri matching is poor). + na = split($0, a, /(^\()|[ ]|([,.)]([ ]|$))/) + for (i = 1; i <= na; i++) + if (match(a[i], /^(((https?|ftp|file|news|irc):\/\/)|(mailto:)).+$/)) + sub(a[i], dolink(a[i], a[i])) + # Inline. + subinline("(\\*\\*)|(__)", "strong") + subinline("\\*", "em") + subinline("`", "code") + text = text (text ? " " : "") $0 + } + + END { + while (ni > 0) + text = text "" + newblock() + while (nl > 0) + print "" + }' +} + +#serve() { while true; do busybox nc -kl -p 1500 -e "$cmd http_request"; done; } +serve() { WAG_FUN=http_request busybox nc -kl -p 1500 -e "$cmd"; } + +test() { + fail=0 pass=0 skip=0 tfilter="$*" + + test_run md2h 'abc __def__ ghi' '

abc def ghi

' + test_run md2h 'abc **def** ghi' '

abc def ghi

' + test_run md2h 'abc *def* ghi' '

abc def ghi

' + test_run md2h 'abc ***def*** ghi' '

abc def ghi

' + test_run md2h 'abc `def` ghi' '

abc def ghi

' + test_run md2h '# h1' '

h1

' + test_run md2h '## h2' '

h2

' + test_run md2h 'h1 +==' '

h1

' + test_run md2h 'h2 +--' '

h2

' + test_run md2h 'abc [github] def' '

abc github def

' + test_run md2h 'abc [github](https://github.com) def' '

abc github def

' + test_run md2h 'abc [github] def + +[github]: https://github.com' '

abc github def

' + test_run md2h '--- +Title: front matter test +--- + +Hello [world]. + +--- +Bye. + +[world]: http://example.com' '

Hello world.

+
+

Bye.

' + + echo "Total: $((pass + fail + skip)), Passed: $pass, Failed: $fail, Skip: $skip" + return "$fail" +} + +test_run() { + eval "test_$1=\$((test_$1 + 1)); ti=\"\$test_$1\"" + [ "$tfilter" ] && + case "$1#$ti" in + $tfilter) ;; + *) skip=$((skip + 1)); return 0 ;; + esac + "$@" + [ "$got" = "$want" ] && { + pass=$((pass + 1)) + return 0 + } + fail=$((fail + 1)) + printf "%s FAIL\n Got: %s\n Want: %s\n" "$1#$ti" "$got" "$want" >&2 + return 1 +} + +[ "$WAG_FUN" ] && { $WAG_FUN; exit; } + +# Execute command line +[ "$1" ] || help && "$@" -- cgit v1.2.3 From 4489efe4b753a2edfad646c4b3506c95f2348813 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Tue, 15 Nov 2022 17:30:45 +0100 Subject: update --- bin/wag | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'bin') diff --git a/bin/wag b/bin/wag index 4e0c802..a1542b6 100755 --- a/bin/wag +++ b/bin/wag @@ -1,6 +1,8 @@ #!/bin/sh ## wag is a tool to generate static web sites +## +## Commands: unset CDPATH export LC_ALL=C IFS=' @@ -8,6 +10,48 @@ export LC_ALL=C IFS=' cmd=$(command -v "$0") +dest=./public + +footer() { + : +} + +# front parses front matter +front() { + { + read a b && [ "$a" = "---" ] && [ "$b" = "" ] || return + while read a b; do + [ "$a" = "---" ] && [ "$b" = "" ] && break + eval "front_${a%:}=\"$b\"" + done + } < "$1" +} + +## gen generates site content from source files +gen() { + [ -d "$dest" ] || mkdir -p "$dest" + for f in *.md; do + [ "$f" = "*.md" ] && continue + g="$dest/${f%.md}.html" + [ "$f" -ot "$g" ] && continue + echo "f: $f $g" + front "$f" + { + header + md2html "$f" + footer + } > "$g" + done +} + +header() { + cat <<- EOT + $front_title + + + EOT +} + ## help prints this program documentation help() { awk '/^## / {print substr($0, 4)}' "$cmd"; } -- cgit v1.2.3 From 0a215cad87338787410af0df999d41c14f00c9e0 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sat, 14 Jan 2023 18:32:46 +0100 Subject: update --- bin/dv | 653 ++++++++++++++++------------------------------------------------- 1 file changed, 155 insertions(+), 498 deletions(-) (limited to 'bin') diff --git a/bin/dv b/bin/dv index 0737295..39164db 100755 --- a/bin/dv +++ b/bin/dv @@ -1,556 +1,213 @@ #!/bin/sh -# Local dv (aka sim) +# - Usage: dv command [options] [args] +# - dv is a dumb version management tool. +# - Options: +# - -a num set ancestor level (default 1) +# - -n dry-run mode, no changes are made +# - -v verbose mode +# - Commands: -dv_version='dv-0.23 Copyright 2013-2014 Marc Vertes, Philippe Bergheaud' unset CDPATH export LC_ALL=C IFS=' ' - -anc() { - usage 'anc [Version]' 'print ancestor' && return - getdv .dv || die no item - case $1 in - (""|.|$PWD) - [ "$Opt_N" ] && Opt_N=$(($Opt_N - 1)) - [ "$Opt_N" ] && info anc $R2 $Opt_N || echo $R2 +version=dv-0.2 +dvlib=${DVLIB:-$HOME/dvlib} +script="$(cd "$(dirname "$0")" && pwd)/${0##*/}" +aopt=1 + +case $(uname -o) in + (Android|Linux) + diffopt='--color=always' + lsopt='-v --color' ;; - (*) info anc $1 ${Opt_N:-1};; - esac -} + (Darwin) + diffopt='--color=always' + lsopt='-G' + ;; + (*BSD) + ;; +esac -cache() { - isremote "$1" || return - o=$1 _d=$cacheprefix/$1; pd=${_d%/*}; ppd=${pd%/*} t=$pd/.${_d##*/} - [ -d "$ppd" ] || mkdir -p "$ppd" - [ -L "$ppd/.dv." ] || ln -s . "$ppd/.dv." - [ -f "$_d/.dv" ] && return - [ -d "$pd" ] || mkdir -p "$pd" - set -- $pd/*; shift $(($# - 1)) - [ -d "$1" ] && ropt="--link-dest=../${1##*/}" || ropt= - rsync -aDS $ropt "$o/" "$t" && mv "$t" "$_d" +# anc [-a num] [dir] - prints the current ancestor of dir or current. +anc() { + set -- "$(dvdir "$1")" + for _ in $(seq "$aopt"); do + set -- "$1/.dv/anc" + done + [ -L "$1" ] && readlink -f "$1" } -client() { - usage 'client [Version]' 'print clients' && return - getdv .dv || die no item +# clone [-nv] v1 [dir] - clones an iterm version into dir (default: item). +clone() { case $1 in - (""|.|$PWD) - while getpdir .dv .. - do - cd "$R1" - getdv ".dv" - [ "$OptM" ] && echo "$R2 -> $PWD" || echo "$R2" - ((Opt_N = Opt_N-1)) || break - done ;; - (*) info cli $1 ${Opt_N:-9999} | sort -u ;; + ('') die 'missing argument' ;; + (*[/.-]*) set -- "$(path "$1")" "$2" ;; + (*) set -- "$(head "$1")" "$2" ;; esac + [ "$2" ] || { set -- "$1" "${1##*/}"; set -- "$1" "${2%%-*}"; } + [ -d "$1" ] || die "not found: $1" + rsync ${vopt:+"$vopt"} ${nopt:+"$nopt"} -a "$1/" "$2/" + rm -f "$2/.dv/anc" && ln -s "$1" "$2/.dv/anc" } -clone() { - usage 'clone [-A Author] [-m Msg] Version [Dir]' \ - 'Copy version to dir' && return - [ "$1" ] || die missing argument - [ "$3" ] && die too many arguments - getpdir .dv . && root=$R1 && getdv $root/.dv && lib=$R1 item=${R2%/*} - new_version=${1##*/} - if [ "$new_version" = "$1" ] - then - getdv .dv && lib=$R1 new_item=${R2%/*} || die no item - else - v=${1%/$new_version} - new_item=${v##*/} - [ "$new_item" = "$v" ] || new_lib=${v%/$new_item} - fi - [ "$PWD" = "$root" ] && [ "$item" != "$new_item" ] && - die "already a clone of $item" - lib=${lib:-$new_lib} - [ "$lib" ] || die no lib - [ "$new_lib" -a "$new_lib" != "$lib" ] && die "already in lib: $lib" - if [ "$new_version" = 0 ] # Init item in lib - then - initlib "$lib" - getdvinfo "$lib" || die "could not lock $lib" - mkdir -p /tmp/dv.$$/$new_item/0 - dv_write $new_item/0 $new_item/0 >/tmp/dv.$$/$new_item/0/.dv - { cat /tmp/dv.$$/$new_item/0/.dv; echo; } >>/tmp/dv.$$/.dvinfo.0 - ln -f /tmp/dv.$$/.dvinfo.0 /tmp/dv.$$/.dvinfo.1 - rsync -a /tmp/dv.$$/ "$lib" - fi - if [ ! "$2" ] # Use the item name as default directory (a la git) - then - set -- "$1" "${1%/*}" - set -- "$1" "${2##*/}" - echo cloning into "$2" - fi - [ ! -d "$2" ] && mkdir "$2" - cd "$2" || die cannot chdir to "$2" - # Rename existing supplier directories to allow supplier symlinks - find . -name .dv | while read l - do - l=${l%/.dv} && [ "$l" = "." ] && continue - mv "$l" "$l.dvold" - done - # Sync from lib, starting from most client item, up to last supplier - d=. s=$new_item/$new_version - while [ "$d" ] - do - [ -L "$d" ] && { - rm -f "$d" - [ -d "$d.dvold" ] && mv "$d.dvold" "$d" - } - cache "$lib/$s" && pref=$cacheprefix/ || pref= - rsync -aDS "$pref$lib/$s/" "$d" - dv_header "$d" Anc "$s" - dv_header "$d" Lib "$lib" - # Find next symlink pointing to a supplier - d= l= - eval "$(find . -type l | while read -r l - do - f=$(readlink "$l") - case $f in - (*/.dv./*) echo "d=\"$l\" s=\"${f#*/.dv./}\"" - break;; - esac - done)" +# desc - prints the descendants of a version +desc() { + set -- "$(path "$1")" + set -- "$1" "${1%/*}" + set -- "$1" "${2##*/}" + for a in "$dvlib/$2"/*/.dv/anc*; do + [ "$a" -ef "$1" ] && echo "${a%/*/*}" done } -desc() { - usage 'desc [Version]' 'print descendants' && return - getdv .dv || die no item - case $1 in - (""|.|$PWD) ;; - (*) info desc $1 ;; - esac +die() { + echo "$@" >&2 + exit 1 } -die() { echo "$0: fatal: $@" >&2; exit 1; } - +# diff [-a num] [[v1] v2] - prints differences (default: current and ancestor). diff() { - usage 'diff [-x pat] [-F File] [V1 [V2]]' 'Print differences' && return - getpdir .dv && getdv "$R1/.dv" || die not in a clone - lib=$R1 item=${R2%/*} old=${R2#*/} - [ "$lib" = "" ] && [ -L "../../.dv." ] && lib=${PWD%/*/*} - if [ "$2" ] - then - case $2 in - (.) new=. new_is_clone=1 ;; - (*) new=$lib/$item/$2 ;; - esac - cache "$new" && np=$cacheprefix/ || np= - old=$lib/$item/$1 - elif [ "$1" ] - then - case $1 in - (.) new=. new_is_clone=1 ;; - (*) new=$lib/$item/$1 ;; - esac - cache "$new" && np=$cacheprefix/ || np= - getdv "$np$new/.dv" && old=${R1:-$lib}/$R2 + if [ "$2" ]; then + set -- "$(path "$2")" "$(path "$1")" + elif [ "$1" ]; then + set -- "$(path "$1")" "$(anc "$(path "$1")")" else - new=$PWD new_is_clone=1 - cache "$new" && np=$cacheprefix/ || np= - getdv "$np$new/.dv" && old=${R1:-$lib}/$R2 + set -- "$(dvdir)" "$(anc)" fi - cache "$old" && op=$cacheprefix/ || op= - [ "$Opts" ] || printf "old %s\nnew %s\n" "$old" "$new" - # Itemized diff for all suppliers, deepest first - cd "$np$new" && for f in $(find . -type f -name .dv | sort -r) - do - d=${f%/.dv} - [ "$d" = . ] && prefix= || prefix=${d#./}/ - getdv "$f" && cache "$lib/$R2" - [ "$new_is_clone" ] && dest=$np$d || dest=$prefix$d - diffdir "$op$lib/$R2" "$dest" - Optx="$Optx --exclude=${d##*/}" - done -} - -# Usage: diffdir oldpath newpath -# Print differences between 2 directories -diffdir() { - [ -f "$2/.dvignore" ] && xf=--exclude-from=$2/.dvignore || xf= - rsync -aDSniv $xf --delete --exclude=".dv.*/" $Optx "$2/" "$1" | - awk -v OptF="${OptF#./}" -v prefix=$prefix ' - NF == 0 {exit} - NR < 2 || /\/*\.dv$/ || /\/$/ {next} - # Match an itemized status for all versions of rsync -i - $1 !~ /^[<>ch.*][fdLDS+][.+?cstpoguaxz]+$/ {next} - {key = $1; file = substr($0, length(key) + 2)} - OptF && OptF != file {next} - key == "*deleting" {print "deleted " prefix file; next} - substr(key, 3, 7) == "+++++++" {print "created " prefix file; next} - { # Avoid false positive if only mtime is changed. - of = "'$1'/" file; gsub("'\''", "'\'\\\\\'\''", of) - nf = "'$2'/" file; gsub("'\''", "'\'\\\\\'\''", nf) - if (substr(key, 2, 1) == "L") { # Symlink - src = target = file - sub(/.* -> /, "", target); - sub(/ -> .*/, "", src); - "readlink '$2'/" src | getline otarget - if (target != otarget) - print "changed " src - } else if (system("cmp -s '\''" of "'\'\ \''" nf "'\''")) - print "changed " file - }' - # Or: scan key for file/link size, checksum or permission change - # rsync -c is required - #key ~ /[cps]/ { print "changed " prefix file }' + [ "$2" ] && command diff $diffopt -U 3 -x .dv "$2" "$1" | less -F } -dv_write() { - cat <<- EOT - From $(username) - Date: $(date +"%F %T %z") - Version: $1 - Anc: $2 - EOT - - [ "$OptA" ] && echo "Author: $OptA" - printf "\n%s\n" "$Optm" +# dvdir prints the dir containing .dv. +dvdir() { + set -- "$(realpath "${1:-.}")" + while [ "$1" ]; do + [ -d "$1/.dv" ] && echo "$1" && return + set -- "${1%/*}" + done } -dv_header() { - awk -v val="$3" '/^'$2':/ {print "'$2': " val; done = 1; next} - NF == 0 && done == 0 {print "'$2': " val; done = 1} - {print}' $1/.dv >$1/.dv.$$ && mv $1/.dv.$$ $1/.dv +# head - prints the branch head versions. +head() { + for v in "$dvlib/${1:-$(name)}/"*; do + [ "$(desc "$v")" ] || echo "$v" + done } -# Usage: getdv path -# Return lib in R1, anc in R2, version in R3 -getdv() { - R1= R2= - while read -r line - do +# help - prints this help text. +help() { + while read -r line; do case $line in - ("Lib: "*) R1=${line#Lib: };; - ("Anc: "*) R2=${line#Anc: };; - ("Version: "*) R3=${line#Version: };; + (\#*\ -\ *) ;; + (*) continue ;; esac - done <$1 - [ "$R1" -o "$R2" ] + set -- "${line% - *}" "${line#* - }" + [ "$1" = '#' ] || printf " %-26s" "${1#\# }" + echo "$2" + done < "$script" } -# Usage: getdvinfo lib -# Get exclusive write access to a dvlib, for new version commit -getdvinfo() { - [ -d /tmp/dv.$$ ] || mkdir /tmp/dv.$$ - [ "$Optn" ] && { rsync "$1/.dvinfo.1" /tmp/dv.$$/.dvinfo.0; return; } - t=0 - for i in 1 2 3 4 5 - do - rsync --remove-source-files "$1/.dvinfo.0" /tmp/dv.$$/ && break - t=$(($t + $i)) && sleep $t - done - test -f /tmp/dv.$$/.dvinfo.0 +# incv increments version. +incv() { + set -- "$1" "${1##*[-.a-z_A-Z]}" + set -- "${1%"$2"}" "$2" + echo "$1$(($2 + 1))" } -putdvinfo() { - [ "$Optn" ] && return - ln -f /tmp/dv.$$/.dvinfo.0 /tmp/dv.$$/.dvinfo.1 - rsync -a /tmp/dv.$$/.dvinfo.[01] "$1/" - isremote "$1" && cp /tmp/dv.$$/.dvinfo.0 "$cacheprefix/$1/" +# init - creates an initial clone in the current directory. +init() { + [ "$(dvdir)" ] && die "already initialized: $(dvdir)" + mkdir .dv } -# Usage: getpdir file [path] -# Return absolute parent dir of path or PWD containing file -getpdir() { - R1="$([ -d "${2:-.}" ] && cd "${2:-.}" && pwd)" - while [ "$R1" ] - do - [ -f "$R1/$1" ] && return || R1=${R1%/*} - done - return 1 -} +# ls [item] - lists dvlib. +ls() ( + cd "$dvlib" && command ls $lsopt "$@" +) -# Usage: getprd file [path] -# Return relative path to parent directory containing file -getprd() { - R2="$(cd "${2:-.}" && pwd)"; R2=${R2%/*} R1=.. - while [ "$R2" ] - do - [ -f "$R2/$1" ] && return || R2=${R2%/*} R1=$R1/.. - done - return 1 +# name prints the item name. +name() { + set -- "$(dvdir)" + [ "$1" ] && echo "${1##*/}" || die "not a dv item, no .dv found" } -info() { - usage info && return - case $2 in - (*:*) lib=${2%/*/*} ;; - (*) getpdir .dv && getdv $R1/.dv && lib=$R1 || die no dvlib found - esac - case $2 in # $2 specifies: - (*/*/*) v=${2%/*/*}; v=${2#$v/} ;; # lib/item/version - (*/*) v=$2 ;; # item/version - (*) v=${R2%/*}/$2 ;; # version +# path prints the version path, if exist. +path() { + case $1 in + (*/*|.) dvdir "$1"; return ;; + ([0-9]*) set -- "$(name)-$1" ;; esac - isremote "$lib" && { - p=$cacheprefix - [ -d "$p/$lib" ] || mkdir -p "$p/$lib" - [ "$Optl" ] || rsync -a "$lib/.dvinfo.1" "$p/$lib/" - } || p= - info_query $1 $v $3 <$p/$lib/.dvinfo.1 -} - -info_query() -{ - awk -v arg0=$1 -v arg1=$2 -v arg2=$3 ' - /^From / { - if (v) msg[v] = var["Body"] - delete var - var["From"] = substr($0, 6) - header = 1 - next - } - header == 1 && NF == 0 { - v = var["Version"]; a = var["Anc"]; s = var["Sup"] - if (a != v) { - anc[v] = a; desc[a] = desc[a] ? desc[a] " " v : v - } - n = split(s, as) - for (i = 1; i <= n; i++) { - cli[as[i]] = cli[as[i]] ? cli[as[i]] " " v : v - } - sup[v] = s; msg[v] = var["Msg"]; date[v] = var["Date"] - if (var["Root"]) root[v] = var["Root"] - header = 0 - next - } - header == 1 { - i = index($0, ":") - var[substr($0, 1, i-1)] = substr($0, i+2) - next - } - header == 0 { - if ($0 ~ />+From /) sub(/>/, "") - var["Body"] = var["Body"] ? var["Body"] "\n" $0 : $0 - } - END { - if (v) msg[v] = var["Body"] - if (arg0 == "graph") anc_graph() - else if (arg0 == "anc") query_anc(arg1, arg2) - else if (arg0 == "sup") query_sup(arg1, arg2) - else if (arg0 == "cli") query_cli(arg1, arg2) - else if (arg0 == "log") print msg[arg1] - else if (arg0 == "from") print from[arg1] - else if (arg0 == "date") print date[arg1] - else if (arg0 == "desc") printl(desc[arg1]) - } - function anc_graph() { - print "digraph G {" - for (v in anc) print "\"" anc[v] "\" -> \"" v "\"" - print "}" - } - function query_anc(v, n) { while (n-- > 0) v = anc[v]; print v } - function query_cli(v, n) { - while (n-- > 0 && v != "") { - num = split(v, av); v = "" - for (i = 1; i <= num; i++) { - printl(cli[av[i]]) - v = v ? v " " cli[av[i]] : cli[av[i]] - } - } - } - function query_sup(v, n) { - while (n-- > 0 && v != "") { - num = split(v, av); v = "" - for (i = 1; i <= num; i++) { - printl(sup[av[i]]) - v = v ? v " " sup[av[i]] : sup[av[i]] - } - } - } - function printl(l, i, n) { - n = split(l, al) - for (i = 1; i <= n; i++) - if (root[al[i]]) - print al[i] " -> '"$PWD/"'" root[al[i]] - else - print al[i] - }' + set -- "${1%%-*}" "${1#*-}" + [ -d "$dvlib/$1/$1-$2" ] && echo "$dvlib/$1/$1-$2" } -initlib() { - rsync --list-only "$1/.dv." >/dev/null 2>&1 && return - mkdir -p /tmp/dv.$$ - ln -s . /tmp/dv.$$/.dv. - >/tmp/dv.$$/.dvinfo.0 - >/tmp/dv.$$/.dvinfo.1 - chmod g+w /tmp/dv.$$/.dvinfo.[01] - rsync -a /tmp/dv.$$/ "$1" -} +# save [-nv] [v1] - creates a new version from clone. +save() { + [ "$1" ] && case $1 in (*[0-9]) ;; (*) die "invalid version: $1" ;; esac + set -- "$(dvdir)" "$(aopt=1 anc)" "$(name)" "${1#[a-zA-Z_]*-}" + set -- "$1" "$2" "$3" "${4:-$(incv "${2##*/[a-zA-Z_]*-}")}" + echo "$dvlib/$3/$3-$4" + command diff -q -x .dv "$2" "$1" >/dev/null 2>&1 && die 'no changes, abort' + [ "$nopt" ] && return -isremote() { case $1 in (*:*) return;; esac; return 1; } + # Update ancestor in clone then copy in dvlib. + rm -f "$1/.dv/anc" && [ "$2" ] && ln -s "../../${2##*/}" "$1/.dv/anc" + rsync ${vopt:+"$vopt"} -a ${2:+--link-dest="$2"} --mkpath "$1/" "$dvlib/$3/$3-$4/" -help_all() { - printf "$dv_version\nUsage: dv command [options] [args]\n" - Opth=1; for c in $Cmdlist; do $c; done + # After save, clone ancestor points to new saved version. + rm -f "$1/.dv/anc" && ln -s "$dvlib/$3/$3-$4" "$1/.dv/anc" } -newversion() { - [ "$OptV" ] && R1=$OptV && return - case $OptB in - ('') R1=$1 ;; - (*[-/]*) die 'illegal character [-/] in branch name' ;; - (*[0-9.]) die 'illegal end character [0-9.] in branch name' ;; - (*) case $1 in (*-${OptB}[1-9]*) R1=$1;; (*) R1=$1-${OptB}0;; esac ;; - esac - R2=${R1##*[!0-9]} - optb=$Optb - [ "$optb" ] && R3=.1 optb=${optb%b} || R3= R1=${R1%$R2}$(($R2 + 1)) - while [ "${optb}" ]; do optb=${optb%b} R3=.0$R3; done - R1=$R1$R3 - while grep -q "^Version: $item/$R1\$" /tmp/dv.$$/.dvinfo.0 - do - R2=${R1##*[!0-9]} - [ "$R2" ] && R1=${R1%$R2}$(($R2 - 1)).1 || R1=${R1}1 - done +# status - prints dv informations. +status() { + echo "lib: $dvlib" + set -- "$(dvdir)" "$(anc)" + [ "$1" ] || die "not a dv item, no .dv found" + echo "root: $1" + [ "$2" ] || die "no ancestor found, please save it first" + echo "anc: $2" + command diff -q -x .dv "$2" "$1" } -patch() { - usage 'patch [-x pat] [-F File] [Dir|Version]' \ - 'Print patch diff from ancestor' && return - diff "$@" | awk -v wdflag="-v${OptD:+3}${OptN:+1}${OptO:+2}" ' - { key = $1; file = substr($0, length(key) + 2) } - key == "old" { old = (file ~ /:/ ? "'$cacheprefix'/" : "") file; next } - key == "new" { new = (file ~ /:/ ? "'$cacheprefix'/" : "") file; next } - { - of = old "/" file; gsub("'\''", "'\'\\\\\'\''", of) - nf = new "/" file; gsub("'\''", "'\'\\\\\'\''", nf) - system("diff -Naup '\''" of "'\'\ \''" nf "'\''; echo") - }' +# sync [url] - synchronize a remote dvlib. +sync() { + set -- "${1:-$(cat "$dvlib/.url" 2>/dev/null)}" + [ "$1" ] || die "missing url" + [ "$(cat "$dvlib/.url" 2>/dev/null)" = "$1" ] || echo "$1" > "$dvlib/.url" + rsync ${vopt:+"$vopt"} ${nopt:+"$nopt"} -aH --mkpath "$1/" "$dvlib/" + rsync ${vopt:+"$vopt"} ${nopt:+"$nopt"} -aH --mkpath "$dvlib/" "$1/" } -save() { - usage 'save [-bln] [-A Author] [-m msg] [-x pat] [-B Branch|-V Version] [Dir]'\ - 'Save dir into a new version' && return - [ "$2" ] && die 'too many arguments' - getpdir .dv "$1" || die "not in a clone: ${1:-$PWD}" && root=$R1 - cd "$root" - getdv .dv && lib=$R1 && getdvinfo "$lib" - # Process supplier subdirs from the deepest up to "." (most client) - for d in $(find . -type f -name .dv | sort -r) - do - d=${d%/.dv} - cd "$root/$d" - OptF= save1 "$d" && nanc=$R1 && echo $nanc - [ "$d" = . ] && continue # not a supplier, done - - # Replace supplier subdir by symlink in lib - getprd .dv && s=$R1/../.dv./$nanc - t=${d##*/}; t=.dv.$t - mv "$root/$d" "$root/${d%/*}/$t" - ln -s "$s" "$root/$d" - done - putdvinfo "$lib" - # Restore supplier subdirs and remove links to lib, deepest first - find . -type d -name ".dv.*" | sort -r | while read d - do - t=${d##*/.dv.}; t=${d%/*}/$t - rm -f "$t" && mv "$d" "$t" - done +# version - prints the current version of dv. +version() { + echo "$version" } -save1() { - getdv .dv && lib=$R1 anc=$R2 && item=${anc%/*} old=${anc#*/} - [ "$(diffdir "$lib/$anc" .)" ] || { R1=$anc; return; } - newversion $old && new=$item/$R1 - [ "$Optn" ] || dv_write "$new" "$anc" >.dv - # Compute list of direct suppliers, for caching in .dvinfo.0 - lsup=$(find . -type l | while read -r l - do - case $l in (*/.dv.*) continue ;; esac - f=$(readlink "$l") - case $f in (*/.dv./*) printf "%s " ${f#*/.dv./} ;; esac - done) - [ ! "$Optn" ] && [ "$lsup" ] && dv_header . Sup "$lsup" - { cat .dv; echo; } >>/tmp/dv.$$/.dvinfo.0 - [ -f ".dvignore" ] && xf=--exclude-from=.dvignore || xf= - rsync -aDS$Optn$Optv --link-dest=../$old $xf --exclude=".dv.*/" \ - ./ "$lib/$new" - R1=$new - [ "$Optn" ] && return - isremote "$lib" && rsync -aDS --link-dest=../$old --exclude=".dv.*/" \ - ./ "$cacheprefix/$lib/$new" - dv_header . Anc "$new" - dv_header . Lib "$lib" -} - -supplier() { - usage 'supplier [Version]' 'print suppliers' && return - getdv .dv || die no item +# Main program starts here. +[ "$DVDEBUG" ] && set -x || case $1 in - (""|.|$PWD) # create a temporary dvinfo file, query it - V=$R2; find . -name .dv | - while read dv - do - [ "$dv" = "./.dv" ] && continue - # get the supplier - getdv "$dv"; v=$R2; dv=${dv%/.dv} - # get the client - getpdir .dv "${dv%/*}"; getdv "$R1/.dv" - # declare the client as supplier - printf "From \nVersion: %s\nSup: %s\n" "$v" "$R2" - [ "$OptM" ] && echo "Root: ${dv#./}"; echo - done | info_query cli $V ${Opt_N:-9999} ;; - (*) info sup $1 ${Opt_N:-9999} ;; - esac | sort -u -} - -usage() { [ "$Opth" ] && printf " %-34s %s\n" "$1" "$2"; } - -username() { - case $(uname -s) in - (Darwin) id -P | awk -v FS=: '{print $8}' ;; - (*) awk -v FS=[:,] '/^'$USER':/ {print $5; exit}' /etc/passwd ;; + (anc|clone|desc|diff|head|help|init|ls|path|save|status|sync|version) ;; + (*) help; exit 1;; esac -} - -wdiff() { - usage 'wdiff [-DNO] [-x pat] [-F File] [Dir|Version]' \ - 'Print word diffs from ancestor' && return - diff "$@" | - awk -v wdflag="-v${OptD:+3}${OptN:+1}${OptO:+2}" ' - { key = $1; file = substr($0, length(key) + 2) } - key == "old" { old = (file ~ /:/ ? "'$cacheprefix'/" : "") file; next } - key == "new" { new = (file ~ /:/ ? "'$cacheprefix'/" : "") file; next } - { - of = old "/" file; gsub("'\''", "'\'\\\\\'\''", of) - nf = new "/" file; gsub("'\''", "'\'\\\\\'\''", nf) - system("wd " wdflag " '\''" of "'\'\ \''" nf "'\''; echo") - }' | less -FCimnqGrX -j5 -h0 +/'\[0' -} - -cacheprefix=$HOME/.cache/dv -Cmdlist='anc client clone desc diff info patch save supplier wdiff' -while getopts :nvV opt # Parse global options -do +cmd="$1" +shift +while getopts :a:nv opt; do case $opt in - (V) echo "$dv_version"; exit ;; - (*) help_all; exit ;; + (a) aopt="$OPTARG" ;; + (n) nopt=-n ;; + (v) vopt=-v ;; + (*) help; exit ;; esac done shift $((OPTIND - 1)) -[ $1 ] && C=$1 && shift 1 || { help_all; exit 1; } -for c in $Cmdlist -do - case $c in - ($C|_$C) cmd=$c; break;; - ($C*) [ $cmd ] && die ambiguous command $C || cmd=$c;; - esac -done -while getopts :0123456789A:bB:F:hl:m:MnNOsvV:x: opt # Parse command options -do - case $opt in - ([0-9]) Opt_N=$Opt_N$opt;; - ([bhlnMNOsv]) eval Opt$opt=\${Opt$opt}$opt ;; - ([ABFmV]) eval Opt$opt=\$OPTARG ;; - (x) Optx="$Optx --exclude=$OPTARG" ;; - (*) Opth=1; $cmd; exit 1;; - esac -done -shift $(($OPTIND - 1)) -trap "rm -rf /tmp/dv.$$" EXIT -[ "$cmd" ] || die "no command \"$C\"" && $cmd "$@" +$cmd "$@" + +# Todo: +# * a function to squash a branch in the trunk +# * merge another ancestor (use anc0, anc1, ...) +# * vendor management +# * show graphs (dot) +# * sync: dvlib sync / mirror: resolve conflict by branching +# * rename version. +# * .dvignore +# * make dvlib versions immutable. -- cgit v1.2.3 From abe00380ff8b708b890371c73116e1006ea05178 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sat, 11 Mar 2023 16:11:37 +0100 Subject: update --- bin/lt | 1 + 1 file changed, 1 insertion(+) (limited to 'bin') diff --git a/bin/lt b/bin/lt index f09f3b7..2a182f7 100755 --- a/bin/lt +++ b/bin/lt @@ -36,6 +36,7 @@ ca() { pp() { pp_s=$1 pp_b=$2 R1='' while true; do + ! [ "$pp_s" ] && R1='' && return [ "$pp_b" = "$pp_s" ] && break pp_s=${pp_s%/*} [ "$R1" ] && R1=$R1/.. || R1=.. -- cgit v1.2.3 From b04d54fc52df277b189c2d794241c49c2ab61fb4 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Sat, 14 Oct 2023 14:24:47 +0200 Subject: update --- bin/idot | 4 ---- bin/status2 | 27 +++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) delete mode 100755 bin/idot create mode 100755 bin/status2 (limited to 'bin') diff --git a/bin/idot b/bin/idot deleted file mode 100755 index 44387e8..0000000 --- a/bin/idot +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -# dot -Gsize=9 -Nshape=box -Nstyle=rounded -Tsvg | convert -- - sixel:/dev/tty -# dot -Gsize=9 -Nfontname=Helvetica -Tsvg | convert -- - sixel:/-dev/tty -dot -Gsize=9 -Nfontname=Arial -Tpng | icat - /dev/tty diff --git a/bin/status2 b/bin/status2 new file mode 100755 index 0000000..1b7ea55 --- /dev/null +++ b/bin/status2 @@ -0,0 +1,27 @@ +#!/bin/sh + +ip() { + while read -r w1 w2 w3; do + [ $w1 = /32 ] && [ $w2 = host ] && [ $ip != 127.0.0.1 ] && break + ip=$w2 + done < /proc/net/fib_trie +} + +power() { + while read -r line; do + case $line in POWER_SUPPLY_CAPACITY*) power=${line#*=}% && break;; esac + done < /sys/class/power_supply/BAT0/uevent +} + +status() { + power + ip + sound="$(pamixer --get-volume-human)" + clock="$(date +'%a %d %b %H:%M')" +} + +while true; do + status && echo "$ip , power $power , sound $sound , $clock" + sleep 17 +done | +root-tail -g '500x20-80-0' -fn '-*-fixed-medium-*-*-*-15-*-*-*-*-*-*-*' --color grey70 - -- cgit v1.2.3