aboutsummaryrefslogtreecommitdiff
path: root/scripts/disk_image
blob: 0f4316586774d31c8e9e8aa9f23811775bd221e2 (plain) (blame)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/bin/bash

# This helps to see what is going on
set -e

PROG_REQ=( kpartx mkfs.ext4 losetup sgdisk readlink )

function check_depends()
{
    for ((i=0; i<${#PROG_REQ[@]}; i++))
    do
        if ! command -v ${PROG_REQ[i]} > /dev/null
        then
            echo "Please install the needed program: ${PROG_REQ[i]}."
            exit 1
        fi
    done
}

function add_partition()
{
    local rootfs="$1"
    _part_size=`stat -L --printf="%s" $rootfs`
    _part_size=$(( $_part_size + $offset - 1))
    _part_size=$(( $_part_size & ~($offset - 1) ))
    # account for gzip compression
    _part_size=$(( $_part_size * 2 ))
    # add some slack
    _part_size=$(( 128*1024*1024 + $_part_size ))
    _part_size=$(( $_part_size / 1024 / 1024 ))
    echo PART size: "$_part_size"MB
    
    prev=$(( $_npart - 1 ))
    _sector_start[$_npart]=$(( ${_sector_end[$prev]} + 1 ))
    _sector_end[$_npart]=$(( $_part_size * 1024 * 1024 / $_sector_size + ${_sector_start[$_npart]} - 1))

    _tot_size=$(( $_tot_size + $_part_size ))
    _npart=$(( $_npart + 1 ))
}

function write_rootfs()
{
    local j=$1
    local rootfs=$2

    # create mount point and mount diskn
    mkdir -p ${DESTDIR}/part/disk$j
    mount -t ext4 /dev/mapper/diskimage$j $DESTDIR/part/disk$j
    
    # Extract rootfs cpio archive into `.../part/vos_$j`
    cd ${DESTDIR}/part/disk$j

    if [[ $rootfs = *.cpio.gz ]]
    then
        cat "${UBOOT_OUT_ABS}/$rootfs" | gunzip | cpio -id
    elif [[ $rootfs = *.tar.gz ]]
    then
        tar xvzf "${UBOOT_OUT_ABS}/$rootfs"
    else
        echo "Ignoring $rootfs: unsupported file format. Use cpio.gz or tar.gz."
    fi

    cd -
    # umount
    sync
    umount $DESTDIR/part/disk$j
}

function print_help
{
    echo "usage:"
    echo "	$0 -c CONFIG_FILE -d UBOOT_DIRECTORY <-w WORK_DIRECTORY> -o IMG_FILE"
    echo "	$0 -h"
    echo "where:"
    echo "	-c CONFIG_FILE - configuration file"
    echo "	-d UBOOT_DIRECTORY - root directory for the paths specified in CONFIG_FILE"
    echo "	-w WORK_DIRECTORY - work directory used when building the image"
    echo "	-o IMG_FILE - the output img file "
    echo "Example:"
    echo "	$0 -c ../config -d ./build42 -w tmp -o disk.img"
}

# before anything else, check if we have root privilege
if ! [ $(id -u) = 0 ]
then
    echo "This script needs root privilege to run, exiting."
    exit 1
fi

while getopts ":w:d:c:o:h" opt
do
    case ${opt} in
    w )
        DESTDIR=$OPTARG
        ;;
    d )
        UBOOT_OUT=$OPTARG
        ;;
    c )
        CFG_FILE=$OPTARG
        ;;
    o )
        IMG=$OPTARG
        ;;
    h )
        print_help
        exit 0
        ;;
    * )
        echo "Unknown option, see \"$0 -h\""
        exit 1
        ;;
    esac
done
shift $((OPTIND -1))

if [ -z "$UBOOT_OUT" ] || [ -z "$CFG_FILE" ] || [ -z "$IMG" ]
then
    echo "Undefined arguments, see \"$0 -h\""
    exit 1
fi

# if the user hasn't specified a working directing, create it
if [ -z "$DESTDIR" ]
then
    DESTDIR="$(mktemp -d /tmp/imagebuilder.XXXXXX)"
    DESTDIR_DEL=true
else
    DESTDIR_DEL=false
fi

UBOOT_OUT_ABS="$(readlink -f $UBOOT_OUT)"
DESTDIR_ABS="$(readlink -f $DESTDIR)"

check_depends

source "$CFG_FILE"

offset=$((2*1024*1024))
_part1_size=`stat -L --printf="%s" $UBOOT_OUT/$XEN`
_part1_size=$(( $_part1_size + `stat -L --printf="%s" $UBOOT_OUT/$DOM0_KERNEL` ))
_part1_size=$(( $_part1_size + `stat -L --printf="%s" $UBOOT_OUT/$DEVICE_TREE` ))
_part1_size=$(( $_part1_size + `stat -L --printf="%s" $UBOOT_OUT/$UBOOT_SOURCE` ))
_part1_size=$(( $_part1_size + `stat -L --printf="%s" $UBOOT_OUT/$UBOOT_SCRIPT` ))
i=0
while test $i -lt $NUM_DOMUS
do
    _part1_size=$(( $_part1_size + `stat -L --printf="%s" $UBOOT_OUT/${DOMU_KERNEL[$i]}` ))
    if test "${DOMU_RAMDISK[$i]}"
    then
        _part1_size=$(( $_part1_size + `stat -L --printf="%s" $UBOOT_OUT/${DOMU_RAMDISK[$i]}` ))
    fi
    if test "${DOMU_PASSTHROUGH_DTB[$i]}"
    then
        _part1_size=$(( $_part1_size + `stat -L --printf="%s" $UBOOT_OUT/${DOMU_PASSTHROUGH_DTB[$i]}` ))
    fi
    i=$(( $i + 1 ))
done

# add 16 MB slack
_part1_size=$(( $_part1_size + 16777216 ))
_part1_size=$(( $_part1_size + $offset - 1))
_part1_size=$(( $_part1_size & ~($offset - 1) ))
_part1_size=$(( $_part1_size / 1024 / 1024 ))
echo PART1 size: "$_part1_size"MB

_sector_size=512
# _sector_start[0] needs to be aligned to 2048
_sector_start[0]=2048
_sector_end[0]=$(( $_part1_size * 1024 * 1024 / $_sector_size + ${_sector_start[0]} - 1))
_tot_size=$(( $_part1_size ))
_npart=1

if test "$DOM0_ROOTFS"
then
    add_partition "$UBOOT_OUT/$DOM0_ROOTFS"
fi

i=0
while test $i -lt $NUM_DOMUS
do
    if test "${DOMU_ROOTFS[$i]}"
    then
        add_partition "$UBOOT_OUT/${DOMU_ROOTFS[$i]}"
    fi
    i=$(( $i + 1 ))
done
_tot_size=$(( $_tot_size + 16 ))

# NOTE: Increase vos_a to 256 to accomodate rootfs
# 528 MiB (256 + 256 + 16)
truncate $IMG -s "$_tot_size"M

# create GPT partition table
sgdisk -og $IMG

i=0
j=1
while test $i -lt $_npart
do
    sgdisk -n $j:${_sector_start[$i]}:${_sector_end[$i]} -c $j:"Linux""$j" -t $j:8300 $IMG
    i=$(( $i + 1 ))
    j=$(( $j + 1 ))
done

# find the first available loop device
_loop_dev=$(losetup -f)

# attach loopback device to $IMG
losetup $_loop_dev $IMG

_disksize=$(blockdev --getsize $_loop_dev)

dmsetup create diskimage --table "0 $_disksize linear $_loop_dev 0"

# ensure that /dev/mapper/diskimage exists
while [ ! -b /dev/mapper/diskimage ]
do
    sleep 2
done

kpartx -a /dev/mapper/diskimage

# ensure that /dev/mapperdiskimage1 exists
while [ ! -b /dev/mapper/diskimage1 ]
do
    sleep 2
done

i=0
j=1
while test $i -lt $_npart
do
    mkfs.ext4 -L vos_$j -F /dev/mapper/diskimage$j
    i=$(( $i + 1 ))
    j=$(( $j + 1 ))
done

# create mount point and mount disk1
mkdir -p ${DESTDIR}/part/disk1
mount -t ext4 /dev/mapper/diskimage1 $DESTDIR/part/disk1

# only copy over files that were counted for the partition size
cd "$UBOOT_OUT"
cp --parents "$XEN" "${DESTDIR_ABS}/part/disk1/"
cp --parents "$DOM0_KERNEL" "${DESTDIR_ABS}/part/disk1/"
cp --parents "$DEVICE_TREE" "${DESTDIR_ABS}/part/disk1/"
cp --parents "$UBOOT_SOURCE" "${DESTDIR_ABS}/part/disk1/"
cp --parents "$UBOOT_SCRIPT" "${DESTDIR_ABS}/part/disk1/"
i=0
while test $i -lt $NUM_DOMUS
do
    cp --parents "${DOMU_KERNEL[$i]}" "${DESTDIR_ABS}/part/disk1/"
    if test "${DOMU_RAMDISK[$i]}"
    then
        cp --parents "${DOMU_RAMDISK[$i]}" "${DESTDIR_ABS}/part/disk1/"
    fi
    if test "${DOMU_PASSTHROUGH_DTB[$i]}"
    then
        cp --parents "${DOMU_PASSTHROUGH_DTB[$i]}" "${DESTDIR_ABS}/part/disk1/"
    fi
    i=$(( $i + 1 ))
done
cd -

# unmount
sync
# This fails for some reason. It could work now because we are not using qemu-user
# fstrim $DESTDIR/part/disk1
umount $DESTDIR/part/disk1

j=2
if test "$DOM0_ROOTFS"
then
    write_rootfs 2 "$DOM0_ROOTFS"
    j=$(( $j + 1 ))
fi

i=0
while test $i -lt $NUM_DOMUS
do
    if test "${DOMU_ROOTFS[$i]}"
    then
        write_rootfs $j "${DOMU_ROOTFS[$i]}"
        j=$(( $j + 1 ))
    fi
    i=$(( $i + 1 ))
done


kpartx -d /dev/mapper/diskimage
dmsetup remove diskimage
losetup -d $_loop_dev

if [ "$DESTDIR_DEL" = true ]
then
    rm -rf "$DESTDIR"
fi