-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcreate-image.sh
More file actions
executable file
·118 lines (109 loc) · 3.5 KB
/
Copy pathcreate-image.sh
File metadata and controls
executable file
·118 lines (109 loc) · 3.5 KB
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
#!/usr/bin/env bash
set -euo pipefail
if [ $(id -u) -ne 0 ]
then
echo "Script must be run as root"
exit 1
fi
IMAGE_SIZE_MB=16384
SWAP_SIZE_MB=4096
ARCH="amd64"
BUILD_DIR="./mnt"
DEBOOTSTRAP_CACHE_DIR="./.cache/debootstrap"
APT_CACHE_DIR="./.cache/apt"
BUILD_IMAGE="./liox.img"
if [ -d "${BUILD_DIR}" ]
then
echo "Directory \`${BUILD_DIR}\` exists"
exit 1
fi
if [ -f "${BUILD_IMAGE}" ]
then
read -p "Image \`${BUILD_IMAGE}\` already exists. Rebuild? [y/N] " prompt
if [[ "${prompt}" != "y" && "${prompt}" != "Y" ]]
then
echo "Aborting"
exit 1
fi
else
echo "Creating new image"
qemu-img create -f raw "${BUILD_IMAGE}" "${IMAGE_SIZE_MB}M"
fi
set -x
BLOCK_DEVICE=$(sudo losetup --show -f "${BUILD_IMAGE}")
SWAP_END=$((513+SWAP_SIZE_MB))
EFIPART="${BLOCK_DEVICE}p1"
SWAPPART="${BLOCK_DEVICE}p2"
ROOTPART="${BLOCK_DEVICE}p3"
parted -s "${BLOCK_DEVICE}" \
mklabel gpt \
mkpart ESP fat32 1MiB 513MiB \
set 1 boot on set 1 esp on \
mkpart primary linux-swap 513MiB "${SWAP_END}MiB" \
mkpart primary ext4 "${SWAP_END}MiB" 100%
mkfs.fat -F32 "${EFIPART}"
mkswap "${SWAPPART}"
mkfs.ext4 "${ROOTPART}"
mkdir -p "${BUILD_DIR}"
mount "${ROOTPART}" "${BUILD_DIR}"
mkdir -p "${BUILD_DIR}/boot/efi"
mount "${EFIPART}" "${BUILD_DIR}/boot/efi"
mkdir -p "${DEBOOTSTRAP_CACHE_DIR}" "${APT_CACHE_DIR}"
debootstrap \
--cache-dir=$(realpath "${DEBOOTSTRAP_CACHE_DIR}") \
--arch "${ARCH}" \
stable "${BUILD_DIR}" https://deb.debian.org/debian
mkdir -p "${BUILD_DIR}/var/cache/apt/archives"
mkdir -p "${BUILD_DIR}/chroot-overlay"
mkdir -p "${BUILD_DIR}/apt-keys"
mkdir -p "${BUILD_DIR}/apt-sources"
mkdir -p "${BUILD_DIR}/packages"
mount -t tmpfs chroot_tmp "${BUILD_DIR}/tmp"
mount --make-rslave --rbind /proc "${BUILD_DIR}/proc"
mount --make-rslave --rbind /sys "${BUILD_DIR}/sys"
mount --make-rslave --rbind /dev "${BUILD_DIR}/dev"
# mount --make-rslave --rbind /run "${BUILD_DIR}/run"
mount --bind "${APT_CACHE_DIR}" "${BUILD_DIR}/var/cache/apt/archives"
mount --make-rslave --rbind -o ro ./chroot-overlay "${BUILD_DIR}/chroot-overlay"
mount --make-rslave --rbind -o ro ./apt-keys "${BUILD_DIR}/apt-keys"
mount --make-rslave --rbind -o ro ./apt-sources "${BUILD_DIR}/apt-sources"
mount --make-rslave --rbind -o ro ./packages "${BUILD_DIR}/packages"
function cleanup_mounts()
{
umount "${BUILD_DIR}/tmp"
umount -l "${BUILD_DIR}/proc"
umount -l "${BUILD_DIR}/sys"
umount -l "${BUILD_DIR}/dev"
# umount -l "${BUILD_DIR}/run"
umount "${BUILD_DIR}/var/cache/apt/archives"
umount "${BUILD_DIR}/boot/efi"
umount -l "${BUILD_DIR}/chroot-overlay"
umount -l "${BUILD_DIR}/apt-keys"
umount -l "${BUILD_DIR}/apt-sources"
umount -l "${BUILD_DIR}/packages"
rmdir "${BUILD_DIR}/chroot-overlay"
rmdir "${BUILD_DIR}/apt-keys"
rmdir "${BUILD_DIR}/apt-sources"
rmdir "${BUILD_DIR}/packages"
umount "${BUILD_DIR}"
losetup -d "${BLOCK_DEVICE}"
rmdir "${BUILD_DIR}"
}
trap cleanup_mounts EXIT
cp ./chroot-script.sh "${BUILD_DIR}"
if [ -f ./contest.env ]; then
cp ./contest.env "${BUILD_DIR}/tmp/"
fi
cat << EOF > "${BUILD_DIR}/etc/apt/apt.conf.d/99cache"
Binary::apt::APT::Keep-Downloaded-Packages "true";
APT::Keep-Downloaded-Packages "true";
EOF
mkdir -p "${BUILD_DIR}/etc/apt/preferences.d"
cp ./apt-preferences/* "${BUILD_DIR}/etc/apt/preferences.d"
chroot "${BUILD_DIR}" /bin/bash -c \
"/bin/env -i \
BLOCK_DEVICE=${BLOCK_DEVICE} \
EFIPART=${EFIPART} \
SWAPPART=${SWAPPART} \
ROOTPART=${ROOTPART} \
/bin/bash /chroot-script.sh"