Skip to content

Instantly share code, notes, and snippets.

@ffledgling
Created August 25, 2016 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ffledgling/27460a617f0e9ef93afd847b6ef3c20a to your computer and use it in GitHub Desktop.
Save ffledgling/27460a617f0e9ef93afd847b6ef3c20a to your computer and use it in GitHub Desktop.
Fire up a cloud instance of Fedora 22 locally with qemu-kvm
# This is a makefile to use the default fedora cloud image to start a container.
# It lets you create projects and save them separately, based off of a common base image
# Define and declare variables
SHELL := /bin/bash
IMG_STORE := /spare/local/$(USER)/.qemu-kvm-image-store
#IMG_NAME := Fedora-Cloud-Base-24-1.2.x86_64.qcow2
IMG_NAME := Fedora-Cloud-Base-22-20150521.x86_64.qcow2
#IMG_URL := https://download.fedoraproject.org/pub/fedora/linux/releases/24/CloudImages/x86_64/images/$(IMG_NAME)
IMG_URL := https://dl.fedoraproject.org/pub/fedora/linux/releases/22/Cloud/x86_64/Images/$(IMG_NAME)
CURRENT_IMG := $(IMG_STORE)/current.qcow2
BASE_IMG := $(IMG_STORE)/$(IMG_NAME)
SSH_PUB_KEY := $(shell cat ~/.ssh/id_rsa.pub)
INIT_FILE := $(IMG_STORE)/init.iso
MOUNT_PATH := <to be filled in>
MOUNT_TAG := <to be filled in>
define META_DATA
instance-id: iid-local01
local-hostname: myhost
endef
define USER_DATA
#cloud-config
password: abc123
chpasswd: { expire: False }
ssh_pwauth: True
ssh_authorized_keys:
- $(SSH_PUB_KEY)
mounts:
- [$(MOUNT_TAG), $(MOUNT_PATH), 9p]
mount_default_fields: [ None, None, "auto", "defaults,nobootwait", "0", "2" ]
packages:
- sshfs
- perl
final_message: "CLOUD CONFIG WAS LOADED"
endef
export META_DATA
export USER_DATA
ifndef VERBOSE
.SILENT:
endif
# BEGIN TARGET DEFITIONS
.phony: init cred-setup download project start-instance vnc shell
# Targets typically go something like this:
# init -> download -> project -> setup-creds -> start-instance -> [vnc,shell]
################################################################################
# TARGET: init
################################################################################
init:
#echo $${PROJECT_NAME}
: $${PROJECT_NAME:?You need to set PROJECT_NAME}
mkdir -p $(IMG_STORE)
################################################################################
# TARGET: clean
################################################################################
clean:
echo "Purging image store, you'll lose all your work!"
read -n 1 -p 'Proceed [N/y]?: ' yes && if [[ $$yes == 'y' ]]; then rm -r $(IMG_STORE); fi
################################################################################
# TARGET: cred-setup
################################################################################
ifeq (,$(wildcard $(INIT_FILE)))
cred-setup: init
$(eval TMP_ISO := $(shell mktemp -d /tmp/creds-XXXXXX))
echo "$$META_DATA" > $(TMP_ISO)/meta-data
cat $(TMP_ISO)/meta-data
echo "$$USER_DATA" > $(TMP_ISO)/user-data
genisoimage -output $(TMP_ISO)/init.iso -volid cidata -joliet -rock $(TMP_ISO)/{user-data,meta-data}
cp $(TMP_ISO)/init.iso $(INIT_FILE)
isoinfo -l -i $(INIT_FILE)
else
cred-setup:
echo "init file exsists at $(INIT_FILE), reusing..." >&2
endif
################################################################################
# TARGET: download
################################################################################
ifeq (,$(wildcard $(BASE_IMG)))
download: init
echo $(BASE_IMG)
echo "Downloading the Fedora cloud base image" >&2
wget --no-parent -P $(IMG_STORE) $(IMG_URL)
else
download: init
echo "Using existing Fedora cloud base image" >&2
endif
################################################################################
# TARGET: project
################################################################################
$(eval PROJECT_IMG := $(IMG_STORE)/$(PROJECT_NAME))
ifeq (,$(wildcard $(PROJECT_IMG)))
project: download
cp $(BASE_IMG) $(PROJECT_IMG)
unlink $(CURRENT_IMG) || true
ln -s $(PROJECT_IMG) $(CURRENT_IMG)
else
project: download
unlink $(CURRENT_IMG) || true
ln -sf $(PROJECT_IMG) $(CURRENT_IMG)
endif
################################################################################
# TARGET: start-instance
################################################################################
start-instance: project cred-setup
qemu-kvm -hda $(CURRENT_IMG) -boot dc -vnc :6 -m 2G -vga std -cpu host -smp 2 -cdrom $(INIT_FILE) -redir tcp:10022::22 -virtfs local,id=apps_dev,path=$(MOUNT_PATH),security_model=passthrough,mount_tag=$(MOUNT_TAG) &
################################################################################
# TARGET: start-instance
################################################################################
start-instance-nographic: project cred-setup
qemu-kvm -boot dc -hda $(CURRENT_IMG) -nographic -m 2G -vga std -cpu host -smp 2 -cdrom $(INIT_FILE) -redir tcp:10022::22 -virtfs local,id=apps_dev,path=/apps/infra,security_model=passthrough,mount_tag=apps_infra
################################################################################
# TARGET: vnc
################################################################################
vnc: start-instance
sleep 3
vncviewer :6
################################################################################
# TARGET: shell
################################################################################
shell: start-instance
sleep 3
ssh -i ~/.ssh/id_rsa fedora@localhost -p 10022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment