This repository has been archived by the owner on Oct 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpi_02_build.sh
87 lines (76 loc) · 2.59 KB
/
rpi_02_build.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
#!/bin/bash
# HOWTO RUN: IMAGE_FILE=raspbian.img CONFIG_DIR=configdir sh rpi_02_build.sh
set -e
if [ "$(id -u)" != "0" ]
then
echo "This script must be run as root" 1>&2
exit
fi
if [ ! -f ${IMAGE_FILE} ]; then
print 'Error: provided image file does not exist'
exit
fi
apt-get update
apt-get -y upgrade
apt-get -y install qemu qemu-user-static expect
PWD=`pwd`
CHROOT_DIR=$PWD'/chroot.tmp.'`date +%Y.%m.%d.%H.%M.%S`
mkdir -p $CHROOT_DIR
init_chroot(){
cp /usr/bin/qemu*arm* ${CHROOT_DIR}/usr/bin/
mount -o bind /run ${CHROOT_DIR}/run
mount -o bind /dev ${CHROOT_DIR}/dev
mount -t devpts pts ${CHROOT_DIR}/dev/pts
mount -t proc none ${CHROOT_DIR}/proc
mount -t sysfs none ${CHROOT_DIR}/sys
mount -o bind /tmp ${CHROOT_DIR}/tmp
mv ${CHROOT_DIR}/etc/ld.so.preload ${CHROOT_DIR}/etc/ld.so.preload.bkp
cp -pf /etc/resolv.conf ${CHROOT_DIR}/etc
cp -pf /etc/environment ${CHROOT_DIR}/etc
}
mount_image(){
OFFSET_BOOT_SIZE=`/sbin/fdisk -lu ${IMAGE_FILE} | tail -n 2 | head -n 1 | awk '{ print $2 }'`
OFFSET_ROOTFS_SIZE=`/sbin/fdisk -lu ${IMAGE_FILE} | tail -n 1 | head -n 1 | awk '{ print $2 }'`
OFFSET_BOOT=$(($OFFSET_BOOT_SIZE * 512))
OFFSET_ROOTFS=$(($OFFSET_ROOTFS_SIZE * 512))
LIMIT_BOOT_SIZE=`/sbin/fdisk -lu ${IMAGE_FILE} | tail -n 2 | head -n 1 | awk '{ print $4 }'`
LIMIT_ROOTFS_SIZE=`/sbin/fdisk -lu ${IMAGE_FILE} | tail -n 1 | head -n 1 | awk '{ print $4 }'`
SIZELIMIT_BOOT=$(($LIMIT_BOOT_SIZE * 512))
SIZELIMIT_ROOTFS=$(($LIMIT_ROOTFS_SIZE * 512))
mount -o loop,offset=${OFFSET_ROOTFS},sizelimit=${SIZELIMIT_ROOTFS} ${IMAGE_FILE} ${CHROOT_DIR}
mount -o loop,offset=${OFFSET_BOOT},sizelimit=${SIZELIMIT_BOOT} ${IMAGE_FILE} ${CHROOT_DIR}/boot
}
clean_up(){
mv -f ${CHROOT_DIR}/etc/ld.so.preload.bkp ${CHROOT_DIR}/etc/ld.so.preload
rm -f ${CHROOT_DIR}/etc/resolv.conf
rm -f ${CHROOT_DIR}/etc/environment
rm -f ${CHROOT_DIR}/usr/bin/qemu*arm*
rm -Rf ${CHROOT_DIR}/tmp/*
umount ${CHROOT_DIR}/dev/pts
umount ${CHROOT_DIR}/dev
umount ${CHROOT_DIR}/run
umount ${CHROOT_DIR}/proc
umount ${CHROOT_DIR}/sys
umount ${CHROOT_DIR}/tmp
umount ${CHROOT_DIR}/boot
umount ${CHROOT_DIR}
rm -Rf ${CHROOT_DIR}
}
trap clean_up EXIT TERM INT
export QEMU_CPU=arm1176
set -x
mount_image
init_chroot
if [ -z "$CONFIG_DIR" ]; then
chroot ${CHROOT_DIR} /bin/bash
else
if [ -d ${CONFIG_DIR}/files ]; then
cp -R $CONFIG_DIR/files/* ${CHROOT_DIR}/.
fi
if [ -f "$CONFIG_DIR/script.sh" ]; then
cp -pf $CONFIG_DIR/script.sh ${CHROOT_DIR}/tmp/inside_chroot.sh
chroot ${CHROOT_DIR} /bin/bash -c "sh /tmp/inside_chroot.sh"
else
chroot ${CHROOT_DIR} /bin/bash
fi
fi