-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.sh
executable file
·103 lines (80 loc) · 3.11 KB
/
compile.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
#!/bin/bash
#
# Copyright 2020 CJ Harries
# Licensed under http://www.apache.org/licenses/LICENSE-2.0
set -xe
# Create a raw image
rm -rf alpine.img
qemu-img create alpine.img 1G
# Get a reference to the image
FILE_POINTER=$(losetup --partscan --show --find alpine.img)
# Partition and format the image
sfdisk $FILE_POINTER < mbr.out
mkfs.fat -F32 ${FILE_POINTER}p1
mkfs.ext4 ${FILE_POINTER}p2
# Set up mounts
mkdir -p mnt
mount ${FILE_POINTER}p2 mnt
mkdir mnt/{boot,dev,proc}
mount ${FILE_POINTER}p1 mnt/boot
mount -t proc none mnt/proc
mount -o bind /dev mnt/dev
ln -s /sys mnt/sys
cd mnt
# Create minirootfs
curl -fL http://dl-cdn.alpinelinux.org/alpine/v3.12/releases/x86_64/alpine-minirootfs-3.12.0-x86_64.tar.gz -o alpine.tgz
tar xzf alpine.tgz
rm -rf alpine.tgz
# Pull in kernel
curl -fL http://dl-cdn.alpinelinux.org/alpine/v3.12/releases/x86_64/alpine-netboot-3.12.0-x86_64.tar.gz -o netboot.tgz
tar xzf netboot.tgz boot/{config,initramfs,System.map,vmlinuz}-virt --no-same-owner
rm -rf netboot.tgz
# Save data for chroot
echo "FILE_POINTER=${FILE_POINTER}" > blocks
echo "BOOT=$(blkid ${FILE_POINTER}p1 --output export | grep --color=never '^UUID')" >> blocks
echo "ROOT=$(blkid ${FILE_POINTER}p2 --output export | grep --color=never '^UUID')" >> blocks
CHROOT_DIR=$PWD
chroot $CHROOT_DIR /bin/sh <<"EOF"
source blocks
echo $FILE_POINTER
echo $BOOT
echo $ROOT
echo "$ROOT / ext4 rw,relatime 0 1" >> /etc/fstab
echo "$BOOT /boot vfat rw,relatime 0 2" >> /etc/fstab
# Install extra packages
rm -r /var/cache/apk
mkdir -p /var/cache/apk
echo 'nameserver 1.1.1.1' > /etc/resolv.conf
apk update
apk add openrc openssh sudo syslinux
# Configure users
adduser -D --home /home/vagrant --shell /bin/sh vagrant
sh -c "echo 'vagrant:vagrant' | chpasswd"
echo 'vagrant ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/vagrant
sh -c "echo 'root:vagrant' | chpasswd"
# Configure SSH
sed -i -E 's/^#?\s*UseDNS.*$/UseDNS no/' /etc/ssh/sshd_config
mkdir -p /home/vagrant/.ssh
wget https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant -o /home/vagrant/.ssh/vagrant
wget https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant.pub -o /home/vagrant/.ssh/vagrant.pub
cat /home/vagrant/.ssh/vagrant.pub > /home/vagrant/.ssh/authorized_keys
chmod 0700 /home/vagrant/.ssh
chmod 0600 /home/vagrant/.ssh/authorized_keys
chmod 0600 /home/vagrant/.ssh/vagrant
chmod 0644 /home/vagrant/.ssh/vagrant.pub
chown -R vagrant:vagrant /home/vagrant
# Configure syslinux
# dd bs=440 count=1 conv=notrunc if=/usr/share/syslinux/gptmbr.bin of=${FILE_POINTER}
sed -i -e 's/^default_kernel_opts.*$/default_kernel_opts="cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory"/' -e 's/^root=.*$/root='"$ROOT"'/' /etc/update-extlinux.conf
update-extlinux
# Tidy up
ls -alh /
rm -rf ./blocks ./vagrant{,.pub}
EOF
cd ..
# Unmount everything
umount mnt/{boot,dev,proc}
umount mnt
losetup -d $FILE_POINTER
virt-filesystems -a alpine.img --all --long --uuid -h
# kvm -m 2048 -drive file=alpine.img,format=raw,index=0,media=disk