0%
create vm example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| #!/bin/bash set -e set -x ID=$1
BACKFILE_IMG="./ubuntu-20.04-server-cloudimg-amd64.img" TARGET_IMG="client-${ID}.qcow2" TARGET_IMG_SIZE="20G"
ADDFILES="http_test"
HOSTNAME="client-${ID}" IF1_NAME="enp1s0" IF1_IPADDRESS="11.1.11.$(( 20 + ${ID} ))/24" IF1_GATEWAY="11.1.11.1"
IF2_NAME="enp2s0" IF2_IPADDRESS="192.168.122.$(( 30 + ${ID} ))/24"
qemu-img create -f qcow2 -b $BACKFILE_IMG $TARGET_IMG
qemu-img resize $TARGET_IMG $TARGET_IMG_SIZE
virt-customize -a ${TARGET_IMG} --root-password password:lab123
http_test_script=$(mktemp /tmp/httpXXXXXXXX.sh) cat > ${http_test_script} << EOF #!/bin/bash set -x while true do /http_test -c 11.1.10.10 -s 90000000 sleep \$(( \$RANDOM % 1 )) done EOF virt-customize -a ${TARGET_IMG} --upload http_test:/http_test virt-customize -a ${TARGET_IMG} --upload ${http_test_script}:/http.sh virt-customize -a ${TARGET_IMG} --run-command 'chmod +x /http.sh'
script=$(mktemp /tmp/scriptXXXXXXXXXX.sh) cat > ${script} << EOF #!/bin/bash ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -q -N "" ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -q -N "" chmod 0600 /etc/ssh/ssh_host* echo PermitRootLogin yes >> /etc/ssh/sshd_config sed -i "s/PasswordAuthentication no/PasswordAuthentication yes/g" /etc/ssh/sshd_config hostnamectl set-hostname ${HOSTNAME} EOF
virt-customize -a ${TARGET_IMG} --firstboot ${script} rm -rf ${script}
network_cfg_tmp=$(mktemp /tmp/networkXXXXXXXX.yaml) cat > ${network_cfg_tmp} << EOF network: ethernets: ${IF1_NAME}: dhcp4: false addresses: - ${IF1_IPADDRESS} gateway4: ${IF1_GATEWAY} ${IF2_NAME}: addresses: [ ${IF2_IPADDRESS} ] version: 2 EOF
virt-customize -a ${TARGET_IMG} --run-command 'rm -rf /etc/netplan/*' virt-customize -a ${TARGET_IMG} --upload ${network_cfg_tmp}:/etc/netplan/00-installer-config.yaml rm -rf ${network_cfg_tmp}
start_script=$(mktemp /tmp/startXXXXXXXX)
cat > ${start_script} << EOF
[Unit] Description=start http test After=network.target
[Service] ExecStart=bash -c /http.sh [Install] WantedBy=multi-user.target EOF
virt-customize -a ${TARGET_IMG} --upload ${start_script}:/lib/systemd/system/http_test.service virt-customize -a ${TARGET_IMG} --run-command 'systemctl daemon-reload; systemctl enable http_test' rm -rf ${start_script}
virt-install \ --name ${HOSTNAME} \ --ram=8192 \ --vcpus 4 \ --os-type linux \ --os-variant ubuntu20.04 \ --graphics none \ --disk /home/lab/cloud/${TARGET_IMG},device=disk,bus=virtio \ --network bridge=br11,model=virtio \ --network bridge=virbr0,model=virtio \ --noautoconsole \ --import
|
resize the disk size
1 2 3
| growpart /dev/vda 1 resize2fs /dev/vda1
|