forked from ayufan-pine64/linux-build
-
Notifications
You must be signed in to change notification settings - Fork 11
/
make_image.sh
executable file
·45 lines (34 loc) · 869 Bytes
/
make_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
#!/bin/bash
set -x
set -e
IMAGE_NAME="$1"
TARBALL="$2"
BOOTLOADER="$3"
if [ -z "$IMAGE_NAME" ] || [ -z "$TARBALL" ] || [ -z "$BOOTLOADER" ]; then
echo "Usage: $0 <image name> <tarball> <bootloader>"
exit 1
fi
if [ "$(id -u)" -ne "0" ]; then
echo "This script requires root."
exit 1
fi
echo "Attaching loop device"
LOOP_DEVICE=$(losetup -f)
losetup -P $LOOP_DEVICE $IMAGE_NAME
echo "Creating filesystems"
mkfs.vfat ${LOOP_DEVICE}p1
mkswap ${LOOP_DEVICE}p2
mkfs.ext4 ${LOOP_DEVICE}p3
TEMP_ROOT=$(mktemp -d)
mkdir -p $TEMP_ROOT
echo "Mounting rootfs"
mount ${LOOP_DEVICE}p3 $TEMP_ROOT
echo "Unpacking rootfs archive"
bsdtar -xpf "$TARBALL" -C "$TEMP_ROOT"
echo "Installing bootloader"
dd if=$TEMP_ROOT/boot/$BOOTLOADER of=${LOOP_DEVICE} bs=8k seek=1
echo "Unmounting rootfs"
umount $TEMP_ROOT
rm -rf $TEMP_ROOT
# Detach loop device
losetup -d $LOOP_DEVICE