-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattach-disk.ps1
74 lines (69 loc) · 2.33 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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"
)
function Write-Log {
param (
[string]$message,
[string]$level = "INFO"
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
if ($level -eq "ERR") {
Write-Err "$timestamp [$level] $message"
}else{
Write-Log "$timestamp [$level] $message"
}
}
function Write-Err {
param (
[string]$message
)
Write-Log $message "ERR"
exit 1
}
Write-Log "Starting disk attachment process..."
Write-Log "Parameters: size=$size, existing=$existing, path=$path"
# find disk which has same size
Write-Log "Searching for disk with size $size..."
$disk = lsblk -o NAME,SIZE,MOUNTPOINT | grep -i "sd" | Where-Object { $_ -match "$size" }
if ($null -eq $disk) {
Write-Err "No disk found with size $size"
return
}
$disk = $disk -split " " | Where-Object { $_ -ne "" }
$disk = $disk[0]
Write-Log "Disk found: $disk"
$partition = $disk + "1"
if (($existing -eq $false) -and (-not (lsblk -o NAME,SIZE,MOUNTPOINT | grep -i "$disk" | grep -i "part"))) {
Write-Log "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-Log "Disk partitioned and formatted."
}
# mount the disk
if (-not (Test-Path $path)) {
Write-Log "Creating mount point $path..."
mkdir $path
}
Write-Log "Mounting /dev/$partition to $path..."
mount /dev/$partition $path
# add to fstab
Write-Log "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-Log "Disk added to /etc/fstab."
} else {
Write-Log "Disk already exists in /etc/fstab."
}
# verify the disk
Write-Log "Verifying the disk..."
lsblk -o NAME,SIZE,MOUNTPOINT | grep -i "sd"
Write-Log "Disk attachment process completed."