~ubuntu-branches/ubuntu/maverick/ubuntu-dev-tools/maverick-proposed

« back to all changes in this revision

Viewing changes to mk-sbuild-lv

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2009-11-28 18:48:35 UTC
  • mfrom: (18.1.66 lucid)
  • Revision ID: james.westby@ubuntu.com-20091128184835-0ot21cscixychr6b
Tags: 0.83debian1
* Merge from Ubuntu Lucid, local Debian changes:
  - Adjust Maintainer and Uploaders.
  - Depend on python-lazr.restfulclient.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/bin/bash
2
 
# Copyright 2006-2007 (C) Canonical Ltd.
3
 
# Created by Kees Cook <kees@ubuntu.com>
4
 
# License: GPLv2
 
2
#
 
3
# Copyright 2006-2009 (C) Canonical Ltd.
 
4
# Author: Kees Cook <kees@ubuntu.com>
 
5
#
 
6
# ##################################################################
 
7
#
 
8
# This program is free software; you can redistribute it and/or
 
9
# modify it under the terms of the GNU General Public License
 
10
# as published by the Free Software Foundation; either version 3
 
11
# of the License, or (at your option) any later version.
 
12
#
 
13
# This program is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
# GNU General Public License for more details.
 
17
#
 
18
# See file /usr/share/common-licenses/GPL for more details.
 
19
#
 
20
# ##################################################################
5
21
#
6
22
# This script creates LVM snapshot chroots via schroot and sbuild.
7
23
# Much love to "man sbuild-setup", https://wiki.ubuntu.com/PbuilderHowto,
8
24
# and https://help.ubuntu.com/community/SbuildLVMHowto.
9
25
#
10
 
# It assumes that sbuild has not be installed and configured before.
11
 
#
12
 
# If using schroot earlier than 1.1.4-1, it's a good idea to apply the
13
 
# process-cleaning patch to /etc/schroot/setup.d/10mount.  Without this, any
14
 
# processes left running from the build (like cron, dbus, etc) will stop
15
 
# schroot from umounting and shutting down cleanly:
16
 
#   http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=391319
17
 
#
18
 
# If using sbuild 0.50 or earlier, and you intend to use the "arch" argument
19
 
# to do i386 builds on amd64, you will need to patch "sbuild" to correctly
20
 
# detect the chroot architecture:
21
 
#   http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=392992
22
 
#
23
 
# Version: 0.11
24
 
 
 
26
# It will deal with sbuild having not be installed and configured before.
25
27
set -e
26
28
 
 
29
# For when schroot enters the chroot, we cannot be in a directory that
 
30
# will not exist in the chroot.
 
31
cd /
 
32
 
27
33
# Make sure we've got a regular user
28
34
if [ -w /etc/passwd ]; then
29
35
    echo "Please run this script as a regular user, not root." >&2
35
41
    # Load all the packages you'll need to do work
36
42
    sudo apt-get install sbuild schroot debootstrap lvm2
37
43
    # Make sure LVM tools that operate on the snapshots have needed module
38
 
    sudo modprobe dm_snapshot
39
 
    sudo bash -c "grep ^dm_snapshot /etc/modules >/dev/null || echo dm_snapshot >> /etc/modules"
 
44
    if ! sudo dmsetup targets | grep -q ^snapshot; then
 
45
        sudo modprobe dm_snapshot
 
46
        echo dm_snapshot | sudo tee -a /etc/modules >/dev/null
 
47
    fi
40
48
    # Add self to the sbuild group
41
49
    sudo adduser "$USER" sbuild
42
50
 
43
 
    # Create some default build/log areas
44
 
    mkdir -p ~/ubuntu/build ~/ubuntu/logs
45
51
    # Prepare a usable default .sbuildrc
46
52
    if [ ! -e ~/.sbuildrc ]; then
47
53
        cat > ~/.sbuildrc <<EOM
65
71
1;
66
72
EOM
67
73
        sensible-editor ~/.sbuildrc
 
74
        # Create target directories, if needed
 
75
        eval $(egrep '^\$(build|log)_dir[       ]*=' ~/.sbuildrc | cut -c2-)
 
76
        if [ -n "$log_dir" ]; then
 
77
            mkdir -p "$log_dir"
 
78
        fi
 
79
        if [ -n "$build_dir" ]; then
 
80
            mkdir -p "$build_dir"
 
81
        fi
68
82
    else
69
83
        echo "Your ~/.sbuildrc already exists -- leaving it as-is."
70
84
    fi
81
95
    exit 1
82
96
fi
83
97
 
 
98
# Set up configurable defaults (loaded after option processing)
 
99
LV_SIZE="5G"
 
100
SNAPSHOT_SIZE="4G"
 
101
 
84
102
function usage()
85
103
{
86
104
    echo "Usage: $0 [OPTIONS] VG Release" >&2
87
105
    echo "Options:"
88
106
    echo "  --arch=ARCH                What architecture to select"
89
107
    echo "  --name=NAME                Base name for the schroot (arch is appended)"
 
108
    echo "  --personality=PERSONALITY  What personality to use (defaults to match --arch)"
90
109
    echo "  --debug                    Turn on script debugging"
 
110
    echo "  --skip-updates             Do not include -updates pocket in sources.list"
91
111
    echo "  --source-template=FILE     Use FILE as the sources.list template"
92
112
    echo "  --debootstrap-mirror=URL   Use URL as the debootstrap source"
 
113
    echo "  --distro=DISTRO            Install specific distro (defaults to 'ubuntu')"
 
114
    echo ""
 
115
    echo "Configuration (via ~/.mk-sbuild-lv.rc)"
 
116
    echo "  LV_SIZE                    Size of source LVs (default ${LV_SIZE})"
 
117
    echo "  SNAPSHOT_SIZE              Size of snapshot LVs (default ${SNAPSHOT_SIZE})"
 
118
    echo "  SCHROOT_CONF_SUFFIX        Lines to append to schroot.conf entries"
 
119
    echo "  SKIP_UPDATES               Enable --skip-updates"
 
120
    echo "  TEMPLATE_SOURCES           A template for sources.list"
 
121
    echo "  TEMPLATE_SCHROOTCONF       A template for schroot.conf stanza"
93
122
    exit 1
94
123
}
95
124
 
97
126
if [ -z "$1" ]; then
98
127
    usage
99
128
fi
100
 
OPTS=`getopt -o '' --long "help,debug,arch:,name:,source-template:,debootstrap-mirror:" -- "$@"`
 
129
OPTS=`getopt -o '' --long "help,debug,skip-updates,arch:,name:,source-template:,debootstrap-mirror:,personality:,distro:" -- "$@"`
101
130
eval set -- "$OPTS"
102
131
 
 
132
DISTRO="ubuntu"
103
133
name=""
104
134
while :; do
105
135
    case "$1" in
111
141
            # By default, use the native architecture.
112
142
            arch_opt="--arch $2"
113
143
            arch_suffix="-$2"
114
 
            shift 2
 
144
            if [ "$2" = "i386" ] || [ "$2" = "lpia" ] && [ -z "$personality" ];
 
145
            then
 
146
                personality="linux32"
 
147
            fi
 
148
            shift 2
 
149
            ;;
 
150
        --personality)
 
151
            personality="$2"
 
152
            shift 2
 
153
            ;;
 
154
        --skip-updates)
 
155
            SKIP_UPDATES="1"
 
156
            shift
115
157
            ;;
116
158
        --name)
117
159
            name="$2"
118
160
            shift 2
119
161
            ;;
120
 
        --source-template)
121
 
            TEMPLATE_SOURCES="$2"
122
 
            shift 2
123
 
            if [ ! -r $TEMPLATE_SOURCES ]; then
124
 
                echo "W: Template file $TEMPLATE_SOURCES is not readable"
125
 
                echo "W: Continuing with default sources!"
126
 
            fi
127
 
            ;;
128
 
        --debootstrap-mirror)
129
 
            DEBOOTSTRAP_MIRROR="$2"
130
 
            shift 2
131
 
            ;;
 
162
        --source-template)
 
163
            TEMPLATE_SOURCES="$2"
 
164
            shift 2
 
165
            if [ ! -r $TEMPLATE_SOURCES ]; then
 
166
                echo "W: Template file $TEMPLATE_SOURCES is not readable"
 
167
                echo "W: Continuing with default sources!"
 
168
            fi
 
169
            ;;
 
170
        --debootstrap-mirror)
 
171
            DEBOOTSTRAP_MIRROR="$2"
 
172
            shift 2
 
173
            ;;
 
174
        --distro)
 
175
            DISTRO="$2"
 
176
            shift 2
 
177
            ;;
132
178
        --)
133
179
            shift
134
180
            break
157
203
CHROOT_PATH="/dev/$VG/$CHROOT_LV"
158
204
CHROOT_NAME="${name}${arch_suffix}"
159
205
 
 
206
# Load customizations
 
207
if [ -r ~/.mk-sbuild-lv.rc ]; then
 
208
    . ~/.mk-sbuild-lv.rc
 
209
fi
 
210
 
160
211
# Does the specified VG exist?  (vgdisplay doesn't set error codes...)
161
212
if [ `sudo vgdisplay -c "$VG" | wc -l` -eq 0 ]; then
162
213
    exit 1 
163
214
fi
164
215
 
165
216
# Is the specified release known to debootstrap?
166
 
if [ ! -f "/usr/lib/debootstrap/scripts/$RELEASE" ]; then
 
217
if [ ! -r "/usr/share/debootstrap/scripts/$RELEASE" ]; then
167
218
    echo "Specified release not known to debootstrap" >&2
168
219
    exit 1
169
220
else
170
221
    variant_opt="--variant=buildd"
171
222
fi
172
223
 
 
224
BUILD_PKGS="build-essential fakeroot devscripts apt-utils"
 
225
# Handle distro-specific logic, unknown to debootstrap
 
226
case "$DISTRO" in
 
227
ubuntu)
 
228
    # are we doing an lpia build?
 
229
    if [ -z "$DEBOOTSTRAP_MIRROR" ] && [ "$arch_opt" = "--arch lpia" ]; then
 
230
        DEBOOTSTRAP_MIRROR="http://ports.ubuntu.com/"
 
231
    fi
 
232
    if [ -z "$DEBOOTSTRAP_MIRROR" ]; then
 
233
        DEBOOTSTRAP_MIRROR="http://archive.ubuntu.com/ubuntu"
 
234
    fi
 
235
    if [ -z "$COMPONENTS" ]; then
 
236
        COMPONENTS="main restricted universe multiverse"
 
237
    fi
 
238
    if [ -z "$SOURCES_SECURITY_SUITE" ]; then
 
239
        SOURCES_SECURITY_SUITE="RELEASE-updates"
 
240
    fi
 
241
    if [ -z "$SOURCES_SECURITY_URL" ]; then
 
242
        SOURCES_SECURITY_URL="http://security.ubuntu.com/ubuntu"
 
243
    fi
 
244
    # Add edgy+ buildd tools
 
245
    if [ "$RELEASE" != "breezy" ] && [ "$RELEASE" != "dapper" ]; then
 
246
        # Disable recommends for a smaller chroot (gutsy and later only)
 
247
        BUILD_PKGS="--no-install-recommends $BUILD_PKGS"
 
248
        # Add buildd tools
 
249
        BUILD_PKGS="$BUILD_PKGS pkg-create-dbgsym pkgbinarymangler"
 
250
    fi
 
251
    ;;
 
252
debian)
 
253
    if [ -z "$DEBOOTSTRAP_MIRROR" ]; then
 
254
        DEBOOTSTRAP_MIRROR="http://ftp.debian.org/debian"
 
255
    fi
 
256
    if [ -z "$COMPONENTS" ]; then
 
257
        COMPONENTS="main non-free contrib"
 
258
    fi
 
259
    # Debian only performs security updates
 
260
    SKIP_UPDATES=1
 
261
    if [ -z "$SOURCES_SECURITY_SUITE" ]; then
 
262
        SOURCES_SECURITY_SUITE="RELEASE/updates"
 
263
    fi
 
264
    if [ -z "$SOURCES_SECURITY_URL" ]; then
 
265
        SOURCES_SECURITY_URL="http://security.debian.org/"
 
266
    fi
 
267
    # Unstable (aka "sid") does not have a security repository
 
268
    if [ "$RELEASE" = 'unstable' ] || [ "$RELEASE" = 'sid' ]; then
 
269
        SKIP_SECURITY=1
 
270
    fi
 
271
    ;;
 
272
*)
 
273
    echo "Unknown --distro '$DISTRO': aborting" >&2
 
274
    exit 1
 
275
    ;;
 
276
esac
 
277
 
173
278
# Allocate the "golden" chroot LV
174
 
sudo lvcreate -n "$CHROOT_LV" -L 5G "$VG"
175
 
sudo mkfs -t ext3 "$CHROOT_PATH"
 
279
sudo lvcreate -n "$CHROOT_LV" -L "$LV_SIZE" "$VG"
 
280
sudo mkfs -t ext4 "$CHROOT_PATH"
176
281
 
177
282
# Mount and debootstrap the chroot
178
283
MNT=`mktemp -d -t schroot-XXXXXX`
187
292
    cat "$TEMPLATE_SOURCES" > "$TEMP_SOURCES"
188
293
else
189
294
    cat > "$TEMP_SOURCES" <<EOM
190
 
deb http://archive.ubuntu.com/ubuntu RELEASE main restricted universe multiverse
191
 
deb-src http://archive.ubuntu.com/ubuntu RELEASE main restricted universe multiverse
192
 
deb http://archive.ubuntu.com/ubuntu RELEASE-updates main restricted universe multiverse
193
 
deb-src http://archive.ubuntu.com/ubuntu RELEASE-updates main restricted universe multiverse
194
 
deb http://security.ubuntu.com/ubuntu RELEASE-security main restricted universe multiverse
195
 
deb-src http://security.ubuntu.com/ubuntu RELEASE-security main restricted universe multiverse
196
 
EOM
 
295
deb ${DEBOOTSTRAP_MIRROR} RELEASE ${COMPONENTS}
 
296
deb-src ${DEBOOTSTRAP_MIRROR} RELEASE ${COMPONENTS}
 
297
EOM
 
298
    if [ -z "$SKIP_UPDATES" ]; then
 
299
        cat >> "$TEMP_SOURCES" <<EOM
 
300
deb ${DEBOOTSTRAP_MIRROR} RELEASE-updates ${COMPONENTS}
 
301
deb-src ${DEBOOTSTRAP_MIRROR} RELEASE-updates ${COMPONENTS}
 
302
EOM
 
303
    fi
 
304
    if [ -z "$SKIP_SECURITY" ]; then
 
305
        cat >> "$TEMP_SOURCES" <<EOM
 
306
deb ${SOURCES_SECURITY_URL} ${SOURCES_SECURITY_SUITE} ${COMPONENTS}
 
307
deb-src ${SOURCES_SECURITY_URL} ${SOURCES_SECURITY_SUITE} ${COMPONENTS}
 
308
EOM
 
309
    fi
197
310
fi
198
311
cat "$TEMP_SOURCES" | sed -e "s|RELEASE|$RELEASE|g" | \
199
312
    sudo bash -c "cat > $MNT/etc/apt/sources.list"
218
331
source-root-groups=root,sbuild,admin
219
332
device=CHROOT_PATH
220
333
mount-options=-o noatime
221
 
lvm-snapshot-options=--size 4G
 
334
lvm-snapshot-options=--size SNAPSHOT_SIZE
222
335
run-setup-scripts=true
223
336
run-exec-scripts=true
224
337
EOM
225
338
fi
 
339
if [ ! -z "$personality" ]; then
 
340
    echo "personality=$personality" >> "$TEMP_SCHROOTCONF"
 
341
fi
 
342
if [ ! -z "$SCHROOT_CONF_SUFFIX" ]; then
 
343
    echo "$SCHROOT_CONF_SUFFIX" >> "$TEMP_SCHROOTCONF"
 
344
fi
226
345
cat "$TEMP_SCHROOTCONF" | sed \
227
346
        -e "s|CHROOT_NAME|$CHROOT_NAME|g" \
228
347
        -e "s|CHROOT_PATH|$CHROOT_PATH|g" \
 
348
        -e "s|SNAPSHOT_SIZE|$SNAPSHOT_SIZE|g" \
229
349
        | \
230
350
        sudo bash -c "cat >> /etc/schroot/schroot.conf"
231
351
rm -f "$TEMP_SCHROOTCONF"
232
352
# Create image finalization script
233
 
BUILD_PKGS="build-essential fakeroot devscripts"
234
 
# Add edgy+ buildd tools
235
 
if [ "$RELEASE" != "breezy" ] && [ "$RELEASE" != "dapper" ]; then
236
 
    BUILD_PKGS="$BUILD_PKGS pkg-create-dbgsym pkgbinarymangler"
237
 
fi
238
353
sudo bash -c "cat >> $MNT/finish.sh" <<EOM
239
354
#!/bin/bash
240
355
#set -x
241
356
set -e
 
357
export http_proxy=${http_proxy}
242
358
# Reload package lists
243
359
apt-get update || true
244
360
# Pull down signature requirements
245
 
#apt-get -y --force-yes install gnupg ubuntu-keyring
 
361
apt-get -y --force-yes install gnupg ${DISTRO}-keyring
246
362
# Reload package lists
247
363
apt-get update || true
248
364
# Disable debconf questions so that automated builds won't prompt
249
365
echo set debconf/frontend Noninteractive | debconf-communicate
250
366
echo set debconf/priority critical | debconf-communicate
251
367
# Install basic build tool set, trying to match buildd
252
 
apt-get -y install $BUILD_PKGS
 
368
apt-get -y --force-yes install $BUILD_PKGS
253
369
# Set up expected /dev entries
254
 
ln -s /proc/self/fd/0 /dev/stdin
255
 
ln -s /proc/self/fd/1 /dev/stdout
256
 
ln -s /proc/self/fd/2 /dev/stderr
 
370
if [ ! -r /dev/stdin ];  then ln -s /proc/self/fd/0 /dev/stdin;  fi
 
371
if [ ! -r /dev/stdout ]; then ln -s /proc/self/fd/1 /dev/stdout; fi
 
372
if [ ! -r /dev/stderr ]; then ln -s /proc/self/fd/2 /dev/stderr; fi
257
373
# Clean up
258
374
apt-get clean
259
375
rm /finish.sh
262
378
sudo umount "$MNT"
263
379
rmdir "$MNT"
264
380
# Run finalization script on the "golden" copy via schroot.
265
 
(cd / && schroot -c "$CHROOT_NAME"-source -u root /finish.sh)
 
381
schroot -c "$CHROOT_NAME"-source -u root /finish.sh
266
382
 
267
383
# Finished
268
384
echo ""
269
385
echo "Done building $CHROOT_NAME."
270
386
echo ""
271
 
echo " To UPDATE the golden image: schroot -c ${CHROOT_NAME}-source -u root"
 
387
echo " To CHANGE the golden image: schroot -c ${CHROOT_NAME}-source -u root"
272
388
echo " To ENTER an image snapshot: schroot -c ${CHROOT_NAME}"
273
389
echo " To BUILD within a snapshot: sbuild -d ${CHROOT_NAME} PACKAGE*.dsc"
274
390
echo ""