Skip to content

Commit

Permalink
examples/chroot_vm: Add the ability to start passt process
Browse files Browse the repository at this point in the history
This also adds the ability to specify passt socket path, if you do not want to start
passt by chroot_vm. (this is useful to see passt log)

Signed-off-by: Matej Hrica <[email protected]>
  • Loading branch information
mtjhrc authored and slp committed Oct 4, 2023
1 parent c2b6a44 commit 907d107
Showing 1 changed file with 52 additions and 3 deletions.
55 changes: 52 additions & 3 deletions examples/chroot_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ static void print_help(char *const name)
fprintf(stderr,
"Usage: %s [OPTIONS] NEWROOT COMMAND [COMMAND_ARGS...]\n"
"OPTIONS: \n"
" -h --help Show help\n"
" --net=NET_MODE Set network mode\n"
" -h --help Show help\n"
" --net=NET_MODE Set network mode\n"
" --passt-socket=PATH Instead of starting passt, connect to passt socket at PATH"
"NET_MODE can be either TSI (default) or PASST\n"
"\n"
"NEWROOT: the root directory of the vm\n"
Expand All @@ -46,12 +47,14 @@ static void print_help(char *const name)
static const struct option long_options[] = {
{ "help", no_argument, NULL, 'h' },
{ "net_mode", required_argument, NULL, 'N' },
{ "passt-socket", required_argument, NULL, 'P' },
{ NULL, 0, NULL, 0 }
};

struct cmdline {
bool show_help;
enum net_mode net_mode;
char const *passt_socket_path;
char const *new_root;
char *const *guest_argv;
};
Expand All @@ -64,6 +67,7 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
*cmdline = (struct cmdline){
.show_help = false,
.net_mode = NET_MODE_TSI,
.passt_socket_path = NULL,
.new_root = NULL,
.guest_argv = NULL,
};
Expand All @@ -86,6 +90,9 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
return false;
}
break;
case 'P':
cmdline->passt_socket_path = optarg;
break;
case '?':
return false;
default:
Expand Down Expand Up @@ -132,6 +139,47 @@ int connect_to_passt()
return socket_fd;
}

int start_passt()
{
int socket_fds[2];
const int PARENT = 0;
const int CHILD = 1;

if (socketpair(AF_UNIX, SOCK_STREAM, 0, socket_fds) < 0) {
perror("Failed to create passt socket fd");
return -1;
}

int pid = fork();
if (pid < 0) {
perror("fork");
return -1;
}

if (pid == 0) { // child
if (close(socket_fds[PARENT]) < 0) {
perror("close PARENT");
}

char fd_as_str[16];
snprintf(fd_as_str, sizeof(fd_as_str), "%d", socket_fds[CHILD]);

printf("passing fd %s to passt", fd_as_str);

if (execlp("passt", "passt", "-f", "--fd", fd_as_str, NULL) < 0) {
perror("execlp");
return -1;
}

} else { // parent
if (close(socket_fds[CHILD]) < 0) {
perror("close CHILD");
}

return socket_fds[PARENT];
}
}


int main(int argc, char *const argv[])
{
Expand Down Expand Up @@ -233,7 +281,8 @@ int main(int argc, char *const argv[])
return -1;
}
} else {
int passt_fd = connect_to_passt();
int passt_fd = cmdline.passt_socket_path ? connect_to_passt(cmdline.passt_socket_path) : start_passt();

if (passt_fd < 0) {
return -1;
}
Expand Down

0 comments on commit 907d107

Please sign in to comment.