如何使用shell脚本安装LXD并创建虚拟机
时间:2019-11-20 08:53:47 来源:igfitidea点击:
阿里云在创建Linux服务器后可以指定运行一些脚本。怎样写一个脚本,让服务器创建好后自动安装LXD服务,并创建一个虚拟机?
云服务器启动时如何创建LXD VM?
脚本参考
创建CentOS LXD虚拟机脚本:
#!/bin/bash
## Set defaults ##
if_net="eth0" # vm interface
br_net="lxdbr0" # host bridge
if_net_sub="10.105.28.1/24" # subnet for br_net
if_net_ip="10.105.28.2" # IP for vm
## VM name ##
vm_name="www-server"
## Vm distro. I am using CentOS ##
## You can use Gentoo, Arch, OpenSuse, Ubuntu, Debian and more ##
vm_distro="centos/7/amd64"
## bin path ##
_apt="/usr/bin/apt-get"
_lxd="/usr/bin/lxd"
_lxc="/usr/bin/lxc"
## Update base host ##
$_apt update
$_apt -y upgrade
## Install LXD on base os ##
$_apt -y install lxd
$_lxd init --auto
## Create new networking bridge ##
$_lxc network create ${br_net} ipv6.address=none ipv4.address=${if_net_sub} ipv4.nat=true
## Create vm ##
$_lxc init images:${vm_distro} ${vm_name}
## Config vm networking ##
$_lxc network attach ${br_net} ${vm_name} ${if_net}
$_lxc config device set ${vm_name} ${if_net} ipv4.address ${if_net_ip}
## Start vm ##
$_lxc start ${vm_name}
## Make sure vm boot after host reboots ##
$_lxc config set ${vm_name} boot.autostart true
## Install updates in CentOS 7 VM ##
$_lxc exec ${vm_name} -- /usr/bin/yum -y update
$_lxc exec ${vm_name} -- /usr/bin/yum -y upgrade
## Install package (optional) ##
$_lxc exec ${vm_name} -- /usr/bin/yum -y install epel-release
$_lxc exec ${vm_name} -- /usr/bin/yum -y install httpd htop

