X-Git-Url: http://git.plutz.net/?a=blobdiff_plain;f=Makefile;h=dc7dd52806e1aeccf2fbaef3484fd6e6d6591691;hb=8a06119f3fc9358f2aec9442af70322bc72edb99;hp=65005eb991388d3fa88fe9faa7e9947bc8e724db;hpb=8bf8f7e95ebb0537eebb01a7a825988b6fbbde99;p=blast diff --git a/Makefile b/Makefile index 65005eb..dc7dd52 100644 --- a/Makefile +++ b/Makefile @@ -1,146 +1,200 @@ - DEFAULT: _target -.PHONY: _target _qemu _partition _format _mount _bootstrap _install _sparse _copy _config _syslinux _umount _partcopy _squash _mbr + +nul_:= +comma_:= , +space_:= ${nul_} ${nul_} + +.PHONY: clean .FORCE _target +.PHONY: _bootloader_raspi _bootloader_pc _partition_gpt _partition_raspi +.PHONY: _squash_modules +.PHONY: _qemu _bootstrap _install _copy _config _squash +.PHONY: _format _mount _sparse _umount _partcopy _mbr ARCH := amd64 KEYRING := keys/debian-archive-stretch-stable.gpg BOOTSTRAP := http://cdn.debian.net/debian +PACKAGES := ${PACKAGES} apt busybox-static debian-archive-keyring init systemd + +# Partition Table (2M) + Boot Partition (126M)+ Root Partition + Slack Space (1M) = 2048M +BOOT_MB := 62 +ROOT_MB := 959 + +config.mk: + cp "config.example" "$@" + +ifndef config + include config.mk +else + include ${config} +endif + +ifndef release + release := stretch +endif -define SOURCES = +define SOURCES := deb http://cdn.debian.net/debian stretch main +deb http://cdn.debian.net/debian buster main +deb http://cdn.debian.net/debian ${release} main endef -PACKAGES := ${PACKAGES} apt btrfs-tools busybox-static bzip2 ca-certificates cron debian-archive-keyring deborphan firmware-linux-free gzip htop ifupdown init iputils-ping irqbalance isc-dhcp-client less make net-tools nmap ntpdate openssh-client psmisc rsync sshfs sudo systemd traceroute unzip vim wget wireless-tools wpasupplicant xz-utils zip ifdef disk -VOLUME := ${disk} -PBOOT := ${disk}1 -PROOT := ${disk}2 -SYSROOT := hdd_mount/ -CFGROOT := hdd_mount/ - -_target: _mbr _umount _config _install _partition - -_partition: -_format: _partition -_mount: _format -_bootstrap: _mount -_install: _bootstrap _mount -_config: _install _mount -_syslinux: _mount -_umount: _mount _config _syslinux -_mbr: _umount _syslinux + VOLUME := ${disk} + PBOOT := ${disk}1 + PROOT := ${disk}2 + SYSROOT := hdd_mount/ + CFGROOT := hdd_mount/ + + _target: _mbr _umount _config _install _partition_gpt + + _partition_gpt: + _format: _partition_gpt + _mount: _format + _bootstrap: _mount + _install: _bootstrap _mount + _config: _install _mount + _bootloader_pc: _mount + _umount: _mount _config _bootloader_pc + _mbr: _umount _bootloader_pc else ifdef image -VOLUME := ${image} -PBOOT := part_boot.img -PROOT := part_root.img -SYSROOT := sys_root/ -CFGROOT := img_mount/ - -_target: _mbr _umount _partcopy _config _install - -_bootstrap: -_install: _bootstrap -_sparse: -_format: _sparse -_mount: _format -_copy: _install _mount -_config: _copy _mount -_syslinux: _mount -_umount: _mount _config _syslinux -_partition: -_partcopy: _partition _umount -_mbr: _partcopy _syslinux + VOLUME := ${image} + PBOOT := part_boot.img + PROOT := part_root.img + SYSROOT := sys_root_${release}/ + CFGROOT := img_mount/ + + _target: _mbr _umount _config _install + + _bootstrap: + _install: _bootstrap + _sparse: + _format: _sparse + _partition_gpt: _sparse + _partcopy: _partition_gpt _format + _img_mount: _partcopy + _copy: _install _img_mount + _config: _copy _img_mount + _bootloader_pc: _img_mount + _umount: _img_mount _config _bootloader_pc + _mbr: _partcopy _bootloader_pc else ifdef squash -SQUASH := ${squash} -SYSROOT := sys_root/ -CFGROOT := cfg_root/ - -_target: _squash _install _config - -_bootstrap: -_install: _bootstrap -_copy: _install -_config: _copy -_squash: _config + SQUASH := ${squash} + SYSROOT := sys_root_${release}/ + CFGROOT := cfg_root/ + + PACKAGES += squashfs-tools nbd-client + + _target: _squash _install _config + + _bootstrap: + _install: _bootstrap + _copy: _install + _config: _copy _squash_modules + _squash: _config else ifdef raspi -VOLUME := ${image} -PBOOT := rpi_boot.img -PROOT := rpi_root.img -SYSROOT := rpi_root/ -CFGROOT := img_mount/ - -ARCH := armhf -KEYRING := keys/raspbian-archive-keyring.gpg -BOOTSTRAP := http://raspbian.raspberrypi.org/raspbian - -define SOURCES = -deb http://raspbian.raspberrypi.org/raspbian stretch main non-free firmware rpi -deb http://archive.raspberrypi.org/debian stretch main -endef -PACKAGES := ${PACKAGES} firmware-brcm8011 libraspberrypi-bin libraspberrypi0 pi-bluetooth raspberrypi-bootloader raspberrypi-kernel - -_target: _umount _partcopy _config _install - -_qemu: -_bootstrap: _qemu -_install: _bootstrap -_sparse: -_format: _sparse -_mount: _format -_copy: _install _mount -_config: _copy _mount -_umount: _mount _config -_partition: -_partcopy: _partition _umount + VOLUME := ${raspi} + PBOOT := rpi_boot.img + PROOT := rpi_root.img + SYSROOT := rpi_root_${release}/ + CFGROOT := img_mount/ + + ARCH := armhf + KEYRING := keys/raspbian-archive-keyring.gpg + BOOTSTRAP := http://raspbian.raspberrypi.org/raspbian + + define SOURCES := + deb http://raspbian.raspberrypi.org/raspbian ${release} main non-free firmware rpi + deb http://archive.raspberrypi.org/debian ${release} main + endef + + PACKAGES += firmware-brcm80211 libraspberrypi-bin libraspberrypi0 pi-bluetooth raspberrypi-bootloader raspberrypi-kernel + + _target: _umount _config + + _qemu: + _bootstrap: _qemu + _install: _bootstrap + _sparse: + _format: _sparse + _partition_raspi: _sparse + _partcopy: _partition_raspi _format + _img_mount: _partcopy + _copy: _install _img_mount + _config: _copy _img_mount + _bootloader_raspi: _img_mount + _umount: _img_mount _config _bootloader_raspi else -_target: + _target: false endif +include modules/default.mk +ifdef modules + include $(addsuffix .mk, $(addprefix modules/, $(subst ${comma_},${space_},${modules}))) +endif + ifeq "${ARCH}" "amd64" -PACKAGES := ${PACKAGES} linux-image-amd64 + PACKAGES += linux-image-amd64 else ifeq "${ARCH}" "i386" -PACKAGES := ${PACKAGES} linux-image-686 + PACKAGES += linux-image-686 endif export SOURCES SYSROOT CFGROOT VOLUME PBOOT PROOT SQUASH ARCH KEYRING BOOTSTRAP -${SYSROOT}: - btrfs subvol create "$$SYSROOT" || mkdir -p "$$SYSROOT}" +sys_root_${release}/: + btrfs subvol create "$@" || mkdir "$@" + chmod 755 "$@" + +cfg_root/: sys_root_${release}/ _install + -btrfs sub del "$@" || rm -r "$@" + btrfs sub snap "$<" "$@" || mkdir "$@" + chmod 755 "$@" + +rpi_root_${release}/: + btrfs subvol create "$@" || mkdir "$@" + chmod 755 "$@" + +img_mount/: + mkdir -p -m 755 "$@" chmod 755 "$@" - + +hdd_mount/: + mkdir -p -m 755 "$@" + _qemu: ${SYSROOT}/usr/bin/qemu-arm-static ${SYSROOT}/usr/bin/qemu-arm-static: /usr/bin/qemu-arm-static ${SYSROOT} - mkdir -p "$${SYSROOT}"/usr/bin - chmod 755 "$${SYSROOT}/usr" "$${SYSROOT}/usr/bin" + mkdir -p -m 755 "$${SYSROOT}"/usr/bin + chmod 755 "$${SYSROOT}"/usr cp -p "$<" "$@" _bootstrap: ${SYSROOT}/bin/sh ${SYSROOT}/bin/sh: | ${SYSROOT} debootstrap --variant=minbase --arch="$${ARCH}" \ --keyring="$${KEYRING}" \ - stretch "$${SYSROOT}" "$${BOOTSTRAP}" + "${release}" "$${SYSROOT}" "$${BOOTSTRAP}" + printf 'APT::Default-Release "%s";' "${release}" >"$${SYSROOT}/etc/apt/apt.conf.d/10release" + .PHONY: _aptkeys _norecommends _aptkeys: ${SYSROOT}/etc/apt/trusted.gpg.d/ _norecommends: ${SYSROOT}/etc/apt/apt.conf.d/10norecommends ${SYSROOT}/etc/apt/apt.conf.d/10norecommends: ${SYSROOT} - mkdir -p "$(dir $@)" + mkdir -p -m 755 "$(dir $@)" printf 'APT::Install-Recommends "false";\n' >"$@" chmod 644 "$@" ${SYSROOT}/etc/apt/trusted.gpg.d/: ${SYSROOT} - mkdir -p "$@" + mkdir -p -m 755 "$@" cp keys/*.gpg "$@" _install: ${SYSROOT} _aptkeys _norecommends - printf %s "$${SOURCES}" >"$${SYSROOT}/etc/apt/sources.list" + printf '%s\n' "$${SOURCES}" |sort -u >"$${SYSROOT}/etc/apt/sources.list" -cp /etc/resolv.conf "$${SYSROOT}/etc/" -chroot "$${SYSROOT}" sh -c 'apt-mark showmanual |xargs apt-mark auto' -chroot "$${SYSROOT}" apt-get update @@ -151,33 +205,154 @@ _install: ${SYSROOT} _aptkeys _norecommends chroot "$${SYSROOT}" apt-get clean chroot "$${SYSROOT}" rm /usr/local/sbin/invoke-rc.d -_copy: ${SYSROOT} - btrfs sub snap "$${SYSROOT}" "$${CFGROOT}" || \ - cp -a "$${SYSROOT}/." "$${CFGROOT}/" +_copy: ${SYSROOT} ${CFGROOT} + cp -au "$${SYSROOT}/." "$${CFGROOT}/" -.PHONY: _main_config -_config: _main_config -_main_config: ${CFGROOT} cfg_seed/ - cp -a cfg_seed/. "$${CFGROOT}" +_config: ${CFGROOT} + ln -sf /dev/null "$${CFGROOT}/etc/systemd/network/99-default.link" + printf '\nsession optional pam_mkhomedir.so umask=0027\n' >>"$${CFGROOT}/etc/pam.d/common-session" + printf 'LABEL=FSROOT / ext4 auto,nofail,rw 0 0\nLABEL=BOOT /boot vfat auto,nofail,ro 0 0\n' \ + >"$${CFGROOT}/etc/fstab" + chroot "$${CFGROOT}" update-initramfs -u -${SQUASH}: ${CFGROOT} - -rm "$@" - mksquashfs "$$CFGROOT" "$$SQUASH" -comp xz - -_squash: ${SQUASH} +${SQUASH}: ${CFGROOT} .FORCE + -rm "$${SQUASH}" + mksquashfs "$${CFGROOT}" "$${SQUASH}" -comp xz + +_squash_modules: ${CFGROOT} + printf '%s\n' squashfs nbd >>"${CFGROOT}/etc/initramfs-tools/modules" + +_squash: ${SQUASH} _squash_modules + +${VOLUME}: .FORCE + ! printf '%s\n' "$${VOLUME}" |grep -q '^/dev/' # abort if volume should be real device + dd count=0 of="$${VOLUME}" # truncate + dd bs=1M count=0 seek=$$((3 + ${BOOT_MB} + ${ROOT_MB})) of="$${VOLUME}" + chmod g+w "$${VOLUME}" + +${PBOOT}: .FORCE + ! printf '%s\n' "$${PBOOT}" |grep -q '^/dev/' # abort if volume should be real device + dd count=0 of="$${PBOOT}" # truncate + dd bs=1M count=0 seek=${BOOT_MB} of="$${PBOOT}" + +${PROOT}: .FORCE + ! printf '%s\n' "$${PROOT}" |grep -q '^/dev/' # abort if volume should be real device + dd count=0 of="$${PROOT}" # truncate + dd bs=1M count=0 seek=${ROOT_MB} of="$${PROOT}" + +_partition_raspi: ${VOLUME} + printf 'label: dos\n\n: start=2MiB, size=%iMiB, type=c\n: start=%iMiB, size=%iMiB, type=83\n' \ + ${BOOT_MB} "$$((${BOOT_MB} + 2))" ${ROOT_MB} | sfdisk "$${VOLUME}" + +_partition_gpt: ${VOLUME} + printf 'label: gpt\n\n: start=2MiB, size=%iMiB, type=1, attrs="LegacyBIOSBootable"\n: size=%iMiB, type=20\n' \ + ${BOOT_MB} ${ROOT_MB} | sfdisk "$${VOLUME}" + +_sparse: ${PBOOT} ${PROOT} + +_format: ${PBOOT} ${PROOT} + -umount "$${CFGROOT}/boot" "$${CFGROOT}" + mkfs.fat -F 32 -n BOOT "$${PBOOT}" + mkfs.ext4 -F -L FSROOT "$${PROOT}" + +.PHONY: _syslinux_efi32 _syslinux_efi64 _syslinux_bios +.PHONY: _grub_efi32 _grub_efi64 + +_syslinux_efi32: ${CFGROOT} + mkdir -p "$${CFGROOT}/boot/EFI/BOOT" + cp /usr/lib/SYSLINUX.EFI/efi32/syslinux.efi "$${CFGROOT}/boot/EFI/BOOT/BOOTIA32.EFI" + mkdir -p "$${CFGROOT}/boot/efi32" + cp $(addprefix /usr/lib/syslinux/modules/efi32/, ldlinux.e32 menu.c32 libutil.c32) "$${CFGROOT}"/boot/efi32/ + cp /usr/lib/syslinux/modules/efi32/ldlinux.e32 "$${CFGROOT}/boot/EFI/BOOT/" # workaround + printf 'PATH /efi32\nINCLUDE /bootmenu.cfg\nMENU TITLE Syslinux EFI-32\n' >"$${CFGROOT}/boot/syslia32.cfg" + +_syslinux_efi64: ${CFGROOT} + mkdir -p "$${CFGROOT}/boot/EFI/BOOT" + cp /usr/lib/SYSLINUX.EFI/efi64/syslinux.efi "$${CFGROOT}/boot/EFI/BOOT/BOOTX64.EFI" + mkdir -p "$${CFGROOT}/boot/efi64" + cp $(addprefix /usr/lib/syslinux/modules/efi64/, ldlinux.e64 menu.c32 libutil.c32) "$${CFGROOT}"/boot/efi64/ + cp /usr/lib/syslinux/modules/efi64/ldlinux.e64 "$${CFGROOT}/boot/EFI/BOOT/" # workaround + printf 'PATH /efi64\nINCLUDE /bootmenu.cfg\nMENU TITLE Syslinux EFI-64\n' >"$${CFGROOT}/boot/syslx64.cfg" + +_syslinux_bios: ${CFGROOT} + mkdir -p "$${CFGROOT}/boot/bios" + cp $(addprefix /usr/lib/syslinux/modules/bios/, menu.c32 libutil.c32) "$${CFGROOT}"/boot/bios/ + printf 'PATH bios\nINCLUDE bootmenu.cfg\nMENU TITLE Syslinux BIOS\n' >"$${CFGROOT}/boot/syslinux.cfg" + +_grub_efi32: ${CFGROOT} + mkdir -p "$${CFGROOT}/boot/EFI/BOOT" + grub-mkimage -p / -O i386-efi fat part_gpt normal linux all_video >"$${CFGROOT}/boot/EFI/BOOT/BOOTIA32.EFI" + +_grub_efi64: ${CFGROOT} + mkdir -p "$${CFGROOT}/boot/EFI/BOOT" + grub-mkimage -p / -O x86_64-efi fat part_gpt normal linux all_video >"$${CFGROOT}/boot/EFI/BOOT/BOOTX64.EFI" + +${CFGROOT}/boot/cmdline.txt: ${CFGROOT} + printf 'root=/dev/mmcblk0p2 rootfstype=ext4 fsck.repair=yes rootwait' >"$@" + +_bootloader_raspi: ${CFGROOT} ${CFGROOT}/boot/cmdline.txt + +_bootloader_pc: ${CFGROOT} _grub_efi32 _grub_efi64 _syslinux_bios #_syslinux_efi32 _syslinux_efi64 + printf 'UI menu.c32\nDEFAULT stretch\nTIMEOUT 30\n' >"$${CFGROOT}/boot/bootmenu.cfg" + printf 'set timeout=3\ndefault=stretch\n\n' >"$${CFGROOT}/boot/grub.cfg" + k="$$(printf ' %s' "$${CFGROOT}/boot/"vmlinuz-*)"; k="$${k##*/}"; \ + i="$$(printf ' %s' "$${CFGROOT}/boot/"initrd.img-*)"; i="$${i##*/}"; \ + printf '\nLABEL stretch\n MENU LABEL Debian Stretch\n KERNEL /%s\n INITRD /%s\n APPEND root=LABEL=FSROOT\n' \ + "$$k" "$$i" >>"$${CFGROOT}/boot/bootmenu.cfg"; \ + printf '\nLABEL rescue\n MENU LABEL Rescue\n KERNEL /%s\n INITRD /%s\n APPEND root=LABEL=FSROOT init=/bin/sh\n' \ + "$$k" "$$i" >>"$${CFGROOT}/boot/bootmenu.cfg"; \ + printf "menuentry 'Debian Stretch' --id 'stretch' {\n linux '/%s' root=LABEL=FSROOT\n initrd '/%s'\n}\n" \ + "$$k" "$$i" >>"$${CFGROOT}/boot/grub.cfg"; \ + printf "menuentry 'Rescue' --id 'rescue' {\n linux '/%s' root=LABEL=FSROOT init=/bin/sh\n initrd '/%s'\n}\n" \ + "$$k" "$$i" >>"$${CFGROOT}/boot/grub.cfg" + +.PHONY: _syslinux_mbr _grub_mbr + +_syslinux_mbr: ${VOLUME} + syslinux -t 2097152 -i "$${VOLUME}" + dd conv=notrunc if=/usr/lib/syslinux/mbr/gptmbr.bin of="$${VOLUME}" + +_grub_mbr: ${VOLUME} + # grub-mkimage -p / -O i386-pc fat part_gpt normal linux all_video |dd conv=notrunc of="$${VOLUME}" + +_mbr: _syslinux_mbr # _grub_mbr + +_partcopy: ${VOLUME} ${PROOT} ${PBOOT} + dd conv=notrunc,sparse bs=1M seek=2 if="$${PBOOT}" of="$${VOLUME}" + dd conv=notrunc,sparse bs=1M seek=$$((2 + ${BOOT_MB})) if="$${PROOT}" of="$${VOLUME}" + +_mount: ${PBOOT} ${PROOT} ${CFG_ROOT} + mount -t ext4 "$${PROOT}" "$${CFGROOT}" + chmod 755 "$${CFGROOT}" + mkdir -p -m 755 "$${CFGROOT}/boot" + mount -t vfat "$${PBOOT}" "$${CFGROOT}/boot" + +_img_mount: + mount -o loop,offset="$$((${BOOT_MB} + 2))"M,sizelimit=${ROOT_MB}M \ + -t ext4 "$${VOLUME}" "$${CFGROOT}" + chmod 755 "$${CFGROOT}" + mkdir -p -m 755 "$${CFGROOT}/boot" + mount -o loop,offset=2M,sizelimit=${BOOT_MB}M \ + -t vfat "$${VOLUME}" "$${CFGROOT}/boot" + +_umount: ${CFGROOT} + -umount "$${CFGROOT}/boot" + umount "$${CFGROOT}" && rmdir "$${CFGROOT}" clean: [ "$${SYSROOT#/}" ] && [ "$${CFGROOT#/}" ] || false # (abort receipe) - -mount |grep -qwF "$${SYSROOT}/boot" && umount "$${SYSROOT}/boot" - -mount |grep -qwF "$${SYSROOT}/proc" && umount "$${SYSROOT}/proc" - -mount |grep -qwF "$${SYSROOT}/sys" && umount "$${SYSROOT}/sys" - -mount |grep -qwF "$${SYSROOT}/dev" && umount "$${SYSROOT}/dev" - -mount |grep -qwF "$${SYSROOT}" && umount "$${SYSROOT}" - -mount |grep -qwF "$${CFGROOT}" && umount "$${CFGROOT}" - if [ -d "$${SYSROOT}" ] && ! mount |grep -qwFe "$${SYSROOT}"; then \ - btrfs sub del "$${SYSROOT}" || echo rm -r "$${SYSROOT}"; \ - fi - if [ -d "$${CFGROOT}" ] && ! mount |grep -qwFe "$${CFGROOT}"; then \ - btrfs sub del "$${CFGROOT}" || echo rm -r "$${CFGROOT}"; \ - fi - -rm "$${SQUASH}" + -umount img_mount/boot + -umount img_mount + -rmdir img_mount + -umount hdd_mount/boot + -umount hdd_mount + -rmdir hdd_mount + if [ -d "sys_root_${release}/" ]; then btrfs sub del "sys_root_${release}/" || echo rm -r "sys_root_${release}/"; fi + if [ -d "cfg_root/" ]; then btrfs sub del "cfg_root/" || echo rm -r "cfg_root/"; fi + if [ -d "rpi_root_${release}/" ]; then btrfs sub del "rpi_root_${release}/" || echo rm -r "rpi_root_${release}/"; fi + -rm part_boot.img + -rm part_root.img + -rm rpi_boot.img + -rm rpi_root.img + -[ -f "$${SQUASH}" ] && rm "$${SQUASH}" + -[ -f "$${VOLUME}" ] && rm "$${VOLUME}"