Переглянути джерело

feat: 使用新版dadk来创建\挂载\卸载磁盘镜像

Signed-off-by: longjin <longjin@DragonOS.org>
longjin 7 місяців тому
батько
коміт
8506942036
9 змінених файлів з 107 додано та 114 видалено
  1. 22 24
      Makefile
  2. 14 0
      config/rootfs.toml
  3. 3 0
      env.mk
  4. 40 0
      tools/check_arch.sh
  5. 0 27
      tools/mount_virt_disk.sh
  6. 1 1
      tools/run-qemu.sh
  7. 0 19
      tools/umount_virt_disk.sh
  8. 25 41
      tools/write_disk_image.sh
  9. 2 2
      user/Makefile

+ 22 - 24
Makefile

@@ -34,19 +34,22 @@ else
 	FMT_CHECK=
 endif
 
+# Check if ARCH matches the arch field in dadk-manifest.toml
+check_arch:
+	@bash tools/check_arch.sh
 
 .PHONY: all 
 all: kernel user
 
 
 .PHONY: kernel
-kernel:
+kernel: check_arch
 	mkdir -p bin/kernel/
 	
 	$(MAKE) -C ./kernel all ARCH=$(ARCH) || (sh -c "echo 内核编译失败" && exit 1)
 	
 .PHONY: user
-user:
+user: check_arch
 	$(MAKE) -C ./user all ARCH=$(ARCH) || (sh -c "echo 用户程序编译失败" && exit 1)
 
 .PHONY: clean
@@ -61,11 +64,6 @@ clean:
 ECHO:
 	@echo "$@"
 
-cppcheck-xml: 
-	cppcheck kernel user --platform=unix64 --std=c11 -I user/libs/ -I=kernel/ --force -j $(NPROCS) --xml 2> cppcheck.xml
-
-cppcheck:
-	cppcheck kernel user --platform=unix64 --std=c11 -I user/libs/ -I=kernel/ --force -j $(NPROCS)
 
 docs: ECHO
 	bash -c "cd docs && make html && cd .."
@@ -81,74 +79,74 @@ else
 endif
 
 # 写入磁盘镜像
-write_diskimage:
+write_diskimage: check_arch
 	@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 .."
+	bash -c "export ARCH=$(ARCH); cd tools && bash grub_auto_install.sh && sudo DADK=$(DADK) ARCH=$(ARCH) bash $(ROOT_PATH)/tools/write_disk_image.sh --bios=legacy && cd .."
 
 # 写入磁盘镜像(uefi)
-write_diskimage-uefi:
-	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 .."
+write_diskimage-uefi: check_arch
+	bash -c "export ARCH=$(ARCH); cd tools && bash grub_auto_install.sh && sudo DADK=$(DADK) ARCH=$(ARCH) bash $(ROOT_PATH)/tools/write_disk_image.sh --bios=uefi && cd .."
 # 不编译,直接启动QEMU
-qemu:
+qemu: check_arch
 	sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=window && cd .."
 
 # 不编译,直接启动QEMU,不显示图像
-qemu-nographic:
+qemu-nographic: check_arch
 	sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=nographic && cd .."
 
 # 不编译,直接启动QEMU(UEFI)
-qemu-uefi:
+qemu-uefi: check_arch
 	sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=window && cd .."
 # 不编译,直接启动QEMU,使用VNC Display作为图像输出
-qemu-vnc:
+qemu-vnc: check_arch
 	sh -c "cd tools && bash run-qemu.sh --bios=legacy --display=vnc && cd .."
 # 不编译,直接启动QEMU(UEFI),使用VNC Display作为图像输出
-qemu-uefi-vnc:
+qemu-uefi-vnc: check_arch
 	sh -c "cd tools && bash run-qemu.sh --bios=uefi --display=vnc && cd .."
 	
 # 编译并写入磁盘镜像
-build:
+build: check_arch
 	$(MAKE) all -j $(NPROCS)
 	$(MAKE) write_diskimage || exit 1
 
 # 在docker中编译,并写入磁盘镜像
-docker:
+docker: check_arch
 	@echo "使用docker构建"
 	sudo bash tools/build_in_docker.sh || exit 1
 	$(MAKE) write_diskimage || exit 1
 	
 # uefi方式启动
-run-uefi:
+run-uefi: check_arch
 	$(MAKE) all -j $(NPROCS)
 	$(MAKE) write_diskimage-uefi || exit 1
 	$(MAKE) qemu-uefi
 	
 # 编译并启动QEMU
-run:
+run: check_arch
 	$(MAKE) all -j $(NPROCS)
 	$(MAKE) write_diskimage || exit 1
 	$(MAKE) qemu
 
 # uefi方式启动,使用VNC Display作为图像输出
-run-uefi-vnc:
+run-uefi-vnc: check_arch
 	$(MAKE) all -j $(NPROCS)
 	$(MAKE) write_diskimage-uefi || exit 1
 	$(MAKE) qemu-uefi-vnc
 	
 # 编译并启动QEMU,使用VNC Display作为图像输出
-run-vnc:
+run-vnc: check_arch
 	$(MAKE) all -j $(NPROCS)
 	$(MAKE) write_diskimage || exit 1
 	$(MAKE) qemu-vnc
 
 # 在docker中编译,并启动QEMU
-run-docker:
+run-docker: check_arch
 	@echo "使用docker构建并运行"
 	sudo bash tools/build_in_docker.sh || exit 1
 	$(MAKE) write_diskimage || exit 1
 	$(MAKE) qemu
 
-fmt:
+fmt: check_arch
 	@echo "格式化代码" 
 	FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C kernel
 	FMT_CHECK=$(FMT_CHECK) $(MAKE) fmt -C user

+ 14 - 0
config/rootfs.toml

@@ -0,0 +1,14 @@
+[metadata]
+# Filesystem type (options: `fat32`)
+fs_type = "fat32"
+# Size of the rootfs disk image (eg, `1G`, `1024M`)
+size = "1G"
+
+[partition]
+# Partition type (options: "none", "mbr", "gpt")
+#
+# If "none" is specified, no partition table will be created, 
+# and the entire disk will be treated as a single partition.
+#
+# Note that the "none" option is incompatible with GRUB boot.
+type = "mbr"

+ 3 - 0
env.mk

@@ -8,3 +8,6 @@ endif
 ifeq ($(EMULATOR), )
 export EMULATOR=__NO_EMULATION__
 endif
+
+
+export DADK?=$(shell which dadk)

+ 40 - 0
tools/check_arch.sh

@@ -0,0 +1,40 @@
+#!/bin/bash
+
+BASE_PATH=$(pwd)
+# 定义错误信息
+ARCH_MISMATCH_ERROR="Error: ARCH in env.mk does not match arch in dadk-manifest.toml"
+
+if [ -z "$ARCH" ]; then
+    echo "Error: ARCH environment variable is not set." >&2
+    exit 1
+fi
+
+
+# Check if ROOT_PATH is set
+if [ -n "$ROOT_PATH" ]; then
+    CHECK_PATH="$ROOT_PATH"
+else
+    # Check if the current directory name is "tools"
+    if [ "$(basename "$BASE_PATH")" = "tools" ]; then
+        # Try the parent directory's dadk-manifest
+        CHECK_PATH=$(dirname "$BASE_PATH")/
+    else
+        # Otherwise, check the current directory
+        CHECK_PATH="$BASE_PATH"
+    fi
+fi
+
+echo "Checking $CHECK_PATH"
+
+
+# 读取dadk-manifest.toml文件中的arch字段
+DADK_ARCH=$(grep -oP '(?<=arch = ")[^"]+' $CHECK_PATH/dadk-manifest.toml)
+
+# 检查arch字段是否为x86_64
+if [ "$ARCH" != $DADK_ARCH ]; then
+    echo "$ARCH_MISMATCH_ERROR" >&2
+    exit 1
+else
+    echo "Arch check passed."
+    exit 0
+fi

+ 0 - 27
tools/mount_virt_disk.sh

@@ -1,27 +0,0 @@
-# ======检查是否以sudo运行=================
-uid=`id -u`
-if [ ! $uid == "0" ];then
- echo "请以sudo权限运行"
- exit
-fi
-
-# 检查是否设置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
-
-mkdir -p ../bin/disk_mount/
-mount ${LOOP_DEVICE}p1 ../bin/disk_mount/ 
-lsblk

+ 1 - 1
tools/run-qemu.sh

@@ -65,7 +65,7 @@ UBOOT_VERSION="v2023.10"
 RISCV64_UBOOT_PATH="arch/riscv64/u-boot-${UBOOT_VERSION}-riscv64"
 
 
-DISK_NAME="disk-${ARCH}.img"
+DISK_NAME="disk-image-${ARCH}.img"
 
 QEMU=qemu-system-${ARCH}
 QEMU_DISK_IMAGE="../bin/${DISK_NAME}"

+ 0 - 19
tools/umount_virt_disk.sh

@@ -1,19 +0,0 @@
-# ======检查是否以sudo运行=================
-uid=`id -u`
-if [ ! $uid == "0" ];then
- echo "请以sudo权限运行"
- 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/
-losetup -d /dev/$LOOP_DEVICE
-echo $LOOP_DEVICE

+ 25 - 41
tools/write_disk_image.sh

@@ -1,5 +1,5 @@
 ###############################################
-# 该脚本用于将disk_mount目录下的文件写入到disk-${ARCH}.img的第一个分区中,
+# 该脚本用于将文件拷贝到磁盘镜像中,
 #       并在磁盘镜像中安装grub引导程序
 #
 # 用法:bash write_disk_image.sh --bios legacy/uefi
@@ -11,15 +11,16 @@
 echo "ARCH=${ARCH}"
 # 给ARCH变量赋默认值
 export ARCH=${ARCH:=x86_64}
+export DADK=${DADK:=dadk}
 
-DISK_NAME=disk-${ARCH}.img
 
 # 内核映像
 root_folder=$(dirname $(pwd))
 kernel="${root_folder}/bin/kernel/kernel.elf"
-boot_folder="${root_folder}/bin/disk_mount/boot"
+mount_folder=$($DADK -w $root_folder rootfs show-mountpoint || exit 1)
+boot_folder="${mount_folder}/boot"
 GRUB_INSTALL_PATH="${boot_folder}/grub"
-mount_folder="${root_folder}/bin/disk_mount"
+
 ARGS=`getopt -o p -l bios: -- "$@"`
 eval set -- "${ARGS}"
 #echo formatted parameters=[$@]
@@ -67,34 +68,17 @@ if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
     fi
 fi
 
-# 判断是否存在硬盘镜像文件,如果不存在,就创建一个(docker模式下,由于镜像中缺少qemu-img不会创建)
-if [ ! -f "${root_folder}/bin/${DISK_NAME}" ]; then
-    echo "创建硬盘镜像文件..."
-    case "$1" in
-        --bios) 
-        case "$2" in
-                uefi)
-            sudo ARCH=${ARCH} bash ./create_hdd_image.sh -P MBR #GPT分区    用GPT分区uefi启动不了 内核没有针对gpt分区表来做处理
-            ;;
-                legacy)
-            sudo ARCH=${ARCH} bash ./create_hdd_image.sh -P MBR #MBR分区
-            ;;
-            esac       
-        ;;
-    *)
-        # 默认创建MBR分区
-        sudo ARCH=${ARCH} bash ./create_hdd_image.sh -P MBR #MBR分区
-        ;;
-    esac
-fi
+# 判断是否存在硬盘镜像文件,如果不存在,就创建一个
+echo "创建硬盘镜像文件..."
+$DADK -w $root_folder rootfs create --skip-if-exists || exit 1
+
+$DADK -w $root_folder rootfs mount || exit 1
 
-# 拷贝程序到硬盘
-mkdir -p ${root_folder}/bin/disk_mount
-bash mount_virt_disk.sh || exit 1
 
-LOOP_DEVICE=$(lsblk | grep disk_mount|sed 's/.*\(loop[0-9]*\)p1.*/\1/1g'|awk 'END{print $0}')
-echo $LOOP_DEVICE
 
+LOOP_DEVICE=$($DADK -w $root_folder rootfs show-loop-device || exit 1)
+echo $LOOP_DEVICE
+echo ${mount_folder}
 # mkdir -p ${GRUB_INSTALL_PATH}
 
 # 检测grub文件夹是否存在
@@ -107,21 +91,20 @@ fi
 
 
 if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
-    cp ${kernel} ${root_folder}/bin/disk_mount/boot/
+    cp ${kernel} ${mount_folder}/boot/
 fi
 
 # 拷贝用户程序到磁盘镜像
-mkdir -p ${root_folder}/bin/disk_mount/bin
-mkdir -p ${root_folder}/bin/disk_mount/dev
-mkdir -p ${root_folder}/bin/disk_mount/proc
-mkdir -p ${root_folder}/bin/disk_mount/usr
-touch ${root_folder}/bin/disk_mount/dev/keyboard.dev
-cp -r ${root_folder}/bin/sysroot/* ${root_folder}/bin/disk_mount/
+mkdir -p ${mount_folder}/bin
+mkdir -p ${mount_folder}/dev
+mkdir -p ${mount_folder}/proc
+mkdir -p ${mount_folder}/usr
+cp -r ${root_folder}/bin/sysroot/* ${mount_folder}/
 
 # 设置 grub 相关数据
 if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
     
-    touch ${root_folder}/bin/disk_mount/boot/grub/grub.cfg
+    touch ${mount_folder}/boot/grub/grub.cfg
 cfg_content='set timeout=15
     set default=0
     insmod efi_gop
@@ -154,7 +137,7 @@ if [ "${INSTALL_GRUB_TO_IMAGE}" = "1" ];then
                 ;;
                     legacy) #传统bios
                     if [ ${ARCH} == "x86_64" ];then
-                        ${GRUB_PATH_I386_LEGACY_INSTALL} --target=i386-pc --boot-directory=${boot_folder} /dev/$LOOP_DEVICE
+                        ${GRUB_PATH_I386_LEGACY_INSTALL} --target=i386-pc --boot-directory=${boot_folder} $LOOP_DEVICE
                     elif [ ${ARCH} == "riscv64" ];then
                         install_riscv64_efi
                     else
@@ -165,11 +148,12 @@ if [ "${INSTALL_GRUB_TO_IMAGE}" = "1" ];then
             ;;
         *)
         #传统bios
-        ${GRUB_PATH_I386_LEGACY_INSTALL} --target=i386-pc --boot-directory=${boot_folder} /dev/$LOOP_DEVICE
+        ${GRUB_PATH_I386_LEGACY_INSTALL} --target=i386-pc --boot-directory=${boot_folder} $LOOP_DEVICE
         ;;
-            
+
     esac
 fi
 
 sync
-bash umount_virt_disk.sh
+
+$DADK -w $root_folder rootfs umount || exit 1

+ 2 - 2
user/Makefile

@@ -41,8 +41,8 @@ endif
 .PHONY: dadk_run
 dadk_run: install_dadk
 	mkdir -p $(DADK_CACHE_DIR)
-	dadk-insiders user build -w $(ROOT_PATH)
-	dadk-insiders user install -w $(ROOT_PATH)
+	$(DADK) user build -w $(ROOT_PATH)
+	$(DADK) user install -w $(ROOT_PATH)
 
 .PHONY: dadk_clean
 dadk_clean: install_dadk