summaryrefslogtreecommitdiffstats
path: root/remote/modules/partitioner/partitioner.sh
blob: f2a7dfc35faa4812ef11516b1118d4cf6e350500 (plain) (blame)
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
#!/bin/bash

# set -e

PARTITIONSPATH="/proc/partitions"
CONFIGPATH="./config"

# BLOCKSIZE=$(sfdisk -l /dev/$CHOSENDISK | 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." 5 40
  exit 1
fi

if [ -z "$SLX_CHOOSEN_DISK" ]; then
  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." 5 40
    exit 1
  fi
fi

# if SLX_PARTITION_TABLE is set then put his content in a associative array
declare -A PARTTBL
COUNTER=1
if [ -n "$SLX_PARTITION_TABLE" ]; then
  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]*")
    PARTTBL["$SLX_CHOOSEN_DISK$COUNTER/mountpoint"]=$3
    if [ -e $4 ]; then
      PARTTBL["$SLX_CHOOSEN_DISK$COUNTER/bootable"]=0
    else
      PARTTBL["$SLX_CHOOSEN_DISK$COUNTER/bootable"]=1
    fi
    # set the partitions to non-existents
    PARTTBL["$SLX_CHOOSEN_DISK$COUNTER/exists"]=0
    COUNTER=$(($COUNTER+1))
  done
  unset IFS
fi
# 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))
PARTSSIZE=0
for (( i = 1; i < $COUNTER; i++ )); do
  PARTSSIZE=$PARTSSIZE+${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}
done
echo "$DISKSIZE -lt $(($PARTSSIZE*1024*1024*1024+$COUNTER*1024*1024))"
if [ $DISKSIZE -lt $(($PARTSSIZE*1024*1024*1024+$COUNTER*1024*1024)) ]; 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" ]; then
  SLX_PARTITION_TYPE=msdos
  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

# asking confirmation to create the partitions
if [ $SLX_AUTOMATIC_PARTITIONING = "no" ]; then
  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?"

  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

# delete partition table
if [ $SLX_DELETE_PARTITION_TABLE = "yes" ]; then
  # :
  sgdisk -Z /dev/$SLX_CHOOSEN_DISK > /dev/null 2> /dev/null

  # 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
  # rm -f /tmp/partitiontable.tmp
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'
  PARTS=$(cat $PARTITIONSPATH | grep -e $SLX_CHOOSEN_DISK[0-9] | tr -s ' ' | cut -d ' ' -f5)  # this line fix the empty PARTS error, continue from here
  for PART in $PARTS; do
    for (( i = 1; i < $COUNTER; i++ )); do
      echo "i=$i PART=$PART"
      if [ $i -ne 4 ]; then
        if [ ${PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]} = 0 ]; then
          echo "sgdisk /dev/$SLX_CHOOSEN_DISK -i ${PART:3} | grep '${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}000000-0000-0000-0000-000000000000'"
          sgdisk /dev/$SLX_CHOOSEN_DISK -i ${PART:3} | grep ${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}"000000-0000-0000-0000-000000000000"
          if [ $? -eq 0 ]; then
            echo "the partition $PART is the ${PARTTBL["$SLX_CHOOSEN_DISK$i/mountpoint"]} partition."
            PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]=$PART
            break
          fi
        fi
      fi
    done
  done
  # FREESPACE needed here
else
# finding existent partitions in MS-DOS
  echo 'MSDOS'
  PARTS=$(cat $PARTITIONSPATH | grep -e $SLX_CHOOSEN_DISK[0-9] | tr -s ' ' | cut -d ' ' -f5)  # this line fix the empty PARTS error, continue from here
  for PART in $PARTS; do
    for (( i = 1; i < $COUNTER; i++ )); do
      echo "i=$i PART=$PART"
      if [ $i -ne 4 ]; then
        if [ ${PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]} = 0 ]; then
          echo "sfdisk -d /dev/$SLX_CHOOSEN_DISK 2> /dev/null | grep $PART | tr -s ' ' | cut -d ' ' -f7 | grep ${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}"
          sfdisk -d /dev/$SLX_CHOOSEN_DISK 2> /dev/null | grep $PART | tr -s ' ' | cut -d ' ' -f7 | grep ${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}
          if [ $? -eq 0 ]; then
            echo "the partition $PART is the ${PARTTBL["$SLX_CHOOSEN_DISK$i/mountpoint"]} partition."
            PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]=$PART
            break
          fi
        fi
      fi
    done
  done
fi

DIALOGSTR="New partitions created:\n\n"
if [ $SLX_PARTITION_TYPE = 'GPT' ]; then
  # create the command (CMD) that will convert the MSDOS type to GPT and change the unique GUIDs
  CMD="sgdisk -g /dev/$SLX_CHOOSEN_DISK"
  for (( i = 1; i < $COUNTER; i++ )); do
    if [ $i -ne 4 ]; then
      if [ ${PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]} = 0 ]; then
        CMD=$CMD" -u "$i":"${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}"000000-0000-0000-0000-000000000000"
      fi
      CMD=$CMD" -c "$i":\""${PARTTBL["$SLX_CHOOSEN_DISK$i/mountpoint"]}"\""
    fi
  done
  CMD=$CMD" > /dev/null"
  $CMD

  for (( i = 1; i < $COUNTER; i++ )); do
    if [ $i -ne 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"   GUID: "$ID"000000-0000-0000-0000-000000000000\n"
      DIALOGSTR=$DIALOGSTR"   Size: "$SIZE" Bytes ("${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}" GB)\n"
    fi
  done
else
  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

dialog --msgbox "$DIALOGSTR" 0 0