ACC SHELL

Path : /usr/share/YaST2/modules/
File Upload :
Current File : //usr/share/YaST2/modules/AutoinstPartition.ycp

/**
 * File:	modules/AutoinstCommon.ycp
 * Package:	Auto-installation/Partition
 * Summary:	Partition related functions module
 * Author:	Sven Schober (sschober@suse.de)
 *
 * $Id: AutoinstPartition.ycp 2813 2008-06-12 13:52:30Z sschober $
 */
{
  module "AutoinstPartition";

  include "autoinstall/types.ycp";
  include "autoinstall/common.ycp";
  include "autoinstall/tree.ycp";

  import "AutoinstCommon";
  import "Partitions";
  import "FileSystems";

  textdomain "autoinst";

  /**
   * defines data structur of a partition
   * provides types for type checking
   */
  define PartitionT fields =
    $[
    "crypt"	: "",
    "crypt_fs"	: false,
    "crypt_key" : "",
    "create"	: true,
    "mount"	: "/",
    "fstopt"	: "",
    "label"	: "",
    "loop_fs"	: false,
    "uuid"	: "",
    "size"	: "10G",
    "format"	: true,
    "filesystem" :  Partitions::DefaultFs(),
    "partition_nr"  : 1,
    "partition_id"  : 131,
    "mountby"	: `device,
    "resize"	: false,
    "lv_name"	: "",
    "stripes"   : 1,
    "stripesize" : 4,
    "lvm_group" : "",
    "raid_name" : "",
    "raid_type" : "",
    "raid_options" : $[]
    ];

  define map<symbol,map> allfs = $[];

  define list<string> defaultMountPoints = [ "/", "/boot", "/home", "/usr", "/usr/local", "swap", "/tmp" ];
  define list<string> allUnits = [ "Byte", "MB", "GB", "%", "max" ];
  define integer maxPartitionNumber = 999;

  global define void AutoinstPartition() {
    allfs = FileSystems::GetAllFileSystems(true, true);
  }
  /**
   * Convenience wrappers for more general object predicates
   */
  global define boolean isPartition( PartitionT partition ){
    return AutoinstCommon::isValidObject( fields, partition );
  }
  global define boolean isField( string field ){
    return AutoinstCommon::isValidField( fields, field );
  }
  global define boolean hasValidType( string field, any value ){
    return AutoinstCommon::hasValidType( fields, field, value );
  }
  global define boolean areEqual( PartitionT p1, PartitionT p2 ){
    return AutoinstCommon::areEqual( p1, p2 );
  }

  /**
   * Convenience wrapper for setter 
   */
  global define PartitionT set( PartitionT partition, string field, any value){
    return AutoinstCommon::set( fields, partition, field, value );
  }


  /**
   * Constructor
   */
  global define PartitionT new( string mp ){
    return set(fields, "mount", mp );
  }

  global define string  getNodeReference(  string parentRef,  integer partitionIndex )
  {
    /* e.g.: parentRef = "drive:0" */
    string driveID = substring( parentRef, findfirstof(parentRef, "_") + 1);
    string idx = tostring( partitionIndex );
    return (sformat( "part_%1_%2", driveID, idx));
  }

  global define string getPartitionDescription( PartitionT p, boolean enableHTML ){
      string part_desc = p["mount"]:"";
      if( "" == part_desc ){
	part_desc = p["lvm_group"]:"";
	if( enableHTML ) {
	  part_desc = sformat( "Physical volume for volume group &lt;<b>%1</b>&gt;", part_desc );
	}
	else{
	  part_desc = sformat( "<%1>", part_desc );
	}
      }
      else{
	if( enableHTML ){
	  part_desc = sformat( "<b>%1</b> Partition", part_desc );
	}
      }
      if( p["create"]:false ){
	part_desc = sformat( "%1 with %2", part_desc, p["size"]:"");
      }
      else{
	if( p["resize"]:false ){
	  part_desc = sformat( "%1 resize part.%2 to %3", part_desc, p["partition_nr"]:999, p["size"]:"" );
	}
	else{
	  part_desc = sformat( "%1 reuse part. %2", part_desc,	p["partition_nr"]:999 );
	}
      }
      part_desc = sformat( "%1,%2", part_desc,Partitions::FsIdToString(p["partition_id"]:131));

      map fs = allfs[p["filesystem"]:`nothing]:$[];
      string fs_name = fs[`name]:"";
      if( "" != fs_name ){
	part_desc = sformat("%1,%2", part_desc, fs_name);
      }

      if ( size( p["region"]:[] ) > 0 )
      {
	string reg_info = sformat("%1 - %2", p["region",0]:0, p["region",1]:0);
	part_desc = sformat("%1,%2",part_desc, reg_info);
      }
      return part_desc;
  }

  global define term
    createTree( PartitionT p, string parentRef, integer idx ){
      string part_desc = getPartitionDescription( p, false );
      return createTreeNode( getNodeReference(parentRef, idx), part_desc, [] );
    }

    define list<string>
      getTokenizedSize( PartitionT part ){
      string encodedSize = part["size"]:"";
      /* regex tokenizes size strings of the liking:
       * "1000", "10KB", "1 GB" and "max"
       */
      return regexptokenize( encodedSize, "([0-9]*)\s*([A-Za-z%]*)" );
    }

    global define integer
      getSize( PartitionT part ){
      list<string> tokenizedSize = getTokenizedSize( part );
      string sizeString = tokenizedSize[0]:"0";
      string unitString = tokenizedSize[1]:"";
      if( "auto" == unitString ||
	  "max" == unitString  ||
	  "" == sizeString	  ) {
	sizeString = "0";
      }
      return tointeger( sizeString ) ;
    }
    global define string
      getUnit( PartitionT part ){
      if( "max" == part["size"]:"" ||
	  "auto" == part["size"]:"" ){
	return part["size"]:"";
      }
      else{
	return getTokenizedSize( part )[1]:"";
      }
    }
    global define boolean
      getFormat( PartitionT part ){
      return part["format"]:false;
    }
    global define boolean
      isPartOfVolgroup( PartitionT part ){
      return part["lvm_group"]:"" != "";
    }
    global define symbol
      getFileSystem( PartitionT part ){
      return part["filesystem"]:`Empty;
    }
    global define PartitionT
      parsePartition( map part ){
      PartitionT newPart = new( part["mount"]:"" );
      newPart = set( newPart, "mountby", part["mountby"]:`device );
      newPart = set( newPart, "create", part["create"]:true );
      newPart = set( newPart, "crypt", part["crypt"]:"" );
      newPart = set( newPart, "crypt_fs", part["crypt_fs"]:false);
      newPart = set( newPart, "crypt_key", part["crypt_key"]:"" );
      newPart = set( newPart, "format", part["format"]:true );
      if( haskey( part, "filesystem" ) ){
	newPart = set( newPart, "filesystem", part["filesystem"]:`Empty );
	newPart = set( newPart, "format", true);
      }
      newPart = set( newPart, "loop_fs", part["loop_fs"]:false );
      newPart = set( newPart, "partition_id", part["partition_id"]:131 );
      if( haskey( part, "partition_nr" ) )
	      newPart = set( newPart, "partition_nr", part["partition_nr"]:0 );
      else
	      newPart = remove( newPart, "partition_nr" );
      newPart = set( newPart, "size", part["size"]:"" );
      newPart = set( newPart, "lv_name", part["lv_name"]:"" );
      newPart = set( newPart, "lvm_group", part["lvm_group"]:"" );
      newPart = set( newPart, "stripes", part["stripes"]:1 );
      newPart = set( newPart, "stripesize", part["stripesize"]:4 );
      if( haskey( part, "fstopt" ) ) {
          newPart = set( newPart, "fstopt", part["fstopt"]:"defaults" );
      }
      if( part["stripes"]:1 == 1 ) {
        newPart = remove( newPart, "stripes" );
        newPart = remove( newPart, "stripesize" );
      }
      /* partition_id enforcement */
      if( haskey( part, "lvm_group" ) ){
	newPart = set( newPart, "partition_id", 142 );
      }
      else if( "swap" == newPart["mount"]:"" ){
	newPart = set( newPart, "partition_id", 130 );
      }
      if( haskey( part, "raid_name" ) ) {
          newPart = set( newPart, "raid_name", part["raid_name"]:"" );
          newPart = set( newPart, "raid_type", part["raid_type"]:"raid0" );
      }
      if( haskey( part, "raid_options" ) ) {
          newPart = set( newPart, "raid_options", part["raid_options"]:$[] );
      }
      return newPart;
    }

    /*
     * Export filtering
     */
    global define PartitionT
      exportPartition( PartitionT part ){
      /* filter out empty string attributes */
      PartitionT result = $[];
      result = (map<string,any>) filter( string key, any value, part, {
	if( is( value, string) && "" == value ){
	    /* false gets filtered out */
	    return false;
	}
	else if( is( value, symbol) && `Empty == value ){
	    return false;
	}
	else{
	  return true;
	}
      });
      return result;
    }

    global define string
      checkSanity( PartitionT part ){
      string result = "";
      if( "%" == getUnit( part ) &&
	  0  == getSize( part ) ){
	  result =
	    sformat( "%1You chose '%%' as size unit but did not provide a correct percentage value.\n", result );
      }
      /* ...
       * other tests maybe
       * ...
       */
      return result;
    }

    /*
     * "static" functions (don't need a PartitionT [this pointer])
     */
    global define list<string>
      getAllUnits(){
      return allUnits;
    }
    global define map<symbol,map>
      getAllFileSystemTypes(){
      return allfs;
    }
    global define list<string>
      getDefaultMountPoints(){
      return defaultMountPoints;
    }
    global define integer
      getMaxPartitionNumber(){
      return maxPartitionNumber;
    }

    global define string
      getLVNameFor( string mountPoint ){
	string result = removePrefix( mountPoint, "/" );
	if( "" == result ){
	  result = "root";
	}
	return result;
    }
}

ACC SHELL 2018