zstack社区版使用技巧

一、开机拉起vm

#创建vm list文件
vi /data/shell/vm_list
aaa  ff59971ee447494186b9e4acxxxxxxxx
bbb  a9e4ac46905c4b03aecce420xxxxxxxx
ccc  7bcc79c96f424f71b7753089xxxxxxxx

#创建脚本文件
vi /data/shell/start_vm.sh
#!/bin/bash
# 脚本路径:/usr/local/bin/start_vm.sh
# 描述:启动虚拟机实例
source /etc/profile

LOG_FILE="/var/log/start_vm.log"
ZSTACK_CLI="/usr/bin/zstack-cli"
HOST="192.168.1.103"
VM_LIST="/data/shell/vm_list"
VM_PASSWD='xxxxxxxx'

${ZSTACK_CLI} -H ${HOST} LogInByAccount accountName=admin password=${VM_PASSWD}

while read VM_NAME UUID; do
    echo "$(date '+%Y-%m-%d %H:%M:%S') - Starting VM with ${VM_NAME} UUID=${UUID}" >> "${LOG_FILE}"
    ${ZSTACK_CLI} -H ${HOST} -P ${VM_PASSWD} StartVmInstance uuid=${UUID} >> "${LOG_FILE}" 2>&1
    echo "$(date '+%Y-%m-%d %H:%M:%S') - VM start script completed" >> "${LOG_FILE}"
    sleep 1
done < ${VM_LIST}

#创建systemd守护文件
vi /etc/systemd/system/start-vm.service
[Unit]
Description=Start VM Instance via zstack-cli
After=network.target multi-user.target
Wants=network-online.target
DefaultDependencies=no

[Service]
Type=oneshot
ExecStart=/data/shell/start_vm.sh
ExecStartPre=/bin/sleep 120
StandardOutput=append:/var/log/start_vm.log
StandardError=append:/var/log/start_vm.log
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

#设置开机启动
systemctl enable start-vm
此条目发表在虚拟化分类目录。将固定链接加入收藏夹。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注