cros_vm_lib: Allow custom private key for ssh_ping

Since CL:286935, test ssh keys can be generated at build time, instead of
copying the static 'testing_rsa'. cros_vm_lib, especially its call to
'ssh_ping', needs to be extended to take a private key as an argument.

TEST=Trybot run with rambi-pre-cq, and a trybot run with a build that has the
USE flag 'generated_key' enabled, i.e., a generated private key instead of the
default key will be used for ssh.

Change-Id: I04e60367f9799d02877377e39ad675af358322c4
Commit-Ready: Daniel Wang <>
Tested-by: Daniel Wang <>
Reviewed-by: Simran Basi <>
diff --git a/lib/ b/lib/
index 8a67179..6e28543 100644
--- a/lib/
+++ b/lib/
@@ -4,6 +4,9 @@
 # Common vm functions for use in crosutils.
 DEFINE_string kvm_pid "" \
   "Use this pid file.  If it exists and is set, use the vm specified by pid."
 DEFINE_boolean copy ${FLAGS_FALSE} "Copy the image file before starting the VM."
@@ -14,6 +17,8 @@
 "This option is used for testing Google Compute Engine-compatible images."
 DEFINE_boolean snapshot ${FLAGS_FALSE} "Don't commit changes to image."
 DEFINE_integer ssh_port 9222 "Port to tunnel ssh traffic over."
+DEFINE_string ssh_private_key "${DEFAULT_PRIVATE_KEY}" \
+    "Path to the private key to use to ssh into test image as the root user."
 DEFINE_string vnc "" "VNC Server to display to instead of SDL "\
 "(e.g. pass ':1' to listen on"
@@ -241,9 +246,9 @@
   "${cmd}" \
     --ssh_port=${FLAGS_ssh_port} \
+    --private_key=${FLAGS_ssh_private_key} \
     --remote= >&2
 # Tries to ssh into live image $1 times.  After first failure, a try involves
 # shutting down and restarting kvm.
 retry_until_ssh() {
@@ -265,6 +270,7 @@
 stop_kvm() {
   if [ "${FLAGS_persist}" -eq "${FLAGS_TRUE}" ]; then
     echo "Persist requested.  Use --ssh_port ${FLAGS_ssh_port} " \
+      "--ssh_private_key ${FLAGS_ssh_private_key} " \
       "--kvm_pid ${KVM_PID_FILE} to re-connect to it." >&2
     echo "Stopping the KVM instance" >&2