#!/bin/sh

# 检查是否安装了docker
if ! [ -x "$(command -v docker)" ]; then
  read -p "系统未安装docker，是否要安装docker？(y/n) " install_docker
  if [ "$install_docker" = "y" ]; then
    sudo apt update
    sudo apt install docker.io
  else
    echo "用户选择不安装docker，退出脚本"
    exit 0
  fi
fi

#这里是添加加速源站
create_folder() {
    local folder_path=$1
    if [[ ! -d $folder_path ]]; then
        sudo mkdir -p $folder_path
        echo "已创建文件夹: $folder_path"
    else
        echo "文件夹已存在: $folder_path"
    fi
}

# 创建文件夹 /etc/docker
create_folder "/etc/docker"

# 设置 registry-mirrors
# 编辑 daemon.json 文件
tee /etc/docker/daemon.json <<-EOF
{
   "registry-mirrors": ["https://docker.fnmqs.work"]
}
EOF


# Debian: 重启 Docker 服务
if [[ -e /etc/debian_version ]]; then
    sudo systemctl daemon-reload
    sudo systemctl restart docker
fi

# Ubuntu: 重启 Docker 服务
if [[ -e /etc/lsb-release ]]; then
    sudo systemctl daemon-reload
    sudo systemctl restart docker
fi

# OpenWrt: 重启 Docker 服务
if [[ -e /etc/openwrt_release ]]; then
    /etc/init.d/dockerd restart
fi

#拉取Ubuntu镜像
docker pull ubuntu

# 判断是否拉取成功
if [ $? -ne 0 ]; then
  # 在绝对路径/root中创建docker_error.log文件，并将错误信息写入文件
  mkdir -p /root
  docker pull ubuntu 2> /root/docker_error.log
  # 在终端提示拉取失败，错误日志保存在了这个里面
  echo "拉取docker镜像失败，请查看错误日志 /root/docker_error.log"
  # 终止脚本
  exit 1
fi

#################这里基于WAN口创建MACVLAN接口#########################
#查找对应WAN口的label
WAN_IFACE="$(uci show network.wan.device | cut -d\' -f2)"
WAN_IP="$(ip addr show | grep $WAN_IFACE | awk '{print $2}' | cut -d/ -f1 | tail -n 1)"
WAN_GATEWAY="$(ip route show default | awk '/default/ {print $3}')"
WAN_IPRANGE="$(echo $WAN_GATEWAY | sed "s/\(.*\)\.\(.*\)/\1.0/")"
MACVLAN_IP="$(echo $WAN_GATEWAY | sed "s/\(.*\)\.\(.*\)/\1.234/")"

ip link set eth1 promisc on

#创建macvlan接口
docker network create -d macvlan --subnet="$WAN_IPRANGE"/24 --gateway="$WAN_GATEWAY" -o parent="$WAN_IFACE" macnet
sleep 2
if ! [ "$(docker network ls | grep macnet)" ]; then
  echo "Create MACVLAN Interface Error, exiting..."
  exit 1
fi

####################################################################

sleep 5s 

#创建Ubuntu容器，并且设置
docker run -dit \
  --name=PcdnUbuntu \
  --restart always \
  --network macnet \
  --ip "$MACVLAN_IP" \
  --privileged \
 ubuntu:latest

#容器外执行准备任务
docker exec PcdnUbuntu bash -c "apt-get update -y" #更新源


sleep 3s 


docker exec PcdnUbuntu bash -c "apt install iproute2 -y" #安装网络工具


sleep 3s 


docker exec PcdnUbuntu bash -c "apt-get install -y openssh-client" #安装ssh客户端


sleep 3s 


docker exec PcdnUbuntu bash -c "apt-get install -y openssh-server" #安装ssh服务端


sleep 3s 


docker exec PcdnUbuntu bash -c "/etc/init.d/ssh start" #开启ssh服务


sleep 3s 


docker exec PcdnUbuntu bash -c "echo 'PermitRootLogin yes' | tee -a /etc/ssh/sshd_config" #开启 root 用户登录


sleep 3s 


docker exec PcdnUbuntu bash -c "service ssh restart" #重启ssh服务


sleep 3s 


docker exec PcdnUbuntu bash -c "echo 'root:123456789' | chpasswd" #修改root密码为123456789


sleep 3s 


docker exec PcdnUbuntu bash -c "service ssh restart" #重启ssh服务


sleep 3s 


docker exec PcdnUbuntu bash -c "ifconfig" 



sleep 3s 


echo "结束配置"

