1. 用脚本来自动创建虚拟机
1. 查看vm6在哪儿
[root@localhost ~]# find / -name centos-7*
/var/lib/tftpboot/images/centos-7-x86_64
/var/lib/cobbler/config/distros.d/centos-7-x86_64.json
/var/lib/cobbler/config/profiles.d/centos-7-x86_64.json
/var/lib/cobbler/kickstarts/centos-7-x86_64.ks
/var/www/cobbler/images/centos-7-x86_64
/var/www/cobbler/ks_mirror/config/centos-7-x86_64.repo
/var/www/cobbler/ks_mirror/centos-7-x86_64
/var/www/cobbler/links/centos-7-x86_64
[root@localhost ~]# cd /var/lib/cobbler/config/
[root@localhost config]# pwd
/var/lib/cobbler/config
[root@localhost config]# ls
distros.d images.d packages.d repos.d
files.d mgmtclasses.d profiles.d systems.d
[root@localhost config]# ls systems.d/
vm6.json
2. 生成一台新虚拟机,获取MAC地址
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/72f800d00b3674ace5b34e3527e4593b.png#pic_center)
3. 写脚本
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# cd /scripts/
[root@localhost scripts]# ls
[root@localhost systems.d]# cat vm6.json 查看vm6,将这些内容复制到脚本里
{"comment": "", "status": "production", "kickstart": "/var/lib/cobbler/kickstarts/centos-7-x86_64.ks", "name_servers_search": [], "ks_meta": {}, "kernel_options_post": {}, "image": "", "redhat_management_key": "<<inherit>>", "virt_path": "<<inherit>>", "power_user": "", "kernel_options": {"biosdevname": "0", "net.ifnames": "0"}, "ctime": 1606202644.640662, "name_servers": ["114.114.114.114"], "mtime": 1606202644.640662, "enable_gpxe": false, "template_files": {}, "gateway": "192.168.50.2", "uid": "MTYwNjIwMjY0NC42NDYzNDIwMDIuNTUzMzI", "virt_auto_boot": 0, "power_type": "ipmitool", "virt_cpus": "<<inherit>>", "mgmt_parameters": "<<inherit>>", "boot_files": {}, "hostname": "maqiang", "repos_enabled": false, "mgmt_classes": [], "power_pass": "", "netboot_enabled": true, "ipv6_autoconfiguration": false, "profile": "centos-7-x86_64", "virt_type": "xenpv", "interfaces": {"eth0": {"ipv6_address": "", "interface_type": "", "static": true, "cnames": [], "mtu": "", "management": false, "interface_master": "", "mac_address": "00:50:56:39:76:A5", "ipv6_prefix": "", "virt_bridge": "xenbr0", "netmask": "255.255.255.0", "bonding_opts": "", "ip_address": "192.168.50.250", "dhcp_tag": "", "static_routes": [], "ipv6_mtu": "", "ipv6_static_routes": [], "if_gateway": "", "dns_name": "", "bridge_opts": "", "connected_mode": false, "ipv6_secondaries": [], "ipv6_default_gateway": ""}}, "power_address": "", "proxy": "<<inherit>>", "fetchable_files": {}, "virt_file_size": "<<inherit>>", "ldap_enabled": false, "monit_enabled": false, "ipv6_default_device": "", "virt_pxe_boot": 0, "virt_disk_driver": "<<inherit>>", "owners": "<<inherit>>", "name": "vm6", "virt_ram": "<<inherit>>", "power_id": "", "server": "<<inherit>>", "redhat_management_server": "<<inherit>>", "depth": 2, "ldap_type": "authconfig", "template_remote_kickstarts": 0}[root@localhost systems.d]#
写脚本
[root@localhost scripts]# vim create_cobbler_system.sh
#!/bin/bash
{
"comment": "",
"status": "production",
"kickstart": "/var/lib/cobbler/kickstarts/centos-7-x86_64.ks",
"name_servers_search": [],
"ks_meta": {},
"kernel_options_post": {},
"image": "",
"redhat_management_key": "<<inherit>>",
"virt_path": "<<inherit>>",
"power_user": "",
"kernel_options": {
"biosdevname": "0",
"net.ifnames": "0"
},
"ctime": 1606202644.640662,
"name_servers": ["114.114.114.114"],
"mtime": 1606202644.640662,
"enable_gpxe": false,
"template_files": {},
"gateway": "192.168.50.2",
"uid": "MTYwNjIwMjY0NC42NDYzNDIwMDIuNTUzMzI",
"virt_auto_boot": 0,
"power_type": "ipmitool",
"virt_cpus": "<<inherit>>",
"mgmt_parameters": "<<inherit>>",
"boot_files": {},
"hostname": "node7.example.com", 将名字改为7
"repos_enabled": false,
"mgmt_classes": [],
"power_pass": "",
"netboot_enabled": true,
"ipv6_autoconfiguration": false,
"profile": "centos-7-x86_64",
"virt_type": "xenpv",
"interfaces": {
"eth0": {
"ipv6_address": "",
"interface_type": "",
"static": true,
"cnames": [],
"mtu": "",
"management": false,
"interface_master": "",
"mac_address": "00:50:56:27:08:EF", 修改MAC地址,就是刚才新创建的虚拟机MAC地址
"ipv6_prefix": "",
"virt_bridge": "xenbr0",
"netmask": "255.255.255.0",
"bonding_opts": "",
"ip_address": "192.168.50.251", 改ip为251,之前是250
"dhcp_tag": "",
"static_routes": [],
"ipv6_mtu": "",
"ipv6_static_routes": [],
"if_gateway": "",
"dns_name": "",
"bridge_opts": "",
"connected_mode": false,
"ipv6_secondaries": [],
"ipv6_default_gateway": ""
}
},
"power_address": "",
"proxy": "<<inherit>>",
"fetchable_files": {},
"virt_file_size": "<<inherit>>",
"ldap_enabled": false,
"monit_enabled": false,
"ipv6_default_device": "",
"virt_pxe_boot": 0,
"virt_disk_driver": "<<inherit>>",
"owners": "<<inherit>>",
"name": "vm7", 将名字改为vm7
"virt_ram": "<<inherit>>",
"power_id": "",
"server": "<<inherit>>",
"redhat_management_server": "<<inherit>>",
"depth": 2,
"ldap_type": "authconfig",
"template_remote_kickstarts": 0
}
EOF
执行脚本
[root@localhost scripts]# bash create_cobbler_system.sh
[root@localhost systems.d]# ls
vm6.json vm7.json
4. 重启服务
[root@localhost systems.d]# systemctl restart cobblerd
[root@localhost systems.d]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 5 127.0.0.1:25151 *:*
LISTEN 0 5 *:873 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 5 :::873 :::*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 :::443 :::*
5. 同步cobbler
[root@localhost systems.d]# cobbler sync
task started: 2020-11-24_163608_sync
task started (id=Sync, time=Tue Nov 24 16:36:08 2020)
running pre-sync triggers
cleaning trees
removing: /var/www/cobbler/images/centos-7-x86_64
removing: /var/lib/tftpboot/pxelinux.cfg/01-00-50-56-39-76-a5
removing: /var/lib/tftpboot/pxelinux.cfg/default
removing: /var/lib/tftpboot/grub/images
removing: /var/lib/tftpboot/grub/grub-x86.efi
removing: /var/lib/tftpboot/grub/grub-x86_64.efi
removing: /var/lib/tftpboot/grub/01-00-50-56-39-76-A5
removing: /var/lib/tftpboot/grub/efidefault
removing: /var/lib/tftpboot/images/centos-7-x86_64
removing: /var/lib/tftpboot/s390x/profile_list
copying bootloaders
trying hardlink /var/lib/cobbler/loaders/grub-x86.efi -> /var/lib/tftpboot/grub/grub-x86.efi
trying hardlink /var/lib/cobbler/loaders/grub-x86_64.efi -> /var/lib/tftpboot/grub/grub-x86_64.efi
copying distros to tftpboot
copying files for distro: centos-7-x86_64
trying hardlink /var/www/cobbler/ks_mirror/centos-7-x86_64/images/pxeboot/vmlinuz -> /var/lib/tftpboot/images/centos-7-x86_64/vmlinuz
trying hardlink /var/www/cobbler/ks_mirror/centos-7-x86_64/images/pxeboot/initrd.img -> /var/lib/tftpboot/images/centos-7-x86_64/initrd.img
copying images
generating PXE configuration files
generating: /var/lib/tftpboot/pxelinux.cfg/01-00-50-56-39-76-a5
generating: /var/lib/tftpboot/grub/01-00-50-56-39-76-A5
generating: /var/lib/tftpboot/pxelinux.cfg/01-00-50-56-27-08-ef
generating: /var/lib/tftpboot/grub/01-00-50-56-27-08-EF
generating PXE menu structure
copying files for distro: centos-7-x86_64
trying hardlink /var/www/cobbler/ks_mirror/centos-7-x86_64/images/pxeboot/vmlinuz -> /var/www/cobbler/images/centos-7-x86_64/vmlinuz
trying hardlink /var/www/cobbler/ks_mirror/centos-7-x86_64/images/pxeboot/initrd.img -> /var/www/cobbler/images/centos-7-x86_64/initrd.img
Writing template files for centos-7-x86_64
rendering DHCP files
generating /etc/dhcp/dhcpd.conf
rendering TFTPD files
generating /etc/xinetd.d/tftp
processing boot_files for distro: centos-7-x86_64
cleaning link caches
running post-sync triggers
running python triggers from /var/lib/cobbler/triggers/sync/post/*
running python trigger cobbler.modules.sync_post_restart_services
running: dhcpd -t -q
received on stdout:
received on stderr:
running: service dhcpd restart
received on stdout:
received on stderr: Redirecting to /bin/systemctl restart dhcpd.service
running shell triggers from /var/lib/cobbler/triggers/sync/post/*
running python triggers from /var/lib/cobbler/triggers/change/*
running python trigger cobbler.modules.manage_genders
running python trigger cobbler.modules.scm_track
running shell triggers from /var/lib/cobbler/triggers/change/*
*** TASK COMPLETE ***
6. 重新登陆cobbler_web页面会有vm7生成
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d6a8d574315d3257984199d1200614e9.png#pic_center)
2. 删除vm6
- 删除vm6.json
[root@localhost systems.d]# ls
vm6.json vm7.json
[root@localhost systems.d]# rm -rf vm6.json
[root@localhost systems.d]# ls
vm7.json
重启
[root@localhost systems.d]# systemctl restart cobblerd
同步
[root@localhost systems.d]# cobbler sync
task started: 2020-11-24_165654_sync
task started (id=Sync, time=Tue Nov 24 16:56:54 2020)
running pre-sync triggers
cleaning trees
removing: /var/www/cobbler/images/centos-7-x86_64
removing: /var/lib/tftpboot/pxelinux.cfg/01-00-50-56-39-76-a5
removing: /var/lib/tftpboot/pxelinux.cfg/01-00-50-56-27-08-ef
removing: /var/lib/tftpboot/pxelinux.cfg/default
removing: /var/lib/tftpboot/grub/images
removing: /var/lib/tftpboot/grub/grub-x86.efi
removing: /var/lib/tftpboot/grub/grub-x86_64.efi
removing: /var/lib/tftpboot/grub/01-00-50-56-39-76-A5
removing: /var/lib/tftpboot/grub/01-00-50-56-27-08-EF
removing: /var/lib/tftpboot/grub/efidefault
removing: /var/lib/tftpboot/images/centos-7-x86_64
removing: /var/lib/tftpboot/s390x/profile_list
copying bootloaders
trying hardlink /var/lib/cobbler/loaders/grub-x86.efi -> /var/lib/tftpboot/grub/grub-x86.efi
trying hardlink /var/lib/cobbler/loaders/grub-x86_64.efi -> /var/lib/tftpboot/grub/grub-x86_64.efi
copying distros to tftpboot
copying files for distro: centos-7-x86_64
trying hardlink /var/www/cobbler/ks_mirror/centos-7-x86_64/images/pxeboot/vmlinuz -> /var/lib/tftpboot/images/centos-7-x86_64/vmlinuz
trying hardlink /var/www/cobbler/ks_mirror/centos-7-x86_64/images/pxeboot/initrd.img -> /var/lib/tftpboot/images/centos-7-x86_64/initrd.img
copying images
generating PXE configuration files
generating: /var/lib/tftpboot/pxelinux.cfg/01-00-50-56-27-08-ef
generating: /var/lib/tftpboot/grub/01-00-50-56-27-08-EF
generating PXE menu structure
copying files for distro: centos-7-x86_64
trying hardlink /var/www/cobbler/ks_mirror/centos-7-x86_64/images/pxeboot/vmlinuz -> /var/www/cobbler/images/centos-7-x86_64/vmlinuz
trying hardlink /var/www/cobbler/ks_mirror/centos-7-x86_64/images/pxeboot/initrd.img -> /var/www/cobbler/images/centos-7-x86_64/initrd.img
Writing template files for centos-7-x86_64
rendering DHCP files
generating /etc/dhcp/dhcpd.conf
rendering TFTPD files
generating /etc/xinetd.d/tftp
processing boot_files for distro: centos-7-x86_64
cleaning link caches
running post-sync triggers
running python triggers from /var/lib/cobbler/triggers/sync/post/*
running python trigger cobbler.modules.sync_post_restart_services
running: dhcpd -t -q
received on stdout:
received on stderr:
running: service dhcpd restart
received on stdout:
received on stderr: Redirecting to /bin/systemctl restart dhcpd.service
running shell triggers from /var/lib/cobbler/triggers/sync/post/*
running python triggers from /var/lib/cobbler/triggers/change/*
running python trigger cobbler.modules.manage_genders
running python trigger cobbler.modules.scm_track
running shell triggers from /var/lib/cobbler/triggers/change/*
*** TASK COMPLETE ***
[root@localhost systems.d]#
- 重新登陆cobbler_web查看vm6已被删除
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b7b72e204ecf11c1ef01d2fe171c14d6.png#pic_center)
3. 开启vm7验证
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/126572b6949d737f6c000466c078b589.png#pic_center)