~cyphermox/partman-auto/merge-125ubuntu1

« back to all changes in this revision

Viewing changes to recipes.sh

  • Committer: Frans Pop
  • Date: 2007-12-05 20:18:51 UTC
  • mto: (408.4.1)
  • mto: This revision was merged to the branch mainline in revision 495.
  • Revision ID: git-v1:6ae2f3bdad65a0cdc348e5aeeadaf68c6f86f21a
Move recipes.sh and auto-shared.sh (partman-auto) to ./lib/

r50355

Show diffs side-by-side

added added

removed removed

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