From d93389115b21597901ec5f2c331d8a6269c50fe9 Mon Sep 17 00:00:00 2001 From: spiritLHLS <103393591+spiritLHLS@users.noreply.github.com> Date: Sun, 9 Apr 2023 00:29:04 +0800 Subject: [PATCH] Update and rename rebuild_qcow2.sh to back/rebuild_qcow2.sh --- back/rebuild_qcow2.sh | 33 +++++++++++++++++++++++++++++++++ rebuild_qcow2.sh | 21 --------------------- 2 files changed, 33 insertions(+), 21 deletions(-) create mode 100644 back/rebuild_qcow2.sh delete mode 100644 rebuild_qcow2.sh diff --git a/back/rebuild_qcow2.sh b/back/rebuild_qcow2.sh new file mode 100644 index 0000000..a83e1c8 --- /dev/null +++ b/back/rebuild_qcow2.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +if ! command -v virt-customize &> /dev/null +then + echo "virt-customize not found, installing libguestfs-tools" + sudo apt-get update + sudo apt-get install -y libguestfs-tools +fi + +if ! command -v rngd &> /dev/null +then + echo "rng-tools not found, installing rng-tools" + sudo apt-get update + sudo apt-get install -y rng-tools +fi + +qcow_file=$1 +echo "转换文件$qcow_file中......" +echo "启用SSH功能..." +virt-customize -a $qcow_file --run-command "systemctl enable ssh" +virt-customize -a $qcow_file --run-command "systemctl start ssh" +echo "启用root登录..." +virt-customize -a $qcow_file --run-command "sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config" +virt-customize -a $qcow_file --run-command "sed -i 's/#Port 22/Port 22/g; s/#AddressFamily any/AddressFamily any/g; s/#ListenAddress 0.0.0.0/ListenAddress 0.0.0.0/g; s/#ListenAddress ::/ListenAddress ::/g' /etc/ssh/sshd_config" +virt-customize -a $qcow_file --run-command "systemctl restart sshd" +echo "创建备份..." +cp $qcow_file ${qcow_file}.bak +echo "复制新文件..." +cp $qcow_file ${qcow_file}.tmp +echo "覆盖原文件..." +mv ${qcow_file}.tmp $qcow_file +rm -rf *.bak +echo "$qcow_file修改完成" diff --git a/rebuild_qcow2.sh b/rebuild_qcow2.sh deleted file mode 100644 index b77b44d..0000000 --- a/rebuild_qcow2.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash - -apt-get update -apt-get install libguestfs-tools -sudo apt-get install rng-tools - -qcow_file=/root/qcow/ubuntu22.qcow2 -echo "启用SSH功能..." -virt-customize -a $qcow_file --run-command "systemctl enable ssh" -virt-customize -a $qcow_file --run-command "systemctl start ssh" -echo "启用root登录..." -virt-customize -a $qcow_file --run-command "sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config" -virt-customize -a $qcow_file --run-command "systemctl restart sshd" -echo "创建备份..." -cp $qcow_file ${qcow_file}.bak -echo "复制新文件..." -cp $qcow_file ${qcow_file}.tmp -echo "覆盖原文件..." -mv ${qcow_file}.tmp $qcow_file -rm -rf *.bak -echo "修改完成"