PLEX 定時開關機的問題

Written by

in

為了響應能源節約的口號,為放置大量電影文件的WD NAS 設置了自動開關機,每天22點會自行關機,每天8點會自行開機。

這就產生了一個問題,運行Plex media server 的debian OS 檢測不到從NAS export 出來的nfs mount 之後他有一定的幾率會死機。

所以要對這台運行在ESXi上的VM 進行定時開關機的操作。

Vmware 官方提供了api,並且提供了一個官方的cli govc,首先當然是安裝他。

Usage: govc <COMMAND> [COMMON OPTIONS] [PATH]...

govmomi is a Go library for interacting with VMware vSphere APIs (ESXi and/or
vCenter Server).
It is licensed under the Apache License, Version 2.0

govc is the CLI for govmomi.

The available commands are listed below. A detailed description of each
command can be displayed with "govc <COMMAND> -h". The description of all
commands can be also found at https://github.com/vmware/govmomi/blob/main/govc/USAGE.md.

使用起來呢,倒是很簡單,主要的問題在於我不能直接使用root 帳戶去操作poweron/shutdown 這類的動作,萬一密碼洩漏了呢。但是,Vmware 也沒有在ESXi v6 的GUI 中提供為新增的用戶賦權的功能。所以,第一次先要使用root 帳戶來為新增的用戶賦權。

設置幾個env,這裡需要注意的是一定要用單引號,因為特殊字符密碼可能會被吃掉:

export GOVC_URL=https://10.1.1.2
export GOVC_USERNAME='root'
export GOVC_PASSWORD='IAM-root-password'
export GOVC_INSECURE=1

看一下about

govc about
FullName:     VMware ESXi 6.7.0 build-15160138
Name:         VMware ESXi
Vendor:       VMware, Inc.
Version:      6.7.0
Build:        15160138
OS type:      vmnix-x86
API type:     HostAgent
API version:  6.7.3
Product ID:   embeddedEsx

govc host.info 
Name:              esxi-s01.bbken.net
  Path:            /ha-datacenter/host/esxi-s01.bbken.net
  Manufacturer:    HP
  Logical CPUs:    6 CPUs @ 2496MHz
  Processor type:  12th Gen Intel(R) Core(TM) i5-12400
  CPU usage:       4609 MHz (30.8%)
  Memory:          40644MB
  Memory usage:    35268 MB (86.8%)
  Boot time:       2026-09-13 09:15:48.082707 +0000 UTC
  State:           connected

列舉一下當前有的這些VM,看起來還蠻多的呢,不過都沒開機:

govc ls /ha-datacenter/vm 
/ha-datacenter/vm/pfSense
/ha-datacenter/vm/PLEX
/ha-datacenter/vm/Webserver
/ha-datacenter/vm/Windows10
/ha-datacenter/vm/Alteon
/ha-datacenter/vm/ROS
/ha-datacenter/vm/OpenWRT
/ha-datacenter/vm/Windwos10CHN
/ha-datacenter/vm/ROS7
/ha-datacenter/vm/Debian
/ha-datacenter/vm/RHEL8
/ha-datacenter/vm/FaceFusion

看一下PLEX 這台VM 的詳細:

govc vm.info "PLEX"
Name:           PLEX
  Path:         /ha-datacenter/vm/PLEX
  Guest name:   Debian GNU/Linux 10 (64-bit)
  Memory:       8192MB
  CPU:          8 vCPU(s)
  Power state:  poweredOn
  Boot time:    2026-09-30 03:10:46.763837 +0000 UTC
  IP address:   10.1.1.5
  Host:         s01.plex.tv

graceful shutdown 的命令是這樣:

govc vm.power -s "PLEX"

加電的命令是這樣:

govc vm.power -on "PLEX"

測試了一下開關機,沒問題。

啊,要先創建一個reboot user,我們就叫reboot-user好了,從ESXi的GUI 創建這個用戶,然後使用root用戶操作govc 賦權:

govc role.create vmpower VirtualMachine.Interact.PowerOn VirtualMachine.Interact.PowerOff VirtualMachine.Interact.Reset

govc permissions.set -principal reboot-user -role vmpower -propagate=true

完成後看一下:

govc permissions.ls /
Role     Entity  Principal    Propagate
vmpower  /       reboot-user  Yes
Admin    /       vpxuser      Yes
Admin    /       dcui         Yes
Admin    /       root         Yes

看起來沒問題,接下來重新設置env,使用剛才創建的reboot-user和他的密碼:

export GOVC_URL=https://10.1.1.2
export GOVC_USERNAME='reboot-user'
export GOVC_PASSWORD='reboot-user-password'
export GOVC_INSECURE=1

govc vm.info "PLEX" | grep "Power state"
Power state:  poweredOn

寫兩個腳本來加入到FreeBSD 的計劃任務:

vm-stop.sh:

#!/bin/sh
export GOVC_URL=https://10.1.1.2
export GOVC_USERNAME='reboot-user'
export GOVC_PASSWORD='reboot-user-password'
export GOVC_INSECURE=1
/usr/local/bin/govc vm.power -s "PLEX"
vm-start.sh:

#!/bin/sh
export GOVC_URL=https://10.1.1.2
export GOVC_USERNAME='reboot-user'
export GOVC_PASSWORD='reboot-user-password'
export GOVC_INSECURE=1
/usr/local/bin/govc vm.power -on "PLEX"

設定權限(腳本含有密碼,請務必設置為 700):
chmod 700 vm-start.sh
chmod 700 vm-stop.sh

crontab -e 加入这两行(21:30 关机、08:30 开机):

30 21 * * * /vm-stop.sh >> /tmp/vm.log 2>&1
30 8 * * * /vm-start.sh >> /tmp/vm.log 2>&1

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *