-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextend-disk.sh
106 lines (88 loc) · 2.34 KB
/
extend-disk.sh
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
set -e
# Function to log messages
log() {
echo "[INFO] $1"
}
# Function to log errors and exit
error() {
echo "[ERROR] $1" >&2
exit 1
}
# Function to prompt user for confirmation
confirm() {
read -r -p "$1 [Y/n] " response
if [[ ! "$response" =~ ^[Yy]$ ]]; then
log "Script aborted by user."
exit 0
fi
}
# Check for required argument
if [ -z "$1" ]; then
error "You must specify the disk device being extended, e.g., /dev/sda"
fi
device="$1"
lvm=""
partition_type="E6D6D379-F507-44C2-A23C-238F2A3DF928"
# Function to get disk size
get_disk_size() {
df -hT "$1" | awk 'NR==2 {print $3}'
}
# Determine if LVM is in use
pvdevice=$(pvs | grep "$device" | awk '{print $1}' | xargs)
if [ -n "$pvdevice" ]; then
lvm=true
fi
# Determine the partition number
if [ -n "$lvm" ]; then
vgdevice=$(pvs | grep "$device" | awk '{print $2}' | xargs)
read -r -p "Enter the partition number for $device: " partition_number
else
last_partition=$(fdisk -l 2> /dev/null | grep "^$device" | awk '{print $1}' | tail -n 1)
read -r -p "Enter the partition number for $device: " partition_number
fi
# Confirm user's intention
confirm "This script will extend the disk partition on $device. Do you want to proceed?"
# Get the previous disk size
prev_disk_size=$(get_disk_size "$device")
# Resize the disk partition
log "Resizing disk partition..."
sfdisk "$device" -N"${partition_number}" --force <<EOF
-,-,-
EOF
sfdisk "$device" -N"${partition_number}" --force <<EOF
-,+,$partition_type
EOF
partprobe
# Resize LVM logical volume (if applicable)
if [ -n "$lvm" ]; then
log "Resizing LVM logical volume..."
pvresize "$pvdevice"
lvpath=$(lvdisplay "$vgdevice" | grep "LV Path" | awk '{print $3}' | xargs)
lvextend -l +100%FREE "$lvpath"
fi
# Resize the file system
log "Resizing file system..."
if [ -n "$lvm" ]; then
filesystem_path="$lvpath"
else
filesystem_path="$last_partition"
fi
case "$(df -Th "$filesystem_path" | tail -1 | awk '{print $2}')" in
ext4)
resize2fs "$filesystem_path"
;;
xfs)
xfs_growfs "$filesystem_path"
;;
*)
error "Unsupported file system type."
;;
esac
# Get the new disk size
new_disk_size=$(get_disk_size "$device")
# Log final information
log "Disk partition on $device resized."
log "Previous Disk Size: $prev_disk_size"
log "New Disk Size: $new_disk_size"
log "File system resize is finished!"