From: acevest Date: Sat, 13 Apr 2024 05:53:45 +0000 (+0800) Subject: 用 docker cp 的方式解决文件不同步的问题 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/doc/roff.3.ps?a=commitdiff_plain;h=121460dc6605fd33c528e85f1cc5a2cd5d5e4f35;p=kernel.git 用 docker cp 的方式解决文件不同步的问题 --- diff --git a/mkiso.sh b/mkiso.sh index 8efcd86..ad5e640 100755 --- a/mkiso.sh +++ b/mkiso.sh @@ -20,32 +20,37 @@ echo "container id ${CONTAINER_ID}" sync +mkrootfs -name rootfs -path initrd + +grub2_boot_dir="/tmp/iso/boot" +docker exec -it $CONTAINER_ID rm -rf /tmp/iso +docker exec -it $CONTAINER_ID rm -rf /tmp/kernel.iso +docker exec -it $CONTAINER_ID mkdir -p $grub2_boot_dir/grub/ + # 指定要拷贝的文件和目标文件路径及名字 -files[0]="KERNEL.ELF:Kernel" -files[1]="scripts/iso.grub.cfg:grub/grub.cfg" +files[0]="KERNEL.ELF:$grub2_boot_dir/Kernel" +files[1]="scripts/iso.grub.cfg:$grub2_boot_dir/grub/grub.cfg" +files[2]="rootfs:$grub2_boot_dir/rootfs" -declare -a params for i in "${!files[@]}"; do file_line="${files[$i]}" IFS=':' read -ra parts <<< "${file_line}" src_file="${parts[0]}" + dst_file="${parts[1]}" src_file_md5=$($MD5 "$src_file" | awk '{ print $1 }') - s="$src_file_md5:$file_line" - - params[$i]="$s" - - echo "$s" + docker cp $src_file $CONTAINER_ID:$dst_file + echo "$src_file $src_file_md5" + docker exec -it $CONTAINER_ID md5sum $dst_file done -#echo ${params[@]} +#docker exec -it $CONTAINER_ID /usr/bin/grub2-mkrescue -o /tmp/kernel.iso /tmp/iso/ +docker exec -it $CONTAINER_ID bash -c "cd /tmp && grub2-mkrescue -o kernel.iso /tmp/iso/" -params_line="${params[@]}" -docker exec -it $CONTAINER_ID /bin/bash -c "cd /root/workspace/kernel && ./scripts/mkiso.sh ${params_line}" -sync +docker cp $CONTAINER_ID:/tmp/kernel.iso . $MD5 kernel.iso diff --git a/scripts/backup/README.md b/scripts/backup/README.md new file mode 100644 index 0000000..b44640b --- /dev/null +++ b/scripts/backup/README.md @@ -0,0 +1,11 @@ +这两个脚本开始是为了解决宿主机和 docker 容器无法及时同步文件的问题. + +但这样写感觉不太优雅,废弃 + +如果要再使用,注意的是要放在正确的目录 + +host.mkiso.sh 放到 kernel/mkiso.sh +container.mkiso.sh 放到 kernel/scripts/mkiso.sh + +host.mkiso.sh要在宿主机调用 +container.mkiso.sh会提交到 docker 容器执行 diff --git a/scripts/mkiso.sh b/scripts/backup/container.mkiso.sh similarity index 100% rename from scripts/mkiso.sh rename to scripts/backup/container.mkiso.sh diff --git a/scripts/backup/host.mkiso.sh b/scripts/backup/host.mkiso.sh new file mode 100755 index 0000000..8efcd86 --- /dev/null +++ b/scripts/backup/host.mkiso.sh @@ -0,0 +1,51 @@ +#!/bin/bash + +# 兼容md5 命令 +MD5=md5sum +if [[ `uname` == 'Darwin' ]]; then + MD5="md5 -q" +fi + + +# 找到容器 +if [ $# -ne 1 ]; then + docker ps -a + echo "input containerid " + read CONTAINER_ID +else + CONTAINER_ID=$1 +fi + +echo "container id ${CONTAINER_ID}" +sync + + + +# 指定要拷贝的文件和目标文件路径及名字 +files[0]="KERNEL.ELF:Kernel" +files[1]="scripts/iso.grub.cfg:grub/grub.cfg" + +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