mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-24 19:28:09 +00:00
Merge pull request #144 from djs55/9pmount-vsock-tool
9pmount vsock tool
This commit is contained in:
commit
4482790f48
@ -72,6 +72,7 @@ COPY packages/vsudd/etc /etc
|
||||
COPY packages/mobyconfig/mobyconfig /usr/bin
|
||||
COPY packages/gummiboot/gummiboot.tar.gz /usr/share/src/
|
||||
COPY packages/oom/etc /etc
|
||||
COPY packages/9pmount-vsock/9pmount-vsock /sbin
|
||||
|
||||
RUN \
|
||||
rc-update add swap boot && \
|
||||
|
1
alpine/packages/9pmount-vsock/.gitignore
vendored
Normal file
1
alpine/packages/9pmount-vsock/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
9pmount-vsock
|
229
alpine/packages/9pmount-vsock/9pmount-vsock.c
Normal file
229
alpine/packages/9pmount-vsock/9pmount-vsock.c
Normal file
@ -0,0 +1,229 @@
|
||||
/*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <getopt.h>
|
||||
#include <syslog.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <err.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include "hvsock.h"
|
||||
|
||||
#define NONE 0
|
||||
#define LISTEN 1
|
||||
#define CONNECT 2
|
||||
|
||||
int mode = NONE;
|
||||
|
||||
char *default_sid = "C378280D-DA14-42C8-A24E-0DE92A1028E2";
|
||||
char *mount = "/bin/mount";
|
||||
|
||||
void fatal(const char *msg)
|
||||
{
|
||||
syslog(LOG_CRIT, "%s Error: %d. %s", msg, errno, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static int handle(int fd, char *tag, char *path)
|
||||
{
|
||||
char *options = NULL;
|
||||
if (asprintf(&options, "trans=fd,dfltuid=1001,dfltgid=50,version=9p2000,rfdno=%d,wfdno=%d", fd, fd) < 0){
|
||||
fatal("asprintf()");
|
||||
}
|
||||
char *argv[] = {
|
||||
mount,
|
||||
"-t", "9p", "-o", options,
|
||||
tag, path,
|
||||
NULL
|
||||
};
|
||||
pid_t pid = fork();
|
||||
if (pid == 0) {
|
||||
execv(mount, argv);
|
||||
fatal("execv()");
|
||||
}
|
||||
int status;
|
||||
if (waitpid(pid, &status, 0) == -1) {
|
||||
syslog(LOG_CRIT, "waitpid failed: %d. %s", errno, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
return WEXITSTATUS(status);
|
||||
}
|
||||
|
||||
static int create_listening_socket(GUID serviceid) {
|
||||
int lsock = -1;
|
||||
SOCKADDR_HV sa;
|
||||
int res;
|
||||
|
||||
lsock = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW);
|
||||
if (lsock == -1) {
|
||||
fatal("socket()");
|
||||
}
|
||||
|
||||
sa.Family = AF_HYPERV;
|
||||
sa.Reserved = 0;
|
||||
sa.VmId = HV_GUID_WILDCARD;
|
||||
sa.ServiceId = serviceid;
|
||||
|
||||
res = bind(lsock, (const struct sockaddr *)&sa, sizeof(sa));
|
||||
if (res == -1) {
|
||||
fatal("bind()");
|
||||
}
|
||||
|
||||
res = listen(lsock, 1);
|
||||
if (res == -1) {
|
||||
fatal("listen()");
|
||||
}
|
||||
return lsock;
|
||||
}
|
||||
|
||||
static int connect_socket(GUID serviceid) {
|
||||
int sock = -1;
|
||||
SOCKADDR_HV sa;
|
||||
int res;
|
||||
|
||||
sock = socket(AF_HYPERV, SOCK_STREAM, HV_PROTOCOL_RAW);
|
||||
if (sock == -1) {
|
||||
fatal("socket()");
|
||||
}
|
||||
|
||||
sa.Family = AF_HYPERV;
|
||||
sa.Reserved = 0;
|
||||
sa.VmId = HV_GUID_PARENT;
|
||||
sa.ServiceId = serviceid;
|
||||
|
||||
res = connect(sock, (const struct sockaddr *)&sa, sizeof(sa));
|
||||
if (res == -1) {
|
||||
fatal("connect()");
|
||||
}
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
static int accept_socket(int lsock) {
|
||||
int csock = -1;
|
||||
SOCKADDR_HV sac;
|
||||
socklen_t socklen = sizeof(sac);
|
||||
|
||||
csock = accept(lsock, (struct sockaddr *)&sac, &socklen);
|
||||
if (csock == -1) {
|
||||
fatal("accept()");
|
||||
}
|
||||
|
||||
syslog(LOG_INFO, "Connect from: "GUID_FMT":"GUID_FMT"\n",
|
||||
GUID_ARGS(sac.VmId), GUID_ARGS(sac.ServiceId));
|
||||
return csock;
|
||||
}
|
||||
|
||||
void usage(char *name)
|
||||
{
|
||||
printf("%s: mount a 9P filesystem from an hvsock connection\n", name);
|
||||
printf("usage:\n");
|
||||
printf("\t[--serviceid <guid>] <listen | connect> <tag> <path>\n");
|
||||
printf("where\n");
|
||||
printf("\t--serviceid <guid>: use <guid> as the well-known service GUID\n");
|
||||
printf("\t (defaults to %s)\n", default_sid);
|
||||
printf("\t--listen: listen forever for incoming AF_HVSOCK connections\n");
|
||||
printf("\t--connect: connect to the parent partition\n");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int res = 0;
|
||||
GUID sid;
|
||||
int c;
|
||||
/* Defaults to a testing GUID */
|
||||
char *serviceid = default_sid;
|
||||
char *tag = NULL;
|
||||
char *path = NULL;
|
||||
|
||||
opterr = 0;
|
||||
while (1) {
|
||||
static struct option long_options[] = {
|
||||
/* These options set a flag. */
|
||||
{"serviceid", required_argument, NULL, 's'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
int option_index = 0;
|
||||
|
||||
c = getopt_long (argc, argv, "s:", long_options, &option_index);
|
||||
if (c == -1) break;
|
||||
|
||||
switch (c) {
|
||||
|
||||
case 's':
|
||||
serviceid = optarg;
|
||||
break;
|
||||
case 0:
|
||||
break;
|
||||
default:
|
||||
usage (argv[0]);
|
||||
exit (1);
|
||||
}
|
||||
}
|
||||
if (optind < argc) {
|
||||
if (strcmp(argv[optind], "listen") == 0) {
|
||||
mode = LISTEN;
|
||||
} else if (strcmp(argv[optind], "connect") == 0) {
|
||||
mode = CONNECT;
|
||||
}
|
||||
optind++;
|
||||
}
|
||||
if (mode == NONE) {
|
||||
fprintf(stderr, "Please supply either listen or connect\n");
|
||||
usage(argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
if (optind < argc) {
|
||||
tag = argv[optind++];
|
||||
}
|
||||
if (optind < argc) {
|
||||
path = argv[optind++];
|
||||
}
|
||||
if (!tag) {
|
||||
fprintf(stderr, "Please supply a tag name\n");
|
||||
usage(argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
if (!path) {
|
||||
fprintf(stderr, "Please supply a path\n");
|
||||
usage(argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
res = parseguid(serviceid, &sid);
|
||||
if (res) {
|
||||
fprintf(stderr, "Failed to parse serviceid as GUID: %s\n", serviceid);
|
||||
usage(argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
openlog(argv[0], LOG_CONS | LOG_NDELAY | LOG_PERROR, LOG_DAEMON);
|
||||
for (;;) {
|
||||
int sock = -1;
|
||||
if (mode == LISTEN) {
|
||||
syslog(LOG_INFO, "starting in listening mode with serviceid=%s, tag=%s, path=%s", serviceid, tag, path);
|
||||
int lsocket = create_listening_socket(sid);
|
||||
sock = accept_socket(lsocket);
|
||||
close(lsocket);
|
||||
} else {
|
||||
syslog(LOG_INFO, "starting in connect mode with serviceid=%s, tag=%s, path=%s", serviceid, tag, path);
|
||||
sock = connect_socket(sid);
|
||||
}
|
||||
int r = handle(sock, tag, path);
|
||||
close(sock);
|
||||
if (r == 0) {
|
||||
syslog(LOG_INFO, "mount successful for serviceid=%s tag=%s path=%s", serviceid, tag, path);
|
||||
exit(0);
|
||||
}
|
||||
/* This can happen if the client times out the connection after we accept it */
|
||||
syslog(LOG_CRIT, "mount failed with %d for serviceid=%s tag=%s path=%s", r, serviceid, tag, path);
|
||||
sleep(1); /* retry */
|
||||
}
|
||||
}
|
10
alpine/packages/9pmount-vsock/Dockerfile
Normal file
10
alpine/packages/9pmount-vsock/Dockerfile
Normal file
@ -0,0 +1,10 @@
|
||||
FROM alpine:3.3
|
||||
|
||||
RUN apk update && apk upgrade && apk add alpine-sdk util-linux-dev linux-headers
|
||||
|
||||
RUN mkdir -p /9pmount-vsock
|
||||
WORKDIR /9pmount-vsock
|
||||
|
||||
COPY . /9pmount-vsock
|
||||
|
||||
RUN make 9pmount-vsock
|
21
alpine/packages/9pmount-vsock/Makefile
Normal file
21
alpine/packages/9pmount-vsock/Makefile
Normal file
@ -0,0 +1,21 @@
|
||||
.PHONY: all
|
||||
|
||||
DEPS=9pmount-vsock.c hvsock.c hvsock.h
|
||||
|
||||
all: Dockerfile $(DEPS)
|
||||
docker build -t 9pmount-vsock:build .
|
||||
docker run --rm 9pmount-vsock:build cat 9pmount-vsock > 9pmount-vsock
|
||||
chmod 755 9pmount-vsock
|
||||
|
||||
9pmount-vsock: 9pmount-vsock.o hvsock.o
|
||||
gcc -Wall -Werror -o 9pmount-vsock 9pmount-vsock.o hvsock.o -lpthread
|
||||
|
||||
9pmount-vsock.o: 9pmount-vsock.c hvsock.h
|
||||
gcc -Wall -Werror -c 9pmount-vsock.c
|
||||
|
||||
hvsock.o: hvsock.c hvsock.h
|
||||
gcc -Wall -Werror -c hvsock.c
|
||||
|
||||
clean:
|
||||
rm -f 9pmount-vsock
|
||||
docker images -q 9pmount-vsock:build | xargs docker rmi -f
|
38
alpine/packages/9pmount-vsock/hvsock.c
Normal file
38
alpine/packages/9pmount-vsock/hvsock.c
Normal file
@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "hvsock.h"
|
||||
|
||||
int parseguid(const char *s, GUID *g)
|
||||
{
|
||||
int res;
|
||||
int p0, p1, p2, p3, p4, p5, p6, p7;
|
||||
|
||||
res = sscanf(s, GUID_FMT,
|
||||
&g->Data1, &g->Data2, &g->Data3,
|
||||
&p0, &p1, &p2, &p3, &p4, &p5, &p6, &p7);
|
||||
if (res != 11)
|
||||
return 1;
|
||||
g->Data4[0] = p0;
|
||||
g->Data4[1] = p1;
|
||||
g->Data4[2] = p2;
|
||||
g->Data4[3] = p3;
|
||||
g->Data4[4] = p4;
|
||||
g->Data4[5] = p5;
|
||||
g->Data4[6] = p6;
|
||||
g->Data4[7] = p7;
|
||||
return 0;
|
||||
}
|
||||
|
||||
DEFINE_GUID(HV_GUID_ZERO,
|
||||
0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
|
||||
DEFINE_GUID(HV_GUID_BROADCAST,
|
||||
0xFFFFFFFF, 0xFFFF, 0xFFFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
|
||||
DEFINE_GUID(HV_GUID_WILDCARD,
|
||||
0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
|
||||
|
||||
DEFINE_GUID(HV_GUID_CHILDREN,
|
||||
0x90db8b89, 0x0d35, 0x4f79, 0x8c, 0xe9, 0x49, 0xea, 0x0a, 0xc8, 0xb7, 0xcd);
|
||||
DEFINE_GUID(HV_GUID_LOOPBACK,
|
||||
0xe0e16197, 0xdd56, 0x4a10, 0x91, 0x95, 0x5e, 0xe7, 0xa1, 0x55, 0xa8, 0x38);
|
||||
DEFINE_GUID(HV_GUID_PARENT,
|
||||
0xa42e7cda, 0xd03f, 0x480c, 0x9c, 0xc2, 0xa4, 0xde, 0x20, 0xab, 0xb8, 0x78);
|
49
alpine/packages/9pmount-vsock/hvsock.h
Normal file
49
alpine/packages/9pmount-vsock/hvsock.h
Normal file
@ -0,0 +1,49 @@
|
||||
/* AF_HYPERV definitions and utilities */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
/* GUID handling */
|
||||
typedef struct _GUID {
|
||||
uint32_t Data1;
|
||||
uint16_t Data2;
|
||||
uint16_t Data3;
|
||||
uint8_t Data4[8];
|
||||
} GUID;
|
||||
|
||||
#define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
|
||||
const GUID name = {l, w1, w2, {b1, b2, b3, b4, b5, b6, b7, b8}}
|
||||
|
||||
/* Helper macros for parsing/printing GUIDs */
|
||||
#define GUID_FMT "%08x-%04hx-%04hx-%02x%02x-%02x%02x%02x%02x%02x%02x"
|
||||
#define GUID_ARGS(_g) \
|
||||
(_g).Data1, (_g).Data2, (_g).Data3, \
|
||||
(_g).Data4[0], (_g).Data4[1], (_g).Data4[2], (_g).Data4[3], \
|
||||
(_g).Data4[4], (_g).Data4[5], (_g).Data4[6], (_g).Data4[7]
|
||||
#define GUID_SARGS(_g) \
|
||||
&(_g).Data1, &(_g).Data2, &(_g).Data3, \
|
||||
&(_g).Data4[0], &(_g).Data4[1], &(_g).Data4[2], &(_g).Data4[3], \
|
||||
&(_g).Data4[4], &(_g).Data4[5], &(_g).Data4[6], &(_g).Data4[7]
|
||||
|
||||
extern int parseguid(const char *s, GUID *g);
|
||||
|
||||
/* HV Socket definitions */
|
||||
#define AF_HYPERV 43
|
||||
#define HV_PROTOCOL_RAW 1
|
||||
|
||||
typedef struct _SOCKADDR_HV
|
||||
{
|
||||
unsigned short Family;
|
||||
unsigned short Reserved;
|
||||
GUID VmId;
|
||||
GUID ServiceId;
|
||||
} SOCKADDR_HV;
|
||||
|
||||
extern const GUID HV_GUID_ZERO;
|
||||
extern const GUID HV_GUID_BROADCAST;
|
||||
extern const GUID HV_GUID_WILDCARD;
|
||||
extern const GUID HV_GUID_CHILDREN;
|
||||
extern const GUID HV_GUID_LOOPBACK;
|
||||
extern const GUID HV_GUID_PARENT;
|
@ -9,6 +9,7 @@ all:
|
||||
$(MAKE) -C vsudd OS=linux
|
||||
$(MAKE) -C llmnrd OS=linux
|
||||
$(MAKE) -C gummiboot OS=linux
|
||||
$(MAKE) -C 9pmount-vsock OS=linux
|
||||
|
||||
arm:
|
||||
$(MAKE) -C transfused OS=linux ARCH=arm
|
||||
@ -31,3 +32,4 @@ clean:
|
||||
$(MAKE) -C vsudd clean
|
||||
$(MAKE) -C llmnrd clean
|
||||
$(MAKE) -C gummiboot clean
|
||||
$(MAKE) -C 9pmount-vsock clean
|
||||
|
Loading…
Reference in New Issue
Block a user