-
Notifications
You must be signed in to change notification settings - Fork 27
/
create_image.sh
executable file
·66 lines (54 loc) · 1.36 KB
/
create_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
#!/bin/bash -ue
auto=0
if [ "$#" -eq 1 ] && [ "$1" = "auto" ]; then
auto=1
fi
FILE="/iso.img"
if [ -f "$FILE" ]; then
if [ "$auto" -eq 0 ]; then
echo "$FILE already exists" 1>&2
exit 1
fi
exit 0
fi
if [ -f "/etc/init.d/resize2fs_once" ] && [ "$auto" -eq 1 ]; then # this is ugly hack :(
echo "/etc/init.d/resize2fs_once exists" 2>&1
exit 1
fi
size=0
if [ "$auto" -eq 0 ]; then
free="$(df -h / | tail -n1 | awk '{print $4}')"
echo -ne "Space available: $free\nSize, e.g. 16G? "
read size
echo -ne "Filesystem: ntfs and fat32 are supported? "
read part_type
else
free="$(df -k / | tail -n1 | awk '{print $4}')"
size=$(($free-(1024*1024*2)))
if [ "$size" -lt "$((free/2))" ]; then
size=$((free/2))
fi
size="${size}k"
part_type="ntfs"
fi
if [ "$part_type" != "ntfs" ] && [ "$part_type" != "fat32" ]; then
echo "$part_type is not supported, choose ntfs or fat32" 1>&2
exit 1
fi
echo "Creating $size image..."
fallocate -l "$size" "$FILE"
dev="$(losetup -fL --show "$FILE")"
parted "$dev" mklabel msdos
parted "$dev" mkpart p "$part_type" 1M 100%
if [ "$part_type" = "ntfs" ]; then
mkfs.ntfs -fL RPiHDD "${dev}p1"
elif [ "$part_type" = "fat32" ]; then
mkfs.vfat "${dev}p1"
fatlabel "${dev}p1" RPIHDD
else
exit 1
fi
losetup -d "$dev"
sync
mkdir -p /iso
echo "Done!"