Just like vmctl(8), this implements the little convenience for ldomctl:

        $ doas ./obj/ldomctl start -c guest4    
        Connected to /dev/ttyV3 (speed 9600)
        ...

To avoid duplicate code, I moved the now common exec routine into
console_exec() which is used by guest_console() and guest_start().

Feedback? OK?


Index: ldomctl.8
===================================================================
RCS file: /cvs/src/usr.sbin/ldomctl/ldomctl.8,v
retrieving revision 1.24
diff -u -p -r1.24 ldomctl.8
--- ldomctl.8   9 Jan 2020 21:30:18 -0000       1.24
+++ ldomctl.8   16 Jan 2020 13:02:43 -0000
@@ -94,8 +94,12 @@ the default behaviour is to enter
 .It Cm select Ar configuration
 Select the next logical domain configuration to use
 (after resetting the machine).
-.It Cm start Ar domain
+.It Cm start Oo Fl c Oc Ar domain
 Start a guest domain.
+.Bl -tag -width Fl
+.It Fl c
+Automatically connect to the guest console.
+.El
 .It Cm status Op Ar domain
 Display status information for
 .Ar domain ,
Index: ldomctl.c
===================================================================
RCS file: /cvs/src/usr.sbin/ldomctl/ldomctl.c,v
retrieving revision 1.34
diff -u -p -r1.34 ldomctl.c
--- ldomctl.c   4 Jan 2020 17:30:41 -0000       1.34
+++ ldomctl.c   16 Jan 2020 13:20:39 -0000
@@ -131,7 +131,9 @@ usage(void)
            "\t%1$s dump|list|list-io\n"
            "\t%1$s init-system [-n] file\n"
            "\t%1$s create-vdisk -s size file\n"
-           "\t%1$s console|panic|start|status|stop [domain]\n", getprogname());
+           "\t%1$s console|panic|start|status|stop [-c] [domain]\n",
+           getprogname());
+
        exit(EXIT_FAILURE);
 }
 
@@ -399,23 +401,61 @@ download(int argc, char **argv)
 }
 
 void
+console_exec(uint64_t gid)
+{
+       struct guest *guest;
+       char console_str[8];
+
+       if (gid == 0)
+               errx(1, "no console for primary domain");
+
+       TAILQ_FOREACH(guest, &guest_list, link) {
+               if (guest->gid != gid)
+                       continue;
+               snprintf(console_str, sizeof(console_str),
+                   "ttyV%llu", guest->gid - 1);
+
+               closefrom(STDERR_FILENO + 1);
+               execl(LDOMCTL_CU, LDOMCTL_CU, "-r", "-l", console_str,
+                   (char *)NULL);
+               err(1, "failed to open console");
+       }
+}
+
+void
 guest_start(int argc, char **argv)
 {
        struct hvctl_msg msg;
        ssize_t nbytes;
+       uint64_t gid;
+       int ch, console = 0;
 
-       if (argc != 2)
+       while ((ch = getopt(argc, argv, "c")) != -1) {
+               switch (ch) {
+               case 'c':
+                       console = 1;
+                       break;
+               default:
+                       usage();
+               }
+       }
+       argc -= optind;
+       argv += optind;
+
+       if (argc != 1)
                usage();
 
        hv_config();
 
+       gid = find_guest(argv[0]);
+
        /*
         * Start guest domain.
         */
        bzero(&msg, sizeof(msg));
        msg.hdr.op = HVCTL_OP_GUEST_START;
        msg.hdr.seq = hvctl_seq++;
-       msg.msg.guestop.guestid = find_guest(argv[1]);
+       msg.msg.guestop.guestid = gid;
        nbytes = write(hvctl_fd, &msg, sizeof(msg));
        if (nbytes != sizeof(msg))
                err(1, "write");
@@ -424,6 +464,9 @@ guest_start(int argc, char **argv)
        nbytes = read(hvctl_fd, &msg, sizeof(msg));
        if (nbytes != sizeof(msg))
                err(1, "read");
+
+       if (console)
+               console_exec(gid);
 }
 
 void
@@ -615,9 +658,7 @@ guest_status(int argc, char **argv)
 void
 guest_console(int argc, char **argv)
 {
-       struct guest *guest;
        uint64_t gid;
-       char console_str[8];
 
        if (argc != 2)
                usage();
@@ -625,20 +666,8 @@ guest_console(int argc, char **argv)
        hv_config();
 
        gid = find_guest(argv[1]);
-       if (gid == 0)
-               errx(1, "no console for primary domain");
 
-       TAILQ_FOREACH(guest, &guest_list, link) {
-               if (guest->gid != gid)
-                       continue;
-               snprintf(console_str, sizeof(console_str),
-                   "ttyV%llu", guest->gid - 1);
-
-               closefrom(STDERR_FILENO + 1);
-               execl(LDOMCTL_CU, LDOMCTL_CU, "-r", "-l", console_str,
-                   (char *)NULL);
-               err(1, "failed to open console");
-       }
+       console_exec(gid);
 }
 
 void

Reply via email to