Sunday, September 29, 2024

Thêm thiết bị vào EVE siêu nhanh

-

Hôm nay mình sẽ chia sẻ cho các bạn cách thêm thiết bị vào EVE cực nhanh do mình viết bằng Python. Như bạn có thể thấy hiện tại mình chưa có thiết bị nào cả nên toàn bộ Devices của mình nó đang có màu xám và không thể sử dụng được.

  • Nếu trường hợp bạn đặt nfs = False thì hệ thống sẽ bỏ qua việc config NFS, việc của bạn bây giờ là copy toàn bộ Images của các thiết bị vào Server EVE và các bạn lưu ý đường dẫn bắt buộc phải là /eve-image/bin (đối với Cisco Cisco IOL) và /eve-image/qemu (đối với các Images khác).
  • Nếu các Images của bạn nằm ở Storage NFS bạn có thể cài đặt NFS = True để hệ thống tự cấu hình NFS Client và Mount thư mục cho bạn.
    • Hãy nhớ khai báo thư mục share tại nfs_folder_share và IP của NFS Server tại nfs_server.
    • Hãy nhớ các Images tại NFS Storage phải nằm trong theo định dạng thư mục /<thư mục cha>/eve-image, ví dụ /volumes-hdd/eve-image.

Nếu bạn không muốn tuân thủ đường dẫn này bạn có thể sửa lại trong code của mình (tham khảo câu thư mục của mình ở dưới).

/eve-image
├── bin
│   ├── i86bi_linux-adventerprisek9-ms.156-3.M3a.bin
│   ├── i86bi_LinuxL2-AdvEnterpriseK9-M_152_May_2018.bin
│   ├── i86bi_linux_l2-adventerprisek9-ms.SSA.high_iron_20190423.bin
│   ├── i86bi_LinuxL3-AdvEnterpriseK9-M2_157_3_May_2018.bin
│   └── L3-ADVENTERPRISEK9-M-15.4-2T.bin
└── qemu
    ├── arubacx-10.07
    │   └── virtioa.qcow2
    ├── aruba-VMC-8.8.0.1_80393
    │   ├── hda.qcow2
    │   ├── hdb.qcow2
    ├── asav-952-204
    │   └── virtioa.qcow2
    ├── asav-981
    │   └── virtioa.qcow2
    ├── c8000v-17.07.01a
    │   └── virtioa.qcow2

Bạn cũng có thể cài nfs-kernel-server vào EVE để kiểm tra các thư mục NFS Server đã share.

apt install nfs-kernel-server -y

Sau đó dùng lệnh showmount -e <NFS_Server_IP> để xem các thư mục mà NFS đã share mà bạn có thể mount từ NFS Server.

Ví dụ dưới đây là kiếm tra các thư mục mà NFS Server đã share, ở bài này chúng ta sử dụng thư mục share /volumes-hdd/eve-image.

$ showmount -e 192.168.13.228
Export list for 192.168.13.228:
/volumes-hdd/eve-image       *
/zfs-draid2/k8s-data/delete *
/zfs-draid2/k8s-data/retain *

Sau khi xác định được sử dụng NFS Share và thư mục share /volumes-hdd/eve-image thì chúng ta tạo file build-image-eve.py và dán nội dung dưới vào.

#!/usr/bin/env python3
import os, subprocess, socket, struct, hashlib

nfs = True
nfs_server = '192.168.13.228'
nfs_folder_share = '/volumes-hdd/eve-image'

class file_option:
    def file_read(file_name):
        with open(file_name) as rf:
            value = rf.read()
            return value

    def file_write(file_name,value):
       with open(file_name, 'w+') as wf:
            value = wf.writelines(value)

    def val_add_to_file(file_name,value):
       with open(file_name, 'a') as wf:
            value = wf.writelines(value)

    def file_remove(file_name):
        if os.path.exists(file_name):
          os.remove(file_name)

def hostfile_config():
    check_hostfile = file_option.file_read('/etc/hosts')
    if 'xml.cisco.com' not in check_hostfile:
        file_option.val_add_to_file('/etc/hosts','127.0.0.127 xml.cisco.com')

def nfs_storage_config():
    try:
        subprocess.check_output('df -hT | grep -c eve-image', shell=True)
    except:
        if nfs == True:
            os.system('apt update && apt install nfs-common -y')
            read_fstab = file_option.file_read('/etc/fstab')
            mount_nfs_config = '%s:%s/eve-image nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0' %(nfs_server,nfs_folder_share)

            if mount_nfs_config not in read_fstab:
                os.system("""mkdir -p /eve-image && echo '%s:%s /eve-image nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0' >> /etc/fstab; mount -a""" %(nfs_server,nfs_folder_share))
            os.system('rm -rf /opt/unetlab/addons/qemu && rm -rf /opt/unetlab/addons/iol/bin && ln -s /eve-image/qemu /opt/unetlab/addons/; ln -s /eve-image/bin /opt/unetlab/addons/iol/')

def CiscoIOUKeygen():
    hostid=os.popen("hostid").read().strip()
    hostname = socket.gethostname()
    ioukey=int(hostid,16)
    for x in hostname:
        ioukey = ioukey + ord(x)
    iouPad1 = b'\x4B\x58\x21\x81\x56\x7B\x0D\xF3\x21\x43\x9B\x7E\xAC\x1D\xE6\x8A'
    iouPad2 = b'\x80' + 39*b'\0'
    md5input=iouPad1 + iouPad2 + struct.pack('!i', ioukey) + iouPad1
    iouLicense=hashlib.md5(md5input).hexdigest()[:16]
    license = ("[license]\n" + hostname + " = " + iouLicense + ";")
    return license

def qemu_devices():
    os.system('clear')
    print('- The process of adding devices to EVE is starting.')
    parent_folder = "/opt/unetlab/addons/qemu/"
    os_type = 'fortinet\|nxos\|titanium\|linux\|mikrotik\|c8000v\|veos\|timos\|pfsense\|sonicwall\|vyos\|zabbix\|aruba\|asav\|clearpass\|winserver\|nsvpx'
    fortigate_listfolder = subprocess.check_output('ls %s | grep "%s"' %(parent_folder,os_type), shell=True).decode("utf-8").strip('\n').split('\n')
    for fortigate_version in fortigate_listfolder:
        folder_full_path = '%s%s' %(parent_folder,fortigate_version)
        list_os_filename = subprocess.check_output('ls %s | grep ".qcow2\|.img"' %(folder_full_path), shell=True).decode("utf-8").strip('\n').split('\n')
        for os_filename in list_os_filename:
            file_full_path = '%s/%s' %(folder_full_path,os_filename)
            os.system('chmod +x %s' %(file_full_path))
            file_permission = '''<?xml version="1.0" encoding="UTF-8"?>\n<permissions>\n  <permission id="0">\n    <file>%s</file>\n  </permission>\n</permissions>''' %(file_full_path)
            file_option.file_write('%s/%s' %(folder_full_path,'permissions.xml'),file_permission)
            print('  + %s' %(file_full_path))
    print('- The process of adding QEMU device is successful.')

def cisco_devices():
    file_option.file_write('/opt/unetlab/addons/iol/bin/iourc',CiscoIOUKeygen())
    print('- The process of adding IOL device is successful.')

nfs_storage_config() 
qemu_devices()
cisco_devices()
os.system('/opt/unetlab/wrappers/unl_wrapper -a fixpermissions >/dev/null 2>&1')

Kiểm tra thư mục mount bằng lênh df -h bạn thấy chưa có thư mục nào được mount.

$ df -h | grep 'eve-image'
< không có kết quả >

Vì file image mình lưu tại nfs nên mình sẽ sử dụng nfs nhé.

Sau khi thiết lập xong, hãy chạy lệnh python3 build-image-eve.py để quá trình build bắt đầu.

root@eve-ng:/home# python3 build-image-eve.py 
Get:1 http://www.eve-ng.net/focal focal InRelease [1,450 B]
Get:2 http://lv.archive.ubuntu.com/ubuntu focal InRelease [265 kB]                         
Get:3 http://www.eve-ng.net/focal focal/main amd64 Packages [11.5 kB]
Get:4 http://lv.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:5 http://lv.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:6 http://lv.archive.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:7 http://lv.archive.ubuntu.com/ubuntu focal/main amd64 Packages [970 kB]
Get:8 http://lv.archive.ubuntu.com/ubuntu focal/main Translation-en [506 kB]
Get:9 http://lv.archive.ubuntu.com/ubuntu focal/main amd64 c-n-f Metadata [29.5 kB]
Get:10 http://lv.archive.ubuntu.com/ubuntu focal/restricted amd64 Packages [22.0 kB]
Get:11 http://lv.archive.ubuntu.com/ubuntu focal/restricted Translation-en [6,212 B]
Get:12 http://lv.archive.ubuntu.com/ubuntu focal/restricted amd64 c-n-f Metadata [392 B]
Get:13 http://lv.archive.ubuntu.com/ubuntu focal/universe amd64 Packages [8,628 kB]
Get:14 http://lv.archive.ubuntu.com/ubuntu focal/universe Translation-en [5,124 kB]                                                                                                                                     
Get:15 http://lv.archive.ubuntu.com/ubuntu focal/universe amd64 c-n-f Metadata [265 kB]                                                                                                                                 
Get:16 http://lv.archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [144 kB]                                                                                                                                     
Get:17 http://lv.archive.ubuntu.com/ubuntu focal/multiverse Translation-en [104 kB] 
Get:49 http://lv.archive.ubuntu.com/ubuntu focal-security/multiverse Translation-en [5,488 B]                                                                                                                           
Get:50 http://lv.archive.ubuntu.com/ubuntu focal-security/multiverse amd64 c-n-f Metadata [528 B]                                                                                                                       
Fetched 27.6 MB in 20s (1,395 kB/s)                                                                                                                                                                                     
Reading package lists... Done
Building dependency tree       
Reading state information... Done
All packages are up to date.
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  keyutils libnfsidmap2 libtirpc-common libtirpc3 rpcbind
Suggested packages:
  watchdog
The following NEW packages will be installed:
  keyutils libnfsidmap2 libtirpc-common libtirpc3 nfs-common rpcbind
0 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
Need to get 405 kB of archives.
After this operation, 1,519 kB of additional disk space will be used.
Get:1 http://lv.archive.ubuntu.com/ubuntu focal-updates/main amd64 libtirpc-common all 1.2.5-1ubuntu0.1 [7,712 B]
Get:2 http://lv.archive.ubuntu.com/ubuntu focal-updates/main amd64 libtirpc3 amd64 1.2.5-1ubuntu0.1 [77.9 kB]
Get:3 http://lv.archive.ubuntu.com/ubuntu focal/main amd64 rpcbind amd64 1.2.5-8 [42.8 kB]
Get:4 http://lv.archive.ubuntu.com/ubuntu focal-updates/main amd64 keyutils amd64 1.6-6ubuntu1.1 [44.8 kB]
Get:5 http://lv.archive.ubuntu.com/ubuntu focal/main amd64 libnfsidmap2 amd64 0.25-5.1ubuntu1 [27.9 kB]
Get:6 http://lv.archive.ubuntu.com/ubuntu focal-updates/main amd64 nfs-common amd64 1:1.3.4-2.5ubuntu3.4 [204 kB]
Fetched 405 kB in 2s (165 kB/s) 
Selecting previously unselected package libtirpc-common.
(Reading database ... 117555 files and directories currently installed.)
Preparing to unpack .../0-libtirpc-common_1.2.5-1ubuntu0.1_all.deb ...
Unpacking libtirpc-common (1.2.5-1ubuntu0.1) ...
Selecting previously unselected package libtirpc3:amd64.
Preparing to unpack .../1-libtirpc3_1.2.5-1ubuntu0.1_amd64.deb ...
Unpacking libtirpc3:amd64 (1.2.5-1ubuntu0.1) ...
Selecting previously unselected package rpcbind.
Preparing to unpack .../2-rpcbind_1.2.5-8_amd64.deb ...
Unpacking rpcbind (1.2.5-8) ...
Selecting previously unselected package keyutils.
Preparing to unpack .../3-keyutils_1.6-6ubuntu1.1_amd64.deb ...
Unpacking keyutils (1.6-6ubuntu1.1) ...
Selecting previously unselected package libnfsidmap2:amd64.
Preparing to unpack .../4-libnfsidmap2_0.25-5.1ubuntu1_amd64.deb ...
Unpacking libnfsidmap2:amd64 (0.25-5.1ubuntu1) ...
Selecting previously unselected package nfs-common.
Preparing to unpack .../5-nfs-common_1%3a1.3.4-2.5ubuntu3.4_amd64.deb ...
Unpacking nfs-common (1:1.3.4-2.5ubuntu3.4) ...
Setting up libtirpc-common (1.2.5-1ubuntu0.1) ...
Setting up keyutils (1.6-6ubuntu1.1) ...
Setting up libnfsidmap2:amd64 (0.25-5.1ubuntu1) ...
Setting up libtirpc3:amd64 (1.2.5-1ubuntu0.1) ...
Setting up rpcbind (1.2.5-8) ...
Created symlink /etc/systemd/system/multi-user.target.wants/rpcbind.service → /lib/systemd/system/rpcbind.service.
Created symlink /etc/systemd/system/sockets.target.wants/rpcbind.socket → /lib/systemd/system/rpcbind.socket.
Setting up nfs-common (1:1.3.4-2.5ubuntu3.4) ...

Creating config file /etc/idmapd.conf with new version
Adding system user `statd' (UID 118) ...
Adding new user `statd' (UID 118) with group `nogroup' ...
Not creating home directory `/var/lib/nfs'.
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-client.target → /lib/systemd/system/nfs-client.target.
Created symlink /etc/systemd/system/remote-fs.target.wants/nfs-client.target → /lib/systemd/system/nfs-client.target.
nfs-utils.service is a disabled or a static unit, not starting it.
Processing triggers for systemd (245.4-4ubuntu3.19) ...
Processing triggers for man-db (2.9.1-1) ...

Progress: [ 100%] [################################################################################################################################################################################################]        
- The process of adding devices to EVE is starting.
  + /opt/unetlab/addons/qemu/arubacx-10.07/virtioa.qcow2
  + /opt/unetlab/addons/qemu/aruba-VMC-8.8.0.1_80393/hda.qcow2
  + /opt/unetlab/addons/qemu/aruba-VMC-8.8.0.1_80393/hdb.qcow2
  + /opt/unetlab/addons/qemu/asav-941/virtioa.qcow2
  + /opt/unetlab/addons/qemu/asav-952-204/virtioa.qcow2
  + /opt/unetlab/addons/qemu/asav-981/virtioa.qcow2
  + /opt/unetlab/addons/qemu/c8000v-17.07.01a/virtioa.qcow2
  + /opt/unetlab/addons/qemu/clearpass-6.4.0.66263/hda.qcow2
  + /opt/unetlab/addons/qemu/clearpass-6.4.0.66263/hdb.qcow2
  + /opt/unetlab/addons/qemu/fortinet-FortiAnalyzer/virtioa.qcow2
  + /opt/unetlab/addons/qemu/fortinet-fortigate5.6/virtioa.qcow2
  + /opt/unetlab/addons/qemu/fortinet-fortigate6.2.1/virtioa.qcow2
  + /opt/unetlab/addons/qemu/fortinet-fortigate6.4.0/virtioa.qcow2
  + /opt/unetlab/addons/qemu/fortinet-fortigate6.4.5/virtioa.qcow2
  + /opt/unetlab/addons/qemu/fortinet-fortigate7.0/virtioa.qcow2
  + /opt/unetlab/addons/qemu/fortinet-fortigate7.2.0/fortios.qcow2
  + /opt/unetlab/addons/qemu/fortinet-FortiNAC/virtioa.qcow2
  + /opt/unetlab/addons/qemu/linux-centos7.3/hda.qcow2
  + /opt/unetlab/addons/qemu/linux-ubuntu-server-16.04.02/hda.qcow2
  + /opt/unetlab/addons/qemu/mikrotik-6.39/hda.qcow2
  + /opt/unetlab/addons/qemu/mikrotik-6.47.1/hda.qcow2
  + /opt/unetlab/addons/qemu/mikrotik-7.1.1/chr-7.1.1.img
  + /opt/unetlab/addons/qemu/nsvpx-10.5.54.9009/virtioa.qcow2
  + /opt/unetlab/addons/qemu/nxosv9k-7.0.3/sataa.qcow2
  + /opt/unetlab/addons/qemu/nxosv9k-9.3.1/sataa.qcow2
  + /opt/unetlab/addons/qemu/pfsense-2.3.3/hda.qcow2
  + /opt/unetlab/addons/qemu/pfsense-CE-2.6/virtioa.qcow2
  + /opt/unetlab/addons/qemu/sonicwall-11.3.0-319/hda.qcow2
  + /opt/unetlab/addons/qemu/timos-12.0.R6/hda.qcow2
  + /opt/unetlab/addons/qemu/titanium-NX-OSv.7.3.0.D1.1/hda.qcow2
  + /opt/unetlab/addons/qemu/veos-4.14.0F/hda.qcow2
  + /opt/unetlab/addons/qemu/vyos-117/hda.qcow2
  + /opt/unetlab/addons/qemu/winserver-S2012-R2-x64/virtioa.qcow2
  + /opt/unetlab/addons/qemu/zabbix-3.0/hda.qcow2
- The process of adding QEMU device is successful.
- The process of adding IOL device is successful.

Nếu thấy xuất hiện 2 thông báo như dưới tức là bạn đã build thành công.

- The process of adding QEMU device is successful.
- The process of adding IOL device is successful.

Hãy thử kiểm tra thư mục mount trước bạn sẽ thấy có 1 thư mục mount /eve-image từ nfs server 192.168.13.228:/volumes-hdd/eve-image.

$ df -h | grep 'eve-image'
192.168.13.228:/volumes-hdd/eve-image  3.6T  114G  3.3T   4% /eve-image

Trong thư mục mount /eve-image có 2 thư mục bin và qemu như mình đã nói ở trên.

root@eve-ng:/home# ls -al /eve-image
total 16
drwxr-xr-x  4 root root 4096 Feb 24 15:19 .
drwxr-xr-x 20 root root 4096 Feb 24 16:55 ..
drwxr-xr-x  2 root root 4096 Feb 24 12:07 bin
drwxr-xr-x 36 root root 4096 Feb 24 13:02 qemu

Bây giờ bạn vào EVE bạn sẽ thấy có thiết bị đã chuyển sang màu xanh đậm, những thiết bị nào đang có màu xám là do mình chưa bỏ file image vào cho nó.

Lần sau bạn chỉ cần download file image và ném vào đúng thư mục này sau đó chạy lại lệnh python3 build-image-eve.py là nó sẽ làm lại tất cả cho bạn.

Chúc các bạn thành công.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

4,956FansLike
256FollowersFollow
223SubscribersSubscribe
spot_img

Related Stories