summaryrefslogtreecommitdiff
path: root/debootstrap.sh
blob: 2ef288bced71a4ce9e88f3319018d26ef7fdfd45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/bin/sh
imgdir=./debootstrap
default_sh_command=bash
default_chroot_command=/bin/bash
default_image_size=1500M # truncate(1) format

warn() { printf 'Warning: %s\n' "$*" >&2; }
die() { printf 'Error: %s\n' "$*" >&2; exit 1; }
[ -d "$imgdir" ] || die "directory does not exist: $imgdir"

usage()
{
    cat <<EOF >&2
Usage:

# creation & deletion
  $0 init <suite>
  $0 new <suite> <new-name>
  $0 clone <suite> <source-name> <new-name>
  $0 rm <suite> <source-name>

# modification
  $0 add      <suite> <name> <package> [packages...]
  $0 chroot   <suite> <name> [command] [args...]
  $0 sh       <suite> <name> [command] [args...]
  $0 with_mp  <suite> <name> <command> [args...]
  $0 with_img <suite> <name> <command> [args...]

# inspection
  $0 list
  $0 show unpackaged <suite> <name>
  $0 show unpackaged-du <suite> <name> ["filtered"]

EOF
    printf "Currently running suite:\n  %s\n" "$(lsb_release -cs)"
    list
    exit 1
}

list()
{
    local f suite name header_printed=
    for f in $imgdir/*.btrfs; do
        [ -e "$f" ] || continue
        f=${f##*/}
        f=${f%.btrfs}
        case "$f" in *.*) continue ;; esac
        suite=${f%%-*}
        [ "$header_printed" ] || echo 'Existing initialized suites:'
        printf '  %s\n' "$suite"
        header_printed=y
    done
    header_printed=
    for f in $imgdir/*.*.btrfs; do
        [ -e "$f" ] || continue
        f=${f##*/}
        f=${f%.btrfs}
        suite=${f%%-*}
        name=${f#*.}
        [ "$header_printed" ] || echo 'Existing images:'
        printf '  %s %s\n' "$suite" "$name"
        header_printed=y
    done
}

validate_suite()
{
    case "$1" in
        jessie|stretch|sid) return 0 ;;
        *) return 1 ;;
    esac
}

i_am_root() { [ "$(id -u)" = 0 ]; }

btrfs_show_subvolume_id()
{
    local result path="$1"
    result=$(btrfs subvolume show "$path" | sed -n -e 's/^[ \t]*Subvolume ID:[ \t]*//p; s/.*is toplevel subvolume/5/p')
    if [ "$result" ]
    then printf '%s\n' "$result"
    else false
    fi
}

mkfs_btrfs()
{
    local target="$1"
    mountpoint="$2" # can't be local because of trap

    mkfs.btrfs "$target"                                    || die "mkfs.btrfs failed"
    mount -t btrfs "$target" "$mountpoint"                  || die "mount failed"
    trap 'umount "$mountpoint"' EXIT

    btrfs subvolume create "$mountpoint"/root               || die "command 'btrfs subvolume create' failed"
    mkdir -p "$mountpoint"/root/var/cache/apt
    btrfs subvolume create "$mountpoint"/root/var/cache/apt/archives || die "command 'btrfs subvolume create' failed"
    subvol_id=$(btrfs_show_subvolume_id "$mountpoint"/root)          || die "could not find btrfs subvolume name"
    btrfs subvolume set-default "$subvol_id" "$mountpoint"           || die "command 'btrfs subvolume set-default' failed"

    trap - EXIT
    umount "$mountpoint"
}

suite_to_basename()
{
    suite=$1
    variant=minbase
    arch=$(dpkg-architecture -q DEB_HOST_ARCH) || die "command 'dpkg-architecture' failed"

    printf '%s\n' "$suite-$variant-$arch.btrfs"
}

suite_name_to_imagename()
{
    suite=$1
    name=$2
    variant=minbase
    arch=$(dpkg-architecture -q DEB_HOST_ARCH) || die "command 'dpkg-architecture' failed"

    printf '%s/%s-%s-%s.%s.btrfs\n' "$imgdir" "$suite" "$variant" "$arch" "$name"
}

show_unpackaged_helper()
{
    suite=$1
    name=$2
    imagename=$(suite_name_to_imagename "$suite" "$name")

    sh_image "$suite" "$name" find -type f | sed 's?^\./??' | sort > ${imagename}.find.txt
    sh_image "$suite" "$name" sh -c 'sort -u var/lib/dpkg/info/*.list' | sed -e 's?^/??' -e 's?^\.$/??' > ${imagename}.dpkg-list.txt
    comm -23 ${imagename}.find.txt ${imagename}.dpkg-list.txt > ${imagename}.unpackaged.txt
}

show_unpackaged()
{
    show_unpackaged_helper "$@"
    cat "${imagename}.unpackaged.txt"
}

show_unpackaged_du()
{
    show_unpackaged_helper "$@"
    if [ "$3" = filtered ]; then
        filter='var/lib/dpkg/|var/lib/apt/|var/log/|var/cache/apt/archives'
        cat "${imagename}.unpackaged.txt" | egrep -v "^($filter)" | sh_image "$1" "$2" xargs du -csh
    else
        cat "${imagename}.unpackaged.txt" | sh_image "$1" "$2" xargs du -csh
    fi
}

with_img()
{
    local suite="$1" name="$2" command="$3"
    shift 3
    [ "$suite" -a "$name" ] || usage
    imagename=$(suite_name_to_imagename "$suite" "$name")
    [ -e "$imagename" ] || die "no such file: $imagename"

    "$command" "$@" "$imagename"
}

run_command_with_mountpoint()
{
    local suite="$1" name="$2" command="$3"
    shift 3
    [ "$suite" -a "$name" ] || usage
    imagename=$(suite_name_to_imagename "$suite" "$name")
    [ -e "$imagename" ] || die "no such file: $imagename"
    [ -d "$imagename".mnt ] || mkdir "$imagename".mnt || die "mkdir"
    mountpoint -q "$imagename".mnt || mount "$imagename" "$imagename".mnt || die "mount"

    "$command" "$imagename".mnt "$@"

    r=$?
    umount "$imagename".mnt
    rmdir "$imagename".mnt
    return $r
}

rotate_args_left()
{
    local fst="$1" snd="$2"
    shift 2
    "$snd" "$@" "$fst"
}

add()
{
    export SKIP_UPDATE=y
    suite=$1
    name=$2
    shift 2
    running_suite=$(lsb_release -cs) || die 'lsb_release failed'
    if [ "$suite" = "$running_suite" -a -x ./src/selfstrap ]; then
        with_mp "$suite" "$name" ./src/selfstrap "$@" -t
    else
        exit 1
    fi
}

with_mp()
{
    suite=$1
    name=$2
    shift 2
    if [ $# = 0 ]; then
        cmd=echo
    else
        cmd=$1
        shift
    fi
    run_command_with_mountpoint "$suite" "$name" rotate_args_left "$cmd" "$@"
}



cd_then_run()
{
    (cd "$1"; shift; "$@")
}

sh_image()
{
    suite=$1
    name=$2
    shift 2
    if [ $# = 0 ]; then
        set -- $default_sh_command
    fi
    run_command_with_mountpoint "$suite" "$name" cd_then_run "$@"
}

chroot_image()
{
    suite=$1
    name=$2
    shift 2
    [ $# -gt 0 ] || set -- $default_chroot_command
    cgdir=/sys/fs/cgroup/pids/vm-$suite-$name
    mkdir "$cgdir"
    set -- \
        cgexec -g pids:vm-$suite-$name \
        unshare -f -m -p \
                chroot . \
                /bin/sh -c 'mount -t proc proc /proc; mount -t devpts devpts /dev/pts; exec "$@"' sh \
                "$@"
    sh_image "$suite" "$name" "$@"

    kill_cgroup "$cgdir"
}

kill_cgroup()
{
    (
        cgdir="$1"

        # exec >/dev/null 2>&1

        kill=
        sleep=0.2
        for n in $(seq 1 20); do

            [ ! -d "$cgdir" ] || rmdir "$cgdir" && return

            pids=$(cat "$cgdir"/tasks)
            [ -z "$pids" ] || continue

            kill $kill $pids
            sleep $sleep
            [ "$n" -eq 5 ] && sleep=1
            [ "$n" -eq 7 ] && sleep=3
            [ "$n" -eq 10 ] && kill=-KILL
        done
        return 1
    )
}

clone()
{
    suite=$1
    source_name=$2
    name=$3
    [ "$suite" -a "$name" -a "$source_name" ] || usage
    image_basename=$imgdir/$(suite_to_basename "$suite") || die 'suite_to_basename'

    source=${image_basename%.btrfs}.$source_name.btrfs
    dest=${image_basename%.btrfs}.$name.btrfs

    [ -e "$source" ] || die "source image not found -- run '$0 new $suite $source_name' to create it"
    [ ! -e "$dest" ] || die "file exists: $dest"
    cp --reflink=always "$source" "$dest"
}

new()
{
    suite=$1
    name=$2
    [ "$suite" -a "$name" ] || usage
    image_basename=$imgdir/$(suite_to_basename "$suite") || die 'suite_to_basename'

    source=$image_basename
    dest=${source%.btrfs}.$name.btrfs

    [ -e "$source" ] || die "source image not found -- run '$0 init $suite' to create it"
    [ ! -e "$dest" ] || die "file exists: $dest"
    cp --reflink=always "$source" "$dest"
}

rm_image()
{
    suite=$1
    name=$2
    [ "$suite" -a "$name" ] || usage
    image_basename=$imgdir/$(suite_to_basename "$suite") || die 'suite_to_basename'
    source=$image_basename
    dest=${source%.btrfs}.$name.btrfs

    rm -i "$dest"
}

init()
{
    suite=$1
    variant=minbase
    arch=$(dpkg-architecture -q DEB_HOST_ARCH) || die "command 'dpkg-architecture' failed"
    running_suite=$(lsb_release -cs) || die 'lsb_release failed'

    size=$default_image_size

    i_am_root || die 'you are not root'

    validate_suite "$suite" || die "invalid suite: '$suite'"

    target=$imgdir/$suite-$variant-$arch.btrfs
    [ -e "$target" ] && return
    [ -e "$target".tmp ] && die "refusing to overwrite existing temporary file: $target.tmp"

    [ -d "$target".mnt ] || mkdir "$target".mnt || die "could not create directory $target.mnt"

    truncate -s "$size" "$target".tmp          || die "truncate failed"
    mkfs_btrfs "$target".tmp "$target".mnt     || die "btrfs filesystem creation failed"
    mount -t btrfs "$target".tmp "$target".mnt || die "mount failed"
    trap 'umount "$target.mnt"' EXIT

    if [ "$suite" = "$running_suite" -a -x ./src/selfstrap ]; then
        # TODO:
        # 1. Use a --chroot arg that will set up cgroups
        # 2. Use --packages arg to implement variant
        # 3. Allow selfstrap to run on already-initialized image
        ./src/selfstrap -t "$target.mnt" || die "selfstrap failed"
    else
        debootstrap_efficiently "$arch" "$variant" "$suite" \
                                "$target".mnt "${target%.btrfs}".debs.txt || die "debootstrap failed"
    fi

    if umount "$target".mnt
    then trap - EXIT
    else warn "umount failed"
    fi

    mv "$target".tmp "$target"
}

debootstrap_efficiently()
{
    local arch="$1" variant="$2" suite="$3" target="$4" savedebs="$5"
    set -- --arch "$arch" --variant "$variant" "$suite" "$target"
    debs=$(debootstrap --print-debs --keep-debootstrap-dir "$@" | tee "$savedebs") || die "debootstrap failed"
    for deb in $debs; do
        cp /var/cache/apt/archives/${deb}_* $target/var/cache/apt/archives/
    done

    debootstrap "$@" || die "debootstrap failed"
}

show()
{
    case "$1" in
        unpackaged) shift; show_unpackaged "$@" ;;
        unpackaged-du) shift; show_unpackaged_du "$@" ;;
        *) usage ;;
    esac
}

case "$1" in
    init|new|clone|list|show|add|with_mp|with_img) cmd=$1; shift; $cmd "$@" ;;
    chroot|sh|rm) cmd=${1}_image; shift; $cmd "$@" ;;
    *) usage ;;
esac