Created
March 2, 2019 20:47
-
-
Save svanellewee/6b78f9a62aa314c94971e2cbe9be6910 to your computer and use it in GitHub Desktop.
linux image tools
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2274631 source image-tools | |
2274661 make-blank-image virtualdisk.img 1000 | |
2274662 make-paritions virtualdisk.img | |
2274663 format-partitions virtualdisk.img | |
2274666 install-to-root virtualdisk.img ~/morty/arch/x86_64/boot/bzImage | |
# the next copies busybox/_install to /tmp/busybox/ and add init. | |
2274633 build-initramfs ~/busybox ~/morty # custom-initramfs.cpio.gz | |
XXXXXXX install-to-root virtualdisk.img custom-initramfs.cpio.gz | |
XXXXXXX cat <<EOF > startup.nsh | |
XXXXXXX \EFI\BOOT\BOOTX64.EFI | |
XXXXXXX EOF | |
2274667 setup-grub /boot/bzImage /boot/custom-initramfs.cpio.gz # creates bootx64.efi | |
2274669 install-to-boot virtualdisk.img bootx64.efi | |
2274668 install-to-boot virtualdisk.img startup.nsh | |
2274674 VBoxManage convertdd virtualdisk.{img,vdi} --format VDI | |
2274675 VirtualBox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
function info() { | |
local GREEN="\e[32m" | |
echo -e "\e[32m[INFO]\e[0m $*" >&2 | |
} | |
function err() { | |
echo -e "\e[31m[ERROR]\e[0m $*" >&2 | |
} | |
function make-blank-image() { | |
local OUTPUT_FILE="${1}" | |
local SIZE="${2}" | |
info "making a image file "${OUTPUT_FILE}" of ${SIZE}Mbytes" | |
dd if=/dev/zero \ | |
of="${OUTPUT_FILE}" \ | |
bs=1M count="${SIZE}" | |
} | |
function make-paritions() { | |
local INPUT_FILE="${1}" | |
local SWAP_SIZE=512 | |
local GPT_START="0%" | |
local GPT_END=257 | |
local SWAP_START=$((${GPT_END} + 1)) | |
local SWAP_END=$((${SWAP_START} + ${SWAP_SIZE})) | |
local MAIN_START=$((${SWAP_END} + 1)) | |
local MAIN_END_KBYTES=$(du ${INTPUT_FILE} | awk '{ print $1 }') | |
local MAIN_END=$(bc <<<"${MAIN_END_KBYTES} / 1024") | |
info "Making GPT from ${GPT_START} ${GPT_END}" | |
parted --script "${INPUT_FILE}" mklabel gpt mkpart p fat32 ${GPT_START} ${GPT_END} set 1 boot on | |
info "Making swap from ${SWAP_START} ${SWAP_END}" | |
parted --script "${INPUT_FILE}" mkpart p linux-swap ${SWAP_START} ${SWAP_END} | |
info "Making main partition from ${MAIN_START} ${MAIN_END}" | |
parted --script "${INPUT_FILE}" mkpart p ext4 ${MAIN_START} ${MAIN_END} | |
} | |
function format-partitions() { | |
local INPUT_FILE="${1}" | |
sudo kpartx -av "${INPUT_FILE}" | |
sleep 1 | |
sudo mkfs.vfat /dev/mapper/loop0p1 | |
sleep 1 | |
sudo mkfs.ext4 /dev/mapper/loop0p3 | |
sleep 1 | |
sudo kpartx -d "${INPUT_FILE}" | |
} | |
function install-to-root() { | |
local INPUT_FILE="${1}" | |
local KERNEL_IMAGE_LOCATION="${2}" | |
mkdir -p /tmp/root | |
sudo kpartx -av "${INPUT_FILE}" | |
sleep 1 | |
sudo mount /dev/mapper/loop0p3 /tmp/root | |
sudo mkdir /tmp/root/boot | |
sudo cp ${KERNEL_IMAGE_LOCATION} /tmp/root/boot | |
sudo umount /tmp/root | |
sudo kpartx -d "${INPUT_FILE}" | |
} | |
function install-to-boot() { | |
set -x | |
local INPUT_FILE="${1}" | |
local BOOT_IMAGE_LOCATION="${2}" | |
mkdir -p /tmp/boot | |
sudo kpartx -av "${INPUT_FILE}" | |
sleep 1 | |
sudo mount /dev/mapper/loop0p1 /tmp/boot | |
sudo mkdir -p /tmp/boot/EFI/boot/ | |
sudo cp ${BOOT_IMAGE_LOCATION} /tmp/boot/EFI/boot/ | |
sudo umount /tmp/boot | |
sudo kpartx -d "${INPUT_FILE}" | |
set +x | |
} | |
function setup-grub(){ | |
local KERNEL_PATH="${1}" | |
local INITRAMFS_PATH="${2}" | |
cat <<- EOF > /tmp/grub.cfg | |
insmod part_gpt | |
insmod part_msdos | |
insmod fat | |
insmod efi_gop | |
insmod efi_uga | |
insmod video_bochs | |
insmod video_cirrus | |
menuentry "Start The Magic.." { | |
set root=(hd1,gpt3) | |
linux ${KERNEL_PATH} | |
initrd ${INITRAMFS_PATH} | |
} | |
EOF | |
grub-mkstandalone -d /usr/lib/grub/x86_64-efi \ | |
-O x86_64-efi \ | |
--modules="part_gpt part_msdos" \ | |
--fonts="unicode" \ | |
-o "./bootx64.efi" "boot/grub/grub.cfg=/tmp/grub.cfg" -v | |
} | |
function build-initramfs() { | |
local BUSYBOX_DIR=${1} | |
cp -ar "${BUSYBOX_DIR}"/_install /tmp/busybox | |
cat <<- "EOF" > /tmp/busybox/init | |
#!/bin/busybox sh | |
echo "Starting kernel $(uname -r)" | |
exec /bin/sh | |
EOF | |
local KERNEL_DIR="${2}" | |
( | |
cat <<- EOF | |
# A simple initramfs | |
dir /dev 0755 0 0 | |
nod /dev/console 0600 0 0 c 5 1 | |
dir /root 0700 0 0 | |
dir /sbin 0755 0 0 | |
dir /bin 0755 0 0 | |
file /bin/busybox /tmp/busybox/bin/busybox 755 0 0 | |
EOF | |
for i in $(find /tmp/busybox/sbin/ -type l ) | |
do | |
cur_link="${i##/tmp/busybox/sbin/}" | |
echo "slink /sbin/${cur_link} busybox 777 0 0" | |
done | |
for i in $(find /tmp/busybox/bin/* -type l ) | |
do | |
cur_link="${i##/tmp/busybox/bin/}" | |
echo "slink /bin/${cur_link} busybox 777 0 0" | |
done | |
echo "file /init /tmp/busybox/init 755 0 0" | |
) > /tmp/initramfs-list | |
${KERNEL_DIR}/usr/gen_init_cpio /tmp/initramfs-list > custom-initramfs.cpio.gz | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment