-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.sh
executable file
·97 lines (82 loc) · 2.26 KB
/
index.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
#!/usr/bin/env bash
set -e
GS_FORCE=false
GS_IMG=""
GS_SIZE=${GOSPAWN_GS_IMG_SIZE:-4GB}
while [ "$1" != "" ]; do
case "$1" in
--force) GS_FORCE=true; shift ;;
--size) GS_SIZE="$2"; shift; shift ;;
--ubuntu) UBUNTU="$2"; shift; shift ;;
--debian) DEBIAN="$2"; shift; shift ;;
--arch) ARCH=true; shift; ;;
--help) shift; ;;
*) GS_IMG="$1"; shift; ;;
esac
done
if [ "$GS_IMG" == "" ]; then
echo "Welcome to benOS and goSpawn. Usage Instructions Are Below."
echo "Usage: gospawn <container.img> [options]"
echo
echo " --force"
echo " --size <image-size>"
echo " --ubuntu <version>"
echo " --debian <version>"
echo " --arch"
echo
echo "Usage Examples:"
echo
echo " gospawn --arch --size 4GB"
echo " gospawn --ubuntu xenial --size 3GB"
echo " gospawn --debian stable"
echo
exit 1
fi
GS_SIZE=$(($(echo $GS_SIZE | sed 's|[bB]||' | sed 's|[kK]|* 1024|' | sed 's|[mM]|* 1024 * 1024|' | sed 's|[gG]|* 1024 * 1024 * 1024|')))
required () {
if [ "$(which $1 2>/dev/null)" == "" ]; then
echo $1 is required
exit 1
fi
}
[ "$ARCH" != "" ] && required pacstrap
[ "$UBUNTU" != "" ] && required debootstrap
[ "$DEBIAN" != "" ] && required debootstrap
if $GS_FORCE; then
rm -f "$GS_IMG"
fi
if [ -f "$GS_IMG" ]; then
echo $GS_IMG already exists
exit 1
fi
echo Allocating image ...
fallocate -l "$GS_SIZE" "$GS_IMG"
echo Writing partition table ...
printf 'n\n\n\n2048\n\na\nw\n' | fdisk "$GS_IMG" -u >/dev/null
echo Formatting to ext4 ...
DEV=$(sudo losetup -f --show "$GS_IMG" --offset=$((2048 * 512)))
MNT="$GS_IMG.mnt"
sudo mkfs.ext4 "$DEV" -q >/dev/null
build () {
mkdir -p "$MNT"
sudo mount "$DEV" "$MNT"
"$@" || ERR=$?
sudo umount "$MNT"
rmdir "$MNT"
sudo losetup -d "$DEV"
[ "$ERR" != "" ] && rm -f "$GS_IMG" && exit $ERR
true
}
if [ "$ARCH" != "" ]; then
echo Installing Arch ...
build sudo pacstrap "$MNT" base
elif [ "$UBUNTU" != "" ]; then
echo Installing Ubuntu ...
build sudo debootstrap "$UBUNTU" "$MNT" http://archive.ubuntu.com/ubuntu/
elif [ "$DEBIAN" != "" ]; then
echo Installing Debian ...
build sudo debootstrap "$DEBIAN" "$MNT" http://deb.debian.org/debian/
else
sudo losetup -d "$DEV"
echo Done. Mount the first partition and install your preferred OS.
fi