From: acevest Date: Sat, 13 Apr 2024 05:14:46 +0000 (+0800) Subject: 解决宿主机和 docker 容器文件不同步的问题 X-Git-Url: http://zhaoyanbai.com/repos/Bv9ARM.html?a=commitdiff_plain;h=483b1393214da6f2bdfdde12fe4676f8b586259d;p=kernel.git 解决宿主机和 docker 容器文件不同步的问题 --- diff --git a/boot/boot.c b/boot/boot.c index 1ee713a..f7ad9f0 100644 --- a/boot/boot.c +++ b/boot/boot.c @@ -112,12 +112,13 @@ void check_kernel(unsigned long addr, unsigned long magic) { break; case MULTIBOOT_TAG_TYPE_MODULE: struct multiboot_tag_module *m = (struct multiboot_tag_module *)tag; + void *mod_start = (void *)m->mod_start; printk("module 0x%08x - 0x%08x size %u cmdline %s\n", m->mod_start, m->mod_end, m->size, m->cmdline); // TODO 在操作系统中保留这段内存 - uint32_t *mod_magic = m->mod_start + 0; - uint32_t *mod_timestamp = m->mod_start + 8; - uint32_t *mod_file_entry_cnt = m->mod_start + 12; - char *mod_name = m->mod_start + 16; + uint32_t *mod_magic = (uint32_t *)(mod_start + 0); + uint32_t *mod_timestamp = (uint32_t *)(mod_start + 8); + uint32_t *mod_file_entry_cnt = (uint32_t *)(mod_start + 12); + char *mod_name = (char *)mod_start + 16; printk("module magic %08x timestamp %u file entry cnt %u name %s \n", *mod_magic, *mod_timestamp, *mod_file_entry_cnt, mod_name); void timestamp_to_date(uint32_t ts); diff --git a/boot/multiboot.S b/boot/multiboot.S index a12335f..da41886 100644 --- a/boot/multiboot.S +++ b/boot/multiboot.S @@ -189,13 +189,13 @@ multiboot2_header_bgn: // .framebuffer_tag_end: // // .align 2 - .multiboot2_header_tag_module: - .short MULTIBOOT_HEADER_TAG_MODULE_ALIGN - .short 0 - .long 8 - .long 0 + // .multiboot2_header_tag_module: + // .short MULTIBOOT_HEADER_TAG_MODULE_ALIGN + // .short 0 + // .long 8 + // .long 0 - .align 2 + .align 8 .multiboot2_tag_end: .short MULTIBOOT_HEADER_TAG_END .short 0 diff --git a/mkiso.sh b/mkiso.sh index 7091b36..8efcd86 100755 --- a/mkiso.sh +++ b/mkiso.sh @@ -1,5 +1,13 @@ #!/bin/bash -# + +# 兼容md5 命令 +MD5=md5sum +if [[ `uname` == 'Darwin' ]]; then + MD5="md5 -q" +fi + + +# 找到容器 if [ $# -ne 1 ]; then docker ps -a echo "input containerid " @@ -9,14 +17,35 @@ else fi echo "container id ${CONTAINER_ID}" +sync -docker exec -it $CONTAINER_ID /bin/bash -c "cd /root/workspace/kernel && ./scripts/mkiso.sh" -MD5=md5sum +# 指定要拷贝的文件和目标文件路径及名字 +files[0]="KERNEL.ELF:Kernel" +files[1]="scripts/iso.grub.cfg:grub/grub.cfg" -if [[ `uname` == 'Darwin' ]]; then - MD5=md5 -fi +declare -a params + +for i in "${!files[@]}"; do + file_line="${files[$i]}" + + IFS=':' read -ra parts <<< "${file_line}" + src_file="${parts[0]}" + src_file_md5=$($MD5 "$src_file" | awk '{ print $1 }') + + s="$src_file_md5:$file_line" + + params[$i]="$s" + + echo "$s" +done + +#echo ${params[@]} + +params_line="${params[@]}" +docker exec -it $CONTAINER_ID /bin/bash -c "cd /root/workspace/kernel && ./scripts/mkiso.sh ${params_line}" + +sync $MD5 kernel.iso diff --git a/qemu.sh b/qemu.sh index 190db49..e18cd69 100755 --- a/qemu.sh +++ b/qemu.sh @@ -1,5 +1,8 @@ #!/bin/bash # +echo "------------------" +echo `md5 kernel.iso` +echo "------------------" # 检查serial_monitor进程是否在运行 process_name="serial_monitor" if ! pgrep -x "$process_name" > /dev/null diff --git a/scripts/mkiso.sh b/scripts/mkiso.sh index 0f6270b..37bd781 100755 --- a/scripts/mkiso.sh +++ b/scripts/mkiso.sh @@ -3,9 +3,71 @@ # 这个代码必需要在x86的linux机器上运行 # 因为如果在其它机器上运行,其grub就不是x86版本 source ~/.bashrc +rm -rf /tmp/iso mkdir -p /tmp/iso/boot/grub/ -cp scripts/iso.grub.cfg /tmp/iso/boot/grub/grub.cfg -cp KERNEL.ELF /tmp/iso/boot/Kernel +sync + + + +copy_file_with_retry() { + local expected_md5="$1" + local src_file="$2" + local dst_file="/tmp/iso/boot/$3" + local max_retries=$4 + local retry_count=0 + + + while [ $retry_count -lt $max_retries ]; do + # 查看源文件的大小是不是不为0 + if [ -s $src_file ]; then + cp -f $src_file $dst_file + + # 拷贝后再校验 md5 如果不一致继续重试 + local dst_file_md5=$(md5sum $dst_file | awk '{ print $1 }') + + echo "file: $src_file md5: ${dst_file_md5} expected md5: $expected_md5" + + if [ "$expected_md5" == "$dst_file_md5" ]; then + echo "File $src_file copied successfully to $dst_file." + return 0 + else + echo "retry" + fi + fi + + # 等1秒再重试 + sleep 1 + ((retry_count++)) + done + + echo "failed to copy file $src_file to $dst_file after $max_retries attempts." + return 1 +} + + + +for arg in "$@" +do + IFS=':' read -ra file_line <<< "$arg" + + md5sum=${file_line[0]} + src_file=${file_line[1]} + dst_file=${file_line[2]} + copy_file_with_retry "${md5sum}" "${src_file}" "${dst_file}" 3 +done + + + +#copy_file_with_retry "KERNEL.ELF" "/tmp/iso/boot/Kernel" +#copy_file_with_retry "scripts/iso.grub.cfg" "/tmp/iso/boot/grub/grub.cfg" +#cp -f scripts/iso.grub.cfg /tmp/iso/boot/grub/grub.cfg +#ls -l KERNEL.ELF +#cp -f KERNEL.ELF /tmp/iso/boot/Kernel +#md5sum KERNEL.ELF +#md5sum /tmp/iso/boot/Kernel mkrootfs -path initrd --name rootfs -cp rootfs /tmp/iso/boot/ +cp -f rootfs /tmp/iso/boot/ grub2-mkrescue -o kernel.iso /tmp/iso/ +md5sum kernel.iso +sync +