Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access guest console when it is running #48

Open
praveenkumar opened this issue Jun 28, 2023 · 11 comments · May be fixed by #113
Open

Access guest console when it is running #48

praveenkumar opened this issue Jun 28, 2023 · 11 comments · May be fixed by #113
Assignees

Comments

@praveenkumar
Copy link
Member

As of now we

vfkit/pkg/vf/virtio.go

Lines 272 to 276 in c9a4b08

if dev.UsesStdio {
if err := setRawMode(os.Stdin); err != nil {
return err
}
serialPortAttachment, err = vz.NewFileHandleSerialPortAttachment(os.Stdin, os.Stdout)
connects the VM serial output to the stdin/stdout of the terminal where vfkit is running but there is no way to connect to it so that we can access VM console even in case of no ssh connectivity.

https://developer.apple.com/documentation/virtualization/vzfilehandleserialportattachment?language=objc => this api allows bidirectional communication using file handles but we don't know how to consume it.

This is important for crc side to debug some of the issues when ssh connection is broken.

@cfergeau
Copy link
Collaborator

@cfergeau
Copy link
Collaborator

Seems to be relatively easy to do https://github.com/cfergeau/vfkit/tree/pty
This is a hack which changes --device virtio-serial,stdio to output to a pseudoterminal instead of stdio.
Need to add cmdline parsing/... to this proof of concept.

@cdrage
Copy link

cdrage commented Jan 24, 2024

Testing with https://github.com/cfergeau/vfkit/tree/pty I made some awesome progress on integrating some of this within PD!

I did encounter some issues however:

  • Running the same command twice on a different image, I would get a different ttys which is to be expected.. but I couldn't access it / read / write.

Start VM 1

./vfkit-arm64 --cpus 2 --memory 2048 \
    --bootloader efi,variable-store=./efi-variable-store,create \
    --device virtio-blk,path=/Users/cdrage/bootc/image/disk.raw \
    --device virtio-serial,pty \
    --device virtio-net,nat,mac=72:20:43:d4:38:62 \
    --device virtio-rng \
    --device virtio-input,keyboard \
    --device virtio-input,pointing

Start VM 2

./vfkit-arm64 --cpus 2 --memory 2048 \
   --bootloader efi,variable-store=./efi-variable-store,create \
   --device virtio-blk,path=/Users/cdrage/bootc/image/disk2.raw \
   --device virtio-serial,pty \
   --device virtio-net,nat,mac=72:20:43:d4:38:62 \
   --device virtio-rng \
   --device virtio-input,keyboard \
   --device virtio-input,pointing

You should get two ttys.

One is /dev/ttys003

The other would be random (mine was /dev/ttys010).

If you do screen /dev/ttys003 there is no issue.

If you do screen /dev/ttys010 you get a permission error.

Video is below!

Screen.Recording.2024-01-24.at.10.02.31.AM.mov
  • It would be nice to specify exactly which path the ttys will go (be able to pick /dev/ttys123, etc.)

  • Unable to see boot screen / GRUB, only was able to see the login prompt.

@cfergeau
Copy link
Collaborator

You can remove

    --device virtio-input,keyboard \
    --device virtio-input,pointing

from your commandline, they are only needed if you use the --gui flag.

@cdrage
Copy link

cdrage commented Jan 25, 2024

You can remove

    --device virtio-input,keyboard \
    --device virtio-input,pointing

from your commandline, they are only needed if you use the --gui flag.

Thanks! I shouldn't of added those inputs haha.

I did try it again without those inputs and I can now "load" the screen, but I get a different error. It's now completely blank / wont load anything.

Screen.Recording.2024-01-25.at.9.21.30.AM.mov

@cfergeau
Copy link
Collaborator

* It would be nice to specify exactly which path the ttys will go (be able to pick /dev/ttys123, etc.)

This is one issue with the current branch, there is no nice way of providing the pty name to vfkit users. It's printed in the logs and that's it. I'm not sure it's possible to choose which tty will be allocated, I'll need to look more closely.

hyperkit allows to specify a file when using the autopty option, but this is the path to a file where the pty name will be written https://github.com/moby/hyperkit/blob/3cb0d5475244761c61e6fd4a562702c26b46f846/src/lib/uart_emul.c#L767-L779 (ie autopty=/tmp/foo will write /dev/ttyS123 to /tmp/foo if this is the pty which was allocated).

I hope to be able to give this info through vfkit REST API, calling /inspect would return a json string which would have the pty name used among a lot of other info.

@cfergeau
Copy link
Collaborator

cfergeau commented Mar 14, 2024

I'm not sure it's possible to choose which tty will be allocated, I'll need to look more closely.

The low-level kernel/libc API is definitely of the form "allocate a pty for me, and give me its name", it does not seem possibe to control this and choose a name. However https://github.com/cfergeau/vfkit/tree/pty now exposes the pty name in the REST API

% curl --unix-socket ~/dev/vfkit/rest.sock "http://vfkit/vm/inspect" |gojq 
{
[snip]
  "devices": [
    {
      "devName": "virtio-blk",
      "imagePath": "overlay.img",
      "kind": "virtioblk"
    },
    {
      "kind": "virtioserial",
      "ptyName": "/dev/ttys005",
      "usesPty": true
    },
    {
      "kind": "virtionet",
      "macAddress": "72:20:43:d4:38:62",
      "nat": true
    },
    {
      "kind": "virtiorng"
    }
  ],
  "memoryBytes": 2147483648,
  "vcpus": 2
}

@cfergeau
Copy link
Collaborator

Running the same command twice on a different image, I would get a different ttys which is to be expected.. but I couldn't access it / read / write.

I have been investigating a bunch of similar issues, sometimes even after killing/starting again the same VM, I can't connect. My current suspicion is that it's related to my non-handling of Close() on the PTY sockets. They are leaked, and go closes them as it sees fit when garbage collection happens. If I assign them to global variables so that they are not garbage collected, behaviour is more predictible so far (I can kill/start/kill/start with no problems, I can run 2 VMs side by side and connect to both, ..)

cfergeau added a commit to cfergeau/vfkit that referenced this issue Mar 15, 2024
This new virtio-serial option allocates a pseudo-tty for the VM console.
It can then be accessed using `screen` for example.
This is a bit similar to the `--device virtio-serial,stdio` option,
except that the console is not tied to the terminal running vfkit, it's
possible to connect/disconnect from the pseudo-tty from any terminal.

This fixes crc-org#48

Signed-off-by: Christophe Fergeau <cfergeau@redhat.com>
@cfergeau
Copy link
Collaborator

Conclusion of the investigations is that it was all related, closing the master file descriptor too early will remove the ttysxxx device which was allocated, and will reset its permissions to root.root.
https://jalopezg.dev/2023/11/14/UNIX98-pts/
The latest iterations of this PR keep the master file descriptor open as long as vfkit runs.

@cfergeau cfergeau linked a pull request Mar 15, 2024 that will close this issue
@gbraad
Copy link

gbraad commented Mar 15, 2024

Are you waiting for peer-review/confirmation? If so, from whom?

@cfergeau
Copy link
Collaborator

Are you waiting for peer-review/confirmation? If so, from whom?

The associated PR will need a review #113

cfergeau added a commit to cfergeau/vfkit that referenced this issue Mar 27, 2024
This new virtio-serial option allocates a pseudo-tty for the VM console.
It can then be accessed using `screen` for example.
This is a bit similar to the `--device virtio-serial,stdio` option,
except that the console is not tied to the terminal running vfkit, it's
possible to connect/disconnect from the pseudo-tty from any terminal.

This fixes crc-org#48

Signed-off-by: Christophe Fergeau <cfergeau@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Ready for review
Development

Successfully merging a pull request may close this issue.

4 participants