~j-harbott/cirros/dev

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
#!/bin/bash
#    part2disk - wrap a partition image in a disk image
#
#    Copyright (C) 2010 Canonical Ltd.
#
#    Authors: Scott Moser <smoser@canonical.com>
#             Marcin Juszkiewicz <marcin.juszkiewicz@linaro.org>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, version 3 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

source "${0%/*}/common-functions.sh"

DEF_SECTOR_SIZE=512
base_d=$(dirname $(readlink -f "${0}"))
PATH="${PATH}:${base_d}"

getsize() {
    local fname="$1" kname="" size=""
    if [ -b "${fname}" ]; then
        kname=$(readlink -f "${fname}") &&
            size=$(awk '$4 == kn { print $3 * 1024 }' \
                         "kn=${kname##*/}" /proc/partitions) &&
            [ -n "${size}" ] || {
                error "failed to read size of ${fname} from /proc/partitions";
                return 1;
            }
    else
        size=$(stat --format "%s" "${fname}") || {
            error "failed to get size of ${fname}"
            return 1;
        }
    fi
    _RET="$size"
}

Usage() {
    cat <<EOF
Usage: ${0##*/} [options] partition-image grub-efi-tarball architecture disk-image

    Create disk image 'disk-image' with 'partition-image' in a partition
    inside it. Add grub if requested.

    options:
      -G | --grub       install grub to disk image mbr
         | --grub1      install grub1 to disk image mbr
         | --grub-efi   install EFI grub to disk image
      -s | --size  S    create the disk image of size 'S'.
                        default is large enough to fit partition-image
      -v | --verbose    increase verbosity
EOF
}
human2bytes() {
    # converts size suitable for input to resize2fs to bytes
    # s:512 byte sectors, K:kilobytes, M:megabytes, G:gigabytes
    # none: block size of the image
    local input=${1} defunit=${2:-1024}
    local unit count;
    case "$input" in
        *s) count=${input%s}; unit=512;;
        *K) count=${input%K}; unit=1024;;
        *M) count=${input%M}; unit=$((1024*1024));;
        *G) count=${input%G}; unit=$((1024*1024*1024));;
        *)  count=${input}  ; unit=${2:-1024};;
    esac
    _RET=$((${count}*${unit}))
}

cleanup() {
    umount "$TMP/efi"
    umount "$TMP/rootfs"
    kpartx -d "$dimg"
}

trap cleanup EXIT

short_opts="b:c:Ghs:v"
long_opts="grub-efi,grub1,grub,help,size:,verbose"
getopt_out=$(getopt --name "${0##*/}" \
    --options "${short_opts}" --long "${long_opts}" -- "$@") &&
    eval set -- "${getopt_out}" ||
    bad_Usage

ssize=${DEF_SECTOR_SIZE}
size_in=""
grub_ptnum=1
grub=0
grub1=0
grub_efi=0
while [ $# -ne 0 ]; do
    cur=${1}; next=${2};
    case "$cur" in
        -G|--grub) grub=1;;
        -G|--grub1) grub1=1;;
        --grub-efi) grub_efi=1;;
        -h|--help) Usage; exit 0;;
        -s|--size) size_in=$2; shift;;
        -v|--verbose) DEBUG=$((${DEBUG}+1));;
        --) shift; break;;
    esac
    shift;
done

[ $# -eq 4 ] || bad_Usage "must supply partition image, grub-efi tarball, architecture and output file"

pimg=${1}
grub_efi_in=${2}
arch=${3}
dimg=${4}

{ [ ${grub} -eq 0 ] || phelper=$(command -v part2disk-grubhelper); } ||
    fail "no part2disk-grubhelper in PATH"
[ $grub1 -eq 0 ] || command -v grub >/dev/null || fail "no 'grub' in path";

[ -f "${pimg}" -o -b "${pimg}" ] || fail "${pimg}: not a file or block device"

getsize "$pimg" ||
    fail "failed to get size of $pimg"
pimg_s="$_RET"

front_pad=$((12*1024*1024))
tot_size=$(($front_pad+$pimg_s))
tot_size_sectors=$(($tot_size/$ssize))

if [ -n "${size_in}" ]; then
    human2bytes "${size_in}" 1 || fail "failed to convert ${size_in} to bytes"
    size=${_RET}
else
    size=$tot_size_sectors
fi

if [ -e "$dimg" ]; then
    getsize "$dimg" ||
        fail "failed to get size of $dimg"
    dimg_s="$_RET"
else
    dimg_s="$size"
fi

if [ "${dimg_s}" -lt "$size" ]; then
    fail "size of $dimg ($dimg_s) not large enough to fit $size"
fi

debug 1 "input is ${pimg_s} bytes."
debug 1 "target is ${tot_size} bytes."

debug 2 "create target image"
dd if=/dev/zero of="${dimg}" bs=$ssize count=$tot_size_sectors \
    2>/dev/null ||
    fail "failed to write to ${dimg}"

debug 2 "create partitions"
sgdisk -n 15:2048:+8M -t 15:ef00 -N 1 $dimg
loop_nr=`kpartx -av $dimg | head -n1 | cut -d" " -f3 | cut -d"p" -f2`

# copy partition image. this writes $pimg bytes even if that is
# not divivisble by $ssize
debug 2 "copying ${pimg} to partition in ${dimg}"
dd if="$pimg" of=/dev/mapper/loop${loop_nr}p1 conv=notrunc \
    2>/dev/null ||
    fail "failed to write ${pimg} into ${dimg}"

if [ ${grub} -ne 0 ]; then
    debug 2 "invoking part2disk-grubhelper ${dimg}"
    sudo "${phelper}" "${dimg}" ||
        fail "part2disk-grubhelper ${dimg} failed"
fi

if [ $grub1 -ne 0 ]; then
    debug 2 "installing grub"
    grub --no-floppy --batch <<EOF
device (hd0) $dimg
root (hd0,0)
setup (hd0)
quit
EOF
fi

if [ $grub_efi -ne 0 ]; then
    case $arch in
        x86_64)  efiarch=x64;;
        aarch64) efiarch=aa64;;
        i386)    efiarch=ia32;;
        arm)     efiarch=arm;;
        *) fail "arch $arch without EFI grub"
    esac
    TMP=$(mktemp -d)
    install -d $TMP/tar $TMP/efi $TMP/rootfs

    mkfs.vfat /dev/mapper/loop${loop_nr}p15
    mount /dev/mapper/loop${loop_nr}p15 $TMP/efi
    install -d $TMP/efi/EFI/BOOT $TMP/efi/EFI/ubuntu
    tar -xf $grub_efi_in -C $TMP/efi/EFI/BOOT

    mount /dev/mapper/loop${loop_nr}p1 $TMP/rootfs
    kernelfile=$(cd $TMP/rootfs/boot; ls vmlinu* | head -n1)
    initrdfile=$(cd $TMP/rootfs/boot; ls initrd.img-* | head -n1)

        
    cat >$TMP/efi/EFI/ubuntu/grub.cfg <<EOF
set default=0
set timeout=1
menuentry 'CirrOS' {
        set root='hd0,gpt1'
        if [ x$feature_platform_search_hint = xy ]; then
          search --no-floppy --label --set=root --hint-bios=hd0,gpt1 --hint-efi=hd0,gpt1 --hint-baremetal=ahci1,gpt1  cirros-rootfs
        else
          search --no-floppy --label --set=root cirros-rootfs
        fi

    linux  /boot/${kernelfile} LABEL=cirros-rootfs ro
    initrd /boot/${initrdfile} 
}
EOF

    umount "$TMP/efi"
    umount "$TMP/rootfs"
fi

kpartx -d "$dimg"
error "wrote to ${dimg}"
# vi: ts=4 noexpandtab