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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
#!/bin/bash
# set -e
PARTITIONSPATH="/proc/partitions"
CONFIGPATH="./config"
# CONFIGPATH="/opt/openslx/config"
# BLOCKSIZE=$(sfdisk -l /dev/$SLX_CHOOSEN_DISK | grep -o -e "blocks of [0-9]* bytes" | cut -d ' ' -f3)
BLOCKSIZE=1024
. $CONFIGPATH
# testing if the sizes and ID's of the disk is defined in the config file
# picking disk that will be used
DISKS=$(cat $PARTITIONSPATH | tr -s ' ' | cut -d ' ' -f5 | grep -e "[a-z]$")
if [ -z "$DISKS" ]; then
dialog --msgbox "ERROR: Can't find an hard disk." 10 40
exit 1
fi
if [ -z "$SLX_CHOOSEN_DISK" ]; then
if [ $SLX_AUTOMATIC_PARTITIONING = "yes" ]; then
dialog --msgbox "ERROR: You didn' define any hard disk in your configuration file." 10 40
exit 1
fi
DIALOGSTR="This computer has the following partitions:\n"
for disk in $DISKS; do
DISKSIZE=$(($(cat $PARTITIONSPATH | grep -e $disk$ | tr -s ' ' | cut -d ' ' -f4)*$BLOCKSIZE))
PARTS=$(cat $PARTITIONSPATH | grep -e $disk[0-9] | tr -s ' ' | cut -d ' ' -f5)
GBSIZE=$(echo "scale=2; $DISKSIZE/1024/1024/1024" | bc -l)
DIALOGSTR=$DIALOGSTR"$disk $DISKSIZE Bytes ($GBSIZE GB)\n"
USED=0
for PART in $PARTS; do
PARTSIZE=$(($(cat $PARTITIONSPATH | grep -e $PART$ | tr -s ' ' | cut -d ' ' -f4)*1024))
USED=$(($USED+$PARTSIZE))
GBSIZE=$(echo "scale=2; $PARTSIZE/1024/1024/1024" | bc -l)
DIALOGSTR=$DIALOGSTR" $PART $PARTSIZE ($GBSIZE GB)\n"
done
DIALOGSTR=$DIALOGSTR" ----------\n"
FREESPACE=$(($DISKSIZE-$USED))
GBSIZE=$(echo "scale=2; $USED/1024/1024/1024" | bc -l)
DIALOGSTR=$DIALOGSTR" Used $USED ($GBSIZE GB)\n"
GBSIZE=$(echo "scale=2; $FREESPACE/1024/1024/1024" | bc -l)
DIALOGSTR=$DIALOGSTR" Free $FREESPACE ($GBSIZE GB)\n\n"
done
if [ $(echo $DISKS | tr ' ' \\n | wc -l) -gt 1 ]; then
DIALOGSTR=$DIALOGSTR"Which of these disks you want to use?\n"
SLX_CHOOSEN_DISK=$(dialog --no-collapse --cr-wrap --inputbox "$DIALOGSTR" 0 0 $(echo $DISKS | cut -d ' ' -f1) 3>&1 1>&2 2>&3)
if [ $? -eq 1 ]; then
exit 1;
fi
# verify if the choosen disk is a valid disk
echo $DISKS | tr ' ' \\n | grep -e ^$SLX_CHOOSEN_DISK$ > /dev/null 2>/dev/null
while [ $? -ne 0 ]; do
dialog --msgbox "Invalid option.\nTry again." 0 0
SLX_CHOOSEN_DISK=$(dialog --no-collapse --cr-wrap --inputbox "$DIALOGSTR" 0 0 $(echo $DISKS | cut -d ' ' -f1) 3>&1 1>&2 2>&3)
if [ $? -eq 1 ]; then
exit 1;
fi
echo $DISKS | tr ' ' \\n | grep -e ^$SLX_CHOOSEN_DISK$ > /dev/null 2>/dev/null
done
# ask a confirmation (all your data will be lost etc)
elif [ $(echo $DISKS | tr ' ' \\n | wc -l) -eq 1 ]; then
SLX_CHOOSEN_DISK=$(echo $DISKS | tr ' ' \\n | head -n1)
else
dialog --msgbox "ERROR: Can't find an hard disk." 10 40
exit 1
fi
fi
if [ -z "$SLX_PARTITION_TABLE" ]; then
if [ $SLX_AUTOMATIC_PARTITIONING = "yes" ]; then
SLX_PARTITION_TABLE='
44,10G,/tmp
45,10G,/var/scratch'
else
dialog --msgbox "ERROR: You didn't set a valid partition table. Please check your config file." 10 40
exit 1
fi
fi
# if SLX_PARTITION_TABLE is set then put his content in a associative array
declare -A PARTTBL
COUNTER=1
for PARTITION in $SLX_PARTITION_TABLE; do
IFS=,
set $PARTITION
if [ $COUNTER -eq 4 ]; then
COUNTER=$(($COUNTER+1))
fi
PARTTBL["$SLX_CHOOSEN_DISK$COUNTER/id"]=$1
PARTTBL["$SLX_CHOOSEN_DISK$COUNTER/size"]=$(echo $2 | egrep -o "[0-9]*")
if [ -n $3 ]; then
PARTTBL["$SLX_CHOOSEN_DISK$COUNTER/mountpoint"]=$3
fi
if [ -n "$4" ]; then
PARTTBL["$SLX_CHOOSEN_DISK$COUNTER/bootable"]=1
else
PARTTBL["$SLX_CHOOSEN_DISK$COUNTER/bootable"]=0
fi
# set the partitions to non-existents
PARTTBL["$SLX_CHOOSEN_DISK$COUNTER/exists"]=0
COUNTER=$(($COUNTER+1))
done
unset IFS
# PARTTBL starts at 1 and ends at COUNTER-1
# check if the choosen disk is greater than the new partition table size.
DISKSIZE=$(($(cat $PARTITIONSPATH | grep -e $SLX_CHOOSEN_DISK$ | tr -s ' ' | cut -d ' ' -f4)*$BLOCKSIZE))
NEEDED_SPACE=0
for (( i = 1; i < $COUNTER; i++ )); do
if [[ $i -ne 4 ]]; then
NEEDED_SPACE=$(($NEEDED_SPACE+${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}))
fi
done
NEEDED_SPACE=$(($NEEDED_SPACE*1024*1024*1024+$COUNTER*1024*1024))
if [ $DISKSIZE -lt $NEEDED_SPACE ]; then
dialog --msgbox "ERROR: Insufficient space in disk /dev/$SLX_CHOOSEN_DISK." 6 40
exit 1
fi
# choosing the partition type (MSDOS or GPT) if it wasn't set in config file
if [ -z "$SLX_PARTITION_TYPE" ] && [ $SLX_DELETE_PARTITION_TABLE = "yes" ]; then
if [ $SLX_AUTOMATIC_PARTITIONING = "yes" ]; then
SLX_PARTITION_TYPE=msdos
else
ANS=$(dialog --no-tags --menu "Choose the partitions type" 0 0 0 "1" " MSDOS " "2" " GPT " 3>&1 1>&2 2>&3)
if [ $? -eq 1 ]; then
exit 1
fi
if [ $ANS -eq 1 ]; then
SLX_PARTITION_TYPE=msdos
else
SLX_PARTITION_TYPE=GPT
fi
fi
fi
GBSIZE=$(echo "scale=2; $DISKSIZE/1024/1024/1024" | bc -l)
DIALOGSTR="$SLX_CHOOSEN_DISK $DISKSIZE Bytes ($GBSIZE GB)\n"
PARTS=$(cat $PARTITIONSPATH | grep -e $SLX_CHOOSEN_DISK[0-9] | tr -s ' ' | cut -d ' ' -f5)
USED=0
for PART in $PARTS; do
PARTSIZE=$(($(cat $PARTITIONSPATH | grep -e $PART$ | tr -s ' ' | cut -d ' ' -f4)*$BLOCKSIZE))
USED=$(($USED+$PARTSIZE))
GBSIZE=$(echo "scale=2; $PARTSIZE/1024/1024/1024" | bc -l)
DIALOGSTR=$DIALOGSTR" $PART $PARTSIZE ($GBSIZE GB)\n"
done
DIALOGSTR=$DIALOGSTR" ----------\n"
FREESPACE=$(($DISKSIZE-$USED))
GBSIZE=$(echo "scale=2; $USED/1024/1024/1024" | bc -l)
DIALOGSTR=$DIALOGSTR" Used $USED ($GBSIZE GB)\n"
GBSIZE=$(echo "scale=2; $FREESPACE/1024/1024/1024" | bc -l)
DIALOGSTR=$DIALOGSTR" Free $FREESPACE ($GBSIZE GB)\n\n"
DIALOGSTR=$DIALOGSTR"All /dev/$SLX_CHOOSEN_DISK partitions will be deleted, ok?"
# asking confirmation to create the partitions
if [[ $SLX_AUTOMATIC_PARTITIONING = "no" && $SLX_DELETE_PARTITION_TABLE = 'yes' ]]; then
dialog --defaultno --yesno "$DIALOGSTR" 0 0
if [ $? -eq 1 ]; then
exit 1
fi
fi
# pick the size of a sector (usually 512 bytes)
# SECTORSIZE=$(sfdisk /dev/$SLX_CHOOSEN_DISK -l -u S 2> /dev/null | grep Units | cut -d ' ' -f5)
SECTORSIZE=512
if [[ $USED -eq 0 ]]; then
SLX_DELETE_PARTITION_TABLE="yes"
fi
if [ $SLX_DELETE_PARTITION_TABLE = "yes" ]; then
# delete partition table
sgdisk -Z /dev/$SLX_CHOOSEN_DISK > /dev/null 2> /dev/null # erase all partitions
if [ $SLX_PARTITION_TYPE = 'GPT' ]; then
DIALOGSTR="Creating Partitions...\n\n"
echo "0" | dialog --gauge "$DIALOGSTR" 0 0 0
for (( i = 1; i < $COUNTER; i++ )); do # loop that will create each GPT partition, change GUIDS, change names, and set bootable flags.
if [ $i -ne 4 ]; then
sgdisk /dev/$SLX_CHOOSEN_DISK -n $i:0:+${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}G 2> /dev/null > /dev/null
# dialog --msgbox "$DIALOGSTR" 0 0
if [ "${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}" = "82" ] || [ "${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}" = "83" ]; then
sgdisk /dev/$SLX_CHOOSEN_DISK -t $i:${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}00
else
sgdisk /dev/$SLX_CHOOSEN_DISK -t $i:${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}000000-0000-0000-0000-000000000000\
sgdisk /dev/$SLX_CHOOSEN_DISK -c $i:\"${PARTTBL["$SLX_CHOOSEN_DISK$i/mountpoint"]}\" >/dev/null 2>/dev/null
fi
if [ ${PARTTBL["$SLX_CHOOSEN_DISK$i/bootable"]} -eq 1 ]; then
# set the bootable flag
sgdisk /dev/$SLX_CHOOSEN_DISK -A $i:set:2 >/dev/null 2>/dev/null
fi
SIZE=$((${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}*1024*1024*1024))
ID=${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}
MOUNTPOINT=${PARTTBL["$SLX_CHOOSEN_DISK$i/mountpoint"]}
DIALOGSTR=$DIALOGSTR$MOUNTPOINT" (/dev/"$SLX_CHOOSEN_DISK$i")\n"
if [ "${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}" = "82" ]; then
DIALOGSTR=$DIALOGSTR" GUID: 0657FD6D-A4AB-43C4-84E5-0933C84B4F4F\n"
elif [ "${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}" = "83" ]; then
DIALOGSTR=$DIALOGSTR" GUID: 0FC63DAF-8483-4772-8E79-3D69D8477DE4\n"
else
DIALOGSTR=$DIALOGSTR" GUID: "$ID"000000-0000-0000-0000-000000000000\n"
fi
DIALOGSTR=$DIALOGSTR" Size: "$SIZE" Bytes ("${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}" GB)\n"
echo $(( $i*100/($COUNTER-1) )) | dialog --gauge "$DIALOGSTR" 0 0 0
fi
done
else
# constructing the sfdisk input file
echo "unit: sectors
"> /tmp/partitiontable.tmp
START=$((1*1024*1024/$SECTORSIZE))
for (( i = 1; i < $COUNTER; i++ )); do
if [ $i -ne 4 ]; then
SIZE=$((${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}*1024*1024*1024/$SECTORSIZE))
ID=${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}
if [ ${PARTTBL["$SLX_CHOOSEN_DISK$i/bootable"]} -eq 1 ]; then
echo "/dev/"$SLX_CHOOSEN_DISK$i" : start= "$START", size= "$SIZE", Id= "$ID", bootable " >> /tmp/partitiontable.tmp
else
echo "/dev/"$SLX_CHOOSEN_DISK$i" : start= "$START", size= "$SIZE", Id= "$ID >> /tmp/partitiontable.tmp
fi
START=$(($START+$SIZE+1*1024*1024/$SECTORSIZE))
else
echo "/dev/"$SLX_CHOOSEN_DISK$i" : start= "$START", size= "$(($DISKSIZE/$SECTORSIZE-1))", Id= 5" >> /tmp/partitiontable.tmp
START=$(($START+1*1024*1024/$SECTORSIZE))
fi
done
sfdisk -q -f /dev/$SLX_CHOOSEN_DISK < /tmp/partitiontable.tmp > /dev/null 2> /dev/null
# rm -f /tmp/partitiontable.tmp
DIALOGSTR="Creating Partitions...\n\n"
for (( i = 1; i < $COUNTER; i++ )); do
if [ $i -lt 4 ]; then
SIZE=$((${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}*1024*1024*1024))
ID=${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}
MOUNTPOINT=${PARTTBL["$SLX_CHOOSEN_DISK$i/mountpoint"]}
DIALOGSTR=$DIALOGSTR$MOUNTPOINT" (/dev/"$SLX_CHOOSEN_DISK$i")\n"
DIALOGSTR=$DIALOGSTR" Type: Primary\n"
DIALOGSTR=$DIALOGSTR" ID: "$ID"\n"
DIALOGSTR=$DIALOGSTR" Size: "$SIZE" Bytes ("${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}" GB)\n"
elif [ $i -gt 4 ]; then
SIZE=$((${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}*1024*1024*1024))
ID=${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}
MOUNTPOINT=${PARTTBL["$SLX_CHOOSEN_DISK$i/mountpoint"]}
DIALOGSTR=$DIALOGSTR$MOUNTPOINT" (/dev/"$SLX_CHOOSEN_DISK$i")\n"
DIALOGSTR=$DIALOGSTR" Type: Logical\n"
DIALOGSTR=$DIALOGSTR" ID: "$ID"\n"
DIALOGSTR=$DIALOGSTR" Size: "$SIZE" Bytes ("${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}" GB)\n"
fi
done
fi
elif [ $(sfdisk -d /dev/$SLX_CHOOSEN_DISK 2> /dev/null | grep $SLX_CHOOSEN_DISK'1' | tr -s ' ' | cut -d ' ' -f7 | cut -d '=' -f2) = 'ee' ]; then
# finding existent partitions in GPT
echo 'GPT'
NONEXISTENT_PARTITIONS=0
for (( i = 1; i < $COUNTER; i++ )); do
if [ $i -ne 4 ]; then
for PART in $PARTS; do
if [ "${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}" = "82" ]; then
sgdisk /dev/$SLX_CHOOSEN_DISK -i ${PART:3} 2> /dev/null | grep "0657FD6D-A4AB-43C4-84E5-0933C84B4F4F" > /dev/null
elif [ "${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}" = "83" ]; then
sgdisk /dev/$SLX_CHOOSEN_DISK -i ${PART:3} 2> /dev/null | grep "0FC63DAF-8483-4772-8E79-3D69D8477DE4" > /dev/null
else
sgdisk /dev/$SLX_CHOOSEN_DISK -i ${PART:3} 2> /dev/null | grep ${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}"000000-0000-0000-0000-000000000000" > /dev/null
fi
if [ $? -eq 0 ]; then
PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]=$PART
NEEDED_SPACE=$(($NEEDED_SPACE-${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}*1024*1024*1024))
break
fi
done
if [[ ${PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]} = "0" ]]; then
NONEXISTENT_PARTITIONS=$(($NONEXISTENT_PARTITIONS+1))
fi
fi
done
# checking if there's sufficient space for the new partitions.
if [[ $FREESPACE -lt $NEEDED_SPACE ]]; then
dialog --msgbox "ERROR: Insufficient free space in disk /dev/$SLX_CHOOSEN_DISK." 6 40
exit 1
fi
if [[ $NONEXISTENT_PARTITIONS -eq 0 ]]; then
DIALOGSTR="All your requested partitions already exists.\n\n"
DIALOGSTR=$DIALOGSTR"Partition ID\n"
for (( i = 1; i < $COUNTER; i++ )); do
if [[ $i -ne 4 ]]; then
DIALOGSTR=$DIALOGSTR"${PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]} ${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}\n"
fi
done
else
DIALOGSTR="Creating partitions...\n\n"
echo "0" | dialog --gauge "$DIALOGSTR" 0 0 0
CREATED=0
for (( i = 1; i < $COUNTER; i++ )); do
j=1
if [[ $i -ne 4 ]]; then
if [[ "${PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]}" = "0" ]]; then
sgdisk /dev/$SLX_CHOOSEN_DISK -n $j:0:+${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}G 2> /dev/null > /dev/null
while [[ $? -eq 4 ]]; do
j=$(($j+1))
sgdisk /dev/$SLX_CHOOSEN_DISK -n $j:0:+${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}G 2> /dev/null > /dev/null
done
PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]=$SLX_CHOOSEN_DISK$j
SIZE=$((${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}*1024*1024*1024))
ID=${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}
MOUNTPOINT=${PARTTBL["$SLX_CHOOSEN_DISK$i/mountpoint"]}
DIALOGSTR=$DIALOGSTR$MOUNTPOINT" (/dev/"${PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]}")\n"
if [ "${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}" = "82" ]; then
DIALOGSTR=$DIALOGSTR" GUID: 0657FD6D-A4AB-43C4-84E5-0933C84B4F4F\n"
elif [ "${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}" = "83" ]; then
DIALOGSTR=$DIALOGSTR" GUID: 0FC63DAF-8483-4772-8E79-3D69D8477DE4\n"
else
sgdisk /dev/$SLX_CHOOSEN_DISK -t $j:$ID"000000-0000-0000-0000-000000000000" -c $j:$MOUNTPOINT >/dev/null 2>/dev/null
DIALOGSTR=$DIALOGSTR" GUID: "$ID"000000-0000-0000-0000-000000000000\n"
fi
DIALOGSTR=$DIALOGSTR" Size: "$SIZE" Bytes ("${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}" GB)\n"
CREATED=$(($CREATED+1))
echo $(( $CREATED*100/($NONEXISTENT_PARTITIONS) )) | dialog --gauge "$DIALOGSTR" 0 0 0
fi
if [ ${PARTTBL["$SLX_CHOOSEN_DISK$i/bootable"]} -eq 1 ]; then
sgdisk /dev/$SLX_CHOOSEN_DISK -A $j:set:2 >/dev/null 2>/dev/null
fi
fi
done
fi
else
# finding existent partitions in MS-DOS
echo 'MSDOS'
for PART in $PARTS; do
for (( i = 1; i < $COUNTER; i++ )); do
if [ $i -ne 4 ]; then
if [ ${PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]} = 0 ]; then
sfdisk -d /dev/$SLX_CHOOSEN_DISK 2> /dev/null | grep $PART | tr -s ' ' | cut -d ' ' -f7 | grep ${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]} > /dev/null
if [ $? -eq 0 ]; then
echo "the partition $PART is the ${PARTTBL["$SLX_CHOOSEN_DISK$i/mountpoint"]} partition."
PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]=$PART
NEEDED_SPACE=$(($NEEDED_SPACE-${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}*1024*1024*1024))
break
fi
fi
fi
done
done
echo "FREESPACE="$FREESPACE
echo "NEEDED_SPACE="$NEEDED_SPACE
if [[ $FREESPACE -lt $NEEDED_SPACE ]]; then
dialog --msgbox "ERROR: Insufficient free space in disk /dev/$SLX_CHOOSEN_DISK." 6 40
exit 1
fi
fi
DIALOGSTR=${DIALOGSTR/Creating Partitions.../New partitions created:}
dialog --msgbox "$DIALOGSTR" 0 0
|