home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / lib / partman / definitions.sh < prev    next >
Encoding:
Text File  |  2006-08-02  |  26.7 KB  |  1,110 lines

  1.  
  2. . /usr/share/debconf/confmodule
  3.  
  4. NBSP=' '
  5. TAB='    '
  6. NL='
  7. '
  8. ORIGINAL_IFS="${ORIGINAL_IFS:-$IFS}"; export ORIGINAL_IFS
  9.  
  10. restore_ifs () {
  11.     IFS="$ORIGINAL_IFS"
  12. }
  13.  
  14. dirname () {
  15.     local x
  16.     x="${1%/}"
  17.     echo "${x%/*}"
  18. }
  19.  
  20. basename () {
  21.     local x
  22.     x="${1%$2}"
  23.     x="${x%/}"
  24.     echo "${x##*/}"
  25. }
  26.  
  27. can_escape () {
  28.     type debconf-escape >/dev/null 2>&1 || return 1
  29.     db_capb
  30.     for cap in $RET; do
  31.         case $cap in
  32.             escape)    return 0 ;;
  33.         esac
  34.     done
  35.     return 1
  36. }
  37.  
  38. maybe_escape () {
  39.     local code saveret
  40.     text="$1"
  41.     shift
  42.     if can_escape; then
  43.         db_capb backup escape
  44.         code=0
  45.         "$@" "$(printf '%s' "$text" | debconf-escape -e)" || code=$?
  46.         saveret="$RET"
  47.         db_capb backup
  48.         RET="$saveret"
  49.         return $code
  50.     else
  51.         "$@" "$text"
  52.     fi
  53. }
  54.  
  55. debconf_select () {
  56.     local IFS priority template choices default_choice default x u newchoices code
  57.     priority="$1"
  58.     template="$2"
  59.     choices="$3"
  60.     default_choice="$4"
  61.     default=''
  62.     # Debconf ignores spaces so we have to remove them from $choices
  63.     newchoices=''
  64.     case $PARTMAN_SNOOP in
  65.         ?*)
  66.             > /var/lib/partman/snoop
  67.             ;;
  68.     esac
  69.     IFS="$NL"
  70.     for x in $choices; do
  71.         local key option
  72.         restore_ifs
  73.         key=$(echo ${x%$TAB*})
  74.         # work around bug #243373
  75.         if [ "$TERM" = xterm -o "$TERM" = bterm ]; then
  76.             debconf_select_lead="$NBSP"
  77.         else
  78.             debconf_select_lead="> "
  79.         fi
  80.         option=$(echo "${x#*$TAB}" | sed 's/ *$//g' | sed "s/^ /$debconf_select_lead/g")
  81.         newchoices="${newchoices:+${newchoices}${NL}}${key}${TAB}${option}"
  82.         if [ "$key" = "$default_choice" ]; then
  83.             default="$option"
  84.         fi
  85.         case $PARTMAN_SNOOP in
  86.             ?*)
  87.                 echo "$key$TAB$option" >> /var/lib/partman/snoop
  88.                 ;;
  89.         esac
  90.     done
  91.     choices="$newchoices"
  92.     u=''
  93.     IFS="$NL"
  94.     # escape the commas and leading whitespace but keep them unescaped
  95.     # in $choices
  96.         for x in $choices; do
  97.                 u="$u, `echo ${x#*$TAB} | sed 's/,/\\\\,/g' | sed 's/^ /\\\\ /'`"
  98.         done
  99.         u=${u#, }
  100.     restore_ifs
  101.     if [ -n "$default" ]; then
  102.             db_set $template "$default"
  103.     fi
  104.     db_subst $template CHOICES "$u"
  105.     code=0
  106.     db_input $priority $template || code=1
  107.     db_go || return 255
  108.     db_get $template
  109.     IFS="$NL"
  110.     for x in $choices; do
  111.         if [ "$RET" = "${x#*$TAB}" ]; then
  112.             RET="${x%$TAB*}"
  113.             break
  114.         fi
  115.     done
  116.     restore_ifs
  117.     return $code
  118. }
  119.  
  120. menudir_default_choice () {
  121.     printf "%s__________%s\n" "$(basename $1/??$2)" "$3" > $1/default_choice
  122. }
  123.  
  124. ask_user () {
  125.     local IFS dir template priority default choices plugin name option
  126.     dir="$1"; shift
  127.     template=$(cat $dir/question)
  128.     priority=$(cat $dir/priority)
  129.     if [ -f $dir/default_choice ]; then
  130.     default=$(cat $dir/default_choice)
  131.     else
  132.     default=""
  133.     fi
  134.     choices=$(
  135.     for plugin in $dir/*; do
  136.         [ -d $plugin ] || continue
  137.         name=$(basename $plugin)
  138.         IFS="$NL"
  139.         for option in $($plugin/choices "$@"); do
  140.         printf "%s__________%s\n" $name "$option"
  141.         done
  142.         restore_ifs
  143.     done
  144.     )
  145.     code=0
  146.     debconf_select $priority $template "$choices" "$default" || code=$?
  147.     if [ $code -ge 100 ]; then return 255; fi
  148.     echo "$RET" >$dir/default_choice
  149.     $dir/${RET%__________*}/do_option ${RET#*__________} "$@" || return $?
  150.     return 0
  151. }
  152.  
  153. partition_tree_choices () {
  154.     local IFS
  155.     local whitespace_hack=""
  156.     for dev in $DEVICES/*; do
  157.     [ -d $dev ] || continue
  158.     printf "%s//\t%s\n" $dev "$(device_name $dev)" # GETTEXT?
  159.     cd $dev
  160.  
  161.     open_dialog PARTITIONS
  162.     partitions="$(read_paragraph)"
  163.     close_dialog
  164.     
  165.     IFS="$TAB"
  166.     echo "$partitions" |
  167.     while { read num id size type fs path name; [ "$id" ]; }; do
  168.         part=${dev}/$id
  169.         [ -f $part/view ] || continue
  170.         printf "%s//%s\t     %s\n" "$dev" "$id" $(cat $part/view)
  171.     done
  172.     restore_ifs
  173.     done | while read line; do
  174.         # A hack to make sure each line in the table is unique and
  175.     # selectable by debconf -- pad lines with varying amounts of
  176.     # whitespace.
  177.         whitespace_hack="$NBSP$whitespace_hack"
  178.     echo "$line$whitespace_hack"
  179.     done
  180. }
  181.  
  182. longint_le () {
  183.     local x y
  184.     # remove the leading 0
  185.     x=$(expr "$1" : '0*\(.*\)')
  186.     y=$(expr "$2" : '0*\(.*\)')
  187.     if [ ${#x} -lt ${#y} ]; then
  188.         return 0
  189.     elif [ ${#x} -gt ${#y} ]; then
  190.         return 1
  191.     elif [ "$x" = "$y" ]; then
  192.         return 0
  193.     elif [ "$x" '<' "$y" ]; then
  194.         return 0
  195.     else
  196.         return 1
  197.     fi
  198. }
  199.  
  200. longint2human () {
  201.     local longint suffix bytes int frac deci
  202.     # fallback value for $deci:
  203.     deci="${deci:-.}"
  204.     case ${#1} in
  205.         1|2|3)
  206.         suffix=B
  207.         longint=${1}00
  208.         ;;
  209.         4|5|6)
  210.         suffix=kB
  211.         longint=${1%?}
  212.         ;;
  213.         7|8|9)
  214.         suffix=MB
  215.         longint=${1%????}
  216.         ;;
  217.         10|11|12)
  218.         suffix=GB
  219.         longint=${1%???????}
  220.         ;;
  221.         *)
  222.         suffix=TB
  223.         longint=${1%??????????}
  224.         ;;
  225.     esac
  226.     longint=$(($longint + 5))
  227.     longint=${longint%?}
  228.     int=${longint%?}
  229.     frac=${longint#$int}
  230.     printf "%i%s%i %s\n" $int $deci $frac $suffix
  231. }
  232.  
  233. human2longint () {
  234.     local human suffix int frac longint
  235.     set -- $*; human="$1$2$3$4$5" # without the spaces
  236.     human=${human%b} #remove last b
  237.     human=${human%B} #remove last B
  238.     suffix=${human#${human%?}} # the last symbol of $human
  239.     case $suffix in
  240.     k|K|m|M|g|G|t|T)
  241.         human=${human%$suffix}
  242.         ;;
  243.     *)
  244.         suffix=''
  245.         ;;
  246.     esac
  247.     int="${human%[.,]*}"
  248.     [ "$int" ] || int=0
  249.     frac=${human#$int}
  250.     frac="${frac#[.,]}0000" # to be sure there are at least 4 digits
  251.     frac=${frac%${frac#????}} # only the first 4 digits of $frac
  252.     longint=$(($int * 10000 + $frac))
  253.     case $suffix in
  254.     k|K)
  255.         longint=${longint%?}
  256.         ;;
  257.     m|M)
  258.         longint=${longint}00
  259.         ;;
  260.     g|G)
  261.         longint=${longint}00000
  262.         ;;
  263.     t|T)
  264.         longint=${longint}00000000
  265.         ;;
  266.     *) # no suffix:
  267.         # bytes
  268.         #longint=${longint%????}
  269.         #[ "$longint" ] || longint=0
  270.         # megabytes
  271.         longint=${longint}00
  272.         ;;
  273.     esac
  274.     echo $longint
  275. }
  276.  
  277. valid_human () {
  278.     local IFS patterns
  279.     patterns='[0-9][0-9]* *$
  280. [0-9][0-9]* *[bB] *$
  281. [0-9][0-9]* *[kKmMgGtT] *$
  282. [0-9][0-9]* *[kKmMgGtT][bB] *$
  283. [0-9]*[.,][0-9]* *$
  284. [0-9]*[.,][0-9]* *[bB] *$
  285. [0-9]*[.,][0-9]* *[kKmMgGtT] *$
  286. [0-9]*[.,][0-9]* *[kKmMgGtT][bB] *$'
  287.     IFS="$NL"
  288.     for regex in $patterns; do
  289.         if expr "$1" : "$regex" >/dev/null; then return 0; fi
  290.     done
  291.     return 1
  292. }
  293.  
  294. stop_parted_server () {
  295.     open_infifo
  296.     write_line "QUIT"
  297.     close_infifo
  298. }
  299.  
  300. # Must call stop_parted_server before calling this.
  301. restart_partman () {
  302.     initcount=`ls /lib/partman/init.d/* | wc -l`
  303.     db_progress START 0 $initcount partman/progress/init/title
  304.     for s in /lib/partman/init.d/*; do
  305.     if [ -x $s ]; then
  306.         base=$(basename $s | sed 's/[0-9]*//')
  307.         if ! db_progress INFO partman/progress/init/$base; then
  308.         db_progress INFO partman/progress/init/fallback
  309.         fi
  310.         if ! $s; then
  311.         db_progress STOP
  312.         exit 255
  313.         fi
  314.     fi
  315.     db_progress STEP 1
  316.     done
  317.     db_progress STOP
  318. }
  319.  
  320. update_partition () {
  321.     local u
  322.     cd $1
  323.     open_dialog PARTITION_INFO $2
  324.     read_line part
  325.     close_dialog
  326.     [ "$part" ] || return 0
  327.     for u in /lib/partman/update.d/*; do
  328.     [ -x "$u" ] || continue
  329.     $u $1 $part
  330.     done
  331. }
  332.         
  333. DEVICES=/var/lib/partman/devices
  334.  
  335. # 0, 1 and 2 are standard input, output and error.
  336. # 3, 4 and 5 are used by cdebconf
  337. # 6=infifo
  338. # 7=outfifo
  339.  
  340. open_infifo() {
  341.     exec 6>/var/lib/partman/infifo
  342. }
  343.  
  344. close_infifo() {
  345.     exec 6>&-
  346. }
  347.  
  348. open_outfifo () {
  349.     exec 7</var/lib/partman/outfifo
  350. }
  351.  
  352. close_outfifo () {
  353.     exec 7<&-
  354. }
  355.  
  356. write_line () {
  357.     log IN: "$@"
  358.     echo "$@" >&6
  359. }
  360.  
  361. read_line () {
  362.     read "$@" <&7
  363. }
  364.  
  365. synchronise_with_server () {
  366.     exec 6>/var/lib/partman/stopfifo
  367.     exec 6>&-
  368. }
  369.  
  370. read_paragraph () {
  371.     local line
  372.     while { read_line line; [ "$line" ]; }; do
  373.     log "paragraph: $line"
  374.     echo "$line"
  375.     done
  376. }
  377.  
  378. read_list () {
  379.     local item list
  380.     list=''
  381.     while { read_line item; [ "$item" ]; }; do
  382.     log "option: $item"
  383.     if [ "$list" ]; then
  384.         list="$list, $item"
  385.     else
  386.         list="$item"
  387.     fi
  388.     done
  389.     echo "$list"
  390. }
  391.  
  392. name_progress_bar () {
  393.     echo $1 >/var/lib/partman/progress_info
  394. }
  395.  
  396. error_handler () {
  397.     local exception_type info state frac type priority message options skipped
  398.     while { read_line exception_type; [ "$exception_type" != OK ]; }; do
  399.     log error_handler: exception with type $exception_type
  400.     case "$exception_type" in
  401.         Timer)
  402.         if [ -f /var/lib/partman/progress_info ]; then
  403.             info=$(cat /var/lib/partman/progress_info)
  404.         else
  405.             info=partman/processing
  406.         fi
  407.         db_progress START 0 1000 partman/text/please_wait
  408.         db_progress INFO $info
  409.         while { read_line frac state; [ "$frac" != ready ]; }; do
  410.             if [ "$state" ]; then
  411.             db_subst $info STATE "$state" 
  412.             db_progress INFO $info
  413.             fi
  414.             db_progress SET $frac
  415.         done
  416.         db_progress STOP
  417.         continue
  418.         ;;
  419.         Information)
  420.         type='Information'
  421.         priority=medium
  422.         ;;
  423.         Warning)
  424.         type='Warning!'
  425.         priority=high
  426.         ;;
  427.         Error)
  428.         type='ERROR!!!'
  429.         priority=critical
  430.         ;;
  431.         Fatal)
  432.         type='FATAL ERROR!!!'
  433.         priority=critical
  434.         ;;
  435.         Bug)
  436.         type='A bug has been discovered!!!'
  437.         priority=critical
  438.         ;;
  439.         No?Implementation)
  440.         type='Not yet implemented!'
  441.         priority=critical
  442.         ;;
  443.         *)
  444.         type="??? $exception_type ???"
  445.         priority=critical
  446.         ;;
  447.     esac
  448.     log error_handler: reading message
  449.     message=$(read_paragraph)
  450.     log error_handler: reading options
  451.     options=$(read_list)
  452.     db_subst partman/exception_handler TYPE "$type"
  453.     maybe_escape "$message" db_subst partman/exception_handler DESCRIPTION
  454.     db_subst partman/exception_handler CHOICES "$options"
  455.     if
  456.         expr "$options" : '.*,.*' >/dev/null \
  457.         && db_input $priority partman/exception_handler
  458.     then
  459.         if db_go; then
  460.         db_get partman/exception_handler
  461.         write_line "$RET"
  462.         else
  463.         write_line "unhandled"
  464.         fi
  465.     else
  466.         db_subst partman/exception_handler_note TYPE "$type"
  467.         maybe_escape "$message" db_subst partman/exception_handler_note DESCRIPTION
  468.         db_input $priority partman/exception_handler_note || true
  469.         db_go || true
  470.         write_line "unhandled"
  471.     fi
  472.     done
  473.     rm -f /var/lib/partman/progress_info
  474. }
  475.  
  476. open_dialog () {
  477.     command="$1"
  478.     shift
  479.     open_infifo
  480.     write_line "$command" "${PWD##*/}" "$@"
  481.     open_outfifo
  482.     error_handler
  483. }
  484.  
  485. close_dialog () {
  486.     close_outfifo
  487.     close_infifo
  488.     exec 6>/var/lib/partman/stopfifo
  489.     exec 6>&-
  490.     exec 7>/var/lib/partman/outfifo
  491.     exec 7>&-
  492.     exec 6>/var/lib/partman/stopfifo
  493.     exec 6>&-
  494.     exec 6</var/lib/partman/infifo
  495.     cat <&6 >/dev/null
  496.     exec 6<&-
  497.     exec 6>/var/lib/partman/stopfifo
  498.     exec 6>&-
  499. }
  500.  
  501. log () {
  502.     local program
  503.     echo $0: "$@" >>/var/log/partman
  504. }
  505.  
  506. ####################################################################
  507. # The functions below are not yet documented
  508. ####################################################################
  509.  
  510. # TODO: this should not be global
  511. humandev () {
  512.     local host bus target part lun idenum targtype scsinum linux
  513.     case "$1" in
  514.     /dev/ide/host*/bus[01]/target[01]/lun0/disc)
  515.         host=`echo $1 | sed 's,/dev/ide/host\(.*\)/bus.*/target[01]/lun0/disc,\1,'`
  516.         bus=`echo $1 | sed 's,/dev/ide/host.*/bus\(.*\)/target[01]/lun0/disc,\1,'`
  517.         target=`echo $1 | sed 's,/dev/ide/host.*/bus.*/target\([01]\)/lun0/disc,\1,'`
  518.         idenum=$((2 * $host + $bus + 1))
  519.         linux=$(mapdevfs $1)
  520.         linux=${linux#/dev/}
  521.         if [ "$target" = 0 ]; then
  522.         db_metaget partman/text/ide_master_disk description
  523.         printf "$RET" ${idenum} ${linux}
  524.         else
  525.         db_metaget partman/text/ide_slave_disk description
  526.         printf "$RET" ${idenum} ${linux}
  527.         fi
  528.         ;;
  529.     /dev/ide/host*/bus[01]/target[01]/lun0/part*)
  530.         host=`echo $1 | sed 's,/dev/ide/host\(.*\)/bus.*/target[01]/lun0/part.*,\1,'`
  531.         bus=`echo $1 | sed 's,/dev/ide/host.*/bus\(.*\)/target[01]/lun0/part.*,\1,'`
  532.         target=`echo $1 | sed 's,/dev/ide/host.*/bus.*/target\([01]\)/lun0/part.*,\1,'`
  533.         part=`echo $1 | sed 's,/dev/ide/host.*/bus.*/target[01]/lun0/part\(.*\),\1,'`
  534.         idenum=$((2 * $host + $bus + 1))
  535.         linux=$(mapdevfs $1)
  536.         linux=${linux#/dev/}
  537.         if [ "$target" = 0 ]; then
  538.         db_metaget partman/text/ide_master_partition description
  539.         printf "$RET" ${idenum} "$part" "${linux}"
  540.         else
  541.         db_metaget partman/text/ide_slave_partition description
  542.         printf "$RET" ${idenum} "$part" "${linux}"
  543.         fi
  544.         ;;
  545.     /dev/hd[a-z])
  546.         drive=$(printf '%d' "'$(echo $1 | sed 's,^/dev/hd\([a-z]\).*,\1,')")
  547.         drive=$(($drive - 97))
  548.         linux=${1#/dev/}
  549.         if [ "$(($drive % 2))" = 0 ]; then
  550.         db_metaget partman/text/ide_master_disk description
  551.         else
  552.         db_metaget partman/text/ide_slave_disk description
  553.         fi
  554.         printf "$RET" "$(($drive / 2 + 1))" "$linux"
  555.         ;;
  556.     /dev/hd[a-z][0-9]*)
  557.         drive=$(printf '%d' "'$(echo $1 | sed 's,^/dev/hd\([a-z]\).*,\1,')")
  558.         drive=$(($drive - 97))
  559.         part=$(echo $1 | sed 's,^/dev/hd[a-z]\([0-9][0-9]*\).*,\1,')
  560.         linux=${1#/dev/}
  561.         if [ "$(($drive % 2))" = 0 ]; then
  562.         db_metaget partman/text/ide_master_partition description
  563.         else
  564.         db_metaget partman/text/ide_slave_partition description
  565.         fi
  566.         printf "$RET" "$(($drive / 2 + 1))" "$part" "$linux"
  567.         ;;
  568.     /dev/scsi/host*/bus*/target*/lun*/disc)
  569.         host=`echo $1 | sed 's,/dev/scsi/host\(.*\)/bus.*/target.*/lun.*/disc,\1,'`
  570.         bus=`echo $1 | sed 's,/dev/scsi/host.*/bus\(.*\)/target.*/lun.*/disc,\1,'`
  571.         target=`echo $1 | sed 's,/dev/scsi/host.*/bus.*/target\(.*\)/lun.*/disc,\1,'`
  572.         lun=`echo $1 | sed 's,/dev/scsi/host.*/bus.*/target.*/lun\(.*\)/disc,\1,'`
  573.         scsinum=$(($host + 1))
  574.         linux=$(mapdevfs $1)
  575.         linux=${linux#/dev/}
  576.         db_metaget partman/text/scsi_disk description
  577.         printf "$RET" ${scsinum} ${bus} ${target} ${lun} ${linux}
  578.         ;;
  579.     /dev/scsi/host*/bus*/target*/lun*/part*)
  580.         host=`echo $1 | sed 's,/dev/scsi/host\(.*\)/bus.*/target.*/lun.*/part.*,\1,'`
  581.         bus=`echo $1 | sed 's,/dev/scsi/host.*/bus\(.*\)/target.*/lun.*/part.*,\1,'`
  582.         target=`echo $1 | sed 's,/dev/scsi/host.*/bus.*/target\(.*\)/lun.*/part.*,\1,'`
  583.         lun=`echo $1 | sed 's,/dev/scsi/host.*/bus.*/target.*/lun\(.*\)/part.*,\1,'`
  584.         part=`echo $1 | sed 's,/dev/scsi/host.*/bus.*/target.*/lun.*/part\(.*\),\1,'`
  585.         scsinum=$(($host + 1))
  586.         linux=$(mapdevfs $1)
  587.         linux=${linux#/dev/}
  588.         db_metaget partman/text/scsi_partition description
  589.         printf "$RET" ${scsinum} ${bus} ${target} ${lun} ${part} ${linux}
  590.         ;;
  591.     /dev/sd[a-z]|/dev/sd[a-z][a-z])
  592.         disk="${1#/dev/}"
  593.         if [ -h "/sys/block/$disk/device" ]; then
  594.         bus_id="$(basename "$(readlink "/sys/block/$disk/device")")"
  595.         host="${bus_id%%:*}"
  596.         bus_id="${bus_id#*:}"
  597.         bus="${bus_id%%:*}"
  598.         bus_id="${bus_id#*:}"
  599.         target="${bus_id%%:*}"
  600.         lun="${bus_id#*:}"
  601.         scsinum="$(($host + 1))"
  602.         db_metaget partman/text/scsi_disk description
  603.         printf "$RET" "$scsinum" "$bus" "$target" "$lun" "$disk"
  604.         else
  605.         # Can't figure out host/bus/target/lun without sysfs, but
  606.         # never mind; if we don't have sysfs then we're probably on
  607.         # 2.4 and devfs anyway.
  608.         echo "$1"
  609.         fi
  610.         ;;
  611.     /dev/sd[a-z][0-9]*|/dev/sd[a-z][a-z][0-9]*)
  612.         part="${1#/dev/}"
  613.         disk="${part%%[0-9]*}"
  614.         part="${part#$disk}"
  615.         if [ -h "/sys/block/$disk/device" ]; then
  616.         bus_id="$(basename "$(readlink "/sys/block/$disk/device")")"
  617.         host="${bus_id%%:*}"
  618.         bus_id="${bus_id#*:}"
  619.         bus="${bus_id%%:*}"
  620.         bus_id="${bus_id#*:}"
  621.         target="${bus_id%%:*}"
  622.         lun="${bus_id#*:}"
  623.         scsinum="$(($host + 1))"
  624.         db_metaget partman/text/scsi_partition description
  625.         printf "$RET" "$scsinum" "$bus" "$target" "$lun" "$part" "$disk"
  626.         else
  627.         # Can't figure out host/bus/target/lun without sysfs, but
  628.         # never mind; if we don't have sysfs then we're probably on
  629.         # 2.4 and devfs anyway.
  630.         echo "$1"
  631.         fi
  632.         ;;
  633.     /dev/cciss/host*|/dev/cciss/disc*)
  634.         # /dev/cciss/hostN/targetM/disc is 2.6 devfs form
  635.         # /dev/cciss/discM/disk seems to be 2.4 devfs form
  636.         line=`echo $1 | sed 's,/dev/cciss/\([a-z]*\)\([0-9]*\)/\(.*\),\1 \2 \3,'`
  637.         controller=`echo "$line" | cut -d" " -f2`
  638.         host=`echo "$line" | cut -d" " -f1`
  639.         line=`echo "$line" | cut -d" " -f3`
  640.         if [ "$host" = host ] ; then
  641.            line=`echo "$line" | sed 's,target\([0-9]*\)/\([a-z]*\)\(.*\),\1 \2 \3,'`
  642.            lun=`echo  "$line" | cut -d" " -f1`
  643.            disc=`echo "$line" | cut -d" " -f2`
  644.            part=`echo "$line" | cut -d" " -f3`
  645.         else
  646.            line=`echo "$line" | sed 's,disc\([0-9]*\)/\([a-z]*\)\(.*\),\1 \2 \3,'`
  647.            lun=`echo  "$line" | cut -d" " -f1`
  648.            if [ "$lun" > 15 ] ; then
  649.               controller=$(($lun / 16))
  650.           lun=$(($lun % 16))
  651.            else
  652.           controller=0
  653.            fi
  654.            disc=`echo "$line" | cut -d" " -f2`
  655.            part=`echo "$line" | cut -d" " -f3`
  656.         fi
  657.         linux=$(mapdevfs $1)
  658.         linux=${linux#/dev/}
  659.         if [ "$disc" = disc ] ; then
  660.            db_metaget partman/text/scsi_disk description
  661.            printf "$RET" ".CCISS" "-" ${controller} ${lun} ${linux}
  662.         else
  663.            db_metaget partman/text/scsi_partition description
  664.            printf "$RET" ".CCISS" "-" ${controller} ${lun} ${part} ${linux}
  665.         fi
  666.         ;;
  667.     /dev/cciss/c*d*)
  668.         # It would be a lot easier to parse the /sys/block/*/device
  669.         # symlink. Unfortunately, unlike other block devices, this
  670.         # doesn't seem to exist in this case, so we just have to live
  671.         # with parsing the device name (note: added in upstream 2.6.18).
  672.         controller="$(echo "$1" | sed 's,/dev/cciss/c\([0-9]*\).*,\1,')"
  673.         lun="$(echo "$1" | sed 's,/dev/cciss/c[0-9]*d\([0-9]*\).*,\1,')"
  674.         case $1 in
  675.         /dev/cciss/c*d*p*)
  676.             # partition
  677.             part="$(echo "$1" | sed 's,/dev/cciss/c[0-9]*d[0-9]*p\([0-9]*\).*,\1,')"
  678.             ;;
  679.         *)
  680.             part=
  681.             ;;
  682.         esac
  683.         linux="$(mapdevfs "$1")"
  684.         linux="${linux#/dev/}"
  685.         if [ -z "$part" ]; then
  686.         db_metaget partman/text/scsi_disk description
  687.         printf "$RET" ".CCISS" "-" "$controller" "$lun" "$linux"
  688.         else
  689.         db_metaget partman/text/scsi_partition description
  690.         printf "$RET" ".CCISS" "-" "$controller" "$lun" "$part" "$linux"
  691.         fi
  692.         ;;
  693.     /dev/md/*)
  694.         device=`echo "$1" | sed -e "s/.*md\/\?\(.*\)/\1/"`
  695.         type=`grep "^md${device}[ :]" /proc/mdstat | sed -e "s/^.* : active raid\([[:alnum:]]\).*/\1/"`
  696.         db_metaget partman/text/raid_device description
  697.         printf "$RET" ${type} ${device}
  698.         ;;
  699.     /dev/mapper/*)
  700.         # First of all, check if this is a dm-crypt device
  701.         type=""
  702.         if [ -x /sbin/dmsetup ]; then
  703.             type=$(/sbin/dmsetup table "$1" | head -n 1 | cut -d " " -f3)
  704.         fi
  705.  
  706.         if [ $type = crypt ]; then
  707.             mapping=${1#/dev/mapper/}
  708.             db_metaget partman/text/dmcrypt_volume description
  709.             printf "$RET" $mapping
  710.         else
  711.             # LVM2 devices are found as /dev/mapper/<vg>-<lv>.  If the vg
  712.             # or lv contains a dash, the dash is replaced by two dashes.
  713.             # In order to decode this into vg and lv, first find the
  714.             # occurance of one single dash to split the string into vg and
  715.             # lv, and then replace two dashes next to each other with one.
  716.             vglv=${1#/dev/mapper/}
  717.             vglv=`echo "$vglv" | sed -e 's/\([^-]\)-\([^-]\)/\1 \2/' | sed -e 's/--/-/g'`
  718.             vg=`echo "$vglv" | cut -d" " -f1`
  719.             lv=`echo "$vglv" | cut -d" " -f2`
  720.             db_metaget partman/text/lvm_lv description
  721.             printf "$RET" $vg $lv
  722.         fi
  723.         ;;
  724.     /dev/loop/*|/dev/loop*)
  725.         n=${1#/dev/loop}
  726.         n=${n#/}
  727.         db_metaget partman/text/loopback description
  728.         printf "$RET" $n
  729.         ;;
  730.     # DASD partition, classic
  731.     /dev/dasd*[0-9]*)
  732.         part="${1#/dev/}"
  733.         disk="${part%%[0-9]*}"
  734.         part="${part#$disk}"
  735.         humandev_dasd_partition /sys/block/$disk/$(readlink /sys/block/$disk/device) $part
  736.         ;;
  737.     # DASD disk, classic
  738.     /dev/dasd*)
  739.         disk="${1#/dev/}"
  740.         humandev_dasd_disk /sys/block/$disk/$(readlink /sys/block/$disk/device)
  741.         ;;
  742.     *)
  743.         # Check if it's an LVM1 device
  744.         vg=`echo "$1" | sed -e 's,/dev/\([^/]\+\).*,\1,'`
  745.         lv=`echo "$1" | sed -e 's,/dev/[^/]\+/,,'`
  746.         if [ -e "/proc/lvm/VGs/$vg/LVs/$lv" ] ; then
  747.         db_metaget partman/text/lvm_lv description
  748.         printf "$RET" $vg $lv
  749.         else
  750.         echo "$1"
  751.         fi
  752.         ;;
  753.     esac
  754. }
  755.  
  756. humandev_dasd_disk () {
  757.     dev=${1##*/}
  758.     discipline=$(cat $1/discipline)
  759.     db_metaget partman/text/dasd_disk description
  760.     printf "$RET" "$dev" "$discipline"
  761. }
  762.  
  763. humandev_dasd_partition () {
  764.     dev=${1##*/}
  765.     discipline=$(cat $1/discipline)
  766.     db_metaget partman/text/dasd_partition description
  767.     printf "$RET" "$dev" "$discipline" "$part"
  768. }
  769.  
  770. device_name () {
  771.     cd $1
  772.     printf "%s - %s %s" "$(humandev $(cat device))" "$(longint2human $(cat size))" "$(cat model)"
  773. }
  774.  
  775. enable_swap () {
  776.     local swaps dev num id size type fs path name method
  777.     local startdir="$(pwd)"
  778.     # do swapon only when we will be able to swapoff afterwards
  779.     [ -f /proc/swaps ] || return 0
  780.     swaps=''
  781.     for dev in $DEVICES/*; do
  782.     [ -d $dev ] || continue
  783.     cd $dev
  784.     open_dialog PARTITIONS
  785.     while { read_line num id size type fs path name; [ "$id" ]; }; do
  786.         [ $fs != free ] || continue
  787.         [ -f "$id/method" ] || continue
  788.         method=$(cat $id/method)
  789.         if [ "$method" = swap ]; then
  790.         swaps="$swaps $path"
  791.         fi
  792.     done
  793.     close_dialog
  794.     done
  795.     for path in $swaps; do
  796.     swapon $path || true
  797.     done
  798.     cd "$startdir"
  799. }
  800.  
  801. disable_swap () {
  802.     [ -f /proc/swaps ] || return 0
  803.     grep '^/dev' /proc/swaps \
  804.     | while read path x; do
  805.           swapoff $path
  806.           done
  807. }
  808.  
  809. default_disk_label () {
  810.     if [ -x /bin/archdetect ]; then
  811.     archdetect=$(archdetect)
  812.     else
  813.     archdetect=unknown/generic
  814.     fi
  815.     arch=${archdetect%/*}
  816.     sub=${archdetect#*/}
  817.     case "$arch" in
  818.     alpha)
  819.         # Load srm_env.o if we can; this should fail on ARC-based systems.
  820.         (modprobe srm_env || true) 2> /dev/null
  821.         if [ -f /proc/srm_environment/named_variables/booted_dev ]; then
  822.                 # Have SRM, so need BSD disklabels
  823.         echo bsd
  824.         else
  825.         echo msdos
  826.         fi;;        
  827.     arm)
  828.         case "$sub" in
  829.         riscstation)
  830.             echo msdos;;
  831.         netwinder)
  832.             echo msdos;;
  833.         ads)
  834.             echo msdos;;
  835.         nslu2)
  836.             echo msdos;;
  837.         *)
  838.             echo UNKNOWN;;
  839.         esac;;
  840.     armeb)
  841.         case "$sub" in
  842.         nslu2)
  843.             echo msdos;;
  844.         *)
  845.             echo UNKNOWN;;
  846.         esac;;
  847.     amd64)
  848.         echo msdos;;
  849.     hppa)
  850.         echo msdos;;
  851.     ia64)
  852.         echo gpt;;
  853.     i386)
  854.         echo msdos;;
  855.     m68k)
  856.         case "$sub" in
  857.         amiga)
  858.             echo amiga;;
  859.         atari)
  860.             echo UNSUPPORTED;; # atari is unsupported by parted
  861.         mac)
  862.             echo mac;;
  863.         *vme*)
  864.             echo msdos;;
  865.         q40)
  866.             echo UNSUPPORTED;; # (same as atari)
  867.         sun*)
  868.                 echo sun;;
  869.         *)
  870.             echo UNKNOWN;;
  871.         esac;;
  872.     mips)
  873.         case "$sub" in
  874.         # Indy
  875.         r4k-ip22 | r5k-ip22 | r8k-ip26 | r10k-ip28)
  876.             echo dvh;;
  877.         # Origin
  878.         r10k-ip27 | r12k-ip27)
  879.             echo dvh;;
  880.         # O2
  881.         r5k-ip32 | r10k-ip32 | r12k-ip32)
  882.             echo dvh;;
  883.         # Broadcom SB1 evaluation boards
  884.         sb1-bcm91250a | sb1a-bcm91480b)
  885.             echo msdos;;
  886.         *)
  887.             echo UNKNOWN;;
  888.         esac;;
  889.     mipsel)
  890.         case "$sub" in
  891.         # DECstation
  892.         r3k-kn02)
  893.             echo msdos;;
  894.         r4k-kn04)
  895.             echo msdos;;
  896.         # Broadcom SB1 evaluation boards
  897.         sb1-bcm91250a | sb1a-bcm91480b)
  898.             echo msdos;;
  899.         cobalt)
  900.             echo msdos;;
  901.         bcm947xx)
  902.             echo msdos;;
  903.         *)
  904.             echo UNKNOWN;;
  905.         esac;;
  906.     powerpc)
  907.         case "$sub" in
  908.         apus)
  909.             echo amiga;;
  910.         amiga)
  911.             echo amiga;;
  912.         chrp)
  913.             echo msdos;;
  914.         chrp_rs6k|chrp_ibm)
  915.             echo msdos;;
  916.         chrp_pegasos)
  917.             echo amiga;;
  918.         prep)
  919.             echo msdos;;
  920.         powermac_newworld)
  921.             echo mac;;
  922.         powermac_oldworld)
  923.             echo mac;;
  924.         *)
  925.             echo UNKNOWN;;
  926.         esac;;
  927.     s390)
  928.         echo msdos;;
  929.     sparc)
  930.         echo sun;;
  931.     *)
  932.         echo UNKNOWN;;
  933.     esac
  934. }
  935.  
  936. # Lock a device or partition against further modifications
  937. partman_lock_unit() {
  938.     local device message dev testdev
  939.     device="$1"
  940.     message="$2"
  941.  
  942.     for dev in $DEVICES/*; do
  943.         [ -d "$dev" ] || continue
  944.         cd $dev
  945.  
  946.         # First check if we should lock a device
  947.         if [ -e "device" ]; then
  948.             testdev=$(mapdevfs $(cat device))
  949.             if [ "$device" = "$testdev" ]; then
  950.                 echo "$message" > locked
  951.                 return 0
  952.             fi
  953.         fi
  954.  
  955.         # Second check if we should lock a partition
  956.         open_dialog PARTITIONS
  957.         while { read_line num id size type fs path name; [ "$id" ]; }; do
  958.             testdev=$(mapdevfs $path)
  959.             if [ "$device" = "$testdev" ]; then
  960.                 echo "$message" > $id/locked
  961.             fi
  962.         done
  963.         close_dialog
  964.     done
  965. }
  966.  
  967. # Unlock a device or partition to allow further modifications
  968. partman_unlock_unit() {
  969.     local device dev testdev
  970.     device="$1"
  971.  
  972.     for dev in $DEVICES/*; do
  973.         [ -d "$dev" ] || continue
  974.         cd $dev
  975.  
  976.         # First check if we should unlock a device
  977.         if [ -e "device" ]; then
  978.             testdev=$(mapdevfs $(cat device))
  979.             if [ "$device" = "$testdev" ]; then
  980.                 rm -f locked
  981.                 return 0
  982.             fi
  983.         fi
  984.  
  985.         # Second check if we should unlock a partition
  986.         open_dialog PARTITIONS
  987.         while { read_line num id size type fs path name; [ "$id" ]; }; do
  988.             testdev=$(mapdevfs $path)
  989.             if [ "$device" = "$testdev" ]; then
  990.                 rm -f $id/locked
  991.             fi
  992.         done
  993.         close_dialog
  994.     done
  995. }
  996.  
  997. # List the changes that are about to be committed and let the user confirm first
  998. confirm_changes () {
  999.     local template dev x part partitions num id size type fs path name filesystem partitems items formatted_previously
  1000.     template="$1"
  1001.  
  1002.     # Compute the changes we are going to do
  1003.     partitems=''
  1004.     items=''
  1005.     for dev in $DEVICES/*; do
  1006.         [ -d "$dev" ] || continue
  1007.         cd $dev
  1008.  
  1009.         open_dialog IS_CHANGED
  1010.         read_line x
  1011.         close_dialog
  1012.         if [ "$x" = yes ]; then
  1013.             partitems="${partitems}   $(humandev $(cat device))
  1014. "
  1015.         fi
  1016.  
  1017.         partitions=
  1018.         open_dialog PARTITIONS
  1019.         while { read_line num id size type fs path name; [ "$id" ]; }; do
  1020.             [ "$fs" != free ] || continue
  1021.             partitions="$partitions $id,$num"
  1022.         done
  1023.         close_dialog
  1024.     
  1025.         formatted_previously=no
  1026.         for part in $partitions; do
  1027.             id=${part%,*}
  1028.             num=${part#*,}
  1029.             [ -f $id/method -a -f $id/format \
  1030.               -a -f $id/visual_filesystem ] || continue
  1031.             # if no filesystem (e.g. swap) should either be not
  1032.             # formatted or formatted before the method is specified
  1033.             [ -f $id/filesystem -o ! -f $id/formatted \
  1034.               -o $id/formatted -ot $id/method ] || continue
  1035.             # if it is already formatted filesystem it must be formatted 
  1036.             # before the method or filesystem is specified
  1037.             [ ! -f $id/filesystem -o ! -f $id/formatted \
  1038.               -o $id/formatted -ot $id/method \
  1039.               -o $id/formatted -ot $id/filesystem ] ||
  1040.             {
  1041.                 formatted_previously=yes
  1042.                 continue
  1043.             }
  1044.             filesystem=$(cat $id/visual_filesystem)
  1045.             db_subst partman/text/confirm_item TYPE "$filesystem"
  1046.             db_subst partman/text/confirm_item PARTITION "$num"
  1047.             db_subst partman/text/confirm_item DEVICE $(humandev $(cat device))
  1048.             db_metaget partman/text/confirm_item description
  1049.             
  1050.             items="${items}   ${RET}
  1051. "
  1052.         done
  1053.     done
  1054.  
  1055.     if [ "$items" ]; then
  1056.         db_metaget partman/text/confirm_item_header description
  1057.         items="$RET
  1058. $items"
  1059.     fi
  1060.     
  1061.     if [ "$partitems" ]; then
  1062.         db_metaget partman/text/confirm_partitem_header description
  1063.         partitems="$RET
  1064. $partitems"
  1065.     fi
  1066.  
  1067.     if [ "$partitems$items" ]; then
  1068.         if [ -z "$items" ]; then
  1069.             x="$partitems"
  1070.         elif [ -z "$partitems" ]; then
  1071.             x="$items"
  1072.         else
  1073.             x="$partitems
  1074. $items"
  1075.         fi
  1076.         maybe_escape "$x" db_subst $template/confirm ITEMS
  1077.         db_input critical $template/confirm
  1078.         db_go || true
  1079.         db_get $template/confirm
  1080.         if [ "$RET" = false ]; then
  1081.             db_reset $template/confirm
  1082.             return 1
  1083.         else
  1084.             db_reset $template/confirm
  1085.             return 0
  1086.         fi
  1087.     else
  1088.         if [ "$formatted_previously" = no ]; then
  1089.             db_input medium $template/confirm_nochanges
  1090.             db_go || true
  1091.             db_get $template/confirm_nochanges
  1092.             if [ "$RET" = false ]; then
  1093.                 db_reset $template/confirm_nochanges
  1094.                 return 1
  1095.             else
  1096.                 db_reset $template/confirm_nochanges
  1097.                 return 0
  1098.             fi
  1099.         else
  1100.             return 0
  1101.         fi
  1102.     fi
  1103. }
  1104.  
  1105. log '*******************************************************'
  1106.  
  1107. # Local Variables:
  1108. # coding: utf-8
  1109. # End:
  1110.