1

I use salt-stack and pyvmomi module to communicate with vcenter and create the VM. On this newly created VM I want to copy files(around 1 GB) from vcenter Datastore. InitiateFileTransferToGuest can be used to upload the file to VM but how can we copy files from datastore to vm ?

2 Answers2

0

The hackiest way I can think of is:

  1. Save the 1GB file as .iso {either use MagicIso or inbuilt tools of linux}.
  2. Now place the file in datastore.
  3. Now when creating the vm,you need to set the cdrom to point to file-data instead of empty string.
  4. You can either edit the vmx file or provide the vmx options while creating itself
  ide1:0.deviceType = "cdrom-image"
    ide1:0.fileName = "/vmfs/volumes/5034a864-xxxxxx/data.iso"
    ide1:0.present = "TRUE"
  1. After powering on the guest, depending upon the guest, you can add a batch/shell to copy to its disk.
  2. If required, you can use Invoke-VMScript powercli cmdlet to do operation 5 for you.

Cheers, zXi

zXi
  • 112
  • 7
  • That i had in mind (wanted to avoid this process) apart from that we can also use salt-get-file to copy the file from master to minion – ashok vijay Oct 10 '16 at 14:12
  • The pyvmomi salt allows only the operations supported from a VC objects. InitiateFileTransferToGuest is the pyvmomi call to transfer files to guests if tools are running. However, the API would need to be triggered from an ESXi server. You could try having a NFS datastore and add the addition of mounting {{mount.mounted}} to guest minions on tools running – zXi Oct 10 '16 at 14:48
  • Please share more detail about NFS and mounting datapoint on minion's. Thanks – ashok vijay Oct 10 '16 at 18:45
0

What about running rsync

  • ESXi Host: enable SSH
  • Some understanding of SSH keys

If this is a one off file transfer you could probably skip the first step.

Setup Public SSH Keys for vCentre. ssh-keygen is found in /usr/lib/vmware/openssh/bin ssh-keygen generates 2 files in ~/.ssh: id_rsa and id_rsa.pub

ssh-keygen -f ~/.ssh/id_rsa -q -P ""

On the remote host, store the public key If using ESXi 5 or below place id_rsa.pub in ~/.ssh/authorized_keys. If using ESXi 5.5 or up public keys are located somewhere else so put id_rsa.pub in /etc/ssh/keys-<username>/authorized_keys

As a note you can store more than one key in this file.

To allow root access, change PermitRootLogin no to PermitRootLogin yes in the /etc/ssh/sshd_config file. To disable password login, ensure that ChallengeResponseAuthentication and PasswordAuthentication are set to no.

Restart the SSH service ESXi - /etc/init.d/SSH restart ESX - service sshd reload

Now that we have authentication sorted we can now copy files through an encrypted channel to our destination. Understanding how the ssh keys work will make your remote execution tasks so much more easier and streamlines deployment and management.

Rsync The basic syntax of rsync is as follows rsync options source destination

Rsync over SSH

Some options - VM as local

Copy a File from a Remote Server(ESXi) to a Local Server(VM) with SSH

rsync -avzhe ssh root@[vcentreIP]:/source_dir_to_copy/ /dest_dir_location/

Copy a File from a Local Server(VM) to a Remote Server(ESXi) with SSH

rsync -avzhe ssh /source_dest_to_copy/ root@[vcentreIP]:/dest_dir_location/

Some options - ESXi as local - ESXi has not got rsync installed as default.

Copy a File from a Remote Server(VM) to a Local Server(ESXi) with SSH

rsync -avzhe ssh root@[VMIP]:/source_dir_to_copy/ /dest_dir_location/

Copy a File from a Local Server(ESXi) to a Remote Server(VM) with SSH

rsync -avzhe ssh /source_dest_to_copy/ root@[VMIP]:/dest_dir_location/

Another way is probably the option for you.

Using the same logic and method above. you could generate a set of keys between the two machines and use an agent to initiate the rsync command.

Copy a File from a VM to ESXi with SSH

rsync -avzhe ssh root@[VMIP]:/source_dir_to_copy/ root@[vCentreIP]:/dest_dir_location/

Copy a File from ESXi to VM with SSH

rsync -avzhe ssh root@[vcentreIP]:/source_dir_to_copy/ root@[VMIP]:/dest_dir_location/

Mark Nicolle
  • 307
  • 1
  • 11