Skip to content

Instantly share code, notes, and snippets.

@Jamlee
Last active May 22, 2022 16:32
Show Gist options
  • Save Jamlee/a125081c38b9df3208b3797668713242 to your computer and use it in GitHub Desktop.
Save Jamlee/a125081c38b9df3208b3797668713242 to your computer and use it in GitHub Desktop.
hyper-v install ubtuntu cloud image
# usage
# ./exsi.sh 192.168.31.31 192.168.50.31
# apt install cloud-utils
hostname=$(echo $1 | sed s/\\./-/g)
{ echo instance-id: $hostname; echo local-hostname: $hostname; } > meta-data
cat > user-data <<EOF
#cloud-config
users:
- default
- name: jamlee
homedir: /home/jamlee
sudo: ALL=(ALL) NOPASSWD:ALL
groups: users, admin
lock_passwd: false
shell: /bin/bash
passwd: \$6\$yO/Yf/zQbbIXlaHN\$cA/i6a6.Cp7cnHl9HEhSPyVLtsitWs3oe/2NARVCKAn54LU2kT92/vqzpeSP3N87SGFkSHGBe7uQxshZXTbL./
ssh_authorized_keys:
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC6/fB4NNy2k+oAHA4Q4pOUCdH997mQq9BtxoOx99MdS+cRKYITVlN1VaLW+beYgTtTYLRdrJhgPBCk9BzriUEXhFO1D6cOslkCxHsQO5M8FPJEU+I/OSRvpU2QYnhYwP9RRKs1XjwdJ2sg924xYLHdfqcRazRFdLGmKnmz8lLrhz0HrBaBIG8Qm58YpfSrkEQ6eAs+1Xf/1VlCTN4sKq2lwLYGFv8GkMOPRndhiEEc5HTZPfGtOp928xJR63WxUoWn2deDGQxU+Z3wGlZ4Ag0SHGM6uZkaRD+LYZHj+m7J979SJ2uqiLC4YCMjoQF5yqUgsu6bzvVh5TesrW2FZ1PrdWjxZGkmS7gv5PJnAdJ4xKLuuG9Bq5QZmWSTPMaKo6Z23HbAbmBj1stUfNbF7apt+GPiGjR4yZk5+tNCQ2n5fRCufNdsFyeBIXnx6MMugbKq2O70F365fyVNR0otleISpmnpnPlfG+n/+rhdf6w5+b0SYaJ3bBYMdXCrTZ8lj68= root@DESKTOP-SBBNAKK
system_info:
default_user:
name: ubuntu
home: /home/ubuntu
password: ubuntu
hostname: ${hostname}
chpasswd:
expire: false
ssh_pwauth: yes
runcmd:
- [ sh, -xc, "echo Here is the network config for your instance" ]
- [ ip, a ]
final_message: "Cloud init is done. Woohoo!"
EOF
cat > network-config <<EOF
version: 2
ethernets:
eth0:
dhcp4: false
gateway4: 192.168.31.1
addresses: ["$1/24"]
eth1:
dhcp4: false
addresses: ["$2/24"]
EOF
cloud-localds --network-config=network-config ./$1.iso user-data meta-data
rm -f network-config user-data meta-data
# download focal-server-cloudimg-amd64.img from offcial web.
qemu-img create -f qcow2 -F qcow2 -b focal-server-cloudimg-amd64.img newdisk.qcow2 100G
qemu-img convert -p -f qcow2 -O vpc $(pwd)/newdisk.qcow2 $(pwd)/newdisk.vhd
# create vm
1. create vm on hyper-v
2. select the disk newdisk.vhd
3. select the iso cidata.iso
# usage
# powershell.exe Start-Process -Verb runas -FilePath wsl
# ./hyper-v.sh tmpl 172.19.0.100
mkdir -p ../$1
hostname=$(echo $2 | sed s/\\./-/g)
{ echo instance-id: $hostname; echo local-hostname: $hostname; } > meta-data
cat > user-data <<EOF
#cloud-config
users:
- default
- name: jamlee
homedir: /home/jamlee
sudo: ALL=(ALL) NOPASSWD:ALL
groups: users, admin
lock_passwd: false
shell: /bin/bash
passwd: \$6\$yO/Yf/zQbbIXlaHN\$cA/i6a6.Cp7cnHl9HEhSPyVLtsitWs3oe/2NARVCKAn54LU2kT92/vqzpeSP3N87SGFkSHGBe7uQxshZXTbL./
ssh_authorized_keys:
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC6/fB4NNy2k+oAHA4Q4pOUCdH997mQq9BtxoOx99MdS+cRKYITVlN1VaLW+beYgTtTYLRdrJhgPBCk9BzriUEXhFO1D6cOslkCxHsQO5M8FPJEU+I/OSRvpU2QYnhYwP9RRKs1XjwdJ2sg924xYLHdfqcRazRFdLGmKnmz8lLrhz0HrBaBIG8Qm58YpfSrkEQ6eAs+1Xf/1VlCTN4sKq2lwLYGFv8GkMOPRndhiEEc5HTZPfGtOp928xJR63WxUoWn2deDGQxU+Z3wGlZ4Ag0SHGM6uZkaRD+LYZHj+m7J979SJ2uqiLC4YCMjoQF5yqUgsu6bzvVh5TesrW2FZ1PrdWjxZGkmS7gv5PJnAdJ4xKLuuG9Bq5QZmWSTPMaKo6Z23HbAbmBj1stUfNbF7apt+GPiGjR4yZk5+tNCQ2n5fRCufNdsFyeBIXnx6MMugbKq2O70F365fyVNR0otleISpmnpnPlfG+n/+rhdf6w5+b0SYaJ3bBYMdXCrTZ8lj68= root@DESKTOP-SBBNAKK
system_info:
default_user:
name: ubuntu
home: /home/ubuntu
password: ubuntu
hostname: ${hostname}
chpasswd:
expire: false
ssh_pwauth: yes
runcmd:
- [ sh, -xc, "echo Here is the network config for your instance" ]
- [ ip, a ]
final_message: "Cloud init is done. Woohoo!"
EOF
cat > network-config-v2 <<EOF
version: 2
ethernets:
eth0:
dhcp4: true
eth1:
dhcp4: false
addresses: ["$2/24"]
EOF
cloud-localds --network-config=network-config-v2 ./cidata.iso user-data meta-data
cp newdisk.vhd ./cidata.iso ../$1
rm -f network-config-v2 user-data meta-data ./cidata.iso
cat > createvm.ps1 <<'EOF'
param (
[string]$name = "defaultserver"
)
write-output $server
$Manufacturer = (Get-WMIObject win32_Processor).Manufacturer
$WindowsVersion = ([environment]::OSVersion.Version).Major
$BuildNumber = ([environment]::OSVersion.Version).Build
$SwitchName = "WanNet"
if ($Manufacturer -eq "GenuineIntel") {
if ($WindowsVersion -eq 10 -and $BuildNumber -lt 14393) {
Write-Error "Hyper-V with nested virtualization is only supported on Windows 10 Anniversary Update (build 10.0.14393) or later" -ErrorAction Stop
}
New-VM -Name $name -Generation 1 -MemoryStartupBytes 2GB -SwitchName $SwitchName
}
ElseIf ($Manufacturer -eq "AuthenticAMD") {
if ($WindowsVersion -eq 10 -and $BuildNumber -lt 19640) {
Write-Error "Windows 10 (build 10.0.19640) or later is required by Hyper-V to support nested virtualization with AMD processors" -ErrorAction Stop
}
New-VM -Name $name -Generation 1 -Version 9.3 -MemoryStartupBytes 4GB -SwitchName $SwitchName
}
Else {
Write-Error "Hyper-V with nested virtualization does not support $Manufacturer processors" -ErrorAction Stop
}
Add-VMDvdDrive -VMName $name -Path ..\$name\"cidata.iso"
Add-VMHardDiskDrive -VMName $name -Path ..\$name\"newdisk.vhd"
Set-VMProcessor -VMName $name -Count 4 -ExposeVirtualizationExtensions $true
Set-VMNetworkAdapter -VMName $name -MacAddressSpoofing On
Add-VMNetworkAdapter -VMName $name -SwitchName "172.19.0.x"
Add-VMNetworkAdapter -VMName $name -SwitchName "172.19.1.x"
EOF
powershell.exe -File createvm.ps1 -name $1
brew install cdrtools qemu
mkisofs -output cidata.iso -volid cidata -joliet -rock {user-data,meta-data,network-config}
#!/bin/bash
# usage
# ./clone.sh ubuntu20.04-cloud-001 172.19.0.12
vbox="/Users/tencent/vbox"
mkdir -p ${vbox}/${1}
hostname=$(echo $2 | sed s/\\./-/g)
{ echo instance-id: $hostname; echo local-hostname: $hostname; } > meta-data
cat >user-data <<EOF
#cloud-config
users:
- default
- name: jamlee
homedir: /home/jamlee
sudo: ALL=(ALL) NOPASSWD:ALL
groups: users, admin
lock_passwd: false
shell: /bin/bash
passwd: \$6\$yO/Yf/zQbbIXlaHN\$cA/i6a6.Cp7cnHl9HEhSPyVLtsitWs3oe/2NARVCKAn54LU2kT92/vqzpeSP3N87SGFkSHGBe7uQxshZXTbL./
ssh_authorized_keys:
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC6/fB4NNy2k+oAHA4Q4pOUCdH997mQq9BtxoOx99MdS+cRKYITVlN1VaLW+beYgTtTYLRdrJhgPBCk9BzriUEXhFO1D6cOslkCxHsQO5M8FPJEU+I/OSRvpU2QYnhYwP9RRKs1XjwdJ2sg924xYLHdfqcRazRFdLGmKnmz8lLrhz0HrBaBIG8Qm58YpfSrkEQ6eAs+1Xf/1VlCTN4sKq2lwLYGFv8GkMOPRndhiEEc5HTZPfGtOp928xJR63WxUoWn2deDGQxU+Z3wGlZ4Ag0SHGM6uZkaRD+LYZHj+m7J979SJ2uqiLC4YCMjoQF5yqUgsu6bzvVh5TesrW2FZ1PrdWjxZGkmS7gv5PJnAdJ4xKLuuG9Bq5QZmWSTPMaKo6Z23HbAbmBj1stUfNbF7apt+GPiGjR4yZk5+tNCQ2n5fRCufNdsFyeBIXnx6MMugbKq2O70F365fyVNR0otleISpmnpnPlfG+n/+rhdf6w5+b0SYaJ3bBYMdXCrTZ8lj68= root@DESKTOP-SBBNAKK
system_info:
default_user:
name: ubuntu
home: /home/ubuntu
password: ubuntu
hostname: ${hostname}
chpasswd:
expire: false
ssh_pwauth: yes
runcmd:
- [ sh, -xc, "echo Here is the network config for your instance" ]
- [ ip, a ]
final_message: "Cloud init is done. Woohoo!"
EOF
cat > network-config <<EOF
version: 2
ethernets:
eth0:
dhcp4: true
eth1:
dhcp4: false
addresses: ["$2/24"]
EOF
mkisofs -output cidata.iso -volid cidata -joliet -rock {user-data,meta-data,network-config}
cp focal-server-cloudimg-amd64-100G.vmdk cidata.iso "${vbox}/${1}"
rm -f {user-data,meta-data,network-config,cidata.iso}
vmname=$1
basedir=$vbox
VBoxManage createvm --name $vmname --ostype '[Os Type, ex: "Debian_64"]' --register --basefolder $basedir
VBoxManage modifyvm $vmname --ioapic on --cpus 4
VBoxManage modifyvm $vmname --memory 2048 --vram 128
VBoxManage modifyvm $vmname --nic1 nat --nictype1 virtio
VBoxManage modifyvm $vmname --nic2 hostonly --nictype2 virtio --hostonlyadapter2 vboxnet0
VBoxManage storagectl $vmname --name "SATA Controller" --add sata --controller IntelAhci
VBoxManage storageattach $vmname --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium $basedir/$vmname/focal-server-cloudimg-amd64-100G.vmdk
VBoxManage storagectl $vmname --name "IDE Controller" --add ide --controller PIIX4
VBoxManage storageattach $vmname --storagectl "IDE Controller" --port 1 --device 0 --type dvddrive --medium $basedir/$vmname/cidata.iso
VBoxManage modifyvm $vmname --boot1 dvd --boot2 disk --boot3 none --boot4 none
# VBoxManage startvm $vmname
# https://superuser.com/questions/135498/run-virtualbox-in-background-without-a-window
VBoxManage startvm ubuntu-cloud-001 --type headless
# usage
# ./clone-newvm.sh ubuntu20.04-cloud-001 172.19.0.12
vmfusion="/Users/tencent/vmfusion"
mkdir -p ${vmfusion}/${1}
hostname=$(echo $2 | sed s/\\./-/g)
{ echo instance-id: $hostname; echo local-hostname: $hostname; } > meta-data
cat >user-data <<EOF
#cloud-config
users:
- default
- name: jamlee
homedir: /home/jamlee
sudo: ALL=(ALL) NOPASSWD:ALL
groups: users, admin
lock_passwd: false
shell: /bin/bash
passwd: \$6\$yO/Yf/zQbbIXlaHN\$cA/i6a6.Cp7cnHl9HEhSPyVLtsitWs3oe/2NARVCKAn54LU2kT92/vqzpeSP3N87SGFkSHGBe7uQxshZXTbL./
ssh_authorized_keys:
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC6/fB4NNy2k+oAHA4Q4pOUCdH997mQq9BtxoOx99MdS+cRKYITVlN1VaLW+beYgTtTYLRdrJhgPBCk9BzriUEXhFO1D6cOslkCxHsQO5M8FPJEU+I/OSRvpU2QYnhYwP9RRKs1XjwdJ2sg924xYLHdfqcRazRFdLGmKnmz8lLrhz0HrBaBIG8Qm58YpfSrkEQ6eAs+1Xf/1VlCTN4sKq2lwLYGFv8GkMOPRndhiEEc5HTZPfGtOp928xJR63WxUoWn2deDGQxU+Z3wGlZ4Ag0SHGM6uZkaRD+LYZHj+m7J979SJ2uqiLC4YCMjoQF5yqUgsu6bzvVh5TesrW2FZ1PrdWjxZGkmS7gv5PJnAdJ4xKLuuG9Bq5QZmWSTPMaKo6Z23HbAbmBj1stUfNbF7apt+GPiGjR4yZk5+tNCQ2n5fRCufNdsFyeBIXnx6MMugbKq2O70F365fyVNR0otleISpmnpnPlfG+n/+rhdf6w5+b0SYaJ3bBYMdXCrTZ8lj68= root@DESKTOP-SBBNAKK
system_info:
default_user:
name: ubuntu
home: /home/ubuntu
password: ubuntu
hostname: ${hostname}
chpasswd:
expire: false
ssh_pwauth: yes
runcmd:
- [ sh, -xc, "echo Here is the network config for your instance" ]
- [ ip, a ]
final_message: "Cloud init is done. Woohoo!"
EOF
cat > network-config <<EOF
version: 2
ethernets:
eth0:
dhcp4: true
eth1:
dhcp4: false
addresses: ["$2/24"]
EOF
cat > ubuntu.vmx <<EOF
.encoding = "UTF-8"
config.version = "8"
virtualHW.version = "18"
mks.enable3d = "TRUE"
pciBridge0.present = "TRUE"
pciBridge4.present = "TRUE"
pciBridge4.virtualDev = "pcieRootPort"
pciBridge4.functions = "8"
pciBridge5.present = "TRUE"
pciBridge5.virtualDev = "pcieRootPort"
pciBridge5.functions = "8"
pciBridge6.present = "TRUE"
pciBridge6.virtualDev = "pcieRootPort"
pciBridge6.functions = "8"
pciBridge7.present = "TRUE"
pciBridge7.virtualDev = "pcieRootPort"
pciBridge7.functions = "8"
vmci0.present = "TRUE"
hpet0.present = "TRUE"
nvram = "ubuntu.nvram"
virtualHW.productCompatibility = "hosted"
powerType.powerOff = "soft"
powerType.powerOn = "soft"
powerType.suspend = "soft"
powerType.reset = "soft"
displayName = "ubuntu-${hostname}"
usb.vbluetooth.startConnected = "TRUE"
guestOS = "ubuntu-64"
tools.syncTime = "TRUE"
tools.upgrade.policy = "upgradeAtPowerCycle"
sound.autoDetect = "TRUE"
sound.fileName = "-1"
sound.present = "TRUE"
numvcpus = "2"
vcpu.hotadd = "TRUE"
memsize = "4096"
mem.hotadd = "TRUE"
scsi0.virtualDev = "lsilogic"
scsi0.present = "TRUE"
sata0.present = "TRUE"
scsi0:0.fileName = "focal-server-cloudimg-amd64-100G.vmdk"
scsi0:0.present = "TRUE"
sata0:1.deviceType = "cdrom-image"
sata0:1.fileName = "cidata.iso"
sata0:1.present = "TRUE"
usb.present = "TRUE"
ehci.present = "TRUE"
svga.graphicsMemoryKB = "8388608"
ethernet0.connectionType = "nat"
ethernet0.addressType = "generated"
ethernet0.virtualDev = "e1000"
ethernet0.linkStatePropagation.enable = "TRUE"
serial0.fileType = "thinprint"
serial0.fileName = "thinprint"
ethernet0.present = "TRUE"
serial0.present = "TRUE"
#extendedConfigFile = "Ubuntu 64 位.vmxf"
floppy0.present = "FALSE"
ehci:0.parent = "-1"
ehci:0.port = "0"
ehci:0.deviceType = "video"
ehci:0.present = "TRUE"
ethernet1.connectionType = "hostonly"
ethernet1.addressType = "generated"
ethernet1.virtualDev = "e1000"
ethernet1.present = "TRUE"
EOF
mkisofs -output cidata.iso -volid cidata -joliet -rock {user-data,meta-data,network-config}
cp ubuntu.vmx focal-server-cloudimg-amd64-100G.vmdk cidata.iso "${vmfusion}/${1}"
rm -f {ubuntu.vmx,user-data,meta-data,network-config,cidata.iso}
# 启动虚拟机
vmrun start "${vmfusion}/${1}/ubuntu.vmx" gui
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment