-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattach-disk.ps1
56 lines (50 loc) · 1.97 KB
/
attach-disk.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# sudo pwsh -Command "iex '& ([scriptblock]::Create((iwr https://raw.githubusercontent.com/abdullahfarook/kube/main/attach-disk.ps1))) -size 32G'"
param (
[string]$size,
[bool]$existing = $true,
[string]$path = "/shared"
)
Write-Output "Starting disk attachment process..."
Write-Output "Parameters: size=$size, existing=$existing, path=$path"
# find disk which has same size
Write-Output "Searching for disk with size $size..."
$disk = lsblk -o NAME,SIZE,MOUNTPOINT | grep -i "sd" | Where-Object { $_ -match "$size" }
if ($null -eq $disk) {
Write-Error "No disk found with size $size"
return
}
$disk = $disk -split " " | Where-Object { $_ -ne "" }
$disk = $disk[0]
Write-Output "Disk found: $disk"
$partition = $disk + "1"
if (($existing -eq $false) -and (-not (lsblk -o NAME,SIZE,MOUNTPOINT | grep -i "$disk" | grep -i "part"))) {
Write-Output "Partitioning and formatting disk $disk..."
# check if disk is already partitioned then do not partition
parted /dev/$disk --script mklabel gpt mkpart xfspart xfs 0% 100%
mkfs.xfs /dev/$partition
partprobe /dev/$partition
Write-Output "Disk partitioned and formatted."
}
# mount the disk
if (-not (Test-Path $path)) {
Write-Output "Creating mount point $path..."
mkdir $path
}
Write-Output "Mounting /dev/$partition to $path..."
mount /dev/$partition $path
# add to fstab
Write-Output "Adding disk to /etc/fstab..."
$uuid = blkid | grep -i $partition | ForEach-Object { $_ -split " " } | Where-Object { $_ -match "UUID" }
$uuid = $uuid -split "=" | Where-Object { $_ -ne "" }
$uuid = $uuid[1]
# if UUID exists in fstab, does not add
if ($null -eq (grep -i $uuid /etc/fstab)) {
Add-Content /etc/fstab "UUID=$uuid $path xfs defaults,nofail 1 2"
Write-Output "Disk added to /etc/fstab."
} else {
Write-Output "Disk already exists in /etc/fstab."
}
# verify the disk
Write-Output "Verifying the disk..."
lsblk -o NAME,SIZE,MOUNTPOINT | grep -i "sd"
Write-Output "Disk attachment process completed."