Source

vinstall-gambas / src / Partitioner.module

Full commit
' Gambas module 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/>.
PUBLIC Partitions AS NEW Object[]
PUBLIC AutoTarget AS Partition

PUBLIC SUB Locate_partitions_with_free_space()
  
  DIM sPreMountList, sDetailedPremounts, sPartArr, SpARTEDoUT AS String[]
  DIM sDump, sPrx AS String
  DIM i AS Integer
  DIM objPart AS Partition
  
  
  SHELL "df -Th | grep \"^/dev\" | cut -f1 -d \' \'" TO sDump
    sDump = Trim(sDump)
    'Message(sDump)
    sPreMountList = Split(sDump, gb.NewLine)
  SHELL "df -ThB 512 | grep \"^/dev\" | tr -s \' \',\"\"" TO sDump
  sDump = Trim(sDump)
  'Message(sDump)
    'information returned in this format.... using 512b blocks (hint: 1sector=512b)
'    partition    fs        size      used      free      use%  mntpoint
    ' /dev/sdb1 reiserfs   195312224  34823992 160488232  18% /
    ' /dev/sdb2 reiserfs   896447768 297441496 599006272  34% /home
    sDetailedPremounts = Split(Trim(sDump), gb.NewLine)
      FOR i = 0 TO sDetailedPremounts.Max
        sPartArr = Split(sDetailedPremounts[i], Space(1))
          ' create new object
          objPart = NEW Partition
          objPart.BlockDevice = Left(sPartArr[0], Len("/dev/hda"))
          objPart.Number = Right(sPartArr[0], Len(sPartArr[0]) - Len(objPart.BlockDevice))
          objPart.CurrentFileSystem = Trim(sPartArr[1])
          'objPart.Size = sPartArr[2]
          objPart.UsedSpace = sPartArr[3]
          'objPart.FreeSpace = sPartArr[3]
          ' now we want to fill in everything else about this partition we will need to use parted for it
' Number  Start      End        Size       Type     File system  Flags
'  1      63s        18943359s  18943297s  primary  reiserfs     lba  
'  2      18943360s  64292129s  45348770s  primary  reiserfs
          IF objPart.Number < 10 THEN 
            sPrx = Space(1) 
          ELSE 
            sprx = "" ' trick to make up for the trailing space in the output
          END IF
          SHELL "parted " & objPart.BlockDevice & " -s \'unit s print\' | grep \"^" & sPrx & objPart.Number & "\" | tr -s \' \'" TO sDump
          sDump = Trim(sDump)
          'Message(sDump)
          SpARTEDoUT = Split(Trim(sDump), " ")
          objPart.Beginning = Replace(SpARTEDoUT[1], "s", "")
          objPart.Fin = Replace(SpARTEDoUT[2], "s", "")
          objPart.Size = Replace(SpARTEDoUT[3], "s", "")
          objPart.Type = Trim(SpARTEDoUT[4])
          objPart.FreeSpace = (objPart.Size - objPart.UsedSpace)
          
          Partitions.Add(objPart)
  NEXT 
  
  ' And now, for the ones that are not mounted.
  
END

PUBLIC SUB detect_resizable_partitions()
  
  DIM sMounted, sDump, sDump1, sDUmp2, sprx AS String
  DIM i, ii AS Integer
  DIM p AS Partition
  DIM bUmount AS Boolean
  DIM ArrMounted, List, arr1, arr2, arr3 AS String[]
  
    SHELL "probepart | grep \'^/dev\' |grep -v -i \'linux swap'| tr -s \' \' | cut -f1 -d \' \'" TO sDump
      List = Split(Trim(sDump), gb.NewLine)
        FOR i = 0 TO List.Max
          SHELL "df -ThB 512 | grep \'^" & Trim(List[i]) & "\' | tr -s \' \'" TO sDump
          sDump = Trim(sDump)
                  IF Len(sDump) <= 0 THEN 
                    ' This partition is not mounted... let's mounted to get some preliminary information.
                    bUmount = TRUE
                      SHELL "mkdir -p " & Replace(list[i], "/dev", "/mnt") WAIT 
                      SHELL "mount -o ro " & List[i] & Space(1) & Replace(List[i], "/dev", "/mnt") WAIT 
                      SHELL "df -ThB 512 | grep \'^" & Trim(List[i]) & "\' | tr -s \' \'" TO sDump
                   END IF
          IF Len(Trim(sDump)) > 0 THEN 
            arr1 = Split(Trim(sDump), " ")
          ELSE 
            DEBUG "Unable to determine partition information for " & List[i]
            CONTINUE 
          END IF
                ' BY NOW WE SHOULD HAVE SOMETHING LIKE THIS IN THE DUMP
                ' /dev/hda2 reiserfs 45347376 13765800 31581576 31% /
                
          p = NEW Partition
          p.BlockDevice = Left(List[i], Len("/dev/hda"))
          p.CurrentFileSystem = Trim(arr1[1])
          p.UsedSpace = Trim(arr1[3])
          p.Number = Right(List[i], Len(List[i]) - Len(p.BlockDevice))
                ' That's all we need from the df output... the rest will come from probepart
          IF p.Number < 10 THEN 
            sPrx = Space(1) 
          ELSE 
            sprx = "" ' trick to make up for the trailing space in the output
          END IF
          SHELL "parted " & p.BlockDevice & " -s \'unit s print\' | grep \"^" & sPrx & p.Number & "\" | tr -s \' \'" TO sDump
              ' This will produce a dump that looks like this:
              ' Number  Start      End        Size       Type     File system  Flags
              '  1      63s        18943359s  18943297s  primary  reiserfs     lba  
              '  2      18943360s  64292129s  45348770s  primary  reiserfs
            IF Len(Trim(sDump)) > 0 THEN 
              arr2 = Split(Trim(sDump), " ")
            ELSE 
              DEBUG "Unable to find detailed information on " & List[i]
              CONTINUE 
            END IF
            
            p.Beginning = Replace(arr2[1], "s", "")
            p.fin = Replace(arr2[2], "s", "")
            p.Size = Replace(arr2[3], "s", "")
            p.Type = Trim(arr2[4])
            p.CurrentFileSystem = Trim(arr2[5])
            p.FreeSpace = (p.Size - p.UsedSpace)
              IF arr2.Count > 5 THEN 
                FOR ii = 6 TO arr2.Max 
                  p.Flags.Add(arr2[ii])
                NEXT 
              END IF            
          Partitions.Add(p)
      NEXT 
            
          
    
  
  
END