-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract-alarmpi
executable file
·76 lines (66 loc) · 1.71 KB
/
extract-alarmpi
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
# SOURCE is a .tar.gz file containing arch linux for raspberry pi
UPSTREAM_SOURCE="ArchLinuxARM-rpi-latest.tar.gz"
UPSTREAM_URL="http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-latest.tar.gz"
if [ "$1" = "-h" ]
then
echo "Usage: extract-alarmpi [SOURCE [TARGET [SIZE]]]" >/dev/stderr
echo " SOURCE should be a .tar.gz file"
echo " SIZE is specified in gigabytes (10^9 bytes)"
fi
if [ -n "$1" ]
then
SOURCE=$1
else
echo "Downloading latest Arch Linux ARM distribution..."
wget -N "${UPSTREAM_URL}"
SOURCE="${UPSTREAM_SOURCE}"
fi
# TARGET is a .img binary file
TARGET=image
[ -n "$2" ] && TARGET=$2
# SIZE in bytes
SIZE=$((1000*1000*1000*8)) # 8G
[ -n "$3" ] && SIZE=$((1000*1000*1000*${3}))
shift 3
LOOP_DEVICE=/dev/loop0
modinfo loop | grep -q max_part || {
echo "You must modprobe 'loop' so it is loaded with 'max_part=2' or greater"
exit 1
}
# Delete any existing image
rm -f "$TARGET"
# Make a file filled with zeros
dd if=/dev/zero of="$TARGET" bs=1024 count=$((SIZE/1024))
sync
losetup ${LOOP_DEVICE} "${TARGET}" || exit 1
# Format the disk to have a FAT32 partition and then an ext4 partition
fdisk "${LOOP_DEVICE}" <<EOF
o
p
n
p
1
+100M
t
c
n
p
2
w
EOF
BOOT_DEVICE="${LOOP_DEVICE}p1"
ROOT_DEVICE="${LOOP_DEVICE}p2"
mkfs.vfat ${BOOT_DEVICE}
mkfs.ext4 ${ROOT_DEVICE}
mkdir /tmp/boot /tmp/root
mount ${BOOT_DEVICE} /tmp/boot
mount ${ROOT_DEVICE} /tmp/root
bsdtar -xpf "${SOURCE}" -C /tmp/root
mv /tmp/root/boot/* /tmp/boot
umount /tmp/boot /tmp/root
rmdir /tmp/root /tmp/boot
sudo losetup -d "${LOOP_DEVICE}"
# Reproducible build
touch -r "${SOURCE}" "${TARGET}"
# Set the file to be owned by the real user and not root
chown ${SUDO_UID:-$(id -r -u)}:${SUDO_GID:-$(id -r -g)} "${TARGET}"