aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/make_dualboot_image.sh
blob: b4f55ea8b89d3d5fce6d86b6698305f9e6082c8c (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
#!/bin/bash

# 
# Creates a new raw image file:
# - creating one vfat partition covering whole size
# - formating partition with vfat
# - provision GRUB boot-code (expecting 'grub' folder on vfat partition)
# - copy custom files to vfat partition
#
sdir=`dirname $(readlink -f "${BASH_SOURCE[0]}")`
rootdir=`dirname $sdir`

usage() {
    echo "Usage: $0 <image-file> <total-size> <mount-point> <provisioning-dir>"
    echo "For <total-size> use G for GiB or M for MiB denominator"
}

if [ -z "${1}" -o -z "${2}" -o -z "${3}" -o -z "${4}" ]; then
    usage
    exit 2
fi

readonly grub_image="data/grub-i386-image01.bin"

IMG_FILE="${1}"
IMG_FILE_SIZE="${2}"
MNT_DIR="${3}"
SRC_DIR="${4}"

dd if=/dev/zero of=${IMG_FILE} bs=4M count=${IMG_FILE_SIZE} conv=notrunc iflag=count_bytes,skip_bytes oflag=seek_bytes,dsync status=progress

sfdisk ${IMG_FILE} << EOF
4MiB,,c,*;
EOF
sync

nbd_dev=$(${rootdir}/imagetool.sh --connect-raw ${IMG_FILE})
if [ -z "${nbd_dev}" ]; then
    echo "No mapped nbd to image, NBD_DEV '${nbd_dev}', abort"
    exit 2
fi

p1dev="/dev/mapper/${nbd_dev}p1"
mkfs.fat -n BOOT -F 32 -v ${p1dev}
sync

${sdir}/grubimg_to_blockdevice.sh "${grub_image}" "/dev/${nbd_dev}"

mount ${p1dev} ${MNT_DIR}
cp -a ${SRC_DIR}/* ${MNT_DIR}/
sync
umount ${MNT_DIR}
sync

# just make sure ..
fsck.vfat -V -y ${p1dev}
fsck.vfat -V -y ${p1dev}

${rootdir}/imagetool.sh --disconnect ${nbd_dev}

echo "New image ${IMG_FILE} partitioned, formatted and provisioned with GRUB boot-code and custom files."