-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_img.sh
executable file
·202 lines (181 loc) · 4.96 KB
/
create_img.sh
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
#!/bin/bash
#
# create_img.sh - Script to create a flashable Linux Image for MOVE-II's CDH Devboard
#
# Copyright (C) 2016 Jonas Sticha ([email protected])
#
# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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/>.
# Stop script on error or if an unset variable is accessed
set -eu
# Define global variables
IMAGE_FILE=
ROOTFS_SIZE_MB="128"
BOOT_SIZE_MB="16"
IMAGE_SIZE=
IMAGE_NAME="core-image-cdh"
BOOT_BIN_URL="https://github.com/move-II/cdh-utilities/raw/binaries/BOOT.BIN"
UBOOT_BIN_URL="https://github.com/move-II/cdh-utilities/raw/binaries/u-boot.bin"
UBOOT_ENV_URL="https://github.com/move-II/cdh-utilities/raw/binaries/uboot.env"
UNCOMPLETE="true"
DEPLOY_DIR=
TMP_DIR=
LOOP_DEV=
function print_usage {
echo "Usage: $0 [options] deploy_path"
}
function parse_arguments {
if [ "$#" -lt 1 ]
then
echo "${#}ERROR: Wrong number of arguments."
print_usage
exit 1
fi
DEPLOY_DIR=$BASH_ARGV
while getopts :s:i: arg; do
case $arg in
s)
ROOTFS_SIZE_MB="$OPTARG"
;;
i)
IMAGE_NAME="$OPTARG"
;;
\?)
echo "ERROR: unknown argument \"${OPTARG}\"!"
print_usage
exit 1
;;
esac
done
IMAGE_FILE="Linux_${IMAGE_NAME}.img"
IMAGE_SIZE="$((1+$BOOT_SIZE_MB+$ROOTFS_SIZE_MB))"
}
function print_dashline {
echo "-----------------------------------------------"
}
function allocate_image_file {
print_dashline
echo "Image size: ${IMAGE_SIZE}MB"
print_dashline
fallocate -l ${IMAGE_SIZE}M $IMAGE_FILE
}
function create_partitions {
print_dashline
echo "Creating partitions..."
parted -s $IMAGE_FILE -- mktable msdos \
mkpart p fat32 1 $(($BOOT_SIZE_MB)) \
mkpart p ext4 $(($BOOT_SIZE_MB+1)) -0
echo "Done:"
parted $IMAGE_FILE print
print_dashline
}
function setup_loopdevice {
print_dashline
echo "Setting up loop device for image..."
LOOP_DEV=$(losetup -Pf --show $IMAGE_FILE)
echo "Loop device set up on $LOOP_DEV."
print_dashline
}
function detach_loopdevice {
echo "Detaching loopdevice $LOOP_DEV"
losetup -D $IMAGE_FILE
LOOP_DEV=
}
function create_filesystems {
print_dashline
echo "Creating filesystems for partitions..."
mkfs.vfat ${LOOP_DEV}p1
mkfs.ext4 ${LOOP_DEV}p2
echo "Done"
print_dashline
}
function mount_tmpdir {
TMP_DIR=$(mktemp -d)
mount $1 $TMP_DIR
}
function umount_tmpdir {
umount $TMP_DIR
rm -r $TMP_DIR
TMP_DIR=
}
function populate_boot_partition {
print_dashline
echo "Populating boot partition..."
mount_tmpdir ${LOOP_DEV}p1
echo "Boot partition mounted to $TMP_DIR"
wget $BOOT_BIN_URL -O $TMP_DIR/BOOT.BIN
wget $UBOOT_BIN_URL -O $TMP_DIR/u-boot.bin
wget $UBOOT_ENV_URL -O $TMP_DIR/uboot.env
cp $DEPLOY_DIR/zImage $TMP_DIR/zImage
cp $DEPLOY_DIR/zImage-at91-sama5d2_xplained.dtb $TMP_DIR/at91-sama5d2_xplained.dtb
umount_tmpdir
echo
echo "Boot partition completed successfully."
print_dashline
}
function populate_rootfs {
print_dashline
echo "Populating rootfs..."
mount_tmpdir ${LOOP_DEV}p2
echo "Rootfs partition mounted to $TMP_DIR"
tar xfz $DEPLOY_DIR/${IMAGE_NAME}-sama5d2-xplained.tar.gz -C $TMP_DIR
tar xfz $DEPLOY_DIR/modules-sama5d2-xplained.tgz -C $TMP_DIR
ls -lah $TMP_DIR
umount_tmpdir
echo
echo "Rootfs populated successfully"
print_dashline
}
function cleanup {
print_dashline
echo "Cleaning up..."
if [ "$TMP_DIR" != "" ]
then
echo "Remove remaining temporary directory $TMP_DIR"
umount_tmpdir
fi
if [ "$LOOP_DEV" != "" ]
then
detach_loopdevice
fi
if [ "$UNCOMPLETE" != "false" ]
then
rm -f $IMAGE_FILE
echo "Remove unfinished image file $IMAGE_FILE"
fi
echo "Cleanup complete."
print_dashline
}
######################
# Start of Execution #
######################
trap cleanup 0
# Check for root permissions
if [ "$(whoami)" != "root" ]
then
echo "ERROR: root permissions required. Try again with sudo."
exit 1
fi
# Execute main process
parse_arguments "$@"
allocate_image_file
create_partitions
setup_loopdevice
create_filesystems
populate_boot_partition
populate_rootfs
detach_loopdevice
print_dashline
echo "Image $IMAGE_FILE was created successfully."
UNCOMPLETE="false"
print_dashline