From 121460dc6605fd33c528e85f1cc5a2cd5d5e4f35 Mon Sep 17 00:00:00 2001 From: acevest Date: Sat, 13 Apr 2024 13:53:45 +0800 Subject: [PATCH] =?utf8?q?=E7=94=A8=20docker=20cp=20=E7=9A=84=E6=96=B9?= =?utf8?q?=E5=BC=8F=E8=A7=A3=E5=86=B3=E6=96=87=E4=BB=B6=E4=B8=8D=E5=90=8C?= =?utf8?q?=E6=AD=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- mkiso.sh | 29 ++++++----- scripts/backup/README.md | 11 ++++ .../{mkiso.sh => backup/container.mkiso.sh} | 0 scripts/backup/host.mkiso.sh | 51 +++++++++++++++++++ 4 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 scripts/backup/README.md rename scripts/{mkiso.sh => backup/container.mkiso.sh} (100%) create mode 100755 scripts/backup/host.mkiso.sh 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 -- 2.44.0