Source

vlocity-vinstall-gambas / Vlocity / Step3a2.class

  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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
' Gambas class file
'    This file is part of vinstall-ng

'    vinstall-ng is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    vinstall-ng  is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with vinstall-ng.  If not, see <http://www.gnu.org/licenses/>.

PRIVATE lLargest AS Long
PRIVATE lTotalRam AS Long
PRIVATE lNeededSwap AS Long
PRIVATE sLargestDev AS String
PRIVATE objTargetOptions AS Object[]

PUBLIC SUB Form_Open()

  ME.tlbanner.Text = "<h2>" & ("Install") & Space(1) & Main.Install.Name & Space(1) & ("to free space on disk") & "</H2>"
  ME.MovieBox1.Path = "images/throbber.gif"
  ME.MovieBox1.Playing = FALSE
  'objTargetOptions = NEW Object[]
  
  'ME.LIST_FREE_SPACE()

END

PUBLIC SUB LIST_FREE_SPACE()
  
  DIM sDiscs, sLine AS String[]
  DIM sDisc, sDump, sFreeDump AS String
  DIM lFreeSpace AS Float ' free space in KB
  DIM lTotalFreeKb, lFreeBegin, lFreeEnd AS Long
  DIM sFreeBeinRaw, sFreeEndRaw AS String
  DIM i AS Integer
  'DIM lTotalRam AS Long
  DIM rb AS RadioButton
  
  ME.tlSelAutoTarget.Text = ("Searching for unused space on your hard drive... please wait")

  'objTargetOptions = NEW Object[]

  PRINT " -*- Determing total RAM available ..."
    SHELL "free | grep -i \"^Mem\" | tr -s \' \',\"\" | cut -f 2 -d \' \'" TO sDump
      lTotalRam = Trim(sDump)
      IF lTotalRam < 512000 THEN 
        lNeededSwap = lTotalRam * 2
      ELSE 
        lNeededSwap = lTotalRam
      END IF
      'Message(lNeededSwap)
    PRINT " -+- Total RAM Available : " & lTotalRam & " KB"
    PRINT " -*- Calculating available free space"
  
    SHELL "probedisk | grep -v cdrom | cut -f 1 -d \'|\'" TO sDump
      sDump = Trim(sDump)
        IF NOT sDump THEN  ' EXIT IF NO HARD DRIVES ARE FOUND
          Message.Error(("No hard disks found on your system. Please select") & "<br>" &
          ("Another partitioning option"))
          RETURN 
        END IF
        
    sDiscs = Split(sDump, "\n")
      FOR i = 0 TO sDiscs.Max

        sDisc = Trim(sDiscs[i])
          ' get the free space from parted
          SHELL "parted " & Trim(sDisc) & " -s \'print free\' | grep -i \"free space\" | tr -s \' \',\"\"" TO sDump
            sDump = Trim(sDump)
              IF NOT sDump THEN CONTINUE 
            ' free space should be on the 3rd column, but it start with a blank space, so it will be 4th
            sLine = Split(sDump, Space(1))
              sFreeDump = Trim(sLine[2])
              sFreeBeinRaw = Trim(sLine[0]) ' Beginning of free space
              sFreeEndRaw = Trim(sLine[1]) ' End of free space
              lFreeSpace = Left(sFreeDump, Len(sFreeDump) - 2) ' take away the KB,MB,GB,TB off the result
                ' fix the units by converting everything to kb
                lTotalFreeKb = ME.ConvertToKb(sFreeDump)
'              Message("Free space on " & sDisc & " begins at " & ME.ConvertToKb(sFreeBeinRaw) & "kb and ends at " & ME.ConvertToKb(sFreeEndRaw) & " kb")
              
              'Message(sFreeDump)
                IF lTotalFreeKb > lLargest THEN 
                   lLargest = lTotalFreeKb
                   sLargestDev = Trim(sDisc)
                    FOR EACH RadioButton1 IN objTargetOptions
                      RadioButton1.Text = Replace(RadioButton1.Text, ("(Recomended)"), "")
                    NEXT 
                END IF
                    PRINT " -+- Found " & lTotalFreeKb & " KB in " & sDisc
                    
                ME.RadioButton1 = NEW RadioButton(ME.hpanel3) AS "TargetOptions"
                  IF (lTotalFreeKb - lNeededSwap) > 4194304 THEN 
                    WITH ME.RadioButton1
                      .Enabled = TRUE
                        IF sLargestDev = sDisc THEN 
                          .Text = sDisc & Space(1) & "-" & Space(1) & lTotalFreeKb & Space(1) & "Kb" & Space(1) & ("of free space") & Space(1) & ("(Recomended)")
                          .Value = TRUE
                        ELSE 
                          .Text = sDisc & Space(1) & "-" & Space(1) & lTotalFreeKb & Space(1) & "Kb" & Space(1) & ("of free space")
                        END IF
                      .Expand = TRUE
                      .Height = 21
                      .Tag = sDisc
                    END WITH 
                  ELSE 
                    WITH ME.RadioButton1
                      .Enabled = FALSE
                      .Text = sDisc & Space(1) & "-" & Space(1) & lTotalFreeKb & Space(1) & "Kb" & Space(1) & ("of free space")
                      .Expand = TRUE
                      .Height = 21
                      .Tag = sDisc
                    END WITH 
                  END IF
                  IF objTargetOptions.Find(ME.RadioButton1) = -1 THEN objTargetOptions.Add(ME.RadioButton1)
                  ME.HBox2 = NEW HBox(ME.HPanel3) AS "spacers"
                    WITH ME.HBox2
                      .Height = 12
                      .Expand = TRUE
                      .Tag = sDisc
                    END WITH  

  
'NOTES
' root:# parted /dev/hda --script "mkpartfs primary linux-swap -512kb -1kb" ' SAMPLE PARTED LINE
' if ram < 512MB then swap = ram*2
' default / fs = reiserfs





    NEXT 
        
    'Message("Largest unused area found: " & sLargestDev & " with " & lLargest & " kb of unused space")
    PRINT " -++- Largest unused area found in :" & sLargestDev & " totaling " & lLargest & " kb of free space"
  
  ME.tlSelAutoTarget.Text = ("Select the location on your hard drive where you would like to install") & Space(1) & ClsGlobal.sDistroName
  
END





PUBLIC SUB ConvertToKb(sRawString AS String) AS Long
  
  DIM fSize AS Float
  DIM sTrimmed AS Float = Left(sRawString, Len(sRawString) - 2)
  DIM sTotal AS Long
    
    IF Right(sRawString, 2) = "MB" THEN 
      fSize = sTrimmed
      sTotal = fSize * 1024
    ELSE IF Right(sRawString, 2) = "GB" THEN 
      fSize = sTrimmed
      sTotal = (fSize * 1024) * 1024
    ELSE IF Right(sRawString, 2) = "KB" OR Right(sRawString, 2) = "kB" THEN 
      fSize = sTrimmed
      sTotal = fSize
    ELSE IF Right(sRawString, 1) = "B"
      fSize = sTrimmed
      sTotal = fSize
    END IF
    
  RETURN sTotal
    
      
  
  
END


PUBLIC SUB PERFORM_PARTITIONING()
  
  DIM i, iLastPartNo AS Integer
  DIM sDrive, sPartedRes, sFreeStart, sFreeEnd, sFreeSize, sDump AS String
  DIM sOutLine, sDumparr AS String[]
  DIM lFreeKB, lFreeStart, lFreeEnd AS Long
  DIM sRootPart, sSwapPart AS String
  DIM sPart, sNum AS String
  
      
    ME.HPanel3.Visible = FALSE
    ME.HPanel4.Visible = TRUE
    ME.MovieBox1.Playing = TRUE
    ME.tlSelAutoTarget.Text = ("Please wait while your hard drive is being partitioned")
    
      FOR EACH RadioButton1 IN objTargetOptions
        IF RadioButton1.Value = TRUE THEN 
          IF RadioButton1.Enabled = TRUE THEN 
          sDrive = RadioButton1.Tag
          END IF
        END IF
      NEXT 
      'Message(sDrive)
      WAIT 1
      IF NOT sdrive THEN 
        Message.Error(("Unable to find enough un-used space to perform auto-partition.") & gb.NewLine &
        ("Please consider using a different partitioning option."))
          ME.MovieBox1.Playing = FALSE
          ME.tlSelAutoTarget.Text = ("Unable to find enough un-used space to perform auto-partition. Please consider using a different partitioning option.")
          ME.MovieBox1.Enabled = FALSE
        RETURN 
      END IF
    ' read from parted again
    SHELL "parted " & Trim(sDrive) & " -s \'unit kb print free\' | grep -i \"free space\" | tr -s \' \',\"\" | cut -f 2-4 -d \' \'" TO sPartedRes
      'Message(sPartedRes)
      sPartedRes = Trim(sPartedRes) ' | START | | END | | SIZE |
        sOutLine = Split(sPartedRes, Space(1))
          sFreeStart = sOutLine[0]
          sFreeEnd = sOutLine[1] 
          sFreeSize = sOutLine[2]
      ' I need to know where the free space stars and ends to do this.
      lFreeKB = ME.ConvertToKb(sFreeSize)
      lFreeStart = ME.ConvertToKb(sFreeStart)
      lFreeEnd = ME.ConvertToKb(sFreeEnd)
      ' keep in mind that only partitions 1-4 can be primary... everything else must be extended
      ' I need to use probepart to see the number of the last partition present in the drive
      ' And figure out the partition numbers for the future partitions.
      SHELL "probepart | grep \"^" & Trim(sDrive) & "\" | tr -s \' \',\"\" | cut -f 1 -d \' \'" TO sDump
        'Message(sDump)
        sDump = Trim(sDump)
          IF NOT sDump THEN 'The entire disc IS not partitioned. 'Message("No partitions found ont his disk")
          iLastPartNo = 0
              ' create new partitions on raw space.
              ' root:# parted /dev/hda --script "mkpartfs primary linux-swap -512kb -1kb" ' SAMPLE PARTED LINE
            
              SHELL "parted " & Trim(sDrive) & " -s \"mkpart primary reiserfs 0kb -" & lNeededSwap & "kb\"" WAIT ' partition 1
              'WAIT 1
          SHELL "probepart | grep -i \"linux swap\"| cut -f 1 -d \' \'" TO sDump
                      sDump = Trim(sDump)   ' this is possibly an array
                        IF sDump THEN 
                          sDumparr = Split(sDump, "\n")
                          PRINT " ---+ Use existing swap partition -> " & sDumparr[0]
                          sSwapPart = Trim(sDumparr[0])
                        
                            
                        ELSE 
                            SHELL "parted " & Trim(sDrive) & " -s \"mkpart primary linux-swap -" & lNeededSwap & "kb -1kb\"" WAIT ' partition 2
                            SHELL "mkswap " & Trim(sDrive) & (iLastPartNo + 2) WAIT ' make the swap partition
                            EXEC ["swapon", Trim(sDrive) & (iLastPartNo + 2)] WAIT 
                              sSwapPart = Trim(sdrive) & (iLastPartNo + 2)      
                        END IF
          sRootPart = Trim(sdrive) & (iLastPartNo + 1)
          
                ' Ready to start setting defined target partitions.
                '###################################################################################################################
              
          ELSE ' There is already some partitions in the disc.... I need to know which numbers to use.
              sDump = Replace(sDump, sDrive, "")
              sDumparr = Split(sDump, "\n")
                 FOR i = 0 TO sDumparr.Max
                   'Message(sDumparr[i])
                   iLastPartNo = Trim(sDumparr[i])
                 NEXT 
                  ' now that we got the last partition number, we can start creating our partitions.
                        ' if there is room for primary partitions, make them
                      IF iLastPartNo <= 2 THEN 
                         'Message(lFreeEnd - lNeededSwap)
                         'Message(lFreeStart)
                          PRINT "start -> " & lFreeStart
                          PRINT "end -> " & lFreeEnd
                          PRINT "Needed swap -> " & lNeededSwap
                          PRINT "Beginning of swap area -> " & (lFreeEnd - lNeededSwap)
                          
                            'PRINT "Debug: " & "parted " & Trim(sDrive) & " -s \"mkpart primary reiserfs " & lFreeStart & "kb " & (lFreeEnd - lNeededSwap) & "kb\""
                          SHELL "parted " & Trim(sDrive) & " -s \"mkpart primary reiserfs " & lFreeStart & "kb " & (lFreeEnd - lNeededSwap) & "kb\"" WAIT 'iLastpart + 1 partition
                          EXEC ["mkfs.reiserfs", "-fq", sDrive & (iLastPartNo + 1)] WAIT 
                            PRINT " --- Created / partition preformatted to reiserfs " & Trim(sdrive) & (iLastPartNo + 1)
                       
                       SHELL "probepart | grep -i \"linux swap\"| cut -f 1 -d \' \'" TO sDump
                      sDump = Trim(sDump)   ' this is possibly an array
                        IF sDump THEN 
                          sDumparr = Split(sDump, "\n")
                          PRINT " ---+ Use existing swap partition -> " & sDumparr[0]
                          sSwapPart = Trim(sDumparr[0])
                        
                            
                        ELSE 
                       
                       
                          SHELL "parted " & Trim(sdrive) & " -s \"mkpart primary linux-swap " & (lFreeEnd - lNeededSwap) & "kb " & lFreeEnd & "kb\"" WAIT ' ilastpart + 2 partitiong
                            PRINT " --- Created swap partition " & Trim(sdrive) & (iLastPartNo + 2)
                          SHELL "mkswap " & Trim(sdrive) & (iLastPartNo + 2) WAIT 
                          SHELL "swapon " & Trim(sdrive) & (iLastPartNo + 2) WAIT 
                          sSwapPart = Trim(sdrive) & (iLastPartNo + 2)
                        END IF
                          sRootPart = Trim(sdrive) & (iLastPartNo + 1)
                          
                          
                 ELSE ' this method skips one number I think 
                      ' BEGIN BY CREATING AN EXTENDED PARTITION TO HOLD LOGICAL PARTITIONS IN IT
                      ' THIS WILL ADD 1 TO THE iLastPartition number 
                        SHELL "parted " & Trim(sdrive) & " -s \"mkpart extended " & lFreeStart & "kb " & lFreeEnd & "kb\"" WAIT 
                          ' I need to re-calculate the free space after this... the extended partition shaves a hair off the free space, so that can throw the whole thing off
                            SHELL "parted " & Trim(sDrive) & " -s \'unit kb print free\' | grep -i \"free space\" | tr -s \' \',\"\" | cut -f 2-4 -d \' \'" TO sPartedRes
                                          'Message(sPartedRes)
                                    sPartedRes = Trim(sPartedRes) ' | START | | END | | SIZE |
                                      sOutLine = Split(sPartedRes, Space(1))
                                        sFreeStart = sOutLine[0]
                                        sFreeEnd = sOutLine[1] 
                                        sFreeSize = sOutLine[2]
                                    ' I need to know where the free space stars and ends to do this.
                                    lFreeKB = ME.ConvertToKb(sFreeSize)
                                    lFreeStart = ME.ConvertToKb(sFreeStart)
                                    lFreeEnd = ME.ConvertToKb(sFreeEnd)
                            
                            ' PROCEED TO CREATE PARTITIONS IN NEWLY CALCULATED FREE SPACE
                          SHELL "parted " & Trim(sDrive) & " -s \"mkpart logical reiserfs " & lFreeStart & "kb " & (lFreeEnd - lNeededSwap) & "kb\"" WAIT 
                          'SHELL "echo yes | parted " & Trim(sdrive) & " mkfs reiserfs " & (iLastPartNo + 2) WAIT 
                          SHELL "mkfs.reiserfs -fq " & sDrive & (iLastPartNo + 2) WAIT 
                        
                        ' search for existing swap partitions
                      SHELL "probepart | grep -i \"linux swap\"| cut -f 1 -d \' \'" TO sDump
                      sDump = Trim(sDump)   ' this is possibly an array
                        IF sDump THEN 
                          sDumparr = Split(sDump, "\n")
                          PRINT " ---+ Use existing swap partition -> " & sDumparr[0]
                          sSwapPart = Trim(sDumparr[0])
                        
                            
                        ELSE 
                                                  
                          ' ####################### RECALCULATE FREE SPACE ########################################
                            SHELL "parted " & Trim(sDrive) & " -s \'unit gb print free\' | grep -i \"free space\" | tr -s \' \',\"\" | cut -f 2-4 -d \' \'" TO sPartedRes
                                          'Message(sPartedRes)
                                    sPartedRes = Trim(sPartedRes) ' | START | | END | | SIZE |
                                      sOutLine = Split(sPartedRes, Space(1))
                                        sFreeStart = sOutLine[0]
                                        sFreeEnd = sOutLine[1] 
                                        sFreeSize = sOutLine[2]
                            SHELL "parted " & Trim(sdrive) & " -s \'mkpart logical linux-swap " & sFreeStart & " " & sFreeEnd & "\'" WAIT 
                            
                            
                                ' ^^^^^^^^ IN CASE THE ABOVE LINE DOESN'T WORK, THE FOLLOWING CODE MAY BE USED TO PERFORM THE SAME OPERATION ^^^^^^^^^^
                            
                            ' ' ' ' ' sDump = "#!/bin/bash \n" &
                            ' ' ' ' ' "parted " & Trim(sdrive) & " mkpart logical linux-swap " & sFreeStart & " " & sFreeEnd & " \n" &
                            ' ' ' ' ' "<<EOF \n" &
                            ' ' ' ' ' "yes \n" &
                            ' ' ' ' ' "EOF"
                            ' ' ' ' ' File.Save("/tmp/swapmaker", sDump)
                            ' ' ' ' ' SHELL "sh /tmp/swapmaker" WAIT 
                            sSwapPart = Trim(sdrive) & (iLastPartNo + 3)
                        END IF
                          sRootPart = Trim(sdrive) & (iLastPartNo + 2)
                          
                      END IF
                    
          END IF
    
    
    ME.tlSelAutoTarget.Text = ("Partitioning stage complete... Click") & Space(1) & ClsGlobal.fRunningMainForm.btnext.text & Space(1) & ("to continue")
     ME.MovieBox1.Playing = FALSE
     
     ' set the flag for partition reading
     ClsGlobal.bAutoPartition = TRUE
     
     sPart = Left(sRootPart, Len("/dev/hda"))
     sNum = Right(sRootPart, Len(sRootPart) - Len(sPart))
         
      ' get the size of the / partition that was just created.
      SHELL "parted " & sPart & " -s \'unit GB print\' | grep \"^ " & sNum & "\" | tr -s \' \',\"\" | cut -f 5 -d \' \'" TO sDump      
      
      sDump = Trim(sDump)      
      ClsGlobal.sTargetRoot = sRootPart & "-" & Trim(sDump)
      Main.Install.Partitions.Add(sRootPart & "-" & Trim(sDump) & "-/-reiserfs")
     
     ' get the size of the swap partition that was just created.
        sPart = Left(sSwapPart, Len("/dev/hda"))
        sNum = Right(sSwapPart, Len(sSwapPart) - Len(sPart))
      SHELL "parted " & sPart & " -s \'unit GB print\' | grep \"^ " & sNum & "\" | tr -s \' \',\"\" | cut -f 5 -d \' \'" TO sDump        
        sDump = Trim(sDump)
     ClsGlobal.sTargetSwap = sSwapPart & "-" & Trim(sDump)
     Main.Install.Partitions.Add(sSwapPart & "-" & Trim(sDump) & "-swap-linux-swap")
      
      
     
END




PUBLIC FUNCTION GETNEXT() AS String
  
  RETURN "Step7"
  
END
PUBLIC SUB STARTUP()
  

  DIM i AS Integer
  
      objTargetOptions = NEW Object[]
  ME.LIST_FREE_SPACE()
  
    FOR EACH RadioButton1 IN objTargetOptions
      IF RadioButton1.Enabled = TRUE THEN INC i
    NEXT 
  
  IF i < 1 THEN 
    Message.Error(("We are unable to find any hard disks with enough unpartitioned space for this operation.") & "<br>" &
    ("Please consider using another partitioning method."))
    
    ME.tlSelAutoTarget.Text = ("Unable to find any hard drives with enough unpartitioned space. Please go back and select  another partitioning method")
  END IF
  
  
END



PUBLIC SUB ONEXIT()
  
  ' CREATE THE PARTITIONS NOW
  ME.PERFORM_PARTITIONING()
  
END

PUBLIC SUB Form_Resize()

  ME.hpanel3.Height = ME.ClientH - ME.HPanel3.Top - 12
  ME.HPanel4.Height = ME.HPanel3.Height

END

PUBLIC SUB Form_Menu()

  'ME.PERFORM_PARTITIONING()

END