Skip to content

Instantly share code, notes, and snippets.

@souenzzo
Created October 2, 2018 02:43
Show Gist options
  • Save souenzzo/54c7fbcda6d811b1ae62b65e8041e656 to your computer and use it in GitHub Desktop.
Save souenzzo/54c7fbcda6d811b1ae62b65e8041e656 to your computer and use it in GitHub Desktop.
cria uma VM com arch linux descartavel em menos de 1 min. Requer ~3Gb de RAM livre (para criar disco+RAM)
#!/usr/bin/env sh
set -x
set -e
MNTDIR="/tmp/lol/mnt"
TEMPDISK="/tmp/lol/tempdisk.img"
SSH_PUB="${HOME}/.ssh/id_rsa_$(hostname).pub"
TEMPDIR="$(dirname "${TEMPDISK}")"
if ! [ -f "${TEMPDISK}" ]; then
[ -d "${TEMPDIR}" ] || mkdir -p "${TEMPDIR}"
qemu-img create -f raw "${TEMPDISK}" 4G
mkfs.ext4 "${TEMPDISK}" | tee "${TEMPDIR}/disk"
[ -d "${MNTDIR}" ] || mkdir -p "${MNTDIR}"
sudo mount "${TEMPDISK}" "${MNTDIR}"
sudo pacstrap -c "${MNTDIR}" base python2 openssh vim htop docker systemd
sudo arch-chroot "${MNTDIR}" groupadd -g 1000 ec2-user
sudo arch-chroot "${MNTDIR}" useradd -m -g ec2-user -u 1000 ec2-user
sudo arch-chroot "${MNTDIR}" sed -i -E \
"s/^MODULES=\(/MODULES=\(virtio virtio_blk virtio_pci virtio_net/" \
"/etc/mkinitcpio.conf"
sudo arch-chroot "${MNTDIR}" mkinitcpio -p linux
mkdir -p "${MNTDIR}/home/ec2-user/.ssh"
cat "${SSH_PUB}" > "${MNTDIR}/home/ec2-user/.ssh/authorized_keys"
sudo arch-chroot "${MNTDIR}" systemctl enable sshd
cp "${MNTDIR}/boot/vmlinuz-linux" "${MNTDIR}/boot/initramfs-linux.img" "/tmp/lol"
sudo umount "${MNTDIR}"
fi
UUID="$(awk '/UUID/ { print $3 }' "${TEMPDIR}/disk")"
qemu-system-x86_64 --enable-kvm \
-kernel /tmp/lol/vmlinuz-linux -initrd /tmp/lol/initramfs-linux.img \
-append "root=UUID=${UUID} rw" \
-m 4G -boot order=c \
-drive file="${TEMPDISK}",format=raw,media=disk,if=virtio,index=0
# 7f802a27-8c03-4baf-9dbb-6e794a3df209
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment