~elementary-os/ubuntu-package-imports/ubiquity-wily

« back to all changes in this revision

Viewing changes to d-i/source/partman-auto-loop/autopartition-loop

  • Committer: RabbitBot
  • Date: 2015-09-13 21:17:45 UTC
  • Revision ID: rabbitbot@elementaryos.org-20150913211745-n41jcvcqcnvmyjyx
Initial import, version 2.21.29

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /bin/sh
 
2
 
 
3
. /lib/partman/lib/base.sh
 
4
. /lib/partman/lib/resize.sh
 
5
. /lib/partman/lib/recipes.sh
 
6
 
 
7
# busybox wants mount -o move; util-linux wants mount --move. Sigh.
 
8
if [ -d /lib/debian-installer ]; then
 
9
        mount_move='-o move'
 
10
else
 
11
        mount_move='--move'
 
12
fi
 
13
 
 
14
modprobe loop >/dev/null 2>&1 || true
 
15
 
 
16
# Set up working directory.
 
17
 
 
18
if type mktemp >/dev/null 2>&1; then
 
19
        recipe_dir="$(mktemp -d /tmp/partman-auto-loop.XXXXXX)"
 
20
        trap "rm -rf $recipe_dir" EXIT HUP INT QUIT TERM
 
21
else
 
22
        recipe_dir=/tmp
 
23
fi
 
24
 
 
25
# Fetch parameters.
 
26
 
 
27
disk="$1"
 
28
 
 
29
cd $disk
 
30
 
 
31
if ! db_get partman-auto-loop/partition || [ -z "$RET" ]; then
 
32
        logger -t partman-auto-loop "Error: No partition number specified in partman-auto-loop/partition"
 
33
        exit 1
 
34
fi
 
35
partition="$RET"
 
36
 
 
37
if ! db_get partman-auto-loop/recipe || [ -z "$RET" ]; then
 
38
        logger -t partman-auto-loop "Error: No recipe specified in partman-auto-loop/recipe"
 
39
        exit 1
 
40
fi
 
41
recipe="$RET"
 
42
echo "$recipe" >"$recipe_dir/loop_recipe"
 
43
 
 
44
# Find the requested partition.
 
45
 
 
46
db_progress START 0 5 partman-auto/text/automatically_partition
 
47
db_progress INFO partman-auto/progress/info
 
48
 
 
49
partition_id=
 
50
partition_fs=
 
51
open_dialog PARTITIONS
 
52
while { read_line num id size type fs path name; [ "$id" ]; }; do
 
53
        if [ "$num" = "$partition" ]; then
 
54
                partition_id="$id"
 
55
                partition_fs="$fs"
 
56
                # go ahead and read all remaining input
 
57
        fi
 
58
done
 
59
close_dialog
 
60
if [ -z "$partition_id" ]; then
 
61
        logger -t partman-auto-loop "Error: Partition number $partition not found in $disk"
 
62
        exit 1
 
63
fi
 
64
 
 
65
# Set up the requested partition in partman.
 
66
 
 
67
existing=no
 
68
for j in $(
 
69
        for i in /lib/partman/valid_filesystems/*; do
 
70
                [ -x $i ] || continue
 
71
                $i $disk $partition_id existing
 
72
        done
 
73
); do
 
74
        if [ "$j" = "$partition_fs" ]; then
 
75
                existing=yes
 
76
        fi
 
77
done
 
78
 
 
79
if [ "$existing" = no ]; then
 
80
        logger -t partman-auto-loop "Error: No filesystem on $disk/$partition_id"
 
81
        exit 1
 
82
fi
 
83
 
 
84
echo keep >$partition_id/method
 
85
rm -f $partition_id/format
 
86
>$partition_id/use_filesystem
 
87
echo $partition_fs >$partition_id/filesystem
 
88
mkdir -p $partition_id/options
 
89
echo / >$partition_id/mountpoint
 
90
update_partition $disk $partition_id
 
91
 
 
92
# Is there enough space to perform the recipe?
 
93
 
 
94
dev="$disk"
 
95
oldid="$partition_id"
 
96
 
 
97
recipe_new=
 
98
firstword=
 
99
imagepaths=
 
100
for word in $(cat "$recipe_dir/loop_recipe"); do
 
101
        case $word in
 
102
                .)
 
103
                        recipe_new="${recipe_new:+$recipe_new }\$imagepath{ $firstword } $word"
 
104
                        firstword=
 
105
                        ;;
 
106
                *)
 
107
                        if [ "$firstword" ]; then
 
108
                                recipe_new="${recipe_new:+$recipe_new }$word"
 
109
                        else
 
110
                                firstword="$word"
 
111
                                imagepaths="${imagepaths:+$imagepaths }$word"
 
112
                        fi
 
113
                        ;;
 
114
        esac
 
115
done
 
116
echo "$recipe_new" >"$recipe_dir/loop_recipe_new"
 
117
decode_recipe "$recipe_dir/loop_recipe_new" loop
 
118
 
 
119
db_progress STEP 1
 
120
 
 
121
fstab="$(
 
122
        for i in /lib/partman/fstab.d/*; do
 
123
                [ -x "$i" ] || continue
 
124
                $i
 
125
        done |
 
126
        while read fs mp type options dump pass; do
 
127
                case $mp in
 
128
                        (/)
 
129
                                echo $fs $mp $type $options $dump $pass
 
130
                                ;;
 
131
                esac
 
132
        done
 
133
)"
 
134
 
 
135
if [ -z "$fstab" ]; then
 
136
        logger -t partman-auto-loop "Error: No fstab output for $disk/$partition_id"
 
137
        exit 1
 
138
fi
 
139
 
 
140
mkdir -p /target
 
141
 
 
142
mountpoint="$(grep "^${fstab%% *} [^ ]* [^ ]* [^ ]*rw" /proc/mounts | cut -d ' ' -f2 | head -n1)" || mountpoint=
 
143
if [ "$mountpoint" = /target ]; then
 
144
        # nothing to do
 
145
        :
 
146
elif [ "$mountpoint" ]; then
 
147
        if ! mount $mount_move "$mountpoint" /target; then
 
148
                logger -t partman-auto-loop "Error: Failed to move $mountpoint to /target"
 
149
                exit 1
 
150
        fi
 
151
        unmount_cmd='umount /target'
 
152
else
 
153
        for m in /lib/partman/mount.d/*; do
 
154
                [ -x "$m" ] || continue
 
155
 
 
156
                unmount_cmd="$($m "$fstab")"
 
157
                if [ "$?" = 0 ]; then
 
158
                        break
 
159
                fi
 
160
        done
 
161
fi
 
162
if [ -d /run ]; then
 
163
        mkdir -p /run/sendsigs.omit.d
 
164
        pidof mount.ntfs >> /run/sendsigs.omit.d/ntfs-3g
 
165
        pidof mount.ntfs-3g >> /run/sendsigs.omit.d/ntfs-3g
 
166
fi
 
167
 
 
168
# TODO: handle errors if no mount succeeded
 
169
mkdir -p /host
 
170
mount $mount_move /target /host # TODO error handling
 
171
 
 
172
# Don't try to mount this again later.
 
173
rm -f $partition_id/mountpoint
 
174
 
 
175
# Ensure there is enough free space.
 
176
check_free_space=false
 
177
requires_disk_space(){
 
178
    [ "$1" != 0 ] || return
 
179
    path="$(echo "$*" | sed 's/.*\$imagepath{  *\([^ }]*\) *}.*/\1/')"
 
180
    [ "$path" != "$*" ] || return
 
181
    case $path in
 
182
                /*)     ;;
 
183
                *)      path="/$path" ;;
 
184
    esac
 
185
    [ -f "/host$path" ] && return 
 
186
    check_free_space=true
 
187
}
 
188
foreach_partition 'requires_disk_space $*' 
 
189
 
 
190
# Skip resize_range check if images are already created.
 
191
if [ $check_free_space = true ]; then
 
192
    case $partition_fs in
 
193
        linux-swap|fat16|fat32|hfs|hfs+|hfsx)
 
194
                get_resize_range
 
195
                ;;
 
196
        ext2|ext3|ext4)
 
197
                if ! search-path tune2fs; then
 
198
                        logger -t partman-auto-loop "Error: tune2fs not found"
 
199
                        exit 1
 
200
                fi
 
201
                if ! search-path resize2fs; then
 
202
                        logger -t partman-auto-loop "Error: resize2fs not found"
 
203
                        exit 1
 
204
                fi
 
205
                if ! get_ext2_resize_range; then
 
206
                        logger -t partman-auto-loop "Error: Failed to get ext2 resize range for $disk/$partition_id"
 
207
                        exit 1
 
208
                fi
 
209
                ;;
 
210
        ntfs)
 
211
                if ! search-path ntfsresize; then
 
212
                        logger -t partman-auto-loop "Error: ntfsresize not found"
 
213
                        exit 1
 
214
                fi
 
215
                if ! get_ntfs_resize_range; then
 
216
                        db_input critical partman-auto-loop/unclean_ntfs || true
 
217
                        db_go || true
 
218
                        logger -t partman-auto-loop "Error: Failed to get NTFS resize range for $disk/$partition_id"
 
219
                        reboot
 
220
                        exit 1
 
221
                fi
 
222
                ;;
 
223
        *)
 
224
                logger -t partman-auto-loop "Cannot calculate free space on filesystems of type $partition_fs"
 
225
                exit 1
 
226
                ;;
 
227
    esac
 
228
    free_size="$(expr \( "$cursize" - "$minsize" \) \* 9 / 10)"
 
229
    # convert to megabytes
 
230
    free_size="$(expr 0000000"$free_size" : '0*\(..*\)......$')"
 
231
    if [ $(min_size) -gt $free_size ]; then
 
232
        logger -t partman-auto-loop "Error: partman-auto-loop/recipe too large ($(min_size) > $free_size)"
 
233
        exit 1
 
234
    fi
 
235
fi
 
236
 
 
237
# Ensure that no old loop images are present and mountable.
 
238
found_images=
 
239
mkdir -p /tmpmountpoint
 
240
for path in $imagepaths; do
 
241
        case $path in
 
242
                /*)     ;;
 
243
                *)      path="/$path" ;;
 
244
        esac
 
245
        if [ -e "/host$path" ]; then
 
246
                if mount -t auto -o loop,ro /host$path /tmpmountpoint 2>/dev/null 3>&-; then
 
247
                        found_images="${found_images:+$found_images }$path"
 
248
                        umount /tmpmountpoint || true
 
249
                        rmdir /tmpmountpoint || true
 
250
                fi
 
251
        fi
 
252
done
 
253
if [ "$found_images" ]; then
 
254
        db_progress STOP
 
255
        db_subst partman-auto-loop/unclean_host PARTITION "$partition"
 
256
        db_subst partman-auto-loop/unclean_host DISK "$disk"
 
257
        db_subst partman-auto-loop/unclean_host IMAGES "$found_images"
 
258
        db_input critical partman-auto-loop/unclean_host || true
 
259
        db_capb
 
260
        db_go || true
 
261
        db_capb backup
 
262
        umount /host || true
 
263
        exit 1
 
264
fi
 
265
 
 
266
db_progress STEP 1
 
267
 
 
268
expand_scheme
 
269
 
 
270
db_progress STEP 1
 
271
 
 
272
clean_method
 
273
 
 
274
db_progress STEP 1
 
275
 
 
276
setup_loop () {
 
277
        [ "$1" != 0 ] || return
 
278
        path="$(echo "$*" | sed 's/.*\$imagepath{  *\([^ }]*\) *}.*/\1/')"
 
279
        [ "$path" != "$*" ] || return
 
280
 
 
281
        case $path in
 
282
                /*)     ;;
 
283
                *)      path="/$path" ;;
 
284
        esac
 
285
        if [ ! -f "/host$path" ]; then
 
286
                mkdir -p "/host${path%/*}"
 
287
                if [ "$4" = "linux-swap" ]; then
 
288
                        # swap requires a file with no holes
 
289
                        dd if=/dev/zero of="/host$path" bs="1000000" count="$1"
 
290
                else
 
291
                        dd if=/dev/zero of="/host$path" bs="1000000" seek="$1" count=0
 
292
                fi
 
293
        fi
 
294
        if ! losetup -f "/host$path"; then
 
295
                shift
 
296
                continue
 
297
        fi
 
298
        if [ "$4" = linux-swap ]; then
 
299
                loops="/host$path"
 
300
        else
 
301
                loops="$(echo /dev/loop* /dev/loop/*)"
 
302
        fi
 
303
        for loop in $loops; do
 
304
                [ -e "$loop" ] || continue
 
305
                case $loop in
 
306
                        /dev/loop*)
 
307
                                loopfile="$(losetup "$loop")" || continue
 
308
                                # The following works with both busybox's
 
309
                                # losetup and util-linux's losetup. Yes,
 
310
                                # this is ugly.
 
311
                                loopfile="$(echo "$loopfile" | sed 's,.*\(/host/[^)]*\).*,\1,')"
 
312
                                ;;
 
313
                        *)
 
314
                                loopfile="$loop"
 
315
                                ;;
 
316
                esac
 
317
                [ "$loopfile" = "/host$path" ] || continue
 
318
 
 
319
                dirname="$(echo "$loop" | sed 's:/:=:g')"
 
320
                dev="$DEVICES/$dirname"
 
321
 
 
322
                rm -rf "$dev"
 
323
                mkdir "$dev" || autopartitioning_failed
 
324
                printf "%s" "$loop" >"$dev/device"
 
325
                printf "%s" "$1" >"$dev/size"
 
326
                echo "Loopback on $loopfile" >"$dev/model"
 
327
                echo "$loopfile" >"$dev/loop"
 
328
 
 
329
                cd "$dev"
 
330
                open_dialog OPEN "$(cat "$dev/device")"
 
331
                read_line response
 
332
                close_dialog
 
333
                if [ "$response" = failed ]; then
 
334
                        cd /
 
335
                        rm -rf "$dev"
 
336
                        autopartitioning_failed
 
337
                fi
 
338
 
 
339
                open_dialog NEW_LABEL loop
 
340
                close_dialog
 
341
 
 
342
                # find the free space
 
343
                open_dialog PARTITIONS
 
344
                free_space=
 
345
                while { read_line num id size type fs path name; [ "$id" ]; }; do
 
346
                        if [ "$fs" = free ]; then
 
347
                                free_space=$id
 
348
                                free_size=$size
 
349
                        fi
 
350
                done
 
351
                close_dialog
 
352
 
 
353
                # create partition in the free space
 
354
                [ "$free_space" ] || autopartitioning_failed
 
355
                open_dialog NEW_PARTITION primary $4 $free_space full ${1}000001
 
356
                read_line num id size type fs path name
 
357
                close_dialog
 
358
 
 
359
                shift; shift; shift; shift
 
360
                setup_partition $id $*
 
361
                break
 
362
        done
 
363
}
 
364
 
 
365
foreach_partition 'setup_loop $*'
 
366
 
 
367
db_progress STEP 1
 
368
 
 
369
update_all
 
370
 
 
371
apt-install lupin-support
 
372
 
 
373
db_progress STOP
 
374
 
 
375
exit 0