How to Upload File to Guest Operating System on VMware

My last post explained how to download file from a guest operating system. Naturally this post is about how to upload file. After a quick sample code, I will discuss how to extend the capability of existing APIs that run program inside guest operating system. My next post will wrap up this series of guest related APIs in vSphere API.

Let’s take a look at a sample code: (To run it, first check out the simple prerequisites in a previous post)

Time to learn how to "Google" and manage your VMware and clouds in a fast and secure

As you can see, the sample uploads a local file (vSphere Java code generator) to the guest operating system. Similar to the downloading directory feature, I implemented a new feature in the Guest Operating System Management API (GuestFileDirector class) that allows you to upload a whole directory recursively to the guest operating system. Again, if you archive a directory and upload the archive, you likely get much better performance than uploading individual files one by one.

Discussion

So far, you’ve seen several samples illustrating the individual APIs in the Guest Operating System Management APIs. Combining them together, you can achieve much more. For example, limited by the vSphere API, the run() method returns only the process ID (PID) of a newly started program. What if you want to get the output of a script you run in the guest operating system? There is no direct way to do it. The workaround is that you pipeline the stand out to a file, then download the file (remember to delete the file afterwards). While implementing this, you may need to handle cases like long run scripts whose output file is continuously changing therefore require continuous downloading.

Less common case is that you want to send a property file, or data input file to a program before it gets run in the guest operating system? You cannot do it without the feature introduced in this post.

Hi Steve,
On trying to upload a file to guest, I am getting the following exceptions:
java.net.UnknownHostException: *
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:559)
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.protocol.https.HttpsClient.(HttpsClient.java:272)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:329)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:916)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1019)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:230)
at org.doublecloud.vi.vmware.guest.GuestFileDirector.uploadData(GuestFileDirector.java:231)
at org.doublecloud.vi.vmware.guest.GuestFileDirector.uploadFromStream(GuestFileDirector.java:218)
at org.doublecloud.vi.vmware.guest.GuestFileDirector.uploadFile(GuestFileDirector.java:189)

Not sure what is the reason.

Code is exactly same as shown in your example. I am have the same issue with download as well.
Also, I was able to run a script in the guest.

Hi, I ran into the same exception as Atul did..
Exception in thread “main” java.net.UnknownHostException: *
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:550)
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
at sun.net.www.protocol.https.HttpsClient.(HttpsClient.java:271)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:328)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:731)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:834)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:230)
at org.doublecloud.vi.vmware.guest.GuestFileDirector.uploadData(GuestFileDirector.java:231)
at org.doublecloud.vi.vmware.guest.GuestFileDirector.uploadFromStream(GuestFileDirector.java:218)
at org.doublecloud.vi.vmware.guest.GuestFileDirector.uploadFile(GuestFileDirector.java:189)
at org.doublecloud.vi.vmware.guest.GuestFileDirector.uploadDirectory(GuestFileDirector.java:172)
at com.vmware.vim25.mo.samples.HelloVM.main(HelloVM.java:123)
==========================================================
However, it works when i just upload a directory to guest with: fd.uploadDirectory(“c:\\temp1”, “c:\\temp1”);
very appreicated if there are any pointers…

Problem seems to be in “InitiateFileTransferToGuest” . It returns xsd::string and the description of the same is as under:
“A URL to which the user has to send a PUT request. The host part of the URL is returned as ‘*’ if the hostname to be used is the name of the server to which the call was made. For example, if the call is made to esx-svr-1.domain1.com, and the file can be uploaded to http://esx-svr-1.domain1.com/guestFile?id=1&token=1234, the URL returned may be http://*/guestFile?id=1&token=1234. The client replaces the asterisk with the server name on which it invoked the call.”

Sorry that I haven’t got time to look into the issue. You can code against GuestFileManager directly. Optionally, you can break your script into multiple commands and run them one by one. I know it may not be an option if you have complex logics in your script.

to test again, this time, I get exception like:
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at java.lang.Class.newInstance0(Class.java:372)
at java.lang.Class.newInstance(Class.java:325)
at com.vmware.vim25.ws.XmlGen.fromXml(XmlGen.java:201)
at com.vmware.vim25.ws.XmlGen.parseSoapFault(XmlGen.java:80)
at com.vmware.vim25.ws.WSClient.invoke(WSClient.java:133)
at com.vmware.vim25.ws.VimStub.initiateFileTransferToGuest(VimStub.java:4199)
at com.vmware.vim25.mo.GuestFileManager.initiateFileTransferToGuest(GuestFileManager.java:99)

From the exception, it looks like the soap request is not formed correctly. Does this API support a empty password? Or, is there a work around for such user case?

As of 5.1 I’m having trouble with the GuestFileDirector I get the following problem.
Exception in thread “main” java.lang.NullPointerException
at org.doublecloud.vi.vmware.guest.GuestFileDirector.(GuestFileDirector.java:44)

How to upload a file to Linux Guest OS. I tried uploading using initiateFileTransferToGuest but was unable to write the file. I even checked the permissions of the file.It has both read and wrire permissiosns.

NEED HELP?

My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__ doublecloud.org.

Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.