90-build-vm.mk 3.37 KB
Newer Older
1 2
# step 4: build the virtual machine image

3 4
IMAGE_PACKAGES = $(DOT_BASE) \
		 $(SYSTEM_PACKAGES) \
5 6 7
		 $(COMMON_PACKAGES) \
		 $(BASE_PACKAGES) \
		 $(THE_PACKAGES) \
8
		 $(call list,$(BASE_LISTS) $(THE_LISTS) $(COMMON_LISTS))
9

10
IMAGE_PACKAGES_REGEXP = $(THE_PACKAGES_REGEXP) \
11
                        $(BASE_PACKAGES_REGEXP)
12

13
ifneq (,$(EFI_BOOTLOADER))
14 15 16 17 18
VM_BOOTLOADER=$(EFI_BOOTLOADER)
else
VM_BOOTLOADER=$(BASE_BOOTLOADER)
endif

19 20
# intermediate chroot archive
VM_TARBALL := $(IMAGE_OUTDIR)/$(IMAGE_NAME).tar
21
VM_OUT_TARBALL := $(IMAGE_OUTDIR)/$(IMAGE_OUTNAME).tar
22
VM_RAWDISK := $(IMAGE_OUTDIR)/$(IMAGE_NAME).raw
23 24
VM_FSTYPE ?= ext4
VM_SIZE ?= 0
25

26 27
VM_GZIP_COMMAND ?= gzip -f
VM_XZ_COMMAND ?= xz -T0 -f
28

29 30 31
# tavolga
RECOVERY_LINE ?= Press ENTER to start

32
# tarball save
33
ifneq (,$(VM_SAVE_TARBALL))
34
ifeq (,$(filter-out img img.xz qcow2 qcow2c vdi vmdk vhd,$(IMAGE_TYPE)))
35 36
ifeq (,$(filter-out tar tar.gz tar.xz,$(VM_SAVE_TARBALL)))
SAVE_TARBALL := convert-image/$(VM_SAVE_TARBALL)
37 38 39 40
endif
endif
endif

41 42 43
check-sudo:
	@if ! type -t sudo >&/dev/null; then \
		echo "** error: sudo not available, see doc/vm.txt" >&2; \
44 45 46 47 48
	fi

check-qemu:
	@if ! type -t qemu-img >&/dev/null; then \
		echo "** error: qemu-img not available" >&2; \
49 50 51
		exit 1; \
	fi

52
tar2fs: $(SAVE_TARBALL) check-sudo prepare-tarball-qemu
53
	@if [ -x /usr/share/mkimage-profiles/bin/tar2fs ]; then \
54
		TOPDIR=/usr/share/mkimage-profiles; \
55 56
	else \
		[ -z "$(MKIMAGE_PROFILES)" ] || TOPDIR=$(MKIMAGE_PROFILES); \
57
	fi; \
58
	if ! sudo $$TOPDIR/bin/tar2fs \
59
		"$(VM_TARBALL)" "$(VM_RAWDISK)" "$(VM_SIZE)" "$(VM_FSTYPE)" \
60
			"$(VM_BOOTLOADER)" "$(ARCH)" "$(VM_PARTTABLE)" \
61
			"$(VM_BOOTTYPE)" "$(VM_BOOTSIZE)"; then \
62
		echo "** error: sudo tar2fs failed, see build log" >&2; \
63 64 65
		exit 1; \
	fi

66 67
# copy $(BUILDDIR)/.work/chroot/.host/qemu* into chroot if qemu is used
prepare-tarball-qemu:
68 69 70 71
	@(cd "$(BUILDDIR)/.work/chroot/"; \
		tar -rf "$(VM_TARBALL)" ./.host/qemu*) ||:

convert-image/tar:
72
ifneq (,$(SAVE_TARBALL))
73 74 75 76
	cp "$(VM_TARBALL)" "$(VM_OUT_TARBALL)"
else
	mv "$(VM_TARBALL)" "$(VM_OUT_TARBALL)"
endif
77

78 79
convert-image/tar.gz: convert-image/tar
	$(VM_GZIP_COMMAND) "$(VM_OUT_TARBALL)"
80

81 82
convert-image/tar.xz: convert-image/tar
	$(VM_XZ_COMMAND) "$(VM_OUT_TARBALL)"
83 84 85 86

convert-image/img: tar2fs
	mv "$(VM_RAWDISK)" "$(IMAGE_OUTPATH)"

87 88 89
convert-image/img.xz: tar2fs
	$(VM_XZ_COMMAND) < "$(VM_RAWDISK)" > "$(IMAGE_OUTPATH)"

90 91
convert-image/qcow2 convert-image/qcow2c convert-image/vmdk \
	convert-image/vdi convert-image/vhd: check-qemu tar2fs
92 93
	@VM_COMPRESS=; \
	case "$(IMAGE_TYPE)" in \
94
	"vhd") VM_FORMAT="vpc";; \
95
	"qcow2c") VM_FORMAT="qcow2"; VM_COMPRESS="-c";; \
96 97
	*) VM_FORMAT="$(IMAGE_TYPE)"; \
	esac; \
98 99 100
	qemu-img convert $$VM_COMPRESS -O "$$VM_FORMAT" \
		"$(VM_RAWDISK)" "$(IMAGE_OUTPATH)"

101 102 103 104 105 106 107 108 109 110
# for tavolga
convert-image/recovery.tar:
	build-recovery-tar \
	    --image-name $(IMAGE_NAME) \
	    --date $(DATE) \
	    --compress-command '$(VM_GZIP_COMMAND)' \
	    --rootfs "$(VM_TARBALL)" \
	    --output "$(IMAGE_OUTPATH)" \
	    --line '$(RECOVERY_LINE)'

111 112 113 114
post-convert:
	@rm -f "$(VM_RAWDISK)"; \
	if [ "0$(DEBUG)" -le 1 ]; then rm -f "$(VM_TARBALL)"; fi

115
convert-image: convert-image/$(IMAGE_TYPE) post-convert; @:
116

117
run-image-scripts: GLOBAL_CLEANUP_PACKAGES := $(CLEANUP_PACKAGES)
118

119
# override
120 121
pack-image: MKI_PACK_RESULTS := tar:$(VM_TARBALL)

122
all: $(GLOBAL_DEBUG) \
123 124
	build-image copy-subdirs copy-tree run-image-patches run-image-scripts \
	pack-image convert-image postprocess $(GLOBAL_CLEAN_WORKDIR)