Skip to content

Commit

Permalink
调整脚本,使得能够创建riscv的磁盘镜像,并引导进入riscv下的grub
Browse files Browse the repository at this point in the history
```shell
export ARCH=riscv64
make write_diskimage
make qemu
```

即可在serial_opt.txt看到进入grub的提示信息
  • Loading branch information
fslongjin committed Nov 20, 2023
1 parent 83f8829 commit fdb832d
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 36 deletions.
14 changes: 10 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ ifeq ($(OS),Darwin) # Assume Mac OS X
NPROCS:=$(shell system_profiler | awk '/Number Of CPUs/{print $4}{next;}')
endif

export ARCH=__x86_64__
# if arch not defined, set it to x86_64
export ARCH?=x86_64

CFLAGS_DEFINE_ARCH="__$(ARCH)__"


export ROOT_PATH=$(shell pwd)

export DEBUG=DEBUG
export GLOBAL_CFLAGS := -mcmodel=large -fno-builtin -m64 -fno-stack-protector -D $(ARCH) -D $(EMULATOR) -O1
export GLOBAL_CFLAGS := -mcmodel=large -fno-builtin -m64 -fno-stack-protector -D $(CFLAGS_DEFINE_ARCH) -D $(EMULATOR) -O1

ifeq ($(DEBUG), DEBUG)
GLOBAL_CFLAGS += -g
Expand Down Expand Up @@ -92,11 +97,12 @@ gdb:

# 写入磁盘镜像
write_diskimage:
bash -c "cd tools && bash grub_auto_install.sh && sudo bash $(ROOT_PATH)/tools/write_disk_image.sh --bios=legacy && cd .."
@echo "write_diskimage arch=$(ARCH)"
bash -c "export ARCH=$(ARCH); cd tools && bash grub_auto_install.sh && sudo ARCH=$(ARCH) bash $(ROOT_PATH)/tools/write_disk_image.sh --bios=legacy && cd .."

# 写入磁盘镜像(uefi)
write_diskimage-uefi:
bash -c "cd tools && bash grub_auto_install.sh && sudo bash $(ROOT_PATH)/tools/write_disk_image.sh --bios=uefi && cd .."
bash -c "export ARCH=$(ARCH); cd tools && bash grub_auto_install.sh && sudo ARCH=$(ARCH)bash $(ROOT_PATH)/tools/write_disk_image.sh --bios=uefi && cd .."
# 不编译,直接启动QEMU
qemu:
sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=window && cd .."
Expand Down
1 change: 1 addition & 0 deletions tools/arch/riscv64/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/u-boot*
26 changes: 17 additions & 9 deletions tools/create_hdd_image.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
########################################################################
# 这是一个用于创建磁盘镜像的脚本
# 用法:./create_hdd_image.sh -P MBR/GPT
# 要创建一个MBR分区表的磁盘镜像,请这样运行它: bash create_hdd_image.sh -P MBR
# 要创建一个GPT分区表的磁盘镜像,请这样运行它: bash create_hdd_image.sh -P GPT
# 要创建一个MBR分区表的磁盘镜像,请这样运行它: ARCH=x86_64 bash create_hdd_image.sh -P MBR
# 要创建一个GPT分区表的磁盘镜像,请这样运行它: ARCH=x86_64 bash create_hdd_image.sh -P GPT
# 请注意,这个脚本需要root权限
# 请注意,运行这个脚本之前,需要在您的计算机上安装qemu-img和fdisk,以及parted
#
# 这个脚本会在当前目录下创建一个名为disk.img的文件,这个文件就是磁盘镜像,
# 这个脚本会在当前目录下创建一个名为disk-${ARCH}.img的文件,这个文件就是磁盘镜像,
# 在完成后,会将这个文件移动到bin目录下
########################################################################

echo "create_hdd_image.sh: Creating virtual disk image... arch=${ARCH}"

# 给变量赋默认值
export ARCH=${ARCH:=x86_64}

DISK_NAME=disk-${ARCH}.img

format_as_mbr() {
echo "Formatting as MBR..."
# 使用fdisk把disk.img的分区表设置为MBR格式(下方的空行请勿删除)
fdisk disk.img << EOF
fdisk ${DISK_NAME} << EOF
o
n
Expand All @@ -28,7 +35,7 @@ EOF

format_as_gpt() {
echo "Formatting as GPT..."
sudo parted disk.img << EOF
sudo parted ${DISK_NAME} << EOF
mklabel gpt
y
mkpart
Expand All @@ -49,7 +56,7 @@ EOF
echo "Creating virtual disk image..."
ARGS=`getopt -o P: -- "$@"`
# 创建一至少为256MB磁盘镜像(类型选择raw)
qemu-img create -f raw disk.img 2048M
qemu-img create -f raw ${DISK_NAME} 2048M
#将规范化后的命令行参数分配至位置参数($1,$2,...)
eval set -- "${ARGS}"
#echo formatted parameters=[$@]
Expand Down Expand Up @@ -77,13 +84,14 @@ case "$1" in
;;
esac

LOOP_DEVICE=$(sudo losetup -f --show -P disk.img) \

LOOP_DEVICE=$(sudo losetup -f --show -P ${DISK_NAME}) \
|| exit 1
echo ${LOOP_DEVICE}p1
sudo mkfs.vfat -F 32 ${LOOP_DEVICE}p1
sudo losetup -d ${LOOP_DEVICE}

echo "Successfully created disk image."
mkdir -p ../bin
chmod 777 disk.img
mv ./disk.img ../bin/
chmod 777 ${DISK_NAME}
mv ./${DISK_NAME} ../bin/
14 changes: 13 additions & 1 deletion tools/mount_virt_disk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@ if [ ! $uid == "0" ];then
exit
fi

LOOP_DEVICE=$(losetup -f --show -P ../bin/disk.img) \
# 检查是否设置ARCH环境变量

if [ ! ${ARCH} ];then
echo "请设置ARCH环境变量"
exit
fi


DISK_NAME=disk-${ARCH}.img

echo "Mounting virtual disk image '${DISK_NAME}'..."

LOOP_DEVICE=$(losetup -f --show -P ../bin/${DISK_NAME}) \
|| exit 1

echo ${LOOP_DEVICE}p1
Expand Down
83 changes: 71 additions & 12 deletions tools/run-qemu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,44 @@ eval set -- "${ARGS}"
echo "$@"
allflags=
# allflags=$(qemu-system-x86_64 -cpu help | awk '/flags/ {y=1; getline}; y {print}' | tr ' ' '\n' | grep -Ev "^$" | sed -r 's|^|+|' | tr '\n' ',' | sed -r "s|,$||")
ARCH="x86_64"
# 设置ARCH环境变量,如果没有设置,就默认为x86_64
export ARCH=${ARCH:=x86_64}
echo "ARCH=${ARCH}"
#ARCH="i386"
# 请根据自己的需要,在-d 后方加入所需的 trace 事件

# 标准的trace events
qemu_trace_std=cpu_reset,guest_errors,trace:virtio*,trace:e1000e_rx*,trace:e1000e_tx*,trace:e1000e_irq*
# 调试usb的trace
qemu_trace_usb=trace:usb_xhci_reset,trace:usb_xhci_run,trace:usb_xhci_stop,trace:usb_xhci_irq_msi,trace:usb_xhci_irq_msix,trace:usb_xhci_port_reset,trace:msix_write_config,trace:usb_xhci_irq_msix,trace:usb_xhci_irq_msix_use,trace:usb_xhci_irq_msix_unuse,trace:usb_xhci_irq_msi,trace:usb_xhci_*
qemu_accel="kvm"
if [ $(uname) == Darwin ]; then
qemu_accel=hvf

# 根据架构设置qemu的加速方式
if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
qemu_accel="kvm"
if [ $(uname) == Darwin ]; then
qemu_accel=hvf
fi
fi

QEMU=qemu-system-x86_64
QEMU_DISK_IMAGE="../bin/disk.img"
# uboot版本
UBOOT_VERSION="v2023.10"
RISCV64_UBOOT_PATH="arch/riscv64/u-boot-${UBOOT_VERSION}-riscv64"


DISK_NAME="disk-${ARCH}.img"

QEMU=qemu-system-${ARCH}
QEMU_DISK_IMAGE="../bin/${DISK_NAME}"
QEMU_MEMORY="512M"
QEMU_MEMORY_BACKEND="dragonos-qemu-shm.ram"
QEMU_MEMORY_BACKEND_PATH_PREFIX="/dev/shm"
QEMU_SHM_OBJECT="-object memory-backend-file,size=${QEMU_MEMORY},id=${QEMU_MEMORY_BACKEND},mem-path=${QEMU_MEMORY_BACKEND_PATH_PREFIX}/${QEMU_MEMORY_BACKEND},share=on "
QEMU_SMP="2,cores=2,threads=1,sockets=1"
QEMU_MONITOR="stdio"
QEMU_TRACE="${qemu_trace_std}"
QEMU_CPU_FEATURES="IvyBridge,apic,x2apic,+fpu,check,+vmx,${allflags}"
QEMU_RTC_CLOCK="clock=host,base=localtime"
QEMU_SERIAL="file:../serial_opt.txt"
QEMU_CPU_FEATURES=""
QEMU_RTC_CLOCK=""
QEMU_SERIAL="-serial file:../serial_opt.txt"
QEMU_DRIVE="id=disk,file=${QEMU_DISK_IMAGE},if=none"
QEMU_ACCELARATE=""

Expand All @@ -68,7 +81,15 @@ if [ -n "${qemu_accel}" ]; then
QEMU_ACCELARATE="-machine accel=${qemu_accel} -enable-kvm "
fi

QEMU_MACHINE=" -machine q35,memory-backend=${QEMU_MEMORY_BACKEND} "
if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
QEMU_MACHINE=" -machine q35,memory-backend=${QEMU_MEMORY_BACKEND} "
QEMU_CPU_FEATURES+="-cpu IvyBridge,apic,x2apic,+fpu,check,+vmx,${allflags}"
QEMU_RTC_CLOCK+=" -rtc clock=host,base=localtime"
else
QEMU_MACHINE=" -machine virt,memory-backend=${QEMU_MEMORY_BACKEND} "

fi


# ps: 下面这条使用tap的方式,无法dhcp获取到ip,暂时不知道为什么
# QEMU_DEVICES="-device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 -net nic,netdev=nic0 -netdev tap,id=nic0,model=virtio-net-pci,script=qemu/ifup-nat,downscript=qemu/ifdown-nat -usb -device qemu-xhci,id=xhci,p2=8,p3=4 "
Expand All @@ -77,10 +98,33 @@ QEMU_DEVICES="-device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 -netdev
# QEMU_DEVICES="-device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 -netdev user,id=hostnet0,hostfwd=tcp::12580-:12580 -net nic,model=e1000e,netdev=hostnet0,id=net0 -netdev user,id=hostnet1,hostfwd=tcp::12581-:12581 -device virtio-net-pci,vectors=5,netdev=hostnet1,id=net1 -usb -device qemu-xhci,id=xhci,p2=8,p3=4 "
QEMU_ARGUMENT="-d ${QEMU_DISK_IMAGE} -m ${QEMU_MEMORY} -smp ${QEMU_SMP} -boot order=d -monitor ${QEMU_MONITOR} -d ${qemu_trace_std} "

QEMU_ARGUMENT+="-s ${QEMU_MACHINE} -cpu ${QEMU_CPU_FEATURES} -rtc ${QEMU_RTC_CLOCK} -serial ${QEMU_SERIAL} -drive ${QEMU_DRIVE} ${QEMU_DEVICES}"
QEMU_ARGUMENT+="-s ${QEMU_MACHINE} ${QEMU_CPU_FEATURES} ${QEMU_RTC_CLOCK} ${QEMU_SERIAL} -drive ${QEMU_DRIVE} ${QEMU_DEVICES} --nographic"
QEMU_ARGUMENT+=" ${QEMU_SHM_OBJECT} "
QEMU_ARGUMENT+=" ${QEMU_ACCELARATE} "

# 安装riscv64的uboot
install_riscv_uboot()
{

if [ ! -d ${RISCV64_UBOOT_PATH} ]; then
echo "正在下载u-boot..."
uboot_tar_name="u-boot-${UBOOT_VERSION}-riscv64.tar.xz"

uboot_parent_path=$(dirname ${RISCV64_UBOOT_PATH}) || (echo "获取riscv u-boot 版本 ${UBOOT_VERSION} 的父目录失败" && exit 1)

if [ ! -f ${uboot_tar_name} ]; then
wget https://mirrors.dragonos.org.cn/pub/third_party/u-boot/${uboot_tar_name} || echo "下载riscv u-boot 版本 ${UBOOT_VERSION} 失败" && exit 1
fi
echo "下载完成"
echo "正在解压u-boot到 '$uboot_parent_path'..."
mkdir -p $uboot_parent_path
tar xvf u-boot-${UBOOT_VERSION}-riscv64.tar.xz -C ${uboot_parent_path} || (echo "解压riscv u-boot 版本 ${UBOOT_VERSION} 失败" && exit 1)
echo "解压完成"
rm -rf u-boot-${UBOOT_VERSION}-riscv64.tar.xz
fi
echo "riscv u-boot 版本 ${UBOOT_VERSION} 已经安装"
}


if [ $flag_can_run -eq 1 ]; then
while true;do
Expand Down Expand Up @@ -114,10 +158,25 @@ if [ ${BIOS_TYPE} == uefi ] ;then
sudo ${QEMU} -bios arch/x86_64/efi/OVMF-pure-efi.fd ${QEMU_ARGUMENT}
elif [ ${ARCH} == i386 ] ;then
sudo ${QEMU} -bios arch/i386/efi/OVMF-pure-efi.fd ${QEMU_ARGUMENT}
elif [ ${ARCH} == riscv64 ] ;then
install_riscv_uboot
sudo ${QEMU} -kernel ${RISCV64_UBOOT_PATH}/u-boot.bin ${QEMU_ARGUMENT}
else
echo "不支持的架构: ${ARCH}"
fi
else
sudo ${QEMU} ${QEMU_ARGUMENT}
# 如果是i386架构或者x86_64架构,就直接启动
if [ ${ARCH} == x86_64 ] || [ ${ARCH} == i386 ] ;then
sudo ${QEMU} ${QEMU_ARGUMENT}
elif [ ${ARCH} == riscv64 ] ;then
# 如果是riscv64架构,就与efi启动一样
install_riscv_uboot
sudo ${QEMU} -kernel ${RISCV64_UBOOT_PATH}/u-boot.bin ${QEMU_ARGUMENT}
else
echo "不支持的架构: ${ARCH}"
fi
fi

# 删除共享内存
sudo rm -rf ${QEMU_MEMORY_BACKEND_PATH_PREFIX}/${QEMU_MEMORY_BACKEND}
else
Expand Down
7 changes: 7 additions & 0 deletions tools/umount_virt_disk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ if [ ! $uid == "0" ];then
exit
fi

if [ ! ${ARCH} ];then
echo "请设置ARCH环境变量"
exit
fi

DISK_NAME=disk-${ARCH}.img

LOOP_DEVICE=$(lsblk | grep disk_mount|sed 's/.*\(loop[0-9]*\)p1.*/\1/1g'|awk 'END{print $0}')

umount -f ../bin/disk_mount/
Expand Down
39 changes: 29 additions & 10 deletions tools/write_disk_image.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
###############################################
# 该脚本用于将disk_mount目录下的文件写入到disk.img的第一个分区中,
# 该脚本用于将disk_mount目录下的文件写入到disk-${ARCH}.img的第一个分区中,
# 并在磁盘镜像中安装grub引导程序
#
# 用法:bash write_disk_image.sh --bios legacy/uefi
# 如果之前创建的disk.img是MBR分区表,那么请这样运行它:bash write_disk_image.sh --bios legacy
# 如果之前创建的disk.img是GPT分区表,那么请这样运行它:bash write_disk_image.sh --bios uefi
# 通过设置ARCH为x86_64或i386,进行64/32位uefi的install,但是请记住该处的ARCH应与run-qemu.sh中的一致
# 如果之前创建的 disk-${ARCH}.img 是MBR分区表,那么请这样运行它:bash write_disk_image.sh --bios legacy
# 如果之前创建的 disk-${ARCH}.img 是GPT分区表,那么请这样运行它:bash write_disk_image.sh --bios uefi
# 通过设置ARCH为x86_64/i386/riscv64,进行64/32位uefi的install,但是请记住该处的ARCH应与run-qemu.sh中的一致
###############################################

ARCH="x86_64"
#ARCH="i386"
echo "ARCH=${ARCH}"
# 给ARCH变量赋默认值
export ARCH=${ARCH:=x86_64}

DISK_NAME=disk-${ARCH}.img

# 内核映像
root_folder=$(dirname $(pwd))
kernel="${root_folder}/bin/kernel/kernel.elf"
Expand All @@ -28,6 +32,7 @@ GRUB_ABS_PREFIX=/opt/dragonos-grub
GRUB_PATH_I386_LEGACY_INSTALL=${GRUB_ABS_PREFIX}/arch/i386/legacy/grub/sbin/grub-install
GRUB_PATH_I386_EFI_INSTALL=${GRUB_ABS_PREFIX}/arch/i386/efi/grub/sbin/grub-install
GRUB_PATH_X86_64_EFI_INSTALL=${GRUB_ABS_PREFIX}/arch/x86_64/efi/grub/sbin/grub-install
GRUB_PATH_RISCV64_EFI_INSTALL=${GRUB_ABS_PREFIX}/arch/riscv64/efi/grub/sbin/grub-install

GRUB_PATH_I386_LEGACY_FILE=${GRUB_ABS_PREFIX}/arch/i386/legacy/grub/bin/grub-file

Expand Down Expand Up @@ -56,22 +61,22 @@ if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
fi

# 判断是否存在硬盘镜像文件,如果不存在,就创建一个(docker模式下,由于镜像中缺少qemu-img不会创建)
if [ ! -f "${root_folder}/bin/disk.img" ]; then
if [ ! -f "${root_folder}/bin/${DISK_NAME}" ]; then
echo "创建硬盘镜像文件..."
case "$1" in
--bios)
case "$2" in
uefi)
sudo bash ./create_hdd_image.sh -P MBR #GPT分区
sudo ARCH=${ARCH} bash ./create_hdd_image.sh -P MBR #GPT分区
;;
legacy)
sudo bash ./create_hdd_image.sh -P MBR #MBR分区
sudo ARCH=${ARCH} bash ./create_hdd_image.sh -P MBR #MBR分区
;;
esac
;;
*)
# 默认创建MBR分区
sudo bash ./create_hdd_image.sh -P MBR #MBR分区
sudo ARCH=${ARCH} bash ./create_hdd_image.sh -P MBR #MBR分区
;;
esac
fi
Expand Down Expand Up @@ -118,6 +123,10 @@ cfg_content='set timeout=15
echo "echo '${cfg_content}' > ${boot_folder}/grub/grub.cfg" | sh
fi

install_riscv64_efi(){
${GRUB_PATH_RISCV64_EFI_INSTALL} --target=riscv64-efi --efi-directory=${mount_folder} --boot-directory=${boot_folder} --removable
}

if [ "${INSTALL_GRUB_TO_IMAGE}" = "1" ];then

case "$1" in
Expand All @@ -128,10 +137,20 @@ if [ "${INSTALL_GRUB_TO_IMAGE}" = "1" ];then
${GRUB_PATH_I386_EFI_INSTALL} --target=i386-efi --efi-directory=${mount_folder} --boot-directory=${boot_folder} --removable
elif [ ${ARCH} == "x86_64" ];then
${GRUB_PATH_X86_64_EFI_INSTALL} --target=x86_64-efi --efi-directory=${mount_folder} --boot-directory=${boot_folder} --removable
elif [ ${ARCH} == "riscv64" ];then
install_riscv64_efi
else
echo "grub install: 不支持的架构"
fi
;;
legacy) #传统bios
if [ ${ARCH} == "x86_64" ];then
${GRUB_PATH_I386_LEGACY_INSTALL} --target=i386-pc --boot-directory=${boot_folder} /dev/$LOOP_DEVICE
elif [ ${ARCH} == "riscv64" ];then
install_riscv64_efi
else
echo "grub install: 不支持的架构"
fi
;;
esac
;;
Expand Down

0 comments on commit fdb832d

Please sign in to comment.