File: common.sh

package info (click to toggle)
debian-cd 3.2.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,848 kB
  • sloc: sh: 6,129; perl: 4,129; makefile: 413
file content (276 lines) | stat: -rw-r--r-- 8,902 bytes parent folder | download | duplicates (3)
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
# This file provides some common code that is intented to be called
# by the various boot-<arch> scripts.

# Make sure that sbin directories are on the PATH too - filesystem
# creation tools are often hidden there
PATH=/sbin:/usr/sbin:$PATH
export PATH

# Expand %ARCH% variable in envvars for location of D-I images
DI_WWW_HOME="$(echo "$DI_WWW_HOME" | sed -e "s|%ARCH%|$ARCH|g")"
DI_DIR="$(echo "$DI_DIR" | sed -e "s|%ARCH%|$ARCH|g")"

# Make sure that DI_DIST is set to something sane
if [ ! "$DI_DIST" ]; then
    DI_DIST="$DI_CODENAME"
fi

# Find out what the default desktop is in tasksel if we've not set it
# (i.e. using "all" for netinst, DVD etc.) - print the name of the
# first desktop task recommended by task-desktop
UNSPEC_DESKTOP_DEFAULT="$($BASEDIR/tools/apt-selection cache depends task-desktop | \
    awk '
    /Recommends:.*desktop/ {
        gsub("task-","")
        gsub("-desktop","")
        print $2
        exit
    }')"

# Only i386 and amd64 use DESKTOP to set the default desktop;
# make sure other arches get a working config
if [ "$ARCH" != i386 ] && [ "$ARCH" != amd64 ]; then
    if [ "$DESKTOP" = all ] || [ "$DESKTOP" = "$UNSPEC_DESKTOP_DEFAULT" ] ; then
        DESKTOP=
        KERNEL_PARAMS="$(echo "$KERNEL_PARAMS" | \
                sed -r "s/desktop=[^ ]* ?//")"
    elif [ "$DESKTOP" = light ] ; then
        DESKTOP=xfce
        KERNEL_PARAMS="$(echo "$KERNEL_PARAMS" | \
                sed -r "s/(desktop=)light/\1xfce/")"
    fi
fi


# Add an option to the mkisofs options for this CD _only_ if it's not
# already set. $1 is the opts file location, "$2" is the new
# option. Call this with _logical groupings_ of options
add_mkisofs_opt() {
   OPTS_FILE=$1
   NEW_OPT="$2"

   if ! ( grep -q -- "$NEW_OPT" $OPTS_FILE 2>/dev/null) ; then
       echo -n "$NEW_OPT " >> $OPTS_FILE
   fi
}

variant_enabled() {
    VARIANT=$1

    echo "$VARIANTS" | grep -qw "$VARIANT"
    return $?
}

# Wrapper around which_deb which looks in all relevant distributions
find_pkg_file() {
    local pkgfile
    for dist in $DI_CODENAME $CODENAME ${UNRELEASED/1/unreleased}; do
        pkgfile=$($BASEDIR/tools/which_deb $MIRROR $dist "$@")
        [ -n "$pkgfile" ] && break
    done
    if [ -z "$pkgfile" ]; then
        echo "WARNING: unable to find the $@ package in $DI_CODENAME or $CODENAME distribution of the mirror" >&2
    fi
    echo $pkgfile
}

# Work out the right boot load size for a specified file
calc_boot_size() {
    FILE=$1

    size=$[($(stat -c%s "$FILE")+2047)/2048]
    echo $size
}

# If we're looking for an image location to download, see if it's in a
# local cache first.
try_di_image_cache() {
    DI_TMP_DIR=${DI_WWW_HOME#*https://}
    if [ -n "$DI_DIR" ] && [ -e "${DI_DIR}/${DI_TMP_DIR}" ] ; then
        DI_DIR="$DI_DIR/${DI_TMP_DIR}"
        DI_WWW_HOME=""
        echo "Using images from local cache: $DI_DIR"
    else
	# If not, we'll end up downloading. Complain if the download
	# link looks insecure (i.e. not https). May cause builds to
	# fail here in future...
        case "$DI_WWW_HOME"x in
	    "http://"*x|"ftp://"*x)
		echo "WARNING WARNING WARNING WARNING WARNING WARNING"
		echo "$0: insecure download for d-i build: $DI_WWW_HOME"
		echo "WARNING WARNING WARNING WARNING WARNING WARNING"
		;;
	esac
    fi
}

# This arch is currently not bootable directly from CD, and there's
# not a lot we can do about that. But add the needed files in the
# right place so that users can find them, at least:
#
# kernel(s)
# initramfs
# DTBs
# etc...
#
# The best wasy to find all the files is to parse d-i's MANIFEST file
# and work from there.
copy_arch_images() {
    mkdir -p $CDDIR/$INSTALLDIR
    cd $CDDIR/$INSTALLDIR

    if [ ! "$DI_WWW_HOME" ];then
	if [ ! "$DI_DIR" ];then
            DI_DIR="$MIRROR/dists/$DI_DIST/main/installer-$ARCH/current/images"
	fi
	cp "$DI_DIR/MANIFEST" MANIFEST
    else
	$WGET "$DI_WWW_HOME/MANIFEST" -O MANIFEST
    fi

    for image in $(awk '{print $1}' MANIFEST); do
	if [ ! -e "$image" ]; then
            dir=$(dirname $image)
            mkdir -p $dir
            if [ -n "$LOCAL"  -a -f "${LOCALDEBS:-$MIRROR}/dists/$DI_DIST/local/installer-$ARCH/current/images/$image" ]; then
		cp "${LOCALDEBS:-$MIRROR}/dists/$DI_DIST/local/installer-$ARCH/current/images/$image" "$image"
            elif [ ! "$DI_WWW_HOME" ];then
		if [ ! "$DI_DIR" ];then
                    DI_DIR="$MIRROR/dists/$DI_DIST/main/installer-$ARCH/current/images"
		fi
		cp -a "$DI_DIR/$image" "$image"
            else
		$WGET --no-parent -r -nH --cut-dirs=3 "$DI_WWW_HOME/$image"
            fi
	fi
    done

    # Clean up in case we had to use $WGET :-(
    find . -name 'index.html*' -delete
}

# Grab the xorriso version and turn it into a number we can use
xorriso_version() {
    $MKISOFS --version 2>&1 | awk '
	/^xorriso version/ {
	    split($4, ver, ".")
	    print ver[1]*10000+ver[2]*100+ver[3]
	}'
}

# Work out how many blocks we need for a FAT filesystem to hold all
# the EFI boot files
calculate_efi_image_size() {
    CDDIR=$1

    sector_bytes=512 # -S
    cluster_sectors=4 # -s
    cluster_bytes=$((sector_bytes * cluster_sectors))

    clusters=4 # 1 cluster for each sub-directory
    for file in $CDDIR/EFI/*/*; do
        [ -f "$file" ] || continue
        clusters=$(($clusters + (($(stat -c %s "$file") + $cluster_bytes - 1) / $cluster_bytes)))
    done
    reserved_sectors=1 # boot-sector -R
    reserved_bytes=$(($reserved_sectors * $sector_bytes))
    fat_copies=2 # -f
    if [ "$clusters" -le $(((1 << 12) - 2)) ]; then
        fat_entry_bytes=3/2 # -F
    elif [ "$clusters" -le $(((1 << 16) - 2)) ]; then
        fat_entry_bytes=2 # -F
    else
        fat_entry_bytes=4 # -F
    fi
    fat_bytes=$((($clusters * $fat_entry_bytes + $sector_bytes - 1) / $sector_bytes * $sector_bytes))
    root_entries=512 # -r
    root_entry_bytes=32
    root_bytes=$(($root_entries * root_entry_bytes))
    size=$(($reserved_bytes + $fat_copies * $fat_bytes + $root_bytes + $clusters * $cluster_bytes))

    track_sectors=32
    track_bytes=$((sector_bytes * $track_sectors))
    tracks=$((($size + $track_bytes - 1) / $track_bytes))
    block_bytes=1024
    blocks=$(($tracks * $track_bytes / $block_bytes))

    echo $blocks
}

extra_image () {
    image=$1
    doppelgangers="$2"
    dir=$(dirname "$image")

    mkdir -p $CDDIR/$INSTALLDIR/$dir
    if [ ! -e $CDDIR/$INSTALLDIR/"$image" ] ; then
        if [ ! "$DI_WWW_HOME" ] ; then
            if [ ! "$DI_DIR" ] ; then
                DI_DIR="$MIRROR/dists/$DI_DIST/main/installer-$ARCH/current/images"
            fi
            cp "$DI_DIR/cdrom/$image" $CDDIR/$INSTALLDIR/"$image"
        else
            $WGET "$DI_WWW_HOME/cdrom/$image" -O $CDDIR/$INSTALLDIR/"$image"
        fi
    fi
    for doppelganger in $doppelgangers ; do
        if [ -f "$CDDIR/$INSTALLDIR/$dir/$doppelganger" ] &&
               cmp -s $CDDIR/$INSTALLDIR/"$image" $CDDIR/$INSTALLDIR/"$dir"/"$doppelganger" ; then
            echo "    $image identical to $doppelganger. Linking"
            ln -nf $CDDIR/$INSTALLDIR/"$dir"/"$doppelganger" $CDDIR/$INSTALLDIR/"$image"
            break
        fi
    done
}

install_firmwares_initrd () {
    initrd=$1
    regex="$2"
    if [ "$FORCE_FIRMWARE"x = "1"x ]; then
        FILES=`$BASEDIR/tools/catz ${MIRROR}/dists/${DI_DIST}/*/binary-${ARCH}/Packages.gz | \
            grep-dctrl -Pe "$regex" -sFilename -n || true`
        if [ -n "${FILES}" ]; then
            echo "    Adding firmwares from" ${FILES}

            initrdgz=$initrd.gz
            FWDIR=$TDIR/firmware

            zcat $initrdgz > $initrd
            rm -f $initrdgz
            for FILE in $FILES; do
                dpkg -x "${MIRROR}/${FILE}" ${FWDIR}
                # Let hw-detect know about packages embedded this way (#1106005):
                basename ${FILE} >> ${FWDIR}/usr/lib/firmware/.embedded-firmware-debs
            done
	    FWLOCATIONS=""
	    for dir in lib/firmware usr/lib/firmware; do
		if [ -d $dir ]; then
		    FWLOCATIONS="$FWLOCATIONS $dir"
		fi
	    done
            (cd ${FWDIR} ; find $FWLOCATIONS | cpio -oA -H newc -F $initrd)
            pigz -9nm $initrd
            rm -fr $FWDIR
	else
	    echo "    WARNING: Could not find firmware package(s) matching $regex"
        fi
    fi
}

change_grub_cfg_uuid () {
    CDDIR=$1

    # We (used to) look for /.disk/info in grub.cfg to find this
    # Debian media once we've booted via EFI. This is not 100%
    # reliable - see #1024346 and #1024720 for examples where this
    # fails. Instead, let's generate a UUID here and use that as a
    # flag file.
    FILE=$CDDIR/EFI/debian/grub.cfg
    if [ -f $FILE ]; then
	UUID=$(uuidgen)
	mkdir -p $CDDIR/.disk/id
	touch $CDDIR/.disk/id/$UUID
	sed -i "/search --file --set=root/s,.disk/info,.disk/id/$UUID," $FILE
	sed -i '/^source.*grub.cfg$/s,^.*$,source $prefix/${grub_cpu}-efi/grub.cfg,' $FILE
    fi
}