Centos|Ubuntu自定义镜像

Centos7自定义镜像

安装自定义镜像工具
  • 安装

    yum -y install createrepo mkisofs openssl rsync syslinux
    
挂载镜像
  • 创建挂载点

    mkdir /mnt/cdrom
    
  • 挂载

    # 上传镜像到/home/baseISO目录下
    mount -o loop /home/baseISO/CentOS-7-x86_64-DVD-2009.iso /mnt/cdrom/
    
  • 同步/mnt/cdrom/下的文件到/home/ISO/

    mkdir /home/ISO
    /usr/bin/rsync -a /mnt/cdrom/ /home/ISO/
    
ks.cfg文件自定义
  • 复制ks.cfg文件

    cp /root/anaconda-ks.cfg /root/ks.cfg
    
  • 密码生成

    # -1: md5加密
    # -salt: 加盐
    # 盐: Wkajs
    # pass: password
    openssl passwd -1 -salt Wkajs password
    
  • 编辑ks.cfg文件

      #  vim /root/ks.cfg
      #version=DEVEL
      # System authorization information
      auth --enableshadow --enablemd5
      # Use CDROM installation media
      cdrom
      # Use graphical install 图形化界面式安装
      # graphical
      # Use text install  文本式安装
      text
      # Run the Setup Agent on first boot
      firstboot --enable
      # 启用会忽略其他硬盘
      ignoredisk --only-use=sda
      # Keyboard layouts
      keyboard --vckeymap=us --xlayouts='us'
      # System language
      lang en_US.UTF-8
      
      # Network information
      network  --bootproto=dhcp --device=eth0  --ipv6=auto --activate
      network  --hostname=localhost.localdomain
      
      # Root password
      rootpw --iscrypted  $1$8q7ORxjl$v8yGB70WIHiLjNcaJRdCh.
      # System services
      services --disabled="chronyd"
      # System timezone
      timezone Asia/Shanghai --isUtc --nontp
      # Partition clearing information
      clearpart --all --initlabel
      
      # System bootloader configuration
      bootloader --append=" crashkernel=512M " --location=mbr  --boot-drive=sda
      # Clear the Master Boot Record
      zerombr
      %addon com_redhat_kdump --enable --reserve-mb='512M'
      %end
      part /boot --asprimary --fstype="ext4" --size=500 --ondisk=sda
      # uefi模式需要
      part /boot/efi --asprimary --fstype="efi" --size=120 --ondisk=sda
      #----------------------以下是创建lvm逻辑卷---------------------
      # 创建第一个物理卷PV分区,序号是01,大小为剩余所有空间
      # --size=1 --grow参数是以剩余所有空间来分区
      part pv.01 --size=1 --grow --ondisk=sda
      # 创建一个逻辑卷组名,名字为virt,包含在物理卷PV.01
      volgroup virt pv.01
      # 创建一个LV逻辑卷,挂载到/  ,从virt组中创建,大小为剩余所有空间,LV的名字是vg_root
      logvol / --fstype="ext4" --vgname=virt --size=1 --grow --name=vg-root
      # 创建第二个物理卷PV分区,序号是02,大小为剩余所有空间
      #part pv.02 --size=1 --grow --ondisk=sdb
      #创建一个逻辑卷组名,名字为data ,包含在物理卷PV.02
      #volgroup vg_data pv.02
      #创建一个LV逻辑卷,挂载到/data  ,从vg_data组中创建,LV的名字是volume-data
      #logvol /data --fstype="ext4" --vgname=vg_data --size=1 --grow --name=volume-data
      
      
      # 最小化安装
      %packages
      @^minimal
      @core
      kexec-tools
      %end
      
      reboot
    
    
ISO文件目录
  • 目录

    # tree /home/ISO/ -d
    /home/ISO/
    ├── EFI
    │   └── BOOT
    │       └── fonts
    ├── images
    │   └── pxeboot
    ├── isolinux
    ├── LiveOS
    ├── Packages
    └── repodata
    
  • 复制ks.cfg到isolinux

    cp /root/ks.cfg /home/ISO/isolinux
    
  • 修改isolinux.cfg文件

    # vim /home/ISO/isolinux/isolinux.cfg
    label linux
      menu label ^Install CentOS 7
      menu default
      kernel vmlinuz
      append initrd=initrd.img  ks=cdrom:/isolinux/ks.cfg net.ifnames=0 biosdevname=0 quiet
    

    describe: net.ifnames=0 biosdevname=0 禁用自动设备命名规则,恢复传统的 ethx 设备名称

生成iso文件
  • 制作iso命令

    # mkisofs制作
    mkisofs \
    -o CentOS-7-9.iso \
    -b isolinux/isolinux.bin \
    -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 \
    -boot-info-table -R -J -v -T /home/ISO
    
    # genisoimage 制作
    genisoimage  \
    -joliet-long \
    -o CentOS-7-9.iso \
    -b isolinux/isolinux.bin \
    -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 \
    -boot-info-table -R -J -v -cache-inodes -T -eltorito-alt-boot \
    -e images/efiboot.img -no-emul-boot /home/ISO
    
  • uefi bios兼容

    mkisofs -o /home/ISO/CentOS-7-9.iso -v -cache-inodes -joliet-long -R -J -T -V centos7  -c isolinux/boot.cat  -b isolinux/isolinux.bin -no-emul-boot -boot-load-size 4 -boot-info-table -eltorito-alt-boot -b images/efiboot.img -no-emul-boot /home/ISO
    
  • uefi模式需修改文件

     # vim EFI/BOOT/grub.cfg
     # 修改内容
     menuentry 'Install CentOS 7' --class fedora --class gnu-linux --class gnu --class os {
          linuxefi /images/pxeboot/vmlinuz ks=cdrom:/isolinux/ks.cfg net.ifnames=0 biosdevname=0   quiet
          initrdefi /images/pxeboot/initrd.img
      }
      #下面内容可注释,只保留当前install选项
      ...
    
  • Hybird模式

    isohybrid -v /home/ISO/CentOS-7-9.iso
    

    notice: isohybrid command not found,需要安装syslinux.命令: yum -y install syslinux

    describe: 采用“hybird模式”(混合模式),操作系统可以直接刻录成物理光盘,也可以直接做成可引导的U盘

ubuntu自定义镜像

安装自定义镜像工具
apt-get install genisoimage
挂载镜像
  • 创建挂载点

    mkdir /mnt/cdrom
    
  • 挂载

    # 上传镜像到/home/user目录下
    mount -o loop /home/user/ubuntu-20.04.6-live-server-amd64.iso /mnt/cdrom/
    
  • 同步/mnt/cdrom/下的文件到/home/ISO/

    mkdir /home/ISO
    rsync -avz /mnt/cdrom/ /home/ISO/
    
创建应答文件
  • 20.04meta-data
instance-id: focal-autoinstall
  • 22.04meta-data
instance-id: jammyl-autoinstall
  • 密码生成
apt install whois -y
mkpasswd -m sha-512 mypassword
  • 20.04user-data
#cloud-config
autoinstall:
 version: 1
 apt:
   primary:
   - arches: [default]
     uri: https://mirrors.aliyun.com/ubuntu
   disable_components: []
   preserve_sources_list: false
 identity:
   hostname: ubuntu-server
   password: "$6$1pRBywQWxTve6eXY$DIuoBf36dCnzOZ3sGMUJ2VJaYtcOryIlmQMmOb4PMJmiJBdkZd3V6hJwuJeyyAEa84P4HNRU38KvXSqrhHvTG1"
   username: ubuntu
 ssh:
   allow-pw: true
   authorized-keys: []
   install-server: true
 network:
   version: 2
   ethernets:
     ens33:
       dhcp4: true
       optional: true
 storage:
   config:
   - ptable: gpt
     path: /dev/sda
     wipe: superblock-recursive
     preserve: false
     name: ''
     grub_device: true
     id: disk-sda
     type: disk
   - device: disk-sda
     size: 1048576
     flag: bios_grub
     number: 1
     preserve: false
     grub_device: false
     offset: 1048576
     path: /dev/sda1
     id: partition-0
     type: partition
   - device: disk-sda
     size: 1073741824
     wipe: superblock
     number: 2
     preserve: false
     grub_device: false
     offset: 2097152
     path: /dev/sda2
     id: partition-1
     type: partition
   - fstype: ext4
     volume: partition-1
     preserve: false
     id: format-0
     type: format
   - device: disk-sda
     size: -1
     wipe: superblock
     number: 3
     preserve: false
     grub_device: false
     offset: 1075839488
     path: /dev/sda3
     id: partition-2
     type: partition
   - name: ubuntu-vg
     devices:
     - partition-2
     preserve: false
     id: lvm_volgroup-0
     type: lvm_volgroup
   - name: ubuntu-root
     volgroup: lvm_volgroup-0
     size: -1
     wipe: superblock
     preserve: false
     path: /dev/ubuntu-vg/ubuntu-root
     id: lvm_partition-0
     type: lvm_partition
   - fstype: ext4
     volume: lvm_partition-0
     preserve: false
     id: format-1
     type: format
   - path: /
     device: format-1
     id: mount-1
     type: mount
   - path: /boot
     device: format-0
     id: mount-0
     type: mount
 packages:
   - vim
   - curl
 late-commands:
   - curtin in-target -- /bin/bash -c 'echo "ubuntu ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers'
  • 22.04 user-data
#cloud-config
autoinstall:
 version: 1
 apt:
   primary:
   - arches: [default]
     uri: https://mirrors.aliyun.com/ubuntu
   disable_components: []
   preserve_sources_list: false
 user-data:
   timezone: Asia/Shanghai
 identity:
   hostname: ubuntu-server
   password: "$6$1pRBywQWxTve6eXY$DIuoBf36dCnzOZ3sGMUJ2VJaYtcOryIlmQMmOb4PMJmiJBdkZd3V6hJwuJeyyAEa84P4HNRU38KvXSqrhHvTG1"
   username: ubuntu
 ssh:
   allow-pw: true
   authorized-keys: []
   install-server: true
 network:
   version: 2
   ethernets:
     ens33:
       dhcp4: true
       optional: true
 storage:
   config:
   - ptable: gpt
     path: /dev/sda
     wipe: superblock-recursive
     preserve: false
     name: ''
     grub_device: true
     id: disk-sda
     type: disk
   - device: disk-sda
     size: 1048576
     flag: bios_grub
     number: 1
     preserve: false
     grub_device: false
     offset: 1048576
     path: /dev/sda1
     id: partition-0
     type: partition
   - device: disk-sda
     size: 1073741824
     wipe: superblock
     number: 2
     preserve: false
     grub_device: false
     offset: 2097152
     path: /dev/sda2
     id: partition-1
     type: partition
   - fstype: ext4
     volume: partition-1
     preserve: false
     id: format-0
     type: format
   - device: disk-sda
     size: -1
     wipe: superblock
     number: 3
     preserve: false
     grub_device: false
     offset: 1075839488
     path: /dev/sda3
     id: partition-2
     type: partition
   - name: ubuntu-vg
     devices:
     - partition-2
     preserve: false
     id: lvm_volgroup-0
     type: lvm_volgroup
   - name: ubuntu-root
     volgroup: lvm_volgroup-0
     size: -1
     wipe: superblock
     preserve: false
     path: /dev/ubuntu-vg/ubuntu-root
     id: lvm_partition-0
     type: lvm_partition
   - fstype: ext4
     volume: lvm_partition-0
     preserve: false
     id: format-1
     type: format
   - path: /
     device: format-1
     id: mount-1
     type: mount
   - path: /boot
     device: format-0
     id: mount-0
     type: mount
 packages:
   - vim
   - curl
 early-commands:
   - sgdisk --clear /dev/sda
   - sgdisk -n 1:2048:+1M -c 1:"System Partition" -t 1:ef02 /dev/sda
   - sgdisk -n 2:4096:+1024M -c 2:"Boot System Partition" -t 2:8304 /dev/sda
   - sgdisk -N 3 -c 3:"Root Partition" -t 3:8304 /dev/sda
 late-commands:
   - curtin in-target -- /bin/bash -c 'echo "ubuntu ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers'
  • user-data 进阶
 #cloud-config
autoinstall:
  version: 1
  apt:
    primary:
    - arches: [default]
      uri: https://mirrors.aliyun.com/ubuntu
    disable_components: []
    preserve_sources_list: false
  user-data:
    timezone: Asia/Shanghai
  identity:
    hostname: ubuntu-server
    password: "$6$1pRBywQWxTve6eXY$DIuoBf36dCnzOZ3sGMUJ2VJaYtcOryIlmQMmOb4PMJmiJBdkZd3V6hJwuJeyyAEa84P4HNRU38KvXSqrhHvTG1"
    username: ubuntu
  ssh:
    allow-pw: true
    authorized-keys: []
    install-server: true
  network:
    version: 2
    ethernets:
      ens33:
        dhcp4: true
        optional: true
  storage:
    config:
    - ptable: gpt
      path: /dev/sda
      wipe: superblock-recursive
      preserve: false
      name: ''
      grub_device: true
      id: disk-sda
      type: disk
    - device: disk-sda
      size: 1048576
      flag: bios_grub
      number: 1
      preserve: false
      grub_device: false
      offset: 1048576
      path: /dev/sda1
      id: partition-0
      type: partition
    - device: disk-sda
      size: 1073741824
      wipe: superblock
      number: 2
      preserve: false
      grub_device: false
      offset: 2097152
      path: /dev/sda2
      id: partition-1
      type: partition
    - fstype: ext4
      volume: partition-1
      preserve: false
      id: format-0
      type: format
    - device: disk-sda
      size: -1
      wipe: superblock
      number: 3
      preserve: false
      grub_device: false
      offset: 1075839488
      path: /dev/sda3
      id: partition-2
      type: partition
    - name: ubuntu-vg
      devices:
      - partition-2
      preserve: false
      id: lvm_volgroup-0
      type: lvm_volgroup
    - name: ubuntu-root
      volgroup: lvm_volgroup-0
      size: -1
      wipe: superblock
      preserve: false
      path: /dev/ubuntu-vg/ubuntu-root
      id: lvm_partition-0
      type: lvm_partition
    - fstype: ext4
      volume: lvm_partition-0
      preserve: false
      id: format-1
      type: format
    - path: /
      device: format-1
      id: mount-1
      type: mount
    - path: /boot
      device: format-0
      id: mount-0
      type: mount
    - ptable: gpt
      path: /dev/sdb
      wipe: superblock-recursive
      preserve: false
      name: ''
      grub_device: false
      id: disk-sdb
      type: disk
    - device: disk-sdb
      size: -1
      wipe: superblock
      number: 1
      preserve: false
      grub_device: false
      offset: 0
      path: /dev/sdb1
      id: partition-sdb1
      type: partition
    - fstype: ext4
      volume: partition-sdb1
      preserve: false
      id: format-sdb1
      type: format
    - path: /var/lib/containerd
      device: format-sdb1
      id: mount-sdb1
      type: mount
  packages:
    - vim
    - curl
    - wget
    - chrony
    - docker.io
  early-commands:
    - sgdisk --clear /dev/sda
    - sgdisk -n 1:2048:+1M -c 1:"System Partition" -t 1:ef02 /dev/sda
    - sgdisk -n 2:4096:+1024M -c 2:"Boot System Partition" -t 2:8304 /dev/sda
    - sgdisk -N 3 -c 3:"Root Partition" -t 3:8304 /dev/sda
    - sgdisk -N 1 /dev/sdb
  late-commands:
    - curtin in-target -- /bin/bash -c 'echo "ubuntu ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers'
    - curtin in-target -- /bin/bash -c 'wget -O /tmp/network.sh http://192.168.5.195:/network.sh'
    - curtin in-target -- /bin/bash -c 'bash /tmp/network.sh'
创建应答文件目录
mkdir /home/ISO/nocloud
mv ./user--data ./meta-data /home/ISO/nocloud
修改.cfg文件
# 20.04文件路径 /home/ISO/isolinux/txt.cfg
default live
label live
  menu label ^Install Ubuntu Server
  kernel /casper/vmlinuz
  # 修改部分
  append   initrd=/casper/initrd quiet autoinstall ds=nocloud;s=/cdrom/nocloud/  ---
# 22.04文件路径/home/ISO/boot/grub/grub.cfg
menuentry "Install Ubuntu Server" {
        set gfxpayload=keep
        linux   /casper/vmlinuz quiet autoinstall ds=nocloud\;s=/cdrom/nocloud/  ---
        initrd  /casper/initrd
}

生成iso文件
  • 20.04
mkisofs -R -J -T -v -no-emul-boot -boot-load-size 4 -boot-info-table -b isolinux/isolinux.bin -c isolinux/boot.cat -o /home/user/ubuntu-20.04.6-auto-server-amd64.iso /home/ISO
  • 22.04
mkisofs -R -J -T -v -joliet-long -no-emul-boot -boot-load-size 4 -boot-info-table -b boot/grub/i386-pc/eltorito.img -c boot/grub/boot.cat -o /home/user/ubuntu-22.04.4-auto-server-amd64.iso /home/ISO
静态网络设置脚本
#!/bin/bash
NETCARDS=$(ls /sys/class/net | grep -v -E "(lo|docker|virb|veth)")
STATIC_NETWORK_CONFIG_DIR="/etc/netplan"
for file in $(ls ${STATIC_NETWORK_CONFIG_DIR}); do
    mv ${STATIC_NETWORK_CONFIG_DIR}/${file}  ${STATIC_NETWORK_CONFIG_DIR}/${file}.old
done

for netcard in $NETCARDS; do
    if ip addr show ${netcard} | grep -E ".*state UP" > /dev/null; then
        STATIC_NETWORK_CONFIG_FILE_NEW="01-installer-config-${netcard}.yaml"
        echo "Configuring $netcard"  # 修正变量名从 $NETCARD 改为 $netcard
        addr=$(ip addr show "${netcard}" | grep -E "inet.*${netcard}" | awk '{print $2}')
        gateway=$(ip route show | grep -E "default.*${netcard}" | awk '{print $3}')
        if [ ! -z "$addr" ] && [ ! -z "$gateway" ]; then
            STATIC_LINE=$(cat <<EOF
# This is the network config written by 'subiquity'
network:
  ethernets:
    ${netcard}:
      addresses:
      - ${addr}
      nameservers:
        addresses:
        - ${gateway}
        search: []
      routes:
      - to: default
        via: ${gateway}
  version: 2
EOF
)
        elif [ ! -z "$addr" ] && [ -z "$gateway" ]; then
            STATIC_LINE=$(cat <<EOF
# This is the network config written by 'subiquity'
network:
  ethernets:
    ${netcard}:
      addresses:
      - ${addr}
  version: 2
EOF
)
        fi
        echo -e "${STATIC_LINE}" > ${STATIC_NETWORK_CONFIG_DIR}/${STATIC_NETWORK_CONFIG_FILE_NEW}
    fi
done
### CentOS7 上 Docker 镜像的导入与导出教程 在 CentOS 7 环境下,可以通过 `docker save` 和 `docker load` 命令实现 Docker 镜像的导出和导入功能。以下是具体的操作方法: #### 1. 准备工作 确保已安装 Docker 并启动服务。如果未安装 Docker,则可通过以下命令完成安装: ```bash yum install -y docker-ce docker-ce-cli containerd.io systemctl start docker ``` 此部分可参考相关内容[^3]。 --- #### 2. Docker 镜像的导出 通过 `docker save` 命令将现有镜像保存为 `.tar` 文件。语法如下: ```bash docker save [选项] IMAGE_NAME > FILE_PATH ``` 例如,导出名为 `ubuntu:latest` 的镜像到当前目录下的 `uu.tar` 文件: ```bash docker save ubuntu:latest > uu.tar ``` 或者指定更详细的路径: ```bash docker save ubuntu:latest > /path/to/save/uu.tar ``` 上述操作会将镜像数据打包并存储至目标位置[^2]。 --- #### 3. Docker 镜像的导入 通过 `docker load` 命令加载之前导出的镜像文件。支持多种方式执行该操作,例如: - 使用 `-i/--input` 参数显式指定输入文件: ```bash docker load --input uu.tar ``` - 或者直接重定向文件内容: ```bash docker load < uu.tar ``` 成功后,Docker 将恢复原始镜像及其元数据。 --- #### 4. 注意事项 - **镜像 ID 和标签保持一致**:通过 `docker load` 导入的镜像会保留原有的镜像 ID 及标签信息。 - 如果需要自定义镜像名称或版本号,在后续处理时可以重新标记 (`docker tag`) 镜像[^4]。 --- #### 示例代码 以下是一个完整的示例流程: ```bash # 步骤 1: 导出镜像 docker save ubuntu:latest > ubuntu_latest.tar # 步骤 2: 删除本地镜像 (验证导入效果) docker rmi ubuntu:latest # 步骤 3: 导入镜像 docker load < ubuntu_latest.tar # 步骤 4: 查看镜像列表确认 docker images ``` --- ### 总结 以上介绍了如何在 CentOS 7 下利用 Docker 工具链完成镜像的导出与导入过程。这些技能对于跨环境迁移、备份以及分发镜像非常有用[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值