~ubuntu-virt/vmbuilder/trunk

« back to all changes in this revision

Viewing changes to ubuntu-vm-builder

  • Committer: Soren Hansen
  • Date: 2008-06-27 12:47:26 UTC
  • Revision ID: soren.hansen@canonical.com-20080627124726-kj690sepircudc3h
Import python rewrite.. It's not quite at a useful point yet (only cli+kvm+hardy is in a usable state), but it's getting there..

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/bash
2
 
#
3
 
#    Ubuntu Jeos image builder
4
 
#    Copyright (C) 2007-2008 Canonical Ltd.
5
 
#
6
 
#    Authors: Soren Hansen <soren@canonical.com>
7
 
#             Neal McBurnett <neal@mcburnett.org>
8
 
#             Michael Vogt <michael.vogt@ubuntu.com>
9
 
#             Mike Frisch
10
 
#             Nick Barcet <nick.barcet@ubuntu.com>
11
 
#             Onno Benschop <onno@itmaze.com.au>
12
 
#
13
 
#    This program is free software; you can redistribute it and/or modify
14
 
#    it under the terms of the GNU General Public License as published by
15
 
#    the Free Software Foundation; either version 2 of the License, or
16
 
#    (at your option) any later version.
17
 
#
18
 
#    This program is distributed in the hope that it will be useful,
19
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 
#    GNU General Public License for more details.
22
 
#
23
 
#    You should have received a copy of the GNU General Public License along
24
 
#    with this program; if not, write to the Free Software Foundation, Inc.,
25
 
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26
 
 
27
 
# Todo:
28
 
#  create launchjeos command for vmware vm's
29
 
#  review security of running as root, messing with $DESTINATION
30
 
 
31
 
test -n "$TMPDIR" || TMPDIR=""
32
 
# make sure variables are initialized and script quits when error are encountered
33
 
set -o nounset
34
 
set -o errexit
35
 
 
36
 
# display debug info
37
 
VERBOSE=""
38
 
info () {
39
 
    if [ -n "$VERBOSE" ]; then
40
 
        echo "$@"
41
 
    fi
42
 
}
43
 
 
44
 
# Keep perl inside the chroot quiet
45
 
HOSTLANG=$LANG
46
 
LANG=C
47
 
 
48
 
# array to hold list of raw "files" to use
49
 
declare -a RAW
50
 
declare -i NUMRAW
51
 
let NUMRAW=0 || true
52
 
 
53
 
usage () {
54
 
        cat <<EOT
55
 
usage: ubuntu-vm-builder <vm> <suite>   
56
 
                            [--addpkg PKG]
57
 
                            [(-a|--arch) [(amd64|i386|lpia]]
58
 
                            [(-d|--dest) <destination (directory)>]
59
 
                            [(-o|--overwrite)]
60
 
                            [-c PATH]
61
 
                            [--domain DOMAIN]
62
 
                            [-h|--help]
63
 
                            [--hostname NAME]
64
 
                            [--mem num]
65
 
                            [--mirror URL]
66
 
                            [--security-mirror URL]
67
 
                            [--removepkg PKG]
68
 
                            [(-t|--tmp) temp_dir]
69
 
                            [--tmpfs options]
70
 
                            [--user username]
71
 
                            [--name fullname]
72
 
                            [--pass password]
73
 
                            [--raw path]
74
 
                            [--part partitionfile]
75
 
                            [--rootsize size]
76
 
                            [--optsize size]
77
 
                            [--swapsize size]
78
 
                            [--kernel-flavour <flavour>]
79
 
                            [--components <comma separated list of components>]
80
 
                            [--ssh-key <public key file>]
81
 
                            [--ssh-user-key <public key file>]
82
 
                            [--exec <script to execute>]
83
 
                            [--firstboot <script to execute>]
84
 
                            [--firstlogin <script to execute>]
85
 
                            [--ip address [
86
 
                            [--mask value]
87
 
                            [--net value]
88
 
                            [--bcast value]
89
 
                            [--gw address]
90
 
                            [--dns address] ]
91
 
 
92
 
vm                 Generate image for the specified virtualisation software.
93
 
                   Valid choices are: vmw6 vmserver vbox qemu kvm
94
 
suite              Use the specified Ubuntu suite.
95
 
                   Valid options: intrepid, intrepid, hardy, gutsy, feisty, edgy, dapper
96
 
--addpkg PKG       Install PKG into the guest (can be specfied multiple times)
97
 
-a, --arch ARCH    Specify the target architecture.
98
 
                   Valid options: amd64 i386 lpia (defaults to host arch)
99
 
-d, --dest         Specify the destination directory.
100
 
                   Default: ubuntu-vm-<SUITE>-<ARCH>-<HOSTNAME>
101
 
-o, --overwrite    Force overwrite of destination directory if it already exist
102
 
-c PATH            Specify a optional configuration file (default: ~/.ubuntu-vm-builder)
103
 
--domain DOMAIN    Set DOMAIN as the domain name of the guest. Default:
104
 
                   The domain of the machine running this script.
105
 
-h, --help         This help text.
106
 
-v, --verbose      Show a lot of information as the vm is generated.
107
 
--hostname NAME    Set NAME as the hostname of the guest. Default: ubuntu
108
 
                   Also uses this name as the VM name
109
 
--mem NN           Assign NN megabytes of memory to the guest vm.
110
 
                   Default: 128
111
 
--mirror URL       Use Ubuntu mirror at URL instead of the default, which
112
 
                   is http://archive.ubuntu.com/ubuntu for official arches
113
 
                   and http://ports.ubuntu.com/ubuntu-ports otherwise
114
 
--security-mirror URL
115
 
                   Use Ubuntu security mirror at URL instead of the default,
116
 
                   which is http://security.ubuntu.com/ubuntu for official
117
 
                   arches and http://ports.ubuntu.com/ubuntu-ports otherwise
118
 
--iso PATH         Use an iso image as the source for installation of file.
119
 
                   Full path to the iso must be provided. If --mirror is also
120
 
                   provided, it will be used in the final sources.list of the vm.
121
 
                   This requires suite and kernel parameter to match what is 
122
 
                   available on the iso, obviously.
123
 
--removepkg PKG    Remove PKG from the guest (can be specfied multiple times)
124
 
-t, --tmp  TMPDIR  Use TMPDIR as temporary working area for the image
125
 
                   generation. Defaults \$TMPDIR if it is defined, or /tmp otherwise.
126
 
                   If it is set to "-", working data will go in same directory as -d 
127
 
                   if specified with a full path or the current directory if not.
128
 
--tmpfs OPTIONS    Use a tmpfs as the working directory, specifying its size or
129
 
                   "-" to use tmpfs default (suid,dev,size=1G).
130
 
--user USERNAME    Set the name of the user to be added. Default: ubuntu.
131
 
--name FULLNAME    Set the full name of the user to be added. Default: Ubuntu.
132
 
--pass PASSWORD    Set the password for the user. Default: ubuntu.
133
 
--raw PATH         Specify a raw file or device to install in.  Can be specified
134
 
                   multiple time in conjunction with --part, paths will be used in
135
 
                   the order presented. In any case you are to insure that the raw
136
 
                   element(s) specified have enough free space.
137
 
--part PATH        Allows to specify a partition table in partfile pointed by PATH
138
 
                   each line of partfile should specify (root first):
139
 
                      mountpoint size
140
 
                   where size is in megabytes. You can have up to 4 virtual
141
 
                                   disks, a new disk starts on a line with ---
142
 
                   ie :
143
 
                      root 1000
144
 
                      /opt 1000
145
 
                      swap 256
146
 
                      ---
147
 
                      /var 2000
148
 
                      /log 1500
149
 
The following three options are not used if --part is specified:
150
 
--rootsize SIZE    The size in MB of the root filesystem (default 4096)
151
 
--optsize SIZE     The size in MB of the /opt filesystem. If not set, no /opt
152
 
                   filesystem will be added.
153
 
--swapsize SIZE    The size in MB of the swap partition (default 1024)
154
 
--kernel-flavour FLAVOUR
155
 
                   The kernel image flavour to install (default: virtual for
156
 
                   >= gutsy on i386, lpia on lpia, server otherwise)
157
 
--components COMP  A comma seperated list of distro components to include
158
 
                   (e.g. main,universe). This defaults to "main"
159
 
--ssh-key PATH     Add the given ssh public key file (absolute path)
160
 
                   to root's authorized keys and install openssh-server
161
 
--in-place         Install directly into the filesystem images. This is needed
162
 
                   if your \$TMPDIR is nodev and/or nosuid, but will result in
163
 
                   slightly larger file system images.
164
 
                   (WARNING: this has strong security implications)
165
 
--ssh-user-key PATH 
166
 
                   Add the given ssh key file (absolute path)
167
 
                   to the user's authorized key and install openssh-server.
168
 
--exec PATH        Run the given script file. You can use 
169
 
                   'chroot root <cmd>' to execute code in the guest.
170
 
                   See 'man ubuntu-vm-builder' for more explanations
171
 
--firstboot PATH   Specify a script that will be copied into the guest and
172
 
                   executed the first time the machine boots.  This script
173
 
                   must not be interactive.
174
 
--firstlogin PATH  Specify a script that will be copied into the guest and
175
 
                   will be executed the first time the user logs in. This
176
 
                   script can be interactive.
177
 
--ip ADDRESS       Ip address in dotted form
178
 
                   (defaults to dhcp if not specified)
179
 
 
180
 
Options below are discarded if --ip is not specified
181
 
--mask VALUE       IP mask in dotted form (default: 255.255.255.0)
182
 
--net VALUE        IP net address (default: X.X.X.0)
183
 
--bcast VALUE      IP broadcast (default: X.X.X.255)
184
 
--gw ADDRESS       Gateway address (default: X.X.X.1)
185
 
--dns ADDRESS      Name server address (default: X.X.X.1)
186
 
 
187
 
('*' denotes default option)
188
 
 
189
 
EOT
190
 
 
191
 
        #
192
 
        # Emit usage info specific to each vm type
193
 
        #
194
 
        for vm in /usr/share/ubuntu-vm-builder/vms/*
195
 
        do
196
 
                vm="`basename "$vm"`"
197
 
                echo "Options specific for vm type $vm:"
198
 
                /usr/share/ubuntu-vm-builder/vms/$vm usage
199
 
        done
200
 
 
201
 
cat <<EOT
202
 
 
203
 
ubuntu-vm-builder is Copyright (C) 2007-2008 Canonical Ltd. and
204
 
written by Soren Hansen <soren@canonical.com>.
205
 
EOT
206
 
}
207
 
 
208
 
if [ ! "`id -u`" = 0 ]
209
 
then
210
 
        echo "$0 must be run as root (e.g. by means of sudo)"
211
 
        exit 1
212
 
fi
213
 
 
214
 
if [ $# -lt 2 ]
215
 
then
216
 
        echo "Please specify vm and suite"
217
 
        usage
218
 
        exit 1
219
 
fi
220
 
 
221
 
#
222
 
# Import a stack of sanity checks
223
 
#
224
 
. /usr/share/ubuntu-vm-builder/sanity-checks
225
 
 
226
 
#
227
 
# First arg is the vm tech to use
228
 
#
229
 
check_vm "$1"
230
 
VM="$1"
231
 
. /usr/share/ubuntu-vm-builder/vms/$VM
232
 
shift
233
 
 
234
 
#
235
 
# Second arg is the Ubuntu suite to install
236
 
#
237
 
check_suite "$1"
238
 
SUITE="$1"
239
 
. /usr/share/ubuntu-vm-builder/suites/$SUITE
240
 
shift
241
 
 
242
 
# read default config from ~/.ubuntu-vm-builder and initialize variables
243
 
. /usr/share/ubuntu-vm-builder/config-handler
244
 
 
245
 
set +e; trap "usage;exit 1" ERR
246
 
TEMP=`getopt -o a:d:ohs:t:vc: --long addpkg:,arch:,dest:,overwrite,domain:,help,hostname:,mem:,mirror:,security-mirror:,iso:,removepkg:,suite:,tmp:,tmpfs:,vm:,user:,name:,pass:,part:,rootsize:,swapsize:,optsize:,raw:,ip:,mask:,net:,bcast:,gw:,dns:,kernel-flavour:,components:,ssh-key:,ssh-user-key:,no-opt,exec:,firstboot:,firstlogin:,verbose,in-place,$vm_getopt_args  -- "$@"` 
247
 
trap - ERR; set -e
248
 
eval set -- "$TEMP"
249
 
 
250
 
while true
251
 
do
252
 
        case "$1" in 
253
 
                --addpkg)
254
 
                        PKGCMD="$PKGCMD $2"
255
 
                        shift 2
256
 
                ;;
257
 
                -a|--arch)
258
 
                        validate_arch $2
259
 
                        ARCH="$2"
260
 
                        shift 2
261
 
                ;;
262
 
                -d|--dest)
263
 
                        check_dest_dir "$2"
264
 
                        if [ ! "${2#/}" = "${2}" ]
265
 
                        then
266
 
                                # Absolute path given
267
 
                                DESTINATION="$2"
268
 
                        else
269
 
                                # Relative path given
270
 
                                DESTINATION="${PWD}/$2"
271
 
                        fi
272
 
                        shift 2
273
 
                ;;
274
 
                -o|--overwrite)
275
 
                        OVERWRITE="YES"
276
 
                        shift 1
277
 
                ;;
278
 
                --domain)
279
 
                        DOMAIN="$2"
280
 
                        shift 2
281
 
                ;;
282
 
                --hostname)
283
 
                        VMHOSTNAME="$2"
284
 
                        shift 2
285
 
                ;;
286
 
                --mem)
287
 
                        MEM="$2"
288
 
                        shift 2
289
 
                ;;
290
 
                --mirror)
291
 
                        MIRROR="$2"
292
 
                        shift 2
293
 
                ;;
294
 
                --security-mirror)
295
 
                        SECURITY_MIRROR="$2"
296
 
                        shift 2
297
 
                ;;
298
 
                --iso)
299
 
                        ISO="$2"
300
 
                        shift 2
301
 
                ;;
302
 
                --removepkg)
303
 
                        PKGCMD="$PKGCMD ${2}-"
304
 
                        shift 2
305
 
                ;;
306
 
                -t|--tmp)
307
 
                        VMTMPDIR="$2"
308
 
                        shift 2
309
 
                ;;
310
 
                --tmpfs)
311
 
                        TMPFS="$2"
312
 
                        shift 2
313
 
                ;;
314
 
                --vm)
315
 
                        check_vm "$2"
316
 
                        shift 2
317
 
                ;;
318
 
                --user)
319
 
                        VMUSER="$2"
320
 
                        shift 2
321
 
                ;;
322
 
                --name)
323
 
                        NAME="$2"
324
 
                        shift 2
325
 
                ;;
326
 
                --pass)
327
 
                        PASS="$2"
328
 
                        shift 2
329
 
                ;;
330
 
                --part)
331
 
                        PARTFILE="$2"
332
 
                        shift 2
333
 
                ;;
334
 
                --rootsize)
335
 
                        ROOTSIZE="$2"
336
 
                        shift 2
337
 
                ;;
338
 
                --swapsize)
339
 
                        SWAPSIZE="$2"
340
 
                        shift 2
341
 
                ;;
342
 
                --optsize)
343
 
                        OPTSIZE="$2"
344
 
                        shift 2
345
 
                ;;
346
 
                --raw)
347
 
                        RAW[$NUMRAW]="$2"
348
 
                        let NUMRAW=NUMRAW+1 
349
 
                        shift 2
350
 
                ;;
351
 
                --kernel-flavour)
352
 
                        KERNEL_FLAVOUR="$2"
353
 
                        shift 2
354
 
                ;;
355
 
                --components)
356
 
                        COMPS="$2"
357
 
                        shift 2
358
 
                ;;
359
 
                --ssh-key)
360
 
                        SSHKEY="$2"
361
 
                        shift 2
362
 
                ;;
363
 
                --ssh-user-key)
364
 
                        SSHUKEY="$2"
365
 
                        shift 2
366
 
                ;;
367
 
                --exec)
368
 
                        EXEC="$2"
369
 
                        shift 2
370
 
                ;;
371
 
                --firstboot)
372
 
                        FIRSTBOOT="$2"
373
 
                        shift 2
374
 
                ;;
375
 
                --firstlogin)
376
 
                        FIRSTLOGIN="$2"
377
 
                        shift 2
378
 
                ;;
379
 
                --ip)
380
 
                        IP="$2"
381
 
                        shift 2
382
 
                ;;
383
 
                --mask)
384
 
                        MASK="$2"
385
 
                        shift 2
386
 
                ;;
387
 
                --net)
388
 
                        NET="$2"
389
 
                        shift 2
390
 
                ;;
391
 
                --bcast)
392
 
                        BCAST="$2"
393
 
                        shift 2
394
 
                ;;
395
 
                --gw)
396
 
                        GW=$2
397
 
                        shift 2
398
 
                ;;
399
 
                --dns)
400
 
                        DNS=$2
401
 
                        shift 2
402
 
                ;;
403
 
                --no-opt)
404
 
                        echo "Ignored obsolete --no-opt option"
405
 
                        shift 1
406
 
                ;;
407
 
                -v|--verbose)
408
 
                        VERBOSE="YES"
409
 
                        shift 1
410
 
                ;;
411
 
                -c)
412
 
                        # already handled in config-handler
413
 
                        shift 2
414
 
                ;;
415
 
                --in-place)
416
 
                        INSTALL_IN_PLACE="YEAH"
417
 
                        shift 1
418
 
                ;;
419
 
                --)
420
 
                        shift
421
 
                        break
422
 
                ;;
423
 
                *)      
424
 
                        argsused=0
425
 
                        vm_getopt "$@" || true
426
 
                        if [ $argsused -gt 0 ]
427
 
                        then
428
 
                                shift $argsused
429
 
                        else
430
 
                                # This code will never be used as getopt will have caught it
431
 
                                # The vm had no clue what this was either
432
 
                                echo "Unknown option: $1"
433
 
                                usage
434
 
                                exit 1
435
 
                        fi
436
 
                ;;
437
 
        esac
438
 
done
439
 
 
440
 
info "suite: $SUITE"
441
 
info "vm: $VM"
442
 
 
443
 
if [ -z "$ARCH" ]
444
 
then
445
 
        ARCH=`dpkg --print-architecture`
446
 
fi
447
 
 
448
 
if [ -z "$KERNEL_FLAVOUR" ]
449
 
then
450
 
        default_kernel
451
 
else
452
 
        check_kernel_flavour "$KERNEL_FLAVOUR"
453
 
fi
454
 
info "kernel: $KERNEL_FLAVOUR"
455
 
 
456
 
if [ -n "$VMTMPDIR" ]
457
 
then
458
 
        if [ "$VMTMPDIR" = "-" ]; then
459
 
                if [ ! "${DESTINATION#/}" = "${DESTINATION}" ]; then
460
 
                        VMTMPDIR=$(dirname $DESTINATION)
461
 
                else
462
 
                        VMTMPDIR="${PWD}"
463
 
                fi
464
 
        fi
465
 
else
466
 
        if [ -n "$TMPDIR" ]; then
467
 
                VMTMPDIR=$TMPDIR
468
 
        else
469
 
                VMTMPDIR="/tmp"
470
 
        fi
471
 
fi
472
 
 
473
 
TEMPLATEDIR=${TEMPLATEDIR:-/usr/share/ubuntu-vm-builder/templates}
474
 
LIBVIRT=${LIBVIRT:-}
475
 
VMUSER=${VMUSER:-"ubuntu"}
476
 
NAME=${NAME:-"ubuntu"}
477
 
PASS=${PASS:-"ubuntu"}
478
 
 
479
 
if [ -z "$MIRROR" ]
480
 
then
481
 
        if [ "$ARCH" = "lpia" ]
482
 
        then
483
 
                MIRROR="http://ports.ubuntu.com/ubuntu-ports"
484
 
        else
485
 
                MIRROR="http://archive.ubuntu.com/ubuntu"
486
 
        fi
487
 
fi
488
 
if [ -z "$SECURITY_MIRROR" ]
489
 
then
490
 
        if [ "$ARCH" = "lpia" ]
491
 
        then
492
 
                SECURITY_MIRROR="http://ports.ubuntu.com/ubuntu-ports"
493
 
        else
494
 
                SECURITY_MIRROR="http://security.ubuntu.com/ubuntu"
495
 
        fi
496
 
fi
497
 
# save the mirror value for the source list if we are using --iso
498
 
ORIGMIRROR=$MIRROR
499
 
 
500
 
MEM=${MEM:-"128"}
501
 
VMHOSTNAME=${VMHOSTNAME:-"ubuntu-vm-$SUITE-$ARCH"}
502
 
DOMAIN=${DOMAIN:-"`hostname -d`"}
503
 
DOMAIN=${DOMAIN:-"localdomain"}
504
 
ROOTSIZE=${ROOTSIZE:-"4096"}
505
 
SWAPSIZE=${SWAPSIZE:-"1024"}
506
 
if [ -z "$COMPS" ]
507
 
then
508
 
        if [ "$KERNEL_FLAVOUR" = "virtual" ]
509
 
        then
510
 
                COMPS="main"
511
 
        else
512
 
                COMPS="main,restricted"
513
 
        fi
514
 
fi
515
 
 
516
 
declare -a MOUNTPOINTS 
517
 
declare -a MOUNTSIZES
518
 
declare -a MOUNTDISKS
519
 
declare -a DISKIMGS
520
 
declare -a DISKSIZES
521
 
declare -a PARTITION_START
522
 
declare -a PARTITION_END
523
 
 
524
 
if [ $# -gt 0 ];
525
 
then
526
 
        usage
527
 
        echo 
528
 
        echo "Excess option(s): $@"
529
 
        exit 1
530
 
fi
531
 
 
532
 
# DISKIMGS holds the base names of the disk image files.
533
 
# LOOPDEV[i] holds the loop device name for disk image i
534
 
# MOUNTPOINTS[i] holds the mountpoint of partition i.
535
 
# MOUNTDISKS[i] holds
536
 
# MOUNTSIZES[i] holds the size of partition i.
537
 
info "Disk"
538
 
 
539
 
partstart=""
540
 
partid=""
541
 
NUMDISKS=""
542
 
letters=("a" "b" "c" "d")
543
 
 
544
 
set +e
545
 
if [ -z "$PARTFILE" ]; then
546
 
        let partstart=0 
547
 
        let partid=0 
548
 
        let NUMDISKS=0 
549
 
        if [ $NUMRAW -gt 0 ]; then
550
 
                DISKIMGS[0]=${RAW[0]}
551
 
        else
552
 
                DISKIMGS[0]="root.raw"
553
 
        fi
554
 
        
555
 
        MOUNTPOINTS[$partid]="root" 
556
 
        MOUNTDISKS[$partid]="a"
557
 
        MOUNTSIZES[$partid]="$ROOTSIZE"
558
 
        let PARTITION_START[$partid]=$partstart
559
 
        let PARTITION_END[$partid]=$partstart+MOUNTSIZES[$partid]
560
 
        let partstart=PARTITION_END[$partid]+1
561
 
 
562
 
        if [ -n "$OPTSIZE" ]
563
 
        then
564
 
                let partid=$partid+1
565
 
                MOUNTPOINTS[$partid]="/opt" 
566
 
                MOUNTDISKS[$partid]="a"
567
 
                MOUNTSIZES[$partid]="$OPTSIZE"
568
 
                let PARTITION_START[$partid]=$partstart
569
 
                let PARTITION_END[$partid]=$partstart+MOUNTSIZES[$partid]
570
 
                let partstart=PARTITION_END[$partid]+1
571
 
        fi
572
 
 
573
 
        let partid=$partid+1
574
 
        MOUNTPOINTS[$partid]="swap" 
575
 
        MOUNTDISKS[$partid]="a"
576
 
        if [ -z "$SWAPSIZE" ]; then
577
 
                MOUNTSIZES[$partid]="1000"
578
 
        else
579
 
                MOUNTSIZES[$partid]="$SWAPSIZE"
580
 
        fi
581
 
        let PARTITION_START[$partid]=$partstart
582
 
        let PARTITION_END[$partid]=$partstart+MOUNTSIZES[$partid]
583
 
        let DISKSIZES[0]=PARTITION_END[$partid]+1
584
 
else
585
 
        let index=0
586
 
        let diskindex=0
587
 
        let partnum=0
588
 
        let partstart=0
589
 
 
590
 
        echo "Disks and partitions:"
591
 
 
592
 
        exec 10<$PARTFILE
593
 
        while read line<&10
594
 
        do
595
 
                if [ "$line" = "---" ]; then
596
 
                        let DISKSIZES[$diskindex]=$partstart
597
 
                        let diskindex=$diskindex+1
598
 
                        let partstart=0
599
 
                        let partnum=0
600
 
                        if [ $diskindex -gt 3 ]; then
601
 
                                echo "Sorry, no more than 4 IDE disks..."
602
 
                                exit 1
603
 
                        fi
604
 
                else
605
 
                        MOUNTDISKS[$index]="${letters[$diskindex]}"
606
 
                        MOUNTPOINTS[$index]=${line%\ *}
607
 
                        MOUNTSIZES[$index]=`echo "${line#*\ }" | sed 's/^[ \t]*//'`
608
 
                        let PARTITION_START[$index]=$partstart
609
 
                        let PARTITION_END[$index]=$partstart+MOUNTSIZES[$index]
610
 
                        if [ $partnum -eq 0 ]; then
611
 
                                if [ $NUMRAW -gt $diskindex ]; then
612
 
                                        DISKIMGS[$diskindex]=${RAW[$diskindex]}
613
 
                                else
614
 
                                        DISKIMGS[$diskindex]=`echo "${MOUNTPOINTS[$index]}" | sed 's%\/%%g' | sed 's/[ \t]*$//'`".raw"
615
 
                                fi
616
 
                                info " Disk: $diskindex ${DISKIMGS[$diskindex]}"
617
 
                        fi
618
 
 
619
 
                        info "  Partition: $partnum ${MOUNTPOINTS[$index]} start: $partstart end: ${PARTITION_END[$index]} size: ${MOUNTSIZES[$index]}"
620
 
                        let partstart=PARTITION_END[$index]+1
621
 
                        let index=$index+1
622
 
                        let partnum=$partnum+1
623
 
                        
624
 
                fi
625
 
        done 
626
 
        let DISKSIZES[$diskindex]=$partstart
627
 
        let NUMDISK=$diskindex
628
 
        if [ $diskindex -gt $NUMRAW && $NUMRAW -gt 0 ]; then
629
 
                echo "Sorry, --raw should match --part number of disks"
630
 
                exit 1
631
 
        fi
632
 
fi
633
 
set -e
634
 
 
635
 
info "VMTMPDIR: $VMTMPDIR"
636
 
WORKINGDIR=$(mktemp -d -p $VMTMPDIR vm-builder-XXXXXXXXXX) || exit 1
637
 
 
638
 
if [ -z "$DESTINATION" ]
639
 
then
640
 
        if [ -n "$VMHOSTNAME" ] && [ ! "$VMHOSTNAME" = "ubuntu-vm-$SUITE-$ARCH" ] ; then
641
 
                DESTINATION="$PWD/ubuntu-vm-$SUITE-$ARCH-$VMHOSTNAME"
642
 
        else
643
 
                DESTINATION="$PWD/ubuntu-vm-$SUITE-$ARCH"
644
 
        fi
645
 
fi
646
 
 
647
 
set +e
648
 
if [ -n "${SUDO_USER}" ]; then
649
 
        sudo -u "${SUDO_USER}" mkdir "${DESTINATION}" 
650
 
else
651
 
        mkdir "${DESTINATION}" 
652
 
fi
653
 
 
654
 
if [ $? -ne 0 ]; then
655
 
        if [ -z $OVERWRITE ]; then
656
 
                echo "Error creating directory ${DESTINATION}. Unable to proceed."
657
 
                rmdir  "${WORKINGDIR}"
658
 
                exit 1
659
 
        else
660
 
                info "Overwriting content of ${DESTINATION}"
661
 
                rm $DESTINATION/*
662
 
        fi
663
 
fi
664
 
set -e
665
 
 
666
 
if [ -z "$IP" ]; then
667
 
        IP="DHCP"
668
 
else
669
 
        if [ -z "$MASK" ]; then
670
 
                MASK="255.255.255.0"
671
 
                echo "setting mask to \"$MASK\""
672
 
        fi
673
 
        if [ -z "$NET" ]; then
674
 
                NET=`echo $IP | sed 's/\./.0./3' | sed 's/\.[[:digit:]]\{1,3\}$//'` 
675
 
                echo "setting network to \"$NET\""
676
 
        fi
677
 
        if [ -z "$BCAST" ]; then
678
 
                BCAST=`echo $IP | sed 's/\./.255./3' | sed 's/\.[[:digit:]]\{1,3\}$//'` 
679
 
                echo "setting broadcast to \"$BCAST\""
680
 
        fi
681
 
        if [ -z "$GW" ]; then
682
 
                GW=`echo $IP | sed 's/\./.1./3' | sed 's/\.[[:digit:]]\{1,3\}$//'`
683
 
                echo "setting gateway to \"$GW\""
684
 
        fi
685
 
        if [ -z "$DNS" ]; then
686
 
                DNS=`echo $IP | sed 's/\./.1./3' | sed 's/\.[[:digit:]]\{1,3\}$//'`
687
 
                echo "setting name server to \"$DNS\""
688
 
        fi
689
 
 
690
 
 
691
 
fi 
692
 
 
693
 
do_initial_user () {
694
 
        #
695
 
        # Set up a user with sudo etc.
696
 
        #
697
 
 
698
 
        chroot root adduser --disabled-password --gecos "${NAME}" ${VMUSER} > /dev/null
699
 
        chroot root chpasswd <<EOF
700
 
${VMUSER}:${PASS}
701
 
EOF
702
 
 
703
 
        chroot root addgroup --system admin >/dev/null 2>&1
704
 
        chroot root adduser ${VMUSER} admin >/dev/null 2>&1
705
 
 
706
 
        cat >> root/etc/sudoers << EOF
707
 
 
708
 
# Members of the admin group may gain root privileges
709
 
%admin ALL=(ALL) ALL
710
 
EOF
711
 
 
712
 
        for group in adm audio cdrom dialout floppy video plugdev dip netdev powerdev lpadmin scanner; do
713
 
                chroot root adduser ${VMUSER} $group >/dev/null 2>&1
714
 
        done
715
 
 
716
 
        # Lock root account
717
 
        chroot root chpasswd <<EOF
718
 
root:!
719
 
EOF
720
 
}
721
 
 
722
 
do_network_setup () {
723
 
        echo $VMHOSTNAME > root/etc/hostname
724
 
 
725
 
        cat > root/etc/hosts <<EOF
726
 
127.0.0.1 localhost
727
 
127.0.1.1 $VMHOSTNAME.$DOMAIN $VMHOSTNAME
728
 
 
729
 
# The following lines are desirable for IPv6 capable hosts
730
 
::1 ip6-localhost ip6-loopback
731
 
fe00::0 ip6-localnet
732
 
ff00::0 ip6-mcastprefix
733
 
ff02::1 ip6-allnodes
734
 
ff02::2 ip6-allrouters
735
 
ff02::3 ip6-allhosts
736
 
EOF
737
 
 
738
 
        cat > root/etc/network/interfaces <<EOF
739
 
# This file describes the network interfaces available on your system
740
 
# and how to activate them. For more information, see interfaces(5).
741
 
 
742
 
# The loopback network interface
743
 
auto lo
744
 
iface lo inet loopback
745
 
 
746
 
# The primary network interface
747
 
auto eth0
748
 
EOF
749
 
        if [ "$IP" = "DHCP" ]; then
750
 
                cat >> root/etc/network/interfaces <<EOF
751
 
iface eth0 inet dhcp
752
 
EOF
753
 
        else
754
 
                cat >> root/etc/network/interfaces <<EOF
755
 
iface eth0 inet static
756
 
        address $IP
757
 
        netmask $MASK 
758
 
        network $NET
759
 
        broadcast $BCAST
760
 
        gateway $GW
761
 
        # dns-* options are implemented by the resolvconf package, if installed
762
 
        dns-nameservers $DNS
763
 
        dns-search $DOMAIN
764
 
EOF
765
 
        fi
766
 
 
767
 
        #temporary fix for /var/run/network not being created
768
 
        if [ ! -e root/var/run/network ]; then 
769
 
                echo "Creating /var/run/network"
770
 
                mkdir root/var/run/network
771
 
        fi
772
 
}
773
 
 
774
 
do_kernel_img_conf () {
775
 
        mkdir -p root/etc
776
 
        cat > root/etc/kernel-img.conf << EOF
777
 
do_symlinks = yes
778
 
relative_links = yes
779
 
do_bootfloppy = no
780
 
do_initrd = yes
781
 
link_in_boot = no
782
 
do_bootloader = no
783
 
EOF
784
 
}
785
 
 
786
 
 
787
 
do_bootloader () {
788
 
        #
789
 
        # The suite will have defined updategrub
790
 
        #
791
 
        sed -e "s,%UPDATEGRUB%,$updategrub,g" << EOF | cat >> target/etc/kernel-img.conf
792
 
postinst_hook = %UPDATEGRUB%
793
 
postrm_hook = %UPDATEGRUB%
794
 
EOF
795
 
        chroot target apt-get --force-yes -y install grub
796
 
 
797
 
        mkdir target/boot/grub
798
 
 
799
 
        if [ $ARCH = "amd64" ]
800
 
        then
801
 
                grubarch="x86_64-pc"
802
 
        else
803
 
                grubarch="i386-pc"
804
 
        fi
805
 
        cp target/$grubroot/$grubarch/* target/boot/grub/
806
 
 
807
 
        #
808
 
        # Create device.map
809
 
        #
810
 
        echo "(hd0) ${DISKIMGS[0]}" >> device.map
811
 
        grub --device-map=device.map --batch<<EOT
812
 
root (hd0,0)
813
 
setup (hd0)
814
 
EOT
815
 
 
816
 
        cat > target/boot/grub/device.map << EOF
817
 
(fd0)   /dev/fd0
818
 
EOF
819
 
        suite_generate_device_map
820
 
 
821
 
        # Hacky-di-hack.. First generate the template, then remove the errant kopt_2_6 (which hardcodes the root device to /dev/hda1) and then rerun update-grub.
822
 
        chroot target $updategrub -y
823
 
        suite_mangle_grub_menu_lst
824
 
        chroot target $updategrub
825
 
        chroot target grub-set-default 0
826
 
}
827
 
 
828
 
do_disk_images() {
829
 
        info "do_disk_images"
830
 
        #
831
 
        # Create disk images, partitions and format
832
 
        #
833
 
 
834
 
        for ((i=0; i<${#DISKIMGS[@]}; i++)); do
835
 
                if [ $NUMRAW -eq 0 ]; then
836
 
                        # create disk image
837
 
                        let size=${DISKSIZES[$i]}*1024
838
 
                        qemu-img create -f raw ${DISKIMGS[$i]} $size > /dev/null
839
 
                fi
840
 
 
841
 
                # label the disk
842
 
                parted --script ${DISKIMGS[$i]} mklabel msdos
843
 
        done
844
 
        
845
 
        let diskindex=-1
846
 
        curdisk=""
847
 
 
848
 
        # create the partitions
849
 
        for ((i=0;i<${#MOUNTPOINTS[@]};i++)); do
850
 
                if [ "$curdisk" != "${MOUNTDISKS[$i]}" ]; then
851
 
                        let diskindex++
852
 
                        img="${DISKIMGS[$diskindex]}"
853
 
                        curdisk=${MOUNTDISKS[$i]}
854
 
                        info "Disk: $curdisk $img"
855
 
                fi
856
 
 
857
 
                # Create partition and format
858
 
                info "  creating partition ${MOUNTPOINTS[$i]} on $img"
859
 
                if [ "${MOUNTPOINTS[$i]}" = "swap" ]; then
860
 
                        parted --script -- $img mkpartfs primary linux-swap ${PARTITION_START[$i]} ${PARTITION_END[$i]}
861
 
                else
862
 
                        parted --script -- $img mkpartfs primary ext2 ${PARTITION_START[$i]} ${PARTITION_END[$i]}
863
 
                fi
864
 
        done
865
 
        
866
 
        #mount the disk loops
867
 
        for ((i=0;i<${#DISKIMGS[@]};i++)); do
868
 
                LOOPDEV[$i]=$(losetup -s -f ${DISKIMGS[$i]})
869
 
                kpartx -a ${LOOPDEV[$i]} > /dev/null
870
 
        done
871
 
        
872
 
        #format the partitions
873
 
        let diskindex=-1
874
 
        curdisk=""
875
 
        for ((i=0;i<${#MOUNTPOINTS[@]};i++)); do
876
 
                if [ "$curdisk" != "${MOUNTDISKS[$i]}" ]; then
877
 
                        let diskindex++ || true
878
 
                        img="${DISKIMGS[$diskindex]}"
879
 
                        curdisk=${MOUNTDISKS[$i]}
880
 
                        let partindex=1
881
 
                        mapper=$(echo ${LOOPDEV[$diskindex]} | sed s-/dev/-/dev/mapper/-)
882
 
                fi
883
 
 
884
 
                if  [ "${MOUNTPOINTS[$i]}" = "swap" ]; then
885
 
                        info formating ${mapper}p${partindex} as swap for ${MOUNTPOINTS[$i]}
886
 
                        mkswap ${mapper}p${partindex}   
887
 
                else 
888
 
                        info formating ${mapper}p${partindex} as ext3 for ${MOUNTPOINTS[$i]}
889
 
                        mkfs.ext3 -q ${mapper}p${partindex}
890
 
                fi
891
 
                /lib/udev/vol_id --uuid  ${mapper}p${partindex} >  ${MOUNTPOINTS[$i]}.uuid
892
 
                let partindex++
893
 
        done
894
 
}
895
 
 
896
 
do_source() {
897
 
        info "do_source $1 $2, iso: $ISO"
898
 
        if [ -n "$ISO" ]; then
899
 
                # source is an ISO file
900
 
                if [ -e "$ISO" ]; then
901
 
                        # the file exists
902
 
                        if [ -n "$2" ]; then
903
 
                                # this is not the first time: need to unmount the previous first
904
 
                                umount $2/isomnt || true
905
 
                                rmdir $2/isomnt
906
 
                                MIRROR="file:///isomnt"
907
 
                        else
908
 
                                MIRROR="file://$1/isomnt"
909
 
                        fi
910
 
                        if [ ! "$1" = "-" ]; then
911
 
                                # we have something to mount
912
 
                                mkdir $1/isomnt
913
 
                                mount -o loop -t iso9660 $ISO $1/isomnt
914
 
                        fi
915
 
                        info "using $MIRROR"
916
 
                else
917
 
                        echo "$ISO not found, using $MIRROR"
918
 
                fi
919
 
        fi
920
 
        if [ -n "$2" ]; then
921
 
                # this is not the first time, update the source.list on the target
922
 
                if [ "$1" = "-" ]; then
923
 
                        # this the last time we are called, push the --mirror info
924
 
                        do_sourceslist $ORIGMIRROR $SECURITY_MIRROR $2
925
 
                else
926
 
                        do_sourceslist $MIRROR $SECURITY_MIRROR $1
927
 
                fi
928
 
                # Update the cache
929
 
                sudo chroot $1 apt-get update &> /dev/null || true
930
 
        fi
931
 
}
932
 
 
933
 
do_debootstrap() {
934
 
        mkdir root
935
 
        if [ -n "$INSTALL_IN_PLACE" ]
936
 
        then
937
 
                ln -s target root
938
 
        else
939
 
                mkdir root &> /dev/null || true 
940
 
        fi
941
 
        info "debootstrap"
942
 
        debootstrap --components="$COMPS" --arch $ARCH --keyring /usr/share/keyrings/ubuntu-archive-keyring.gpg $SUITE root "$MIRROR"
943
 
}
944
 
 
945
 
do_kernel_n_friends () {
946
 
        info "kernel_n_friends"
947
 
        PKGS="linux-$KERNEL_FLAVOUR"
948
 
        vm_extra_packages
949
 
        suite_extra_packages
950
 
        chroot root apt-get -o APT::Install-Recommends=False --force-yes -y install $PKGS
951
 
}
952
 
 
953
 
do_add_remove_packages () {
954
 
        info "add_remove_packages"
955
 
        cmd="apt-get --purge install "
956
 
        chroot root apt-get -y --force-yes --purge install $PKGCMD || true
957
 
}
958
 
 
959
 
do_add_ssh_key () {
960
 
        info "add_ssh_key"
961
 
        sshinstalled=""
962
 
        test -z "$SSHKEY" && test -z "$SSHUKEY" && return
963
 
        if [ -f "$SSHKEY" ]
964
 
        then
965
 
                mkdir root/root/.ssh
966
 
                cp "$SSHKEY" root/root/.ssh/authorized_keys
967
 
                chroot root chown -R root:root /root/.ssh
968
 
                sshinstalled="true"
969
 
        else
970
 
                test -n "$SSHKEY" && echo "No ssh root key found: $SSHKEY"
971
 
        fi
972
 
        if [ -f "$SSHUKEY" ]
973
 
        then
974
 
                mkdir root/home/$VMUSER/.ssh
975
 
                cp "$SSHUKEY" root/home/$VMUSER/.ssh/authorized_keys
976
 
                chroot root chown -R $VMUSER:$VMUSER /home/$VMUSER/.ssh
977
 
                sshinstalled="true"
978
 
        else
979
 
                test -n "$SSHUKEY" && echo "No ssh user key found: $SSHUKEY"
980
 
        fi
981
 
        if [ -n "$sshinstalled" ]; then
982
 
                chroot root apt-get install --force-yes -y openssh-server || true
983
 
        fi
984
 
}
985
 
 
986
 
 
987
 
 
988
 
do_sourceslist () {
989
 
        info "sourceslists $1 $2 $3"
990
 
        cat > $3/etc/apt/sources.list << EOF
991
 
deb $1 ${SUITE} main restricted
992
 
#deb-src $1 ${SUITE} main restricted
993
 
 
994
 
## Major bug fix updates produced after the final release of the
995
 
## distribution.
996
 
deb $1 ${SUITE}-updates main restricted
997
 
#deb-src $1 ${SUITE}-updates main restricted 
998
 
 
999
 
deb $2 ${SUITE}-security main restricted
1000
 
#deb-src $2 ${SUITE}-security main restricted
1001
 
EOF
1002
 
        ## UNIVERSE ##
1003
 
 
1004
 
        if echo $COMPS | grep universe &> /dev/null 
1005
 
        then
1006
 
                cat >> $3/etc/apt/sources.list << EOF
1007
 
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
1008
 
## team, and may not be under a free licence. Please satisfy yourself as to
1009
 
## your rights to use the software. Also, please note that software in
1010
 
## universe WILL NOT receive any review or updates from the Ubuntu security
1011
 
## team.
1012
 
deb $1 ${SUITE} universe
1013
 
#deb-src $1 ${SUITE} universe
1014
 
deb $1 ${SUITE}-updates universe
1015
 
#deb-src $1 ${SUITE}-updates universe
1016
 
deb $2 ${SUITE}-security universe
1017
 
#deb-src $2 ${SUITE}-security universe
1018
 
EOF
1019
 
        else
1020
 
                cat >> $3/etc/apt/sources.list << EOF
1021
 
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
1022
 
## team, and may not be under a free licence. Please satisfy yourself as to
1023
 
## your rights to use the software. Also, please note that software in
1024
 
## universe WILL NOT receive any review or updates from the Ubuntu security
1025
 
## team.
1026
 
#deb $1 ${SUITE} universe
1027
 
#deb-src $1 ${SUITE} universe
1028
 
#deb $1 ${SUITE}-updates universe
1029
 
#deb-src $1 ${SUITE}-updates universe
1030
 
#deb $2 ${SUITE}-security universe
1031
 
#deb-src $2 ${SUITE}-security universe
1032
 
EOF
1033
 
        fi
1034
 
 
1035
 
        ## MULTIVERSE ##
1036
 
                
1037
 
        if echo $COMPS | grep multiverse &> /dev/null 
1038
 
        then
1039
 
                cat >> $3/etc/apt/sources.list << EOF
1040
 
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu 
1041
 
## team, and may not be under a free licence. Please satisfy yourself as to 
1042
 
## your rights to use the software. Also, please note that software in 
1043
 
## multiverse WILL NOT receive any review or updates from the Ubuntu
1044
 
## security team.
1045
 
deb $1 ${SUITE} multiverse
1046
 
#deb-src $1 ${SUITE} multiverse
1047
 
deb $1 ${SUITE}-updates multiverse
1048
 
#deb-src $1 ${SUITE}-updates multiverse
1049
 
deb $2 ${SUITE}-security multiverse
1050
 
#deb-src $2 ${SUITE}-security multiverse
1051
 
EOF
1052
 
        else
1053
 
                cat >> $3/etc/apt/sources.list << EOF
1054
 
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu 
1055
 
## team, and may not be under a free licence. Please satisfy yourself as to 
1056
 
## your rights to use the software. Also, please note that software in 
1057
 
## multiverse WILL NOT receive any review or updates from the Ubuntu
1058
 
## security team.
1059
 
#deb $1 ${SUITE} multiverse
1060
 
#deb-src $1 ${SUITE} multiverse
1061
 
#deb $1 ${SUITE}-updates multiverse
1062
 
#deb-src $1 ${SUITE}-updates multiverse
1063
 
#deb $2 ${SUITE}-security multiverse
1064
 
#deb-src $2 ${SUITE}-security multiverse
1065
 
EOF
1066
 
        fi
1067
 
        cat >> $3/etc/apt/sources.list << EOF
1068
 
## Uncomment the following two lines to add software from the 'backports'
1069
 
## repository.
1070
 
## N.B. software from this repository may not have been tested as
1071
 
## extensively as that contained in the main release, although it includes
1072
 
## newer versions of some applications which may provide useful features.
1073
 
## Also, please note that software in backports WILL NOT receive any review
1074
 
## or updates from the Ubuntu security team.
1075
 
# deb $1 ${SUITE}-backports main restricted universe multiverse
1076
 
# deb-src $1 ${SUITE}-backports main restricted universe multiverse
1077
 
 
1078
 
## Uncomment the following two lines to add software from Canonical's
1079
 
## 'partner' repository. This software is not part of Ubuntu, but is
1080
 
## offered by Canonical and the respective vendors as a service to Ubuntu
1081
 
## users.
1082
 
# deb http://archive.canonical.com/ubuntu hardy partner
1083
 
# deb-src http://archive.canonical.com/ubuntu hardy partner
1084
 
EOF
1085
 
}
1086
 
 
1087
 
do_copy_settings () {
1088
 
        info "do_copy_settings"
1089
 
        #copy the host settings to the new guest (locale, console-setup, tz)
1090
 
        cp /etc/default/locale root/etc/default/
1091
 
        cp -a /etc/console-setup root/etc/
1092
 
        cp /etc/default/console-setup root/etc/default/
1093
 
        cp /etc/timezone root/etc/
1094
 
        chroot root dpkg-reconfigure -pcritical tzdata
1095
 
        LANG=$HOSTLANG
1096
 
        chroot root locale-gen en_US
1097
 
        chroot root locale-gen $LANG
1098
 
        chroot root dpkg-reconfigure -pcritical locales
1099
 
        chroot root dpkg-reconfigure -pcritical console-setup
1100
 
        info "do_copy_settings done"
1101
 
}
1102
 
 
1103
 
 
1104
 
do_exec_script () {
1105
 
        info "exec_script"
1106
 
        if [ -z "$EXEC" ]
1107
 
        then
1108
 
                return
1109
 
        fi
1110
 
 
1111
 
        #
1112
 
        # Execute the script.
1113
 
        #
1114
 
        info "Executing $EXEC"
1115
 
        sh -c "$EXEC" || {
1116
 
                echo "Error executing user defined script" 
1117
 
                do_cleanup
1118
 
                exit 1
1119
 
        }
1120
 
}
1121
 
 
1122
 
do_firstboot () {
1123
 
        info "firstboot"
1124
 
        if [ -z "$FIRSTBOOT" ]; then
1125
 
                return  
1126
 
        elif [ ! -f "$FIRSTBOOT" ]; then
1127
 
                echo "Script '$FIRSTBOOT' not found."
1128
 
                return
1129
 
        fi
1130
 
        #
1131
 
        # Install the script to be executed
1132
 
        #
1133
 
        cp $FIRSTBOOT root/root/firstboot.sh
1134
 
        chmod a+x root/root/firstboot.sh
1135
 
        mv root/etc/rc.local root/etc/rc.local.orig
1136
 
        cat > root/etc/rc.local <<EOT
1137
 
#!/bin/sh -e
1138
 
#execute firstboot.sh only once
1139
 
if [ ! -e /root/firstboot_done ]; then
1140
 
        if [ -e /root/firstboot.sh ]; then
1141
 
                /root/firstboot.sh
1142
 
        fi
1143
 
        touch /root/firstboot_done
1144
 
fi
1145
 
exit 0
1146
 
EOT
1147
 
        chmod a+x root/etc/rc.local
1148
 
}
1149
 
 
1150
 
do_firstlogin () {
1151
 
        info "firstlogin"
1152
 
        dofirst="YES"
1153
 
        if [ -z "$FIRSTLOGIN" ]; then
1154
 
                dofirst=""
1155
 
        elif [ ! -f "$FIRSTLOGIN" ]; then
1156
 
                echo "Script '$FIRSTLOGIN' not found."
1157
 
                dofirst=""
1158
 
        fi
1159
 
        #
1160
 
        # Install the script to be executed
1161
 
        #
1162
 
        if [ -n "$dofirst" ]; then 
1163
 
                cp $FIRSTLOGIN root/root/firstlogin.sh
1164
 
                chmot a+x root/root/firstlogin.sh
1165
 
        fi
1166
 
        cp root/etc/bash.bashrc root/etc/bash.bashrc.orig
1167
 
        cat >> root/etc/bash.bashrc <<EOT
1168
 
if [ ! -e /root/firstlogin_done ]; then
1169
 
        if [ -e /root/firstlogin.sh ]; then
1170
 
                /root/firstlogin.sh
1171
 
        fi
1172
 
        # This part should not be necessary any more
1173
 
        #echo "Setting up your keyboard, will need sudo password."
1174
 
        #sudo dpkg-reconfigure -p critical console-setup &> /dev/null
1175
 
        sudo touch /root/firstlogin_done
1176
 
fi
1177
 
EOT
1178
 
}
1179
 
 
1180
 
mount_parts () {
1181
 
        info "mount_parts"
1182
 
        base="$1"
1183
 
        if [ "$base" = "" ]; then
1184
 
                echo base is empty....  exiting
1185
 
                exit 0
1186
 
        fi
1187
 
        let diskindex=-1
1188
 
        curdisk=""
1189
 
        for ((i=0;i<${#MOUNTPOINTS[@]};i++)); do
1190
 
                if [ "$curdisk" != "${MOUNTDISKS[$i]}" ]; then
1191
 
                        let diskindex++
1192
 
                        img="${DISKIMGS[$diskindex]}"
1193
 
                        curdisk=${MOUNTDISKS[$i]}
1194
 
                        let partindex=1
1195
 
                        mapper=$(echo ${LOOPDEV[$diskindex]} | sed s-/dev/-/dev/mapper/-)
1196
 
                fi
1197
 
 
1198
 
                if  [ "${MOUNTPOINTS[$i]}" = "root" ]; then
1199
 
                        mount ${mapper}p${partindex} $base
1200
 
                elif  [ "${MOUNTPOINTS[$i]}" != "swap" ]; then
1201
 
                        mkdir $base${MOUNTPOINTS[$i]}
1202
 
                        mount ${mapper}p${partindex} $base${MOUNTPOINTS[$i]}
1203
 
                fi
1204
 
                let partindex++
1205
 
        done
1206
 
}
1207
 
 
1208
 
umount_parts () {
1209
 
        info "umount_parts"
1210
 
        base="$1"
1211
 
        for ((i=$(( ${#MOUNTPOINTS[@]} -1));i>-1;i--)); do
1212
 
                if  [ "${MOUNTPOINTS[$i]}" = "root" ]; then
1213
 
                        echo umount  $base
1214
 
                        umount  $base
1215
 
                elif  [ "${MOUNTPOINTS[$i]}" != "swap" ]; then
1216
 
                        echo umount  $base${MOUNTPOINTS[$i]}
1217
 
                        umount  $base${MOUNTPOINTS[$i]}
1218
 
                fi
1219
 
        done
1220
 
}
1221
 
 
1222
 
# Taken from debootstrap
1223
 
do_avoid_starting_daemons() {
1224
 
        info "avoid_starting_daemons"
1225
 
        cat > root/usr/sbin/policy-rc.d <<EOT
1226
 
#!/bin/sh
1227
 
 
1228
 
while true; do
1229
 
case "\$1" in
1230
 
  -*) shift ;;
1231
 
  makedev) exit 0;;
1232
 
  x11-common) exit 0;;
1233
 
  *)  exit 101;;
1234
 
esac
1235
 
done
1236
 
EOT
1237
 
        chmod 755 "root/usr/sbin/policy-rc.d"
1238
 
                        
1239
 
}
1240
 
 
1241
 
# Taken from debootstrap
1242
 
do_undo_avoid_starting_daemons() {
1243
 
        info "undo_avoid_starting_daemons"
1244
 
        rm root/usr/sbin/policy-rc.d
1245
 
}
1246
 
 
1247
 
#
1248
 
# linux-restricted-modules unconditionally mounts a tmpfs.
1249
 
#
1250
 
do_unmount_restricted_tmpfs() {
1251
 
        info "unmount_restriced_tmpfs"
1252
 
        umount root/lib/modules/*/volatile > /dev/null 2>&1 || true
1253
 
}
1254
 
 
1255
 
do_mount_target() {
1256
 
        info "mount_target"
1257
 
        mkdir target 
1258
 
        mount_parts target
1259
 
}
1260
 
 
1261
 
do_umount_target() {
1262
 
        info "unmount_target"
1263
 
        umount_parts target
1264
 
        rmdir target
1265
 
}
1266
 
 
1267
 
do_copy_to_disk_images () {
1268
 
        chroot root apt-get clean
1269
 
        if [ -e root/isomnt ]; then
1270
 
                umount root/isomnt
1271
 
        fi
1272
 
        if [ -z "$INSTALL_IN_PLACE" ]
1273
 
        then
1274
 
                #
1275
 
                # Copy the debootstrapped filesystem into the partitions
1276
 
                #
1277
 
                info Copy the debootstrapped filesystem 
1278
 
                cp -a root/* target
1279
 
        fi
1280
 
        rm -rf root
1281
 
 
1282
 
}
1283
 
 
1284
 
do_target_vm_conversion () {
1285
 
        info Convert to format $VM
1286
 
        FILES=""
1287
 
        vm_target_conversion
1288
 
        
1289
 
        #do image conversion if not asked to do raw images
1290
 
        if  [ $NUMRAW -eq 0 ]; then
1291
 
                for ((i=0;i<${#DISKIMGS[@]};i++)); do
1292
 
                        img=$( echo ${DISKIMGS[$i]} | sed s/.raw// )
1293
 
                        qemu-img convert $QEMU_IMG_CONVERT_OPTS ${DISKIMGS[$i]} "$img${QEMU_IMG_CONVERT_EXTENSION}"
1294
 
                done
1295
 
 
1296
 
                test -n "${SUDO_USER}" && chown "$SUDO_USER" $FILES
1297
 
                mv $FILES $DESTINATION
1298
 
                echo "Done.  Images are in ${DESTINATION}."
1299
 
        else 
1300
 
                echo "Done, raw images/devices ready."
1301
 
        fi
1302
 
 
1303
 
}
1304
 
 
1305
 
do_cleanup () {
1306
 
        info "Cleaning up..."
1307
 
        if [ -n "$VERBOSE" ]; then
1308
 
                echo "Press return to go ahead with cleanup"
1309
 
                read
1310
 
        fi
1311
 
        #unmount iso if present
1312
 
        if [ -e $WORKINGDIR/isomnt ]; then
1313
 
                umount $WORKINGDIR/isomnt
1314
 
        elif [ -e $WORKINGDIR/target/isomnt ]; then
1315
 
                umount $WORKINGDIR/target/isomnt
1316
 
        elif [ -e $WORKINGDIR/root/isomnt ]; then
1317
 
                umount $WORKINGDIR/root/isomnt
1318
 
        fi
1319
 
 
1320
 
        #umount target if present
1321
 
        if [ -e target ]; then
1322
 
                do_umount_target
1323
 
        fi
1324
 
 
1325
 
        #unmount the disk loops
1326
 
        for ((i=0;i<${#DISKIMGS[@]};i++)); do
1327
 
                kpartx -d ${LOOPDEV[$i]} > /dev/null
1328
 
                losetup -d ${LOOPDEV[$i]} 
1329
 
        done
1330
 
 
1331
 
        #remove .uuid files
1332
 
        for ((i=0;i<${#MOUNTPOINTS[@]};i++)); do
1333
 
                rm ${MOUNTPOINTS[$i]}.uuid
1334
 
        done
1335
 
        
1336
 
        if  [ $NUMRAW -eq 0 ]; then
1337
 
                #remove .raw files
1338
 
                for ((i=0;i<${#DISKIMGS[@]};i++)); do
1339
 
                        rm ${DISKIMGS[$i]} 
1340
 
                done
1341
 
        fi
1342
 
 
1343
 
        #restore curdir
1344
 
        popd > /dev/null
1345
 
 
1346
 
        #unmount and remove working directory
1347
 
        if [ -n "$TMPFS" ]; then
1348
 
                umount "$WORKINGDIR"
1349
 
        fi
1350
 
        rm -rf "$WORKINGDIR"
1351
 
}
1352
 
 
1353
 
function errorHandler () {
1354
 
        echo "Error"
1355
 
        do_cleanup
1356
 
        exit $?
1357
 
}
1358
 
 
1359
 
function interruptHandler () {
1360
 
        echo "User interupt"
1361
 
        do_cleanup
1362
 
        exit 2
1363
 
}
1364
 
 
1365
 
if [ -n "$TMPFS" ]; then
1366
 
        if [ "$TMPFS" = "-" ]; then
1367
 
                mount -t tmpfs ubuntu-vm-builder -o suid,dev,size=1G "$WORKINGDIR"
1368
 
        else
1369
 
                mount -t tmpfs ubuntu-vm-builder -o $TMPFS "$WORKINGDIR"
1370
 
        fi
1371
 
fi
1372
 
info "Working in $WORKINGDIR"
1373
 
pushd "$WORKINGDIR" > /dev/null
1374
 
 
1375
 
do_disk_images
1376
 
set +e; trap errorHandler ERR 
1377
 
trap interruptHandler SIGINT SIGTERM
1378
 
do_source $WORKINGDIR ""
1379
 
do_mount_target
1380
 
do_debootstrap
1381
 
suite_do_fstab
1382
 
do_avoid_starting_daemons
1383
 
do_copy_settings
1384
 
do_initial_user
1385
 
do_network_setup
1386
 
do_source "root" $WORKINGDIR
1387
 
do_kernel_img_conf 
1388
 
do_kernel_n_friends
1389
 
do_add_remove_packages
1390
 
do_add_ssh_key
1391
 
do_exec_script
1392
 
do_firstboot
1393
 
do_firstlogin
1394
 
do_undo_avoid_starting_daemons
1395
 
do_unmount_restricted_tmpfs
1396
 
do_copy_to_disk_images 
1397
 
do_source "target" "root"
1398
 
do_bootloader
1399
 
do_source "-" "target"
1400
 
do_umount_target
1401
 
do_target_vm_conversion
1402
 
trap - ERR; set -e
1403
 
do_cleanup