private async Task AllocateSpace(ByteSize requiredSize) { Log.Verbose("Verifying the available space..."); Log.Verbose("We will need {Size} of free space for Windows", requiredSize); var hasEnoughSpace = await phone.IsThereEnoughSpace(requiredSize); if (!hasEnoughSpace) { Log.Verbose("There's not enough space in the phone. We will try to allocate it automatically"); var success = await spaceAllocators.ToObservable() .Select(x => Observable.FromAsync(() => x.TryAllocate(phone, requiredSize))) .Merge(1) .Any(successful => successful); if (!success) { Log.Verbose("Allocation attempt failed"); throw new NotEnoughSpaceException($"Could not allocate {requiredSize} on the phone. Please, try to allocate the necessary space manually and retry."); } Log.Verbose("Space allocated correctly"); } else { Log.Verbose("We have enough available space to deploy Windows"); } }
public async Task <bool> TryAllocate(IPhone phone, ByteSize requiredSpace) { Log.Verbose("Shrinking Data partition..."); var dataVolume = await phone.GetDataVolume(); if (dataVolume == null) { return(false); } var phoneDisk = await phone.GetDeviceDisk(); var data = dataVolume.Size; var allocated = phoneDisk.AllocatedSize; var available = phoneDisk.AvailableSize; var newData = data - (requiredSpace - available); Log.Verbose("Total size allocated: {Size}", allocated); Log.Verbose("Space available: {Size}", available); Log.Verbose("Space needed: {Size}", requiredSpace); Log.Verbose("'Data' size: {Size}", data); Log.Verbose("Calculated new size for the 'Data' partition: {Size}", newData); Log.Verbose("Resizing 'Data' to {Size}", newData); await dataVolume.Partition.Resize(newData); Log.Verbose("Resize operation completed successfully"); return(await phone.IsThereEnoughSpace(requiredSpace)); }