forked from michaelfranzl/rpi23-gen-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-image.sh
executable file
·106 lines (95 loc) · 3.43 KB
/
install-image.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
#!/bin/bash
##################################################
# Shell script to install raspberry pi
# image created using the tools at
# https://github.com/michaelfranzl/rpi23-gen-image
##################################################
runcommand()
{
echo -e "\t$*";
$TEST $*;
}
printUsage()
{
cat <<.EOM
Usage: $0 [options] imageName
Install the image specified. The <imagename> must be one of the
directories in the \"images\" directory.
Options:
--test
Print the commands that will be executed without executing
them.
The script uses the following environment variables to customize the installation:
BOOT_DEVICE the name of the device for the boot partition as seen by the target system. Default: /dev/mmcblk0p1
ROOT_DEVICE the name of the device for the root filesystem as seen by the target system. Default: /dev/mmcblk0p2
HOST_BOOT_DEV the name of th device for the boot partition as seen by the host PC. Default: /dev/mmcblk0p1
HOST_ROOT_DEV the name of th device for the root filesystem as seen by the host PC. Default: /dev/mmcblk0p2
For SD cards, the host and the target device names will most likely be the same. When installing on USB storage,
the USB drive may have a different location, e.g. it may be /dev/sda on the target but /dev/sdb on the host PC.
.EOM
exit 2
}
# Are we running as root?
if [ "$(id -u)" -ne "0" ] ; then
echo "error: this script must be executed with root privileges!"
exit 1
fi
if [ "$#" -eq 0 ] ; then
printUsage
fi
# Set the default values
TEST=""
HOSTNAME=""
while [ "$#" -gt 0 ] ;
do
case "$1" in
--test) TEST="echo";;
--host=*) if [[ "$1" =~ --host=(.*) ]] ; then
HOSTNAME=${BASH_REMATCH[1]}
else
echo -e "Could not find hostname."
fi;;
--host) HOSTNAME=$2
shift;;
-?) printUsage;;
--help) printUsage;;
*) echo -e "Setting IMAGE to $1"
IMAGE=$1;;
esac
shift
done
BOOT_DEVICE=${BOOT_DEVICE:="/dev/mmcblk0p1"}
ROOT_DEVICE=${ROOT_DEVICE:="/dev/mmcblk0p2"}
HOST_BOOT_DEV=${HOST_BOOT_DEV:="/dev/mmcblk0p1"}
HOST_ROOT_DEV=${HOST_ROOT_DEV:="/dev/mmcblk0p2"}
if [ ! -d ./images/${IMAGE}/build/chroot ] ; then
echo -e "Cannot find image directory: \"./images/${IMAGE}/build/chroot\""
exit 2
fi
echo -n -e "Install \"${IMAGE}\" on ${HOST_BOOT_DEV} and ${HOST_ROOT_DEV}? [Y/n]"
read resp
#echo "Response is \"$resp\""
if [[ $resp == "" || $resp == [yY]* ]] ; then
# now let's get to work! Assumes that the partitions are already set up.
runcommand umount ${HOST_BOOT_DEV}
runcommand umount ${HOST_ROOT_DEV}
runcommand mkfs.vfat -n "BOOT" ${HOST_BOOT_DEV}
runcommand mkfs.ext4 -L "rootfs" ${HOST_ROOT_DEV}
if [ -d /mnt/raspcard ] ; then
runcommand mkdir -p /mnt/raspcard
fi
runcommand mount ${HOST_ROOT_DEV} /mnt/raspcard
runcommand mkdir -p /mnt/raspcard/boot/firmware
runcommand mount ${HOST_BOOT_DEV} /mnt/raspcard/boot/firmware
runcommand rsync -a ./images/${IMAGE}/build/chroot/ /mnt/raspcard
if [ ! -z "$HOSTNAME" ] ; then
CURR_HOSTNAME=`cat ./images/${IMAGE}/build/chroot/etc/hostname`
echo -e "\tUpdating hostname from \"${CURR_HOSTNAME}\" to \"$HOSTNAME\""
runcommand sed -i s/${CURR_HOSTNAME}/$HOSTNAME/g /mnt/raspcard/etc/hostname
runcommand sed -i s/${CURR_HOSTNAME}/$HOSTNAME/g /mnt/raspcard/etc/hosts
fi
runcommand umount ${HOST_BOOT_DEV}
runcommand umount ${HOST_ROOT_DEV}
else
exit 0
fi