AdditionalDiskService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Services\Vps;
  3. use MGProvision\Proxmox\v2\models\HardDisk;
  4. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  5. use ModulesGarden\ProxmoxAddon\App\Services\Utility;
  6. use ModulesGarden\ProxmoxAddon\App\Enum\Vps\ConfigurableOption;
  7. use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;
  8. class AdditionalDiskService
  9. {
  10. use WhmcsParams;
  11. use ApiService;
  12. use ProductService;
  13. public function hasDisk(){
  14. foreach ($this->vm()->getHardDisks() as $hardDisk)
  15. {
  16. if (!$hardDisk->isMaster())
  17. {
  18. return true;
  19. }
  20. }
  21. return false;
  22. }
  23. public function create(){
  24. $diskSize = $this->getSize();
  25. if(!$diskSize || $diskSize == "-1" ){
  26. return;
  27. }
  28. $formats = $this->configuration()->getAdditionalDiskFormat();
  29. $typeArray = $this->configuration()->getAdditionalDiskType();
  30. $type = $typeArray[0];
  31. foreach($typeArray as $t){
  32. $bus = $this->vm()->findFreeDeviceId($t);
  33. if($bus===null){
  34. $type = $t;
  35. break;
  36. }
  37. }
  38. $harDisk = new HardDisk($type . $bus);
  39. $harDisk->setSize($diskSize)
  40. ->setApi($this->api())
  41. ->setPath($this->vm()->getPath() . "/config")
  42. ->setMedia("disk")
  43. ->setBackup( 0)//off
  44. ->setStorage($this->configuration()->getAdditionalDiskStorage())
  45. ->setCache($this->configuration()->getAdditionalDiskCache())
  46. ->setFormat($formats[0])
  47. ->setMbps_rd($this->configuration()->getAdditionalDiskMbpsRd())
  48. ->setMbps_wr($this->configuration()->getAdditionalDiskMbpsRd())
  49. ->setDiscard($this->configuration()->isAdditionalDiskDiscard() ? "on" : null)
  50. ->setIops_rd($this->configuration()->getAdditionalDiskIopsRd())
  51. ->setIops_rd_max($this->configuration()->getAdditionalDiskIopsRdMax())
  52. ->setIops_wr($this->configuration()->getAdditionalDiskIopsWr())
  53. ->setIops_wr_max($this->configuration()->getAdditionalDiskIopsWrMax())
  54. ->setReplicate($this->configuration()->isAdditionalDiskReplicate() ? 0 : null);
  55. if ($this->configuration()->isAdditionalDiskIoThread() && in_array($type, ['virtio', 'scsi']))
  56. {
  57. $harDisk->setIothread(1);
  58. }
  59. if($this->configuration()->isPermissionAdditionalDiskBackup()){
  60. $harDisk->setBackup(null);//on
  61. }
  62. $harDisk->create();
  63. }
  64. public function resize(){
  65. $diskSize = $this->getSize();
  66. if(!$diskSize || $diskSize == "-1" ){
  67. return;
  68. }
  69. foreach ($this->vm()->getHardDisks() as $hardDisk)
  70. {
  71. if ($hardDisk->isMaster())
  72. {
  73. continue;
  74. }
  75. if($hardDisk->getGb() == $diskSize){
  76. return true;
  77. }
  78. if ( (int) $hardDisk->getGb() > (int) $diskSize){
  79. throw new \InvalidArgumentException("Downgrading the additional disk size is restricted");
  80. }
  81. $size = "+" . abs((int)$diskSize - $hardDisk->getGb()) . "G";
  82. $hardDisk->resize($size);
  83. return true;
  84. }
  85. }
  86. private function getSize(){
  87. $diskSize = $this->configuration()->getAdditionalDiskSize();
  88. if ($this->isWhmcsConfigOption(ConfigurableOption::ADDITIONAL_DISKS_SIZE))
  89. {
  90. $diskSize = $this->getWhmcsConfigOption(ConfigurableOption::ADDITIONAL_DISKS_SIZE);
  91. Utility::unitFormat($diskSize, $this->configuration()->getAdditionalDiskUnit(), 'gb');
  92. }
  93. return $diskSize;
  94. }
  95. }