forked from ahurac/dotfiles
qemush : suppression, maintenant hébergé dans le repo ahurac/qemush
This commit is contained in:
parent
bf4dd08139
commit
f5a42ddb1e
8 changed files with 0 additions and 282 deletions
124
bin/qemush
124
bin/qemush
|
@ -1,124 +0,0 @@
|
|||
#!/bin/bash
|
||||
|
||||
exec_as() {
|
||||
local user
|
||||
user="$1"
|
||||
shift
|
||||
|
||||
if [ "$(whoami)" != "$user" ]; then
|
||||
exec sudo -u "$user" "$0" "$@"
|
||||
else
|
||||
cd || exit
|
||||
fi
|
||||
}
|
||||
|
||||
exec_as qemu "$@"
|
||||
|
||||
bin=bin
|
||||
images=images
|
||||
if [ -t 1 ]; then
|
||||
cat=src-hilite-lesspipe.sh
|
||||
else
|
||||
cat='cat'
|
||||
fi
|
||||
PATH="./${bin}:${PATH}"
|
||||
EDITOR="${EDITOR:-nvim}"
|
||||
alias ls='ls --color=auto'
|
||||
alias exec='exec '
|
||||
shopt -s expand_aliases
|
||||
umask 027
|
||||
|
||||
perror() {
|
||||
>&2 printf '\033[1;31mERROR:\033[0m \033[1m%s\033[0m\n' "$*"
|
||||
}
|
||||
|
||||
public_help() {
|
||||
local name
|
||||
name="$(basename "$0")"
|
||||
exec cat << EOF
|
||||
${name}: usage:
|
||||
${name} active - (default behaviour) list active VMs
|
||||
${name} start <VM name> - start a VM
|
||||
${name} watch <VM name> - attach VM screen session
|
||||
${name} ls - list available VMs
|
||||
${name} edit <VM name> - edit VM launching script
|
||||
${name} rm <VM name> - delete launch script
|
||||
${name} diskls - list available disk images
|
||||
${name} diskadd <disk name> <size> - create a disk image
|
||||
${name} diskrm <disk name> - delete disk image
|
||||
${name} shell - start a shell as user qemu
|
||||
${name} help - show this help
|
||||
EOF
|
||||
}
|
||||
|
||||
error_usage() {
|
||||
perror "Invalid usage"
|
||||
>&2 public_help
|
||||
return 1
|
||||
}
|
||||
|
||||
public_start() {
|
||||
local vm_name="$1"
|
||||
shift
|
||||
|
||||
exec screen -S "$vm_name" "$vm_name" "$@"
|
||||
}
|
||||
|
||||
public_watch() {
|
||||
exec screen -dr "$1"
|
||||
}
|
||||
|
||||
public_active() {
|
||||
exec screen -ls
|
||||
}
|
||||
|
||||
public_ls() {
|
||||
echo "Available machines:"
|
||||
exec ls "$bin"
|
||||
}
|
||||
|
||||
diskpath() {
|
||||
printf "%s.qcow2" "${images}/${1}"
|
||||
}
|
||||
|
||||
public_diskadd() {
|
||||
exec qemu-img create -f qcow2 "$(diskpath "$1")" "$2"
|
||||
}
|
||||
|
||||
public_diskrm() {
|
||||
exec rm -vi -- "$(diskpath "$1")"
|
||||
}
|
||||
|
||||
public_diskls() {
|
||||
echo "Available disks:"
|
||||
exec ls "$images"
|
||||
}
|
||||
|
||||
public_edit() {
|
||||
local file="${bin}/${1}"
|
||||
"$EDITOR" "$file"
|
||||
exec chmod u+x "$file"
|
||||
}
|
||||
|
||||
public_rm() {
|
||||
exec rm -vi -- "${bin}/${1}"
|
||||
}
|
||||
|
||||
public_shell() {
|
||||
exec bash -i
|
||||
}
|
||||
|
||||
public_cat() {
|
||||
exec "$cat" "${bin}/${1}"
|
||||
}
|
||||
|
||||
function="$1"
|
||||
shift
|
||||
|
||||
if [ -z "$function" ]; then
|
||||
public_active
|
||||
elif declare -F | cut -d \ -f 3- | grep '^public_' | sed 's/^public_//' | grep -q "^${function}$"; then
|
||||
"public_${function}" "$@"
|
||||
else
|
||||
error_usage
|
||||
fi
|
|
@ -1,8 +0,0 @@
|
|||
#!/bin/sh -x
|
||||
qemu-system-x86_64 \
|
||||
-enable-kvm \
|
||||
-M q35 \
|
||||
-cpu host -smp "$(("$(nproc)" / 2))" \
|
||||
-m "$(("$(free | awk '($1 == "Mem:") { print $2 }')" / 2))K" \
|
||||
-net nic \
|
||||
"$@"
|
|
@ -1,8 +0,0 @@
|
|||
#!/bin/sh -x
|
||||
exec linux \
|
||||
-vga qxl \
|
||||
-device virtio-serial \
|
||||
-chardev spicevmc,id=vdagent,debug=0,name=vdagent \
|
||||
-device virtserialport,chardev=vdagent,name=com.redhat.spice.0 \
|
||||
-spice port="$(first-free-port 5900)",addr=127.0.0.1,disable-ticketing=on \
|
||||
"$@"
|
|
@ -1,10 +0,0 @@
|
|||
#!/bin/sh -x
|
||||
exec qemu-system-x86_64 \
|
||||
-enable-kvm \
|
||||
-M q35 \
|
||||
-cpu host -smp "$(("$(nproc)" / 2))" \
|
||||
-m "$(("$(free | grep '^Mem:\s' | awk '{ print $NF }')" / 2))K" \
|
||||
-device e1000,netdev=net0 \
|
||||
-device nec-usb-xhci,id=xhci \
|
||||
-device ich9-intel-hda -device hda-duplex \
|
||||
"$@"
|
|
@ -1,4 +0,0 @@
|
|||
#!/bin/sh -x
|
||||
exec redox \
|
||||
-vga qxl -spice port="$(first-free-port 5900)",addr=127.0.0.1,disable-ticketing=on \
|
||||
"$@"
|
|
@ -1,9 +0,0 @@
|
|||
#!/bin/sh -x
|
||||
qemu-system-x86_64 \
|
||||
-enable-kvm \
|
||||
-cpu host -smp "$(("$(nproc)" / 2))" \
|
||||
-m "$(("$(free | grep '^Mem: ' | awk '{ print $NF }')" / 2))K" \
|
||||
-net nic \
|
||||
-device virtio-serial \
|
||||
-usbdevice tablet \
|
||||
"$@"
|
|
@ -1,7 +0,0 @@
|
|||
#!/bin/sh -x
|
||||
exec windows \
|
||||
-vga qxl \
|
||||
-spice port="$(first-free-port 5900)",addr=127.0.0.1,disable-ticketing=on \
|
||||
-chardev spicevmc,id=vdagent,name=vdagent \
|
||||
-device virtserialport,chardev=vdagent,name=com.redhat.spice.0 \
|
||||
"$@"
|
|
@ -1,112 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <sysexits.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MIN_TCP_PORT 1
|
||||
#define TCP_TABLE "/proc/net/tcp"
|
||||
#define TCP_TABLE_LINE_LENGTH 151
|
||||
#define LOCALHOST_HEX "0100007F"
|
||||
#define WILDCARD_HEX "00000000"
|
||||
#define LISTENING_HEX "0A"
|
||||
#define PORTS_BLOCKS_TO_ALLOW 4
|
||||
|
||||
/**
|
||||
* Get all the listening TCP ports
|
||||
*/
|
||||
static unsigned short *get_listening_ports(unsigned short *listening_ports, FILE *tcp_table_fptr) {
|
||||
char line[TCP_TABLE_LINE_LENGTH];
|
||||
char delimiter[] = " ";
|
||||
unsigned short len = 0;
|
||||
char *address;
|
||||
char *state;
|
||||
char *field;
|
||||
size_t allowed_for_ports = PORTS_BLOCKS_TO_ALLOW;
|
||||
|
||||
// Skip first line (header)
|
||||
fgets(line, sizeof(line), tcp_table_fptr);
|
||||
|
||||
// Tokenize lines one by one
|
||||
while (fgets(line, sizeof(line), tcp_table_fptr) != NULL) {
|
||||
strtok(line, delimiter);
|
||||
|
||||
address = strtok(NULL, delimiter);
|
||||
strtok(NULL, delimiter);
|
||||
state = strtok(NULL, delimiter);
|
||||
field = strtok(NULL, delimiter);
|
||||
|
||||
while (field != NULL) {
|
||||
field = strtok(NULL, delimiter);
|
||||
}
|
||||
|
||||
if ((!strncmp(address, LOCALHOST_HEX, 8) || !strncmp(address, WILDCARD_HEX, 8)) && !strncmp(state, LISTENING_HEX, 2)) {
|
||||
if (len == allowed_for_ports) {
|
||||
allowed_for_ports = allowed_for_ports + PORTS_BLOCKS_TO_ALLOW;
|
||||
listening_ports = realloc(listening_ports, allowed_for_ports * sizeof(unsigned short));
|
||||
}
|
||||
listening_ports[len] = strtol(address + strlen(address) - 4, NULL, 16);
|
||||
len++;
|
||||
}
|
||||
}
|
||||
|
||||
if (len == allowed_for_ports) {
|
||||
allowed_for_ports = allowed_for_ports + PORTS_BLOCKS_TO_ALLOW;
|
||||
listening_ports = realloc(listening_ports, allowed_for_ports * sizeof(unsigned short));
|
||||
}
|
||||
listening_ports[len] = 0;
|
||||
|
||||
return listening_ports;
|
||||
}
|
||||
|
||||
static unsigned char is_port_available(const unsigned short port, const unsigned short *listening_ports) {
|
||||
unsigned short i = 0;
|
||||
while (listening_ports[i] != 0 && listening_ports[i] != port) {
|
||||
i++;
|
||||
}
|
||||
return listening_ports[i] == 0;
|
||||
}
|
||||
|
||||
static inline unsigned char is_valid_tcp_port(const unsigned short tcp_port) {
|
||||
return tcp_port >= MIN_TCP_PORT;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
unsigned short current_port;
|
||||
|
||||
if (argc >= 2) {
|
||||
current_port = atoi(argv[1]);
|
||||
} else {
|
||||
current_port = 0;
|
||||
}
|
||||
|
||||
if (!is_valid_tcp_port(current_port)) {
|
||||
fprintf(stderr, "Provide a valid TCP port number as first argument.\n");
|
||||
return EX_USAGE;
|
||||
}
|
||||
|
||||
// Open TCP table
|
||||
FILE *tcp_table_fptr = fopen(TCP_TABLE, "r");
|
||||
if (tcp_table_fptr == NULL) {
|
||||
fprintf(stderr, "Error opening the TCP table.\n");
|
||||
return EX_OSFILE;
|
||||
}
|
||||
|
||||
unsigned short *listening_ports = malloc(PORTS_BLOCKS_TO_ALLOW * sizeof(unsigned short));
|
||||
listening_ports = get_listening_ports(listening_ports, tcp_table_fptr);
|
||||
|
||||
// Check if the current port is available, add
|
||||
while (!is_port_available(current_port, listening_ports)) {
|
||||
current_port++;
|
||||
}
|
||||
|
||||
free(listening_ports);
|
||||
|
||||
if (is_valid_tcp_port(current_port)) {
|
||||
printf("%d\n", current_port);
|
||||
return EXIT_SUCCESS;
|
||||
} else {
|
||||
fprintf(stderr, "No more ports available. How did you fuck up that bad ???\n");
|
||||
return EX_TEMPFAIL;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue