-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
funcs.sh
91 lines (80 loc) · 2.41 KB
/
funcs.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
#!/bin/bash
ARCH='arm64'
qemu_bin='/usr/bin/qemu-aarch64-static'
machine='debian'
ENV='-E RUNLEVEL=1 -E LANG=C -E DEBIAN_FRONTEND=noninteractive -E DEBCONF_NOWARNINGS=yes'
LOOP=`losetup -f`
BOOT_P=${LOOP}p1
ROOT_P=${LOOP}p2
WORK_DIR=`dirname $0`
ROOTFS=${WORK_DIR}/kali_rootfs_tmp
banner()
{
cat <<'EOF'
-----------------------------------------
_____ _ _____ _
| | |___| |_| | |_ _ ___| |_ ___ ___
| | | | -_| _| | | | | _| -_| _|
|_|___|___|_| |__|__|___|_|_|_| |___|_|
_____
| _ |___ ___
| __| _| . | Image Generator
|__| |_| |___| by Shubham Vishwakarma
twitter/git: shubhamvis98
-----------------------------------------
EOF
}
nspawn-exec() {
systemd-nspawn --bind-ro $qemu_bin -M $machine --capability=cap_setfcap $ENV -D ${ROOTFS} "$@"
}
mkimg() {
set -e
[[ -z $2 || $2 -lt 3 ]] && echo -e "Usage:\n\tmkimg {filename} {size_in_GB}\n\nNote: Size must be more than 3GB" && return
IMG=$1
SIZE=$2
PARTS=$3
[ -e ${IMG} ] && echo -e '[*]$IMG already exists. So, skipping mkimg' && return
echo "[*]Creating blank Image: ${IMG} of size ${SIZE}GB..."
dd if=/dev/zero of=${IMG} bs=1M count=$((1024*$SIZE)) status=progress
source ./partuuid
if [ $PARTS -eq 1 ]
then
losetup ${LOOP} ${IMG}
mkfs.ext4 -L ROOT -U ${ROOT_UUID} ${LOOP}
mkdir -pv ${ROOTFS}
mount -v ${LOOP} ${ROOTFS}
else
echo '[*]Partitioning Image: 512MB BOOT and rest ROOTFS...'
sleep 1
cat << 'EOF' | sfdisk ${IMG}
label: gpt
device: test.img
unit: sectors
first-lba: 2048
sector-size: 512
1 : start=2048, size=1048576, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B
2 : start=1050624, type=B921B045-1DF0-41C3-AF44-4C6F280D3FAE
EOF
echo '[*]Formatting Partitions...'
losetup ${LOOP} -P ${IMG}
[ -e ${BOOT_P} ] && mkfs.ext4 -L BOOT -U ${BOOT_UUID} ${BOOT_P}
[ -e ${ROOT_P} ] && mkfs.ext4 -L ROOT -U ${ROOT_UUID} ${ROOT_P}
echo '[*]Mounting Partitions...'
mkdir -pv ${ROOTFS}
mount -v ${ROOT_P} ${ROOTFS}
mkdir -pv ${ROOTFS}/boot
mount -v ${BOOT_P} ${ROOTFS}/boot
fi
}
cleanup() {
set -x
echo '[*]Unounting Partitions...'
mountpoint -q ${ROOTFS}/boot && umount ${ROOTFS}/boot
mountpoint -q ${ROOTFS} && umount ${ROOTFS}
rm -rf ${ROOTFS} ./partuuid
losetup -d ${LOOP}
}
trap ctrl_c INT
ctrl_c() {
exit 1
}