-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean_arch_env.sh
executable file
·136 lines (120 loc) · 3.99 KB
/
clean_arch_env.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash
# Simple tool to create a test arch virtual machine with QEMU.
# Script arguments
BUILD_ARG="-b"
RUN_ARG="-r"
RUN_INSTALLER_ARG="-ri"
CLEAN_ARG="-c"
# Path variables
IMAGE_NAME="clean_arch"
PATH_OF_SCRIPT="$PWD"
PATH_OF_INSTALLER="$PWD/installers/archlinux.iso"
VM_PATH="$PWD/arch_vm"
TOOL_PATH="$PWD/arch-tools"
# UEFI Paths
UEFI_PATH="/usr/share/edk2-ovmf/x64"
OVMF_CODE="$UEFI_PATH/OVMF_CODE.fd"
OG_OVMF_VARS="$UEFI_PATH/OVMF_VARS.fd"
OVMF_VARS="$PATH_OF_SCRIPT/$IMAGE_NAME/OVMF_VARS.fd"
# VM Parameters
# All sizes are in GB
DRIVE_SIZE="30"
RAM_SIZE="32"
CORE_NUM="14"
# Helper functions
# Setup VM directory
setup_path() {
echo "Creating directory..."
if [[ -d $PATH_OF_SCRIPT/$IMAGE_NAME ]]; then
echo "Error: directory \"$PATH_OF_SCRIPT/$IMAGE_NAME/\" already exists."
exit 1
else
mkdir "$PATH_OF_SCRIPT/$IMAGE_NAME"
fi
echo "Copying UEFI vars..."
cp $OG_OVMF_VARS "$OVMF_VARS"
chmod u=rw,g=rw,o=rw "$OVMF_VARS"
}
# Create virtual drive
make_drive() {
echo "Creating virtual ${DRIVE_SIZE}GB drive..."
if [[ -e $PATH_OF_SCRIPT/$IMAGE_NAME/drive.cow ]]; then
echo "Error: $PATH_OF_SCRIPT/$IMAGE_NAME/drive.cow already exists."
exit 1
else
qemu-img create -f qcow2 "$PATH_OF_SCRIPT/$IMAGE_NAME/drive.cow" ${DRIVE_SIZE}G
fi
}
# Option functions
build() {
echo "Building..."
setup_path
make_drive
}
run() {
echo "Running..."
qemu-system-x86_64 -boot c \
-drive if=pflash,format=raw,readonly=on,file="$OVMF_CODE" \
-drive if=pflash,format=raw,file="$OVMF_VARS" \
-cdrom "$PATH_OF_INSTALLER" \
-drive file="$PATH_OF_SCRIPT/$IMAGE_NAME/drive.cow" \
-virtfs local,path="$VM_PATH",mount_tag=host0,security_model=passthrough,id=host0 \
-virtfs local,path="$TOOL_PATH",mount_tag=host1,security_model=passthrough,id=host1 \
-machine q35 \
-enable-kvm \
-device intel-iommu,caching-mode=on \
-cpu max \
-smp cores=$CORE_NUM \
-vga virtio \
-m ${RAM_SIZE}G
}
# Run mount -t 9p host0 --mkdir arch_vm
# mount -t 9p host1 --mkdir arch-tools
# cd arch_vm/after_installer
# ./arch_vm_after_install.sh
run_installer() {
echo "Entering install environment..."
qemu-system-x86_64 -boot d \
-drive if=pflash,format=raw,readonly=on,file="$OVMF_CODE" \
-drive if=pflash,format=raw,file="$OVMF_VARS" \
-cdrom "$PATH_OF_INSTALLER" \
-drive file="$PATH_OF_SCRIPT/$IMAGE_NAME/drive.cow" \
-virtfs local,path="$VM_PATH",mount_tag=host0,security_model=passthrough,id=host0 \
-virtfs local,path="$TOOL_PATH",mount_tag=host1,security_model=passthrough,id=host1 \
-machine q35 \
-enable-kvm \
-device intel-iommu,caching-mode=on \
-cpu max \
-smp cores=$CORE_NUM \
-vga virtio \
-m ${RAM_SIZE}G
}
# Run mount -t 9p host0 --mkdir arch_vm
# mount -t 9p host1 --mkdir arch-tools
# cd arch_vm/installer
# ./arch_vm_install.sh
clean() {
echo "Cleaning environment..."
rm -r "${PATH_OF_SCRIPT:?}/$IMAGE_NAME"
}
# Make sure one and only one argument was passed.
if [[ -e $2 ]]; then
echo "Error: more than one arguement was passed."
exit 1
elif [[ -z $1 ]]; then
echo "Error: either -b, -r, or -c must be passed."
exit 1
fi
# Option runs
if [[ $1 == "$BUILD_ARG" ]]; then
build
elif [[ $1 == "$RUN_ARG" ]]; then
run
elif [[ $1 == "$RUN_INSTALLER_ARG" ]]; then
run_installer
elif [[ $1 == "$CLEAN_ARG" ]]; then
clean
else
echo "Error: invalid arguement, either -b, -r, or -c must be passed."
exit 1
fi