summaryrefslogtreecommitdiff
path: root/debootstrap.sh
blob: e0bbc4b19b7be5266599b9bb9e3f561da97a315a (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
#!/bin/sh
imgdir=./debootstrap
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:
  $0 init <suite>
  $0 new <suite> <new-name>
  $0 clone <suite> <source-name> <new-name>
  $0 chroot <suite> <name>
  $0 list

EOF
    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"
}

chroot_image()
{
    suite=$1
    name=$2
    [ "$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"

    unshare -f -m -p \
            chroot "$imagename".mnt \
            /bin/sh -c 'mount -t proc proc /proc; mount -t devpts devpts /dev/pts; exec /bin/bash'

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

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"
}

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

    size=1G # truncate(1) format

    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

    debootstrap_efficiently "$arch" "$variant" "$suite" \
                            "$target".mnt "${target%.btrfs}".debs.txt || die "debootstrap failed"

    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"
}

case "$1" in
    init|new|clone|list) cmd=$1; shift; $cmd "$@" ;;
    chroot) cmd=chroot_image; shift; $cmd "$@" ;;
    *) usage ;;
esac