Compare commits

...

8 commits

6 changed files with 62 additions and 178 deletions

View file

@ -1,65 +1,41 @@
# SRC = source
# DST = destination
# *D = folder containing *
NAME := qemush
# qemu Unix user
QEMU_USER := qemu
# sudo as qemu
SUDO_QEMU := sudo -u $(QEMU_USER)
QEMUSH := /usr/local/bin/$(NAME)
SRC_QEMUSH := bin/$(NAME)
SRCD := src
SRC_BIN := bin/$(NAME)
DST_BIN := /usr/local/$(SRC_BIN)
# This repo's equivalent of effective qemu Unix user home
SRC_QEMU_HOME := qemu
SRC_QEMU_BIN := $(SRC_QEMU_HOME)/bin
SRC_LAUNCHERSD := $(SRC_QEMU_HOME)/launchers
# Actual qemu Unix user home
QEMU_HOME := $(shell echo ~$(QEMU_USER))
QEMU_BIN := $(QEMU_HOME)/bin
LAUNCHERSD := $(QEMU_HOME)/launchers
SRC_HOME := $(QEMU_USER)
DST_HOME := $(shell echo ~$(QEMU_USER))
# Names of all qemush modules
SRC_MODULES_NAMES := $(notdir $(wildcard $(SRC_QEMU_BIN)/*))
# Target location of modules
MODULES := $(addprefix $(QEMU_BIN)/,$(SRC_MODULES_NAMES))
# Names of launchers scripts
SRC_LAUNCHERS_NAMES := $(notdir $(wildcard $(SRC_LAUNCHERSD)/*))
# Target location of launchers
LAUNCHERS := $(addprefix $(LAUNCHERSD)/,$(SRC_LAUNCHERS_NAMES))
# Mode to apply
BINS_MODE := 740
SRC_BIND := $(SRC_HOME)/bin
SRC_LAUNCHERSD := $(SRC_HOME)/launchers
DST_BIND := $(DST_HOME)/bin
DST_LAUNCHERSD := $(DST_HOME)/launchers
# Compiler options
CC := cc
CC_OPTIONS = -O2
# C sources file format
SRC_FMT := .c
# Names of C programs to compile
SRC_C_SOURCES_NAMES := $(notdir $(basename $(wildcard $(SRCD)/*$(SRC_FMT))))
# Location of C binaries
C_BINARIES := $(addprefix $(QEMU_BIN)/,$(SRC_C_SOURCES_NAMES))
MODULES_NAMES := $(notdir $(wildcard $(SRC_BIND)/*))
LAUNCHERS_NAMES := $(notdir $(wildcard $(SRC_LAUNCHERSD)/*))
# Directories in ~qemu necessary for qemush to work
QEMUSH_DIRS_NAMES := bin launchers disks sockets/monitors sockets/spice
QEMUSH_DIRS := $(addprefix $(QEMU_HOME)/,$(QEMUSH_DIRS_NAMES))
DST_MODULES := $(addprefix $(DST_BIND)/,$(MODULES_NAMES))
DST_LAUNCHERS := $(addprefix $(DST_LAUNCHERSD)/,$(LAUNCHERS_NAMES))
QEMUSH_DIRS := $(addprefix $(DST_HOME)/,bin launchers disks sockets/monitors sockets/spice)
.PHONY: install
install: $(QEMUSH_DIRS) $(MODULES) $(C_BINARIES) $(QEMUSH) $(LAUNCHERS)
install: $(QEMUSH_DIRS) $(DST_MODULES) $(DST_BIN) $(DST_LAUNCHERS)
$(QEMUSH_DIRS):
$(SUDO_QEMU) mkdir -p $@
$(SUDO_QEMU) chmod 750 $@
sudo -u $(QEMU_USER) mkdir -m 750 -p -- $@
$(QEMUSH): $(SRC_QEMUSH)
sudo install -m 755 $^ $@
$(DST_BIN): $(SRC_BIN)
sudo install -m 755 -- $^ $@
$(MODULES): $(QEMU_HOME)%: $(SRC_QEMU_HOME)%
$(SUDO_QEMU) install -m $(BINS_MODE) $^ $@
$(DST_MODULES): $(DST_HOME)%: $(SRC_HOME)%
sudo -u $(QEMU_USER) install -m 740 -- $^ $@
$(C_BINARIES): $(QEMU_BIN)%: $(SRCD)%$(SRC_FMT)
$(SUDO_QEMU) $(CC) $(CC_OPTIONS) -o $@ $^
$(SUDO_QEMU) chmod $(BINS_MODE) $@
$(LAUNCHERS): $(LAUNCHERSD)%: $(SRC_LAUNCHERSD)%
$(SUDO_QEMU) install -m $(BINS_MODE) $^ $@
$(DST_LAUNCHERS): $(DST_LAUNCHERSD)%: $(SRC_LAUNCHERSD)%
sudo -u $(QEMU_USER) install -m 740 -- $^ $@

View file

@ -38,11 +38,9 @@ From version 0.9.0, `qemush` is written in pure POSIX shell! The previous depend
All dependencies are common packages for a distribution, you'll be able to grab them from your favorite packages sources.
- `qemu` - this is literally a QEMU wrapper so there's a chance you'll need it
- A POSIX compliant shell - `bash` (POSIX mode), `*ash`, `*ksh` are POSIX shells
- `coreutils` - used for basic OS operations
- `sudo` - execute commands as `qemu`
- `socat` - monitor machines via Unix sockets
- any text editor - used for builtin function to edit launching scripts
## Installation instructions

View file

@ -1,6 +1,6 @@
#!/usr/bin/env sh
# version=0.11.0
# version=1.0.0
# Re-exec the script as qemu via sudo (only if needed)
[ "$(whoami)" != qemu ] && exec sudo -E -H -u qemu -- "$0" "$@"

34
qemu/bin/first-free-port Executable file
View file

@ -0,0 +1,34 @@
#!/usr/bin/env sh
#set -x
name=$(basename "$0")
used_tcp_ports=$(sed 1d /proc/net/tcp \
| awk '($4 == "0A") { print $2 }' \
| awk -F : '($1 == "00000000" || $1 == "0100007F") { print $2 }' \
| perl -e 'foreach my $line (<>) { print hex($line) . "\n"; }'
)
perror() {
>&2 printf "%s: %s\n" "$name" "$*"
}
port_is_free() {
for used_port in $used_tcp_ports; do
[ "$port" = "$used_port" ] && return 1
done
:
}
port=$1
if ! { [ "$port" -gt 0 ] && [ "$port" -lt 65536 ]; } 2> /dev/null; then
perror "you must specify a valid TCP port"
exit 1
fi
while ! port_is_free; do
port=$((port + 1))
done
printf %s\\n "$port"

View file

@ -13,7 +13,7 @@ esac
[ -z "$QEMUSH_NPROC" ] && QEMUSH_NPROC=$(($(nproc) / 2))
# How much RAM to use
[ -z "$QEMUSH_RAM" ] && QEMUSH_RAM=$(($(free | grep '^Mem:\s' | awk '{ print $NF }') / 2))K
[ -z "$QEMUSH_RAM" ] && QEMUSH_RAM=$(($(grep '^MemAvailable:\s' /proc/meminfo | awk '{ print $2 }') / 2))K
# Use selected QEMUSH_MACHINE type if set
[ -n "$QEMUSH_MACHINE" ] && set -- -M "$QEMUSH_MACHINE" "$@"

View file

@ -1,124 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sysexits.h>
#include <string.h>
#include <errno.h>
#define MIN_TCP_PORT 1
#define TCP_TABLE_PATH "/proc/net/tcp"
#define TCP_TABLE_LINE_LENGTH 151
#define LOCALHOSTIP_HEX "0100007F"
#define WILDCARDIP_HEX "00000000"
#define LISTENING_HEX "0A"
#define BLOCKS_TO_ALLOC 4
/**
* Print a nice error message
*/
static void print_error(char *name, const char *message) {
fprintf(stderr, "%s: %s\n", name, message);
}
/**
* Get all the listening TCP ports
*/
static unsigned short *get_listening_ports(FILE *tcp_table_fptr) {
unsigned short *listening_ports = malloc(BLOCKS_TO_ALLOC * sizeof(short));
char line[TCP_TABLE_LINE_LENGTH];
char delimiter[] = " ";
unsigned short len = 0;
char *address;
char *state;
char *field;
size_t blocks_allocated = BLOCKS_TO_ALLOC;
// 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, LOCALHOSTIP_HEX, 8) || !strncmp(address, WILDCARDIP_HEX, 8)) && !strncmp(state, LISTENING_HEX, 2)) {
if (len == blocks_allocated) {
blocks_allocated = blocks_allocated + BLOCKS_TO_ALLOC;
listening_ports = realloc(listening_ports, blocks_allocated * sizeof(short));
}
listening_ports[len] = strtol(address + strlen(address) - 4, NULL, 16);
len++;
}
}
if (len == blocks_allocated) {
listening_ports = realloc(listening_ports, blocks_allocated + 1);
}
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)) {
print_error(argv[0], "provide a valid TCP port number as first argument.");
return EX_USAGE;
}
// Open TCP table
FILE *tcp_table_fptr = fopen(TCP_TABLE_PATH, "r");
if (tcp_table_fptr == NULL) {
print_error(argv[0], "error opening the TCP table.");
return errno;
}
unsigned short *listening_ports = get_listening_ports(tcp_table_fptr);
if (fclose(tcp_table_fptr) != 0) {
print_error(argv[0], "can't close the TCP table.");
return errno;
}
// 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 {
print_error(argv[0], "no more ports available; how did you fuck up that bad ???");
return EX_TEMPFAIL;
}
}