AKAI TSUKI

System development or Technical something

Create a new VM in CentOS 7.x(KVM)

create Hash Password

1) execute below command and input root's Password

[root@pm01 ~]# python -c 'import crypt,getpass; pw=getpass.getpass(); print(crypt.crypt(pw) if (pw==getpass.getpass("Re-Password: ")) else exit())'
Password: <<input password>>
Re-Password: <<re-input password>>
<<can see Hash password>>
[root@pm01 ~]#

kickstart config file

1) creat kickstart config file
2) change "< Hash root's Password >" to actual hash password

[root@pm01 ~]# cat vm03.ks
#version=DEVEL

# Use CDROM installation media
cdrom

# Use text install
text
cmdline
skipx

# # Run the Setup Agent on first boot
# firstboot --enable
ignoredisk --only-use=vda

# Keyboard layouts
keyboard --vckeymap=jp --xlayouts='jp','us'
# System language
lang en_US.UTF-8

# Network information
network  --bootproto=dhcp --device=eth0 --onboot=on --noipv6 --activate
# network  --bootproto=static --device=eth0 --ip=192.168.122.102 --netmask=255.255.255.0 --gateway=192.168.122.1 --nameserver=192.168.122.1 --noipv6 --activate
# network  --bootproto=dhcp --device=eth0 --onboot=on --activate
network  --hostname=vm03.localdomain

# System authorization information
auth --enableshadow --passalgo=sha512
# Root password
rootpw --iscrypted < Hash root's Password >

# # System services
# services --disabled="chronyd"

# System timezone
timezone Asia/Tokyo --isUtc --nontp


# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=vda
autopart --type=lvm
# Partition clearing information
clearpart --all --initlabel --drives=vda

selinux --disabled

%packages
@^minimal
@core
kexec-tools

%end

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

reboot --eject
[root@pm01 ~]#

execute script for virt-install

1) create script file using virt-install

[root@pm01 ~]# cat create_vm03.sh
#!/bin/bash

name=vm03
cpu=1
memory=4096
disk=30

qemu-img create -f qcow2 /var/lib/libvirt/images/${name}.qcow2 ${disk}G

# virt-install --connect=qemu:///system \
virt-install \
 --name=${name} \
 --disk /var/lib/libvirt/images/${name}.qcow2,format=qcow2,bus=virtio \
 --network network=default,model=virtio \
 --initrd-inject=./${name}.ks \
 --extra-args="ks=file:/${name}.ks biosdevname=0 net.ifnames=0 console=tty0 console=ttyS0,115200n8" \
 --vcpus=${cpu} \
 --ram=${memory} \
 --accelerate \
 --hvm \
 --virt-type kvm \
 --location='/var/lib/libvirt/images/CentOS-7-x86_64-Minimal-1908.iso' \
 --nographics \
 --os-type=linux \
 --os-variant=centos7.0 \
 --arch=x86_64

[root@pm01 ~]#

2) start to create a new VM by below command

[root@pm01 ~]# sh create_vm03.sh