home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / lib / partman / recipes.sh < prev    next >
Encoding:
Text File  |  2006-12-12  |  8.8 KB  |  409 lines

  1.  
  2. # If you are curious why partman-auto is so slow, it is because
  3. # update-all is slow
  4. update_all () {
  5.     local dev num id size type fs path name partitions
  6.     for dev in $DEVICES/*; do
  7.     [ -d "$dev" ] || continue
  8.     cd $dev
  9.     partitions=''
  10.     open_dialog PARTITIONS
  11.     while { read_line num id size type fs path name; [ "$id" ]; }; do
  12.         partitions="$partitions $id"
  13.     done
  14.     close_dialog
  15.     for id in $partitions; do
  16.         update_partition $dev $id
  17.     done
  18.     done
  19. }
  20.  
  21. autopartitioning_failed () {
  22.     db_input critical partman-auto/autopartitioning_failed || true
  23.     db_go || true
  24.     update_all
  25.     exit 1
  26. }
  27.  
  28. unnamed=0
  29.  
  30. decode_recipe () {
  31.     local ignore ram line word min factor max fs -
  32.     ignore=''
  33.     if [ "$2" ]; then
  34.     ignore="${2}ignore"
  35.     fi
  36.     unnamed=$(($unnamed + 1))
  37.     ram=$(grep ^Mem: /proc/meminfo | { read x y z; echo $y; }) # in bytes
  38.     if [ -z "$ram" ]; then
  39.         ram=$(grep ^MemTotal: /proc/meminfo | { read x y z; echo $y; })000
  40.     fi
  41.     ram=$(expr 0000000"$ram" : '0*\(..*\)......$') # convert to megabytes
  42.     name="Unnamed.${unnamed}"
  43.     scheme=''
  44.     line=''
  45.     for word in $(cat $1); do
  46.     case $word in
  47.         :)
  48.         name=$line
  49.         line=''
  50.         ;;
  51.         ::)
  52.         db_metaget $line description
  53.         if [ "$RET" ]; then
  54.             name=$RET
  55.         else
  56.             name="Unnamed.${unnamed}"
  57.         fi
  58.         line=''
  59.         ;;
  60.         .)
  61.         # we correct errors in order not to crash parted_server
  62.         set -- $line
  63.         if expr "$1" : '[0-9][0-9]*$' >/dev/null; then
  64.             min=$1
  65.         elif expr "$1" : '[0-9][0-9]*%$' >/dev/null; then
  66.             min=$(($ram * ${1%?} / 100))
  67.         else # error
  68.             min=2200000000 # there is no so big storage device jet
  69.         fi
  70.         if expr "$2" : '[0-9][0-9]*%$' >/dev/null; then
  71.             factor=$(($ram * ${2%?} / 100))
  72.         elif expr "$2" : '[0-9][0-9]*$' >/dev/null; then
  73.             factor=$2
  74.         else # error
  75.             factor=$min # do not enlarge the partition
  76.         fi
  77.         if [ "$factor" -lt "$min" ]; then
  78.             factor="$min"
  79.         fi
  80.         if expr "$3" : '[0-9][0-9]*$' >/dev/null; then
  81.             max=$3
  82.         elif expr "$3" : '[0-9][0-9]*%$' >/dev/null; then
  83.             max=$(($ram * ${3%?} / 100))
  84.         else # error
  85.             max=$min # do not enlarge the partition
  86.         fi
  87.         if [ "$max" -lt "$min" ]; then
  88.             max="$min"
  89.         fi
  90.         case "$4" in # allow only valid file systems
  91.             ext2|ext3|xfs|reiserfs|jfs|linux-swap|fat16|fat32|hfs)
  92.             fs="$4"
  93.             ;;
  94.             *)
  95.             fs=ext2
  96.             ;;
  97.         esac
  98.         shift; shift; shift; shift
  99.         line="$min $factor $max $fs $*"
  100.  
  101.         # Exclude partitions that have ...ignore set
  102.         if [ "$ignore" ] && [ "$(echo $line | grep "$ignore")" ]; then
  103.             :
  104.         else
  105.             if [ "$scheme" ]; then
  106.             scheme="${scheme}${NL}${line}"
  107.             else
  108.             scheme="$line"
  109.             fi
  110.         fi
  111.         line=''
  112.         ;;
  113.         *)
  114.         if [ "$line" ]; then
  115.             line="$line $word"
  116.         else
  117.             line="$word"
  118.         fi
  119.     esac
  120.     done
  121. }
  122.  
  123. foreach_partition () {
  124.     local - doing IFS partition former last
  125.     doing=$1
  126.     IFS="$NL"
  127.     former=''
  128.     for partition in $scheme; do
  129.     restore_ifs
  130.     if [ "$former" ]; then
  131.         set -- $former
  132.         last=no
  133.         eval "$doing"
  134.     fi
  135.     former="$partition"
  136.     done
  137.     if [ "$former" ]; then
  138.     set -- $former
  139.     last=yes
  140.     eval "$doing"
  141.     fi
  142. }
  143.  
  144. min_size () {
  145.     local size
  146.     size=0
  147.     foreach_partition '
  148.     size=$(($size + $1))'
  149.     echo $size
  150. }
  151.  
  152. factor_sum () {
  153.     local factor
  154.     factor=0
  155.     foreach_partition '
  156.     factor=$(($factor + $2))'
  157.     echo $factor
  158. }
  159.  
  160. partition_before () {
  161.     local num id size type fs path name result found
  162.     result=''
  163.     found=no
  164.     open_dialog PARTITIONS
  165.     while { read_line num id size type fs path name; [ "$id" ]; }; do
  166.     if [ "$id" = "$1" ]; then
  167.         found=yes
  168.     fi
  169.     if [ $found = no ]; then
  170.         result=$id
  171.     fi
  172.     done
  173.     close_dialog
  174.     echo $result
  175. }
  176.  
  177. partition_after () {
  178.     local num id size type fs path name result found
  179.     result=''
  180.     found=no
  181.     open_dialog PARTITIONS
  182.     while { read_line num id size type fs path name; [ "$id" ]; }; do
  183.     if [ $found = yes -a -z "$result" ]; then
  184.         result=$id
  185.     fi
  186.     if [ "$id" = "$1" ]; then
  187.         found=yes
  188.     fi
  189.     done
  190.     close_dialog
  191.     echo $result
  192. }
  193.  
  194. pull_primary () {
  195.     primary=''
  196.     logical=''
  197.     foreach_partition '
  198.         if
  199.             [ -z "$primary" ] \
  200.             && echo $* | grep '\''\$primary{'\'' >/dev/null
  201.         then
  202.             primary="$*"
  203.         else
  204.             if [ -z "$logical" ]; then
  205.                 logical="$*"
  206.             else
  207.                 logical="${logical}${NL}$*"
  208.             fi
  209.         fi'
  210. }
  211.  
  212. setup_partition () {
  213.     local id flags file line
  214.     id=$1; shift
  215.     while [ "$1" ]; do
  216.     case "$1" in
  217.         \$bootable{)
  218.             while [ "$1" != '}' -a "$1" ]; do
  219.             shift
  220.         done
  221.         open_dialog GET_FLAGS $id
  222.         flags=$(read_paragraph)
  223.         close_dialog
  224.         open_dialog SET_FLAGS $id
  225.         write_line "$flags"
  226.         write_line boot
  227.         write_line NO_MORE
  228.         close_dialog
  229.         ;;
  230.         \$*{)
  231.                 while [ "$1" != '}' -a "$1" ]; do
  232.             shift
  233.         done
  234.         ;;
  235.         *{)
  236.         file=${1%?}
  237.         [ -d $id ] || mkdir $id
  238.         case $file in
  239.             */*)
  240.             mkdir -p $id/${file%/*}
  241.             ;;
  242.         esac
  243.         >$id/$file
  244.         shift
  245.         line=''
  246.             while [ "$1" != '}' -a "$1" ]; do
  247.             if [ "$1" = ';' ]; then
  248.             echo "$line" >>$id/$file
  249.             else
  250.             if [ "$line" ]; then
  251.                 line="$line $1"
  252.             else
  253.                 line="$1"
  254.             fi
  255.             fi
  256.             shift
  257.         done
  258.         echo "$line" >>$id/$file
  259.     esac
  260.     shift
  261.     done
  262.     return 0
  263. }
  264.  
  265. get_recipedir () {
  266.     local archdetect arch sub recipedir
  267.  
  268.     if type archdetect >/dev/null 2>&1; then
  269.     archdetect=$(archdetect)
  270.     else
  271.     archdetect=unknown/generic
  272.     fi
  273.     arch=${archdetect%/*}
  274.     sub=${archdetect#*/}
  275.  
  276.     for recipedir in \
  277.     /lib/partman/recipes-$arch-$sub \
  278.     /lib/partman/recipes-$arch \
  279.     /lib/partman/recipes
  280.     do
  281.     if [ -d $recipedir ]; then
  282.         echo $recipedir
  283.         break
  284.     fi
  285.     done
  286. }
  287.  
  288. choose_recipe () {
  289.     local recipes recipedir free_size choices min_size type target
  290.  
  291.     type=$1
  292.     target="$2"
  293.     free_size=$3
  294.     
  295.     # Preseeding of recipes.
  296.     db_get partman-auto/expert_recipe
  297.     if [ -n "$RET" ]; then
  298.     echo "$RET" > /tmp/expert_recipe
  299.     db_set partman-auto/expert_recipe_file /tmp/expert_recipe
  300.     fi
  301.     db_get partman-auto/expert_recipe_file
  302.     if [ ! -z "$RET" ] && [ -e "$RET" ]; then
  303.         recipe="$RET"
  304.     decode_recipe $recipe $type
  305.     if [ $(min_size) -le $free_size ]; then
  306.         return 0
  307.     else
  308.         logger -t partman-auto \
  309.         "Expert recipe too large ($(min_size) > $free_size); skipping"
  310.     fi
  311.     fi
  312.  
  313.     recipedir=$(get_recipedir)
  314.     
  315.     choices=''
  316.     default_recipe=no
  317.     db_get partman-auto/choose_recipe
  318.     old_default_recipe="$RET"
  319.     for recipe in $recipedir/*; do
  320.     [ -f "$recipe" ] || continue
  321.     decode_recipe $recipe $type
  322.     if [ $(min_size) -le $free_size ]; then
  323.         choices="${choices}${recipe}${TAB}${name}${NL}"
  324.         if [ no = "$default_recipe" ]; then
  325.         default_recipe="$recipe"
  326.         fi
  327.         if [ "$old_default_recipe" = "$name" ]; then
  328.         default_recipe="$recipe"
  329.             fi
  330.     fi
  331.     done
  332.     
  333.     if [ -z "$choices" ]; then
  334.        db_input critical partman-auto/no_recipe || true
  335.        db_go || true # TODO handle backup right
  336.        return 1
  337.     fi
  338.  
  339.     db_subst partman-auto/choose_recipe TARGET "$target"
  340.     debconf_select medium partman-auto/choose_recipe "$choices" "$default_recipe"
  341.     if [ "$?" = 255 ]; then
  342.     exit 0
  343.     fi
  344.     recipe="$RET"
  345. }
  346.  
  347. expand_scheme() {
  348.     # Make factors small numbers so we can multiply on them.
  349.     # Also ensure that fact, max and fs are valid
  350.     # (Ofcourse in valid recipes they must be valid.)
  351.     # TODO: if factsum ever becomes zero, we'll crash. This happens when the
  352.     # whole recipe is used up and there's still disk space left to allocate.
  353.     factsum=$(($(factor_sum) - $(min_size)))
  354.     scheme=$(
  355.         foreach_partition '
  356.             local min fact max fs
  357.             min=$1
  358.             fact=$((($2 - $min) * 100 / $factsum))
  359.             max=$3
  360.             fs=$4
  361.             case "$fs" in
  362.                 ext2|ext3|linux-swap|fat16|fat32|hfs)
  363.                     true
  364.                     ;;
  365.                 *)
  366.                     fs=ext2
  367.                     ;;
  368.             esac
  369.            shift; shift; shift; shift
  370.            echo $min $fact $max $fs $*'
  371.     )
  372.  
  373.     oldscheme=''
  374.     while [ "$scheme" != "$oldscheme" ]; do
  375.         oldscheme="$scheme"
  376.         factsum=$(factor_sum)
  377.         unallocated=$(($free_size - $(min_size)))
  378.         if [ $unallocated -lt 0 ]; then
  379.             unallocated=0
  380.         fi
  381.         scheme=$(
  382.             foreach_partition '
  383.                 local min fact max newmin
  384.                 min=$1
  385.                 fact=$2
  386.                 max=$3
  387.                 shift; shift; shift
  388.                 newmin=$(($min + $unallocated * $fact / $factsum))
  389.                 if [ $newmin -le $max ]; then
  390.                     echo $newmin $fact $max $*
  391.                 else
  392.                     echo $max 0 $max $*
  393.                 fi'
  394.         )
  395.     done
  396. }
  397.  
  398. clean_method() {
  399.     for device in $DEVICES/*; do
  400.         [ -d "$device" ] || continue
  401.         cd $device
  402.         open_dialog PARTITIONS
  403.         while { read_line num id size type fs path name; [ "$id" ]; }; do
  404.             rm -f $id/method
  405.         done
  406.        close_dialog
  407.     done
  408. }
  409.