示例#1
0
        //---------------------------------------------------------------------
        // Instance members

        /// <inheritdoc/>
        public async Task <GrpcBaseReply> AddVmAsync(GrpcAddVmRequest request, CallContext context = default)
        {
            await SyncContext.Clear;

            try
            {
                var extraDrives = new List <VirtualDrive>();

                if (request.ExtraDrives != null)
                {
                    foreach (var grpcDrive in request.ExtraDrives)
                    {
                        extraDrives.Add(grpcDrive.ToLocal());
                    }
                }

                hyperv.AddVm(
                    machineName:       request.MachineName,
                    memorySize:        request.MemorySize,
                    processorCount:    request.ProcessorCount ?? 4,
                    driveSize:         request.DriveSize,
                    drivePath:         request.DrivePath,
                    checkpointDrives:  request.CheckpointDrives,
                    templateDrivePath: request.TemplateDrivePath,
                    switchName:        request.SwitchName,
                    extraDrives:       extraDrives);

                return(new GrpcBaseReply());
            }
            catch (Exception e)
            {
                return(new GrpcBaseReply(e));
            }
        }
示例#2
0
        /// <summary>
        /// Creates a virtual machine.
        /// </summary>
        /// <param name="machineName">The machine name.</param>
        /// <param name="memorySize">
        /// A string specifying the memory size.  This can be a long byte count or a
        /// byte count or a number with units like <b>512MiB</b>, <b>0.5GiB</b>, <b>2GiB</b>,
        /// or <b>1TiB</b>.  This defaults to <b>2GiB</b>.
        /// </param>
        /// <param name="processorCount">
        /// The number of virutal processors to assign to the machine.  This defaults to <b>4</b>.
        /// </param>
        /// <param name="driveSize">
        /// A string specifying the primary disk size.  This can be a long byte count or a
        /// byte count or a number with units like <b>512MB</b>, <b>0.5GiB</b>, <b>2GiB</b>,
        /// or <b>1TiB</b>.  Pass <c>null</c> to leave the disk alone.  This defaults to <c>null</c>.
        /// </param>
        /// <param name="drivePath">
        /// Optionally specifies the path where the virtual hard drive will be located.  Pass
        /// <c>null</c> or empty to default to <b>MACHINE-NAME.vhdx</b> located in the default
        /// Hyper-V virtual machine drive folder.
        /// </param>
        /// <param name="checkpointDrives">Optionally enables drive checkpoints.  This defaults to <c>false</c>.</param>
        /// <param name="templateDrivePath">
        /// If this is specified and <paramref name="drivePath"/> is not <c>null</c> then
        /// the hard drive template at <paramref name="templateDrivePath"/> will be copied
        /// to <paramref name="drivePath"/> before creating the machine.
        /// </param>
        /// <param name="switchName">Optional name of the virtual switch.</param>
        /// <param name="extraDrives">
        /// Optionally specifies any additional virtual drives to be created and
        /// then attached to the new virtual machine.
        /// </param>
        /// <remarks>
        /// <note>
        /// The <see cref="VirtualDrive.Path"/> property of <paramref name="extraDrives"/> may be
        /// passed as <c>null</c> or empty.  In this case, the drive name will default to
        /// being located in the standard Hyper-V virtual drivers folder and will be named
        /// <b>MACHINE-NAME-#.vhdx</b>, where <b>#</b> is the one-based index of the drive
        /// in the enumeration.
        /// </note>
        /// </remarks>
        public void AddVm(
            string machineName,
            string memorySize        = "2GiB",
            int processorCount       = 4,
            string driveSize         = null,
            string drivePath         = null,
            bool checkpointDrives    = false,
            string templateDrivePath = null,
            string switchName        = null,
            IEnumerable <VirtualDrive> extraDrives = null)
        {
            if (isAdmin)
            {
                hypervClient.AddVm(
                    machineName:       machineName,
                    memorySize:        memorySize,
                    processorCount:    processorCount,
                    driveSize:         driveSize,
                    drivePath:         drivePath,
                    checkpointDrives:  checkpointDrives,
                    templateDrivePath: templateDrivePath,
                    switchName:        switchName,
                    extraDrives:       extraDrives);
            }
            else
            {
                var request = new GrpcAddVmRequest(
                    machineName:       machineName,
                    memorySize:        memorySize,
                    processorCount:    processorCount,
                    driveSize:         driveSize,
                    drivePath:         drivePath,
                    checkpointDrives:  checkpointDrives,
                    templateDrivePath: templateDrivePath,
                    switchName:        switchName,
                    extraDrives:       extraDrives?.Select(drive => drive.ToProto()));

                desktopService.AddVmAsync(request).Result.Error.EnsureSuccess();
            }
        }