###############
KVM下
用的image xenial-server-cloudimg-amd64-disk1.img,format=qcow2
cat > hostpass <<EOF
#cloud-config
password: root123
chpasswd: { expire: False }
ssh_pwauth: True
cloud-localds cloud.img hostpass #用hostpass文件生成cloud.img
qemu-img info cloud.img #raw格式的
在create VM的时候将上面这个cloud.img作为cdrom mount到VM上
virt-install --virt-type kvm --name host-1 --ram 1024 --vcpus 1 --import \
--disk /var/lib/libvirt/images/host-1/xenial-server-cloudimg-amd64-disk1.img,format=qcow2 \
--disk /var/lib/libvirt/images/host-1/cloud.img,device=cdrom \
--network bridge=br-mgmt,model=e1000,mac=52:54:00:33:22:11 \
--network bridge=br-leaf1-link3,model=e1000 \
--network bridge=br-leaf2-link3,model=e1000 \
--graphics none \
--noautoconsole
virsh host-1
ubuntu/root123
然后可以给root设置密码,enable ssh password login
#############
openstack下
[email protected]:~# openstack keypair create --public-key ~/.ssh/id_rsa.pub mykey
[email protected]:~# openstack keypair list
+-------+-------------------------------------------------+
| Name | Fingerprint |
+-------+-------------------------------------------------+
| mykey | aa:28:b1:45:24:be:c8:4a:7f:23:7a:49:e5:ad:c8:2e |
+-------+-------------------------------------------------+
openstack server create --flavor m1.small --image <image name> --nic net-id=<nic uuid> --security-group allow_ipv4 --key-name mykey --user-data cloud-config.txt instance-test2
[email protected]:~# ip netns list
qdhcp-b56652f9-5f3d-4a67-97e0-d399d2aa4cd0 #id=network uuid, net172
ip netns exec qdhcp-b56652f9-5f3d-4a67-97e0-d399d2aa4cd0 ssh [email protected] #instance IP