Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/usr-scratch.sh
blob: 9068ef33ddd024e65b38a125cb1d2aebec9fc1f9 (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
#!/bin/bash
##
## makedistro.sh
## 
## Made by David Farning
## Login   <dfarning@acer>
## 
## Started on  Sat Jan  9 03:45:30 2010 David Farning
## Last update Sun Jan 31 19:04:58 2010 David Farning
##

set -eu

function usage()
{
    echo "The script needs 2 parameters:

    * Action to do: debootstrap|iso|squash|all
    * Architecture to build: i386|amd64

    Usage: $0 debootstrap|iso|squash|source|all|torrent i386|amd64"
}

function failure()
{
    unmount_directories
    echo "$@"
    exit 2
}

function unmount_directory()
{
    DIR_TO_UNMOUNT="$1"
    if mountpoint -q ${DIR_TO_UNMOUNT}; then #FIXME should be able to detect in tmp is mounted
    	echo "Unmounting directory ${DIR_TO_UNMOUNT}..."
    	umount -l ${DIR_TO_UNMOUNT} || failure "Cannot unmount directory ${DIR_TO_UNMOUNT}, error=$?"
    fi
}

function unmount_directories()
{
    # check for runs of the script
    if [ -d ${CHROOT} ] ; then
        echo "Cleaning up old build tree"
        for MOUNT in /tmp /lib/modules/*/volatile /proc /sys /dev/pts /var/run ; do
            unmount_directory ${CHROOT}${MOUNT}
        done
    fi
}

function replace_ssd()
{
    # replace the start-stop-daemon, to make /etc/init.d/ scripts do nothing for a while
    mv ${CHROOT}/sbin/start-stop-daemon ${CHROOT}/sbin/start-stop-daemon.REAL
    cat <<EOF > "$CHROOT/sbin/start-stop-daemon"
#!/bin/sh
echo
echo "Warning: Fake start-stop-daemon called, doing nothing"
EOF
    chmod 755 ${CHROOT}/sbin/start-stop-daemon
}

function restore_ssd()
{
    # enable /etc/init.d/ scripts
    mv ${CHROOT}/sbin/start-stop-daemon.REAL ${CHROOT}/sbin/start-stop-daemon
}

function prepare_chroot()
{
    export LANG=C
    export LANGUAGE=C

    mount -t proc proc ${CHROOT}/proc || failure "Failed to mount ${CHROOT}/proc, error=$?"
    mount -t sysfs sysfs ${CHROOT}/sys || failure "Failed to mount ${CHROOT}/sys, error=$?"
    mount -t devpts none ${CHROOT}/dev/pts || failure "Failed to mount ${CHROOT}/dev/pts, error=$?"
    mount -o bind /var/run ${CHROOT}/var/run
    #mount -o bind /tmp ${CHROOT}/tmp FIXME do we need temp

    echo "Copying resolv.conf..."
    cp -f /etc/resolv.conf ${CHROOT}/etc/resolv.conf || failure "Failed to copy resolv.conf to image directory, error=$?"

    echo "Creating DBUS uuid"
    #${CCMD} dbus-uuidgen --ensure
    echo "done"
}

function in_chroot()
{
    cp usr-chroot.sh ${CHROOT}   
    ${CCMD} ./usr-chroot.sh
    #rm ${CHROOT}/usr-chroot.sh
}

function clean_chroot()
{
    unmount_directories

    echo "Cleaning up apt"
    ${CCMD} apt-get clean || failure "Failed to run apt-get clean, error=$?"
    ${CCMD} apt-get autoclean || failure "Failed to run apt-get autoclean, error=$?"

    echo "Cleaning up temporary directories..."
    ${CCMD} rm -rf '/tmp/*' '/tmp/.*' '/var/tmp/*' '/var/tmp/.*' #2>/dev/null
 
    ${CCMD} rm -rf /var/lib/dbus/machine-id # 2>/dev/null

    echo "Restoring hosts..."
    ${CCMD} rm -f ${CHROOT}/etc/hosts || failure "Failed to hosts, error=$?"
    
    echo "Restoring resolv.conf..."
    ${CCMD} rm -f ${CHROOT}/etc/resolv.conf || failure "Failed to remove resolv.conf, error=$?"
}

function do_debootstrap() {
    date
    echo "DIST=${DIST}
    ARCH=${ARCH}
    CHROOT=${CHROOT}"

    unmount_directories
    rm -rf $CHROOT || exit 1

    # debootstrab the base system
    mkdir ${CHROOT}
    debootstrap --arch=${ARCH} lucid ${CHROOT} ${MIRROR}

#    replace_ssd
    prepare_chroot
    in_chroot

    cp casper $CHROOT/etc/init.d/casper

    clean_chroot
#    restore_ssd
}

function prepare_image_directory()
{
    echo "Preparing directory for image"
    if [ -e ${IMAGE} ]; then
    	rm -rf ${IMAGE} || failure "Failed to remove directory ${IMAGE}"
    fi
    mkdir -p ${IMAGE}
}

pack_squashfs()
{
    prepare_image_directory
    cp -a USR-master/* ${IMAGE}/ 

    echo "Updating files lists..."
    ${CCMD} dpkg-query -W --showformat='${Package} ${Version}\n' > ${IMAGE}/casper/filesystem.manifest || failure "Cannot update filesystem.manifest, error=$?"
   
    cp  ${IMAGE}/casper/filesystem.manifest ${IMAGE}/casper/filesystem.manifest-desktop

    sed -i '/ubiquity/d' ${IMAGE}/casper/filesystem.manifest-desktop
    sed -i '/casper/d' ${IMAGE}/casper/filesystem.manifest-desktop


    echo "Packing SquashFS image..."
    if [ -e ${IMAGE}/casper/filesystem.squashfs ]; then
        rm -f ${IMAGE}/casper/filesystem.squashfs || failure "Cannot remove ${IMAGE}/casper/filesystem.squashfs to make room for created squashfs image, error=$?"
    fi

    printf $(du -sx --block-size=1 ${CHROOT} | cut -f1) > ${IMAGE}/casper/filesystem.size
    mksquashfs ${CHROOT} ${IMAGE}/casper/filesystem.squashfs || failure "Failed to create squashfs image to ${IMAGE}/casper/filesystem.squashfs, error=$?"

    chmod 644  ${IMAGE}/casper/filesystem.squashfs
}

pack_iso(){
    #update the kernel image in the master dir
    cp -v ${CHROOT}/boot/initrd.img*  ${IMAGE}/casper/initrd
    cp -v ${CHROOT}/boot/vmlinuz*  ${IMAGE}/casper/vmlinuz

    echo "Updating md5sums..."
    pushd ${CHROOT}
    find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt
    popd

    mkisofs -o ${ISO} \
	-b "isolinux/isolinux.bin" -c "isolinux/boot.cat" \
	-no-emul-boot -boot-load-size 4 -boot-info-table \
	-cache-inodes -r -J -l \
	${IMAGE}

    RESULT=$?
    if [ $RESULT -ne 0 ]; then
    	failure "Failed to pack ISO image, error=${RESULT}"
    fi
}


case $1 in
debootstrap|iso|squash|source|all|torrent)     ACTION=$1
                ;;
*)              usage
                exit 1
                ;;
esac

case $2 in
i386|amd64)	ARCH=$2
		;;
*)              usage
		exit 1
		;;
esac

export DEBIAN_FRONTEND=noninteractive

MIRROR="http://10.0.02:3142/archive.ubuntu.com/ubuntu"
DIST="USR"
CHROOT=${DIST}-${ARCH}-FS
IMAGE=${DIST}-${ARCH}-IMAGE
ISO=${DIST}-${ARCH}.iso
LOG=${CHROOT}.log
CCMD="chroot ${CHROOT}"
echo ${CCMD}

#do_debootstrap
pack_squashfs
pack_iso