-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vm: add tools and docs for convenient ssh integration
- Loading branch information
1 parent
f09e2cc
commit 1ddabd0
Showing
6 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func vmExistsCommand() *cobra.Command { | ||
existsCmd := &cobra.Command{ | ||
Use: "exists vm_name", | ||
Short: "Check whether a VM exists", | ||
Long: `Check whether a VM exists and was created by Virter.`, | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
v, err := InitVirter() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer v.ForceDisconnect() | ||
|
||
if err := v.VMExists(args[0]); err != nil { | ||
os.Exit(1) | ||
} | ||
}, | ||
ValidArgsFunction: suggestVmNames, | ||
} | ||
return existsCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func vmHostKeyCommand() *cobra.Command { | ||
hostKeyCmd := &cobra.Command{ | ||
Use: "host-key vm_name", | ||
Short: "Get the host key for a VM", | ||
Long: `Get the host key for a VM in the format of an OpenSSH known_hosts file.`, | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
v, err := InitVirter() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer v.ForceDisconnect() | ||
|
||
knownHostsText, err := v.VMGetKnownHosts(args[0]) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Print(knownHostsText) | ||
}, | ||
ValidArgsFunction: suggestVmNames, | ||
} | ||
return hostKeyCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Virter SSH Integration | ||
|
||
The simplest way to connect to a virtual machine that was started with Virter | ||
is to use `virter vm ssh ...`. However, you may also choose to use `ssh` and | ||
related tools to connect. This can be made very convenient by adding the | ||
following to your `~/.ssh/config`: | ||
|
||
``` | ||
Match exec "virter vm exists %h" | ||
User root | ||
IdentityAgent none | ||
IdentityFile ~/.config/virter/id_rsa | ||
KnownHostsCommand /usr/bin/env virter vm host-key %n | ||
``` | ||
|
||
Now you can easily connect: | ||
|
||
``` | ||
$ ssh foo | ||
[root@foo ~]# | ||
``` | ||
|
||
## Name resolution | ||
|
||
Depending on your configuration, `ssh` may or may not be able to resolve the VM | ||
name to a hostname. If not, you will see an error similar to: | ||
|
||
``` | ||
$ ssh foo | ||
ssh: Could not resolve hostname foo: Name or service not known | ||
``` | ||
|
||
Fix this by installing and configuring the [libvirt NSS | ||
modules](https://libvirt.org/nss.html). In particular, you will need to install | ||
a package such as `libvirt-nss` or `libnss-libvirt`. Then add `libvirt_guest` | ||
to the `hosts:` configuration in the file `/etc/nsswitch.conf`. | ||
|
||
## SSH integration with qualified names | ||
|
||
If you have configured a network domain in your libvirt network, you can also | ||
connect to the VM using the fully qualified domain name (FQDN). For instance, | ||
with the domain name `test`, you can use this configuration in your | ||
`~/.ssh/config`: | ||
|
||
``` | ||
Host *.test | ||
User root | ||
IdentityAgent none | ||
IdentityFile ~/.config/virter/id_rsa | ||
KnownHostsCommand /bin/bash -c 'virter vm host-key "$(basename "%n" .test)"' | ||
``` | ||
|
||
Now you can easily connect: | ||
|
||
``` | ||
$ ssh foo.test | ||
[root@foo ~]# | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters