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 / resize.sh < prev   
Encoding:
Text File  |  2007-03-26  |  9.7 KB  |  344 lines

  1. # Sets $virtual; used by other functions here.
  2. check_virtual () {
  3.     open_dialog VIRTUAL $oldid
  4.     read_line virtual
  5.     close_dialog
  6. }
  7.  
  8. get_real_device () {
  9.     local backupdev num
  10.     # A weird way to get the real device path.  The partition numbers
  11.     # in parted_server may be changed and the partition table is still
  12.     # not commited to the disk
  13.     backupdev=/var/lib/partman/backup/${dev#/var/lib/partman/devices/}
  14.     if [ -f $backupdev/$oldid/view -a -f $backupdev/device ]; then
  15.     num=$(sed 's/^[^0-9]*\([0-9]*\)[^0-9].*/\1/' $backupdev/$oldid/view)
  16.     bdev=$(cat $backupdev/device)
  17.     case $bdev in
  18.         */disc)
  19.         bdev=${bdev%/disc}/part$num
  20.         ;;
  21.         /dev/[hs]d[a-z])
  22.         bdev=$bdev$num
  23.         ;;
  24.         *)
  25.         log "get_real_device: strange device name $bdev"
  26.         return
  27.         ;;
  28.     esac
  29.     if [ ! -b $bdev ]; then
  30.         bdev=
  31.     fi
  32.     fi
  33. }
  34.  
  35. do_ntfsresize () {
  36.     local RET
  37.     ntfsresize="$(ntfsresize $@ 2>&1)"
  38.     RET=$?
  39.     echo "$ntfsresize" | grep -v "percent completed" | \
  40.         logger -t ntfsresize
  41.     return $RET
  42. }
  43.  
  44. get_ntfs_resize_range () {
  45.     local bdev size
  46.     open_dialog GET_VIRTUAL_RESIZE_RANGE $oldid
  47.     read_line minsize cursize maxsize
  48.     close_dialog
  49.     get_real_device
  50.     if [ "$bdev" ]; then
  51.     if ! do_ntfsresize -f -i $bdev; then
  52.         logger -t partman "Error running 'ntfsresize --info'"
  53.         return 1
  54.     fi
  55.     size=$(echo "$ntfsresize" \
  56.         | grep '^You might resize at' \
  57.         | sed 's/^You might resize at \([0-9]*\) bytes.*/\1/' \
  58.         | grep '^[0-9]*$')
  59.     if [ "$size" ]; then
  60.         minsize=$size
  61.     fi
  62.     fi
  63. }
  64.  
  65. get_ext2_resize_range () {
  66.     local bdev tune2fs block_size block_count free_blocks
  67.     open_dialog GET_VIRTUAL_RESIZE_RANGE $oldid
  68.     read_line minsize cursize maxsize
  69.     close_dialog
  70.     get_real_device
  71.     if [ "$bdev" ]; then
  72.     if ! tune2fs="$(tune2fs -l $bdev)"; then
  73.         logger -t partman "Error running 'tune2fs -l $bdev'"
  74.         return 1
  75.     fi
  76.     block_size="$(echo "$tune2fs" | grep '^Block size:' | head -n1 | sed 's/.*:[[:space:]]*//')"
  77.     block_count="$(echo "$tune2fs" | grep '^Block count:' | head -n1 | sed 's/.*:[[:space:]]*//')"
  78.     free_blocks="$(echo "$tune2fs" | grep '^Free blocks:' | head -n1 | sed 's/.*:[[:space:]]*//')"
  79.     if expr "$block_size" : '[0-9][0-9]*$' >/dev/null && \
  80.        expr "$block_count" : '[0-9][0-9]*$' >/dev/null && \
  81.        expr "$free_blocks" : '[0-9][0-9]*$' >/dev/null; then
  82.         minsize="$(expr \( "$block_count" - "$free_blocks" \) \* "$block_size")"
  83.     fi
  84.     fi
  85.     return 0
  86. }
  87.  
  88. get_resize_range () {
  89.     open_dialog GET_RESIZE_RANGE $oldid
  90.     read_line minsize cursize maxsize
  91.     close_dialog
  92. }
  93.  
  94. human_resize_range () {
  95.     hminsize=$(longint2human $minsize)
  96.     hcursize=$(longint2human $cursize)
  97.     hmaxsize=$(longint2human $maxsize)
  98.     minpercent="$(expr 100 \* "$minsize" / "$maxsize")"
  99. }
  100.  
  101. ask_for_size () {
  102.     local noninteractive digits minmb
  103.     noninteractive=true
  104.     while true; do
  105.     newsize=''
  106.     while [ ! "$newsize" ]; do
  107.         db_set partman-partitioning/new_size "$hcursize"
  108.         db_subst partman-partitioning/new_size MINSIZE "$hminsize"
  109.         db_subst partman-partitioning/new_size MAXSIZE "$hmaxsize"
  110.         db_subst partman-partitioning/new_size PERCENT "$minpercent%"
  111.         db_input critical partman-partitioning/new_size || $noninteractive
  112.         noninteractive="return 1"
  113.         db_go || return 1
  114.         db_get partman-partitioning/new_size
  115.         case "$RET" in
  116.         max)
  117.             newsize=$maxsize
  118.             ;;
  119.         *%)
  120.             digits=$(expr "$RET" : '\([1-9][0-9]*\) *%$')
  121.             if [ "$digits" ]; then
  122.             maxmb=$(expr 0000000"$maxsize" : '0*\(..*\)......$')
  123.             newsize=$(($digits * $maxmb / 100))000000
  124.             fi
  125.             ;;
  126.         *)
  127.             if valid_human "$RET"; then
  128.             newsize=$(human2longint "$RET")
  129.             fi
  130.             ;;
  131.         esac
  132.         if [ -z "$newsize" ]; then
  133.         db_input high partman-partitioning/bad_new_size || true
  134.         db_go || true
  135.         elif ! longint_le "$newsize" "$maxsize"; then
  136.         db_input high partman-partitioning/big_new_size || true
  137.         db_go || true
  138.         newsize=''
  139.         elif ! longint_le "$minsize" "$newsize"; then
  140.         db_input high partman-partitioning/small_new_size || true
  141.         db_go || true
  142.         newsize=''
  143.         fi
  144.     done
  145.     if perform_resizing; then break; fi
  146.     done
  147.     return 0
  148. }
  149.  
  150. perform_resizing () {
  151.     if [ "$virtual" = no ]; then
  152.     for s in /lib/partman/commit.d/*; do
  153.         if [ -x $s ]; then
  154.         $s || {
  155.             db_input high partman-partitioning/new_size_commit_failed || true
  156.             db_go || true
  157.             for s in /lib/partman/init.d/*; do
  158.             if [ -x $s ]; then
  159.                 $s || exit 100
  160.             fi
  161.             done
  162.             exit 100
  163.         }
  164.         fi
  165.     done
  166.     fi
  167.  
  168.     disable_swap
  169.  
  170.     if \
  171.     [ "$virtual" = no ] \
  172.     && [ -f $oldid/detected_filesystem ] \
  173.     && [ "$(cat $oldid/detected_filesystem)" = ntfs ]
  174.     then
  175.     # resize NTFS
  176.     db_progress START 0 1000 partman/text/please_wait
  177.     db_progress INFO partman-partitioning/progress_resizing
  178.     if longint_le "$cursize" "$newsize"; then
  179.         open_dialog VIRTUAL_RESIZE_PARTITION $oldid $newsize
  180.         read_line newid
  181.         close_dialog
  182.         open_dialog COMMIT
  183.         close_dialog
  184.         open_dialog PARTITION_INFO $newid
  185.         read_line x1 x2 x3 x4 x5 path x7
  186.         close_dialog
  187.         # Wait for the device file to be created again
  188.         update-dev
  189.         if ! echo y | do_ntfsresize -f $path; then
  190.         logger -t partman "Error resizing the NTFS file system to the partition size"
  191.         db_input high partman-partitioning/new_size_commit_failed || true
  192.         db_go || true
  193.         db_progress STOP
  194.         exit 100
  195.         fi
  196.     else
  197.         open_dialog COMMIT
  198.         close_dialog
  199.         open_dialog PARTITION_INFO $oldid
  200.         read_line x1 x2 x3 x4 x5 path x7
  201.         close_dialog
  202.         # Wait for the device file to be created
  203.         update-dev
  204.         if echo y | do_ntfsresize -f --size "$newsize" $path; then
  205.         open_dialog VIRTUAL_RESIZE_PARTITION $oldid $newsize
  206.         read_line newid
  207.         close_dialog
  208.         # Wait for the device file to be created
  209.         update-dev
  210.         if ! echo y | do_ntfsresize -f $path; then
  211.             logger -t partman "Error resizing the NTFS file system to the partition size"
  212.             db_input high partman-partitioning/new_size_commit_failed || true
  213.             db_go || true
  214.             db_progress STOP
  215.             exit 100
  216.         fi
  217.         else
  218.         logger -t partman "Error resizing the NTFS file system"
  219.         db_input high partman-partitioning/new_size_commit_failed || true
  220.         db_go || true
  221.         db_progress STOP
  222.         exit 100
  223.         fi
  224.     fi
  225.     db_progress SET 1000
  226.     db_progress STOP
  227.     elif \
  228.     [ "$virtual" = no ] \
  229.     && [ -f $oldid/detected_filesystem ] \
  230.     && ([ "$(cat $oldid/detected_filesystem)" = ext2 ] \
  231.         || [ "$(cat $oldid/detected_filesystem)" = ext3 ])
  232.     then
  233.     # resize ext2/ext3; parted can handle simple cases but can't deal
  234.     # with certain common features such as resize_inode
  235.     fs="$(cat $oldid/detected_filesystem)"
  236.     db_progress START 0 1000 partman/text/please_wait
  237.     open_dialog PARTITION_INFO $oldid
  238.     read_line num x2 x3 x4 x5 x6 x7
  239.     close_dialog
  240.     db_metaget "partman/filesystem_short/$fs" description || RET=
  241.     [ "$RET" ] || RET="$fs"
  242.     db_subst partman-basicfilesystems/progress_checking TYPE "$RET"
  243.     db_subst partman-basicfilesystems/progress_checking PARTITION "$num"
  244.     db_subst partman-basicfilesystems/progress_checking DEVICE "$(humandev $(cat device))"
  245.     db_progress INFO partman-basicfilesystems/progress_checking
  246.     if longint_le "$cursize" "$newsize"; then
  247.         open_dialog VIRTUAL_RESIZE_PARTITION $oldid $newsize
  248.         read_line newid
  249.         close_dialog
  250.         open_dialog COMMIT
  251.         close_dialog
  252.         open_dialog PARTITION_INFO $newid
  253.         read_line x1 x2 x3 x4 x5 path x7
  254.         close_dialog
  255.     else
  256.         open_dialog COMMIT
  257.         close_dialog
  258.         open_dialog PARTITION_INFO $oldid
  259.         read_line x1 x2 x3 x4 x5 path x7
  260.         close_dialog
  261.     fi
  262.     # Wait for the device file to be created
  263.     update-dev
  264.     e2fsck_code=0
  265.     e2fsck -f -p $path || e2fsck_code=$?
  266.     if [ $e2fsck_code -gt 1 ]; then
  267.         db_subst partman-basicfilesystems/check_failed TYPE "$fs"
  268.         db_subst partman-basicfilesystems/check_failed PARTITION "$num"
  269.         db_subst partman-basicfilesystems/check_failed DEVICE "$(humandev $(cat device))"
  270.         db_set partman-basicfilesystems/check_failed 'true'
  271.         db_input critical partman-basicfilesystems/check_failed || true
  272.         db_go || true
  273.         db_get partman-basicfilesystems/check_failed
  274.         if [ "$RET" = 'true' ]; then
  275.         exit 100
  276.         fi
  277.     fi
  278.     db_progress INFO partman-partitioning/progress_resizing
  279.     db_progress SET 500
  280.     if longint_le "$cursize" "$newsize"; then
  281.         if ! resize2fs $path; then
  282.         logger -t partman "Error resizing the ext2/ext3 file system to the partition size"
  283.         db_input high partman-partitioning/new_size_commit_failed || true
  284.         db_go || true
  285.         db_progress STOP
  286.         exit 100
  287.         fi
  288.     else
  289.         if resize2fs $path "$(expr "$newsize" / 1024)K"; then
  290.         open_dialog VIRTUAL_RESIZE_PARTITION $oldid $newsize
  291.         read_line newid
  292.         close_dialog
  293.         # Wait for the device file to be created
  294.         update-dev
  295.         if ! resize2fs $path; then
  296.             logger -t partman "Error resizing the ext2/ext3 file system to the partition size"
  297.             db_input high partman-partitioning/new_size_commit_failed || true
  298.             db_go || true
  299.             db_progress STOP
  300.             exit 100
  301.         fi
  302.         else
  303.         logger -t partman "Error resizing the ext2/ext3 file system"
  304.         db_input high partman-partitioning/new_size_commit_failed || true
  305.         db_go || true
  306.         db_progress STOP
  307.         exit 100
  308.         fi
  309.     fi
  310.     db_progress SET 1000
  311.     db_progress STOP
  312.     else
  313.     # resize virtual partitions, ext2, ext3, swap, fat16, fat32
  314.     # and probably reiserfs 
  315.     name_progress_bar partman-partitioning/progress_resizing
  316.     open_dialog RESIZE_PARTITION $oldid $newsize
  317.     read_line newid
  318.     close_dialog
  319.     fi
  320.  
  321.     if [ -n "$newid" -a "$newid" != "$oldid" ]; then
  322.     [ ! -e "$newid" ] || rm -rf $newid
  323.     mkdir $newid
  324.     cp -r $oldid/* $newid/
  325.     fi
  326.     if [ "$virtual" = no ]; then
  327.     for s in /lib/partman/init.d/*; do
  328.         if [ -x $s ]; then
  329.         $s || exit 100
  330.         fi
  331.     done
  332.     else 
  333.     partitions=''
  334.     open_dialog PARTITIONS
  335.     while { read_line num part size type fs path name; [ "$part" ]; }; do
  336.         partitions="$partitions $part"
  337.     done
  338.     close_dialog
  339.     for part in $partitions; do
  340.         update_partition $dev $part
  341.     done
  342.     fi
  343. }
  344.