aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog.md14
-rwxr-xr-xkvmx72
2 files changed, 86 insertions, 0 deletions
diff --git a/ChangeLog.md b/ChangeLog.md
index 14e33d4..b99c8cf 100644
--- a/ChangeLog.md
+++ b/ChangeLog.md
@@ -1,5 +1,19 @@
# ChangeLog
+## 0.4.0 - Unreleased
+
+* Adds `kvmx sshdir` action, which SSH's to the guest and changes to a given
+ folder. If no arguments are given, it tries to change to the equivalent
+ folder in the guest, so it can be a way to "cd" to the same directory but
+ inside the guest.
+
+ If the folder in the host is also mounted in the guest in a similar
+ mountpoint, it's a handy way to move to the same folder, but inside the
+ guest.
+
+ For folders inside $HOME, user name conversion is automatically done since
+ the user inside the guest might not match the user in the host.
+
## 0.3.0 - 2024-09-19
* Increase the maximum number of shared folders to avoid error in when KVMX
diff --git a/kvmx b/kvmx
index f00d3f7..a2578d8 100755
--- a/kvmx
+++ b/kvmx
@@ -965,6 +965,78 @@ function kvmx_ssh {
$ssh_env $SSH_COMMAND -p $SSH 127.0.0.1 $*
}
+# Log into the guest using SSH and cd to a given folder
+#
+# If no arguments are given, it tries to change to the equivalent folder in the
+# guest, so it can be a way to "cd" to the corresponding directory but inside
+# the guest.
+#
+# If the folder in the host is also mounted in the guest in a similar
+# mountpoint, it's a handy way to move to the same folder, but inside the
+# guest.
+#
+# For folders inside $HOME, user name conversion is automatically done since
+# the user inside the guest might not match the user in the host.
+#
+# There are a number of ways this can be done:
+#
+# 1. Trying to move to a folder, and then exec a login shell, which don't
+# always work.
+#
+# 2. Setting a special environment variable which is interpreted by the shell
+# startup script and then changes to the requested folder upon login.
+# Variable passing can happen directly through command invocation in
+# the remote host, or using SSH's SendEnv and AcceptEnv, but this last
+# method involves server-side configuration.
+#
+# Ideally, all approaches should be implemented, and KVMX could choose the
+# best one opportunistially, or could have a configuration option to choose,
+# defaulting to the one which could work for most cases.
+#
+function kvmx_sshdir {
+ if [ "$ssh_support" != "y" ]; then
+ echo "$BASENAME: SSH support for $VM is disabled"
+ exit 1
+ fi
+
+ if ! kvmx_running || kvmx_suspended; then
+ echo "$BASENAME: $VM not running, trying to start it..."
+ kvmx up $VM || exit 1
+ #kvmx_up || exit 1
+ #echo "$BASENAME: guest $VM is not running"
+ #exit 1
+ fi
+
+ DEST="$1"
+
+ # Defaults to the current folder
+ if [ -z "$DEST" ]; then
+ DEST="`pwd`"
+
+ # Fix ~/ path
+ if echo $DEST | grep -q -e "^$HOME"; then
+ DEST="$(echo $DEST | sed -e "s|^$HOME|/home/$SSH_LOGIN|")"
+ fi
+ fi
+
+ # Get the SSH configuration
+ SSH="`cat $SSHFILE`"
+
+ # Implementation using approach 1: change to folder and invoke the login shell
+ #
+ # References and discussion:
+ #
+ # * https://serverfault.com/questions/167416/how-can-i-automatically-change-directory-on-ssh-login
+ # * https://stackoverflow.com/questions/626533/how-can-i-ssh-directly-to-a-particular-directory#626670
+ # * https://unix.stackexchange.com/questions/86941/how-to-ssh-into-a-specific-directory
+ #
+ $ssh_env $SSH_COMMAND -t -p $SSH 127.0.0.1 "cd $DEST && exec \$SHELL --login"
+
+ # Implementation using approach 2, with a special environment variable
+ # STARTUP_FOLDER, which needs to be supported by the shell startup scripts
+ #$ssh_env $SSH_COMMAND -t -p $SSH 127.0.0.1 "export STARTUP_FOLDER=$DEST && exec \$SHELL --login"
+}
+
# Enhanced SSH login into the guest
function kvmx_login {
# This allows the usage of a custom login command