• Ubuntu20.04 Focal Cloudimage扩容以及KVM安装的问题记录


    运行Ubuntu20.04的KVM虚机遇到一些问题, 单独总结一下

    镜像扩容

    不能用virt-resize --expand /dev/sda1 old.qcow2 new.qcow2这样的命令, 这样制作出来的镜像, 丢失了/dev/sda14, /dev/sda15这两个分区, 变成了 /dev/sda1, /dev/sda2, /dev/sda3, 会导致install --import时卡住. 用virt-filesystems --long --parts --blkdevs -h -a new.qcow2命令查看, 会发现/dev/sda1的大小变成了4MB

    因为镜像是为云服务提供的, 安装启动时配合init脚本扩容, 如果要脱机扩容的话, 网上几乎查不到能解决这个问题的办法, 最后是通过 转换为raw格式->parted调整分区大小->转回qcow2完成的, 参考 https://blog.richliu.com/2018/08/25/2318/change-ubuntu-cloud-image-size/

    具体过程为

    # resize原始镜像
    qemu-img resize focal-server-cloudimg-amd64-50g.qcow2 50G
    # 转换为raw格式镜像
    qemu-img convert -f qcow2 -O raw focal-server-cloudimg-amd64-50g.qcow2 focal-server-cloudimg-amd64.raw
    # 重命名以防混淆
    mv focal-server-cloudimg-amd64.raw focal-server-cloudimg-amd64-50g.raw
    # 运行parted
    parted focal-server-cloudimg-amd64-50g.raw 
    ######################## 开始
    GNU Parted 3.1
    Using /data/backup/vm_images/focal-server-cloudimg-amd64-50g.raw
    Welcome to GNU Parted! Type 'help' to view a list of commands.
    (parted) p                                                                
    Error: The backup GPT table is not at the end of the disk, as it should be.
    This might mean that another operating system believes the disk is smaller.
    Fix, by moving the backup to the end (and removing the old backup)?
    Fix/Ignore/Cancel? Fix                                                    
    Warning: Not all of the space available to
    /data/backup/vm_images/focal-server-cloudimg-amd64-50g.raw appears to be used,
    you can fix the GPT to use all of the space (an extra 100245504 blocks) or
    continue with the current setting? 
    Fix/Ignore? Fix                                                           
    Model:  (file)
    Disk /data/backup/vm_images/focal-server-cloudimg-amd64-50g.raw: 53.7GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags: 
    
    Number  Start   End     Size    File system  Name  Flags
    14      1049kB  5243kB  4194kB                     bios_grub
    15      5243kB  116MB   111MB   fat32              boot
     1      116MB   2361MB  2245MB  ext4
    
    (parted) resizepart                                                     
    Partition number? 1                                                       
    End?  [2361MB]? 53.7G                                                     
    (parted) p                                                                
    Model:  (file)
    Disk /data/backup/vm_images/focal-server-cloudimg-amd64-50g.raw: 53.7GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags: 
    
    Number  Start   End     Size    File system  Name  Flags
    14      1049kB  5243kB  4194kB                     bios_grub
    15      5243kB  116MB   111MB   fat32              boot
     1      116MB   53.7GB  53.6GB  ext4
    (parted) q                                                                
    ########################### 结束
    # 查看raw格式的分区大小是否正确
    virt-filesystems --long --parts --blkdevs -h -a focal-server-cloudimg-amd64-50g.raw 
    Name        Type       MBR  Size  Parent
    /dev/sda1   partition  -    50G   /dev/sda
    /dev/sda14  partition  -    4.0M  /dev/sda
    /dev/sda15  partition  -    106M  /dev/sda
    /dev/sda    device     -    50G   -
    
    # 转回qcow2格式
    qemu-img convert -f raw -O qcow2 focal-server-cloudimg-amd64-50g.raw focal-server-cloudimg-amd64-50g-resized.qcow2
    
    # 再次检查
    virt-filesystems --long --parts --blkdevs -h -a focal-server-cloudimg-amd64-50g-resized.qcow2 
    Name        Type       MBR  Size  Parent
    /dev/sda1   partition  -    50G   /dev/sda
    /dev/sda14  partition  -    4.0M  /dev/sda
    /dev/sda15  partition  -    106M  /dev/sda
    /dev/sda    device     -    50G   -
    

    经过上面的操作, 这个镜像在安装后只需要执行resize2fs /dev/vda1就能扩充分区了.

    root口令

    focal-server-cloudimg-xxx 这些镜像是为云环境创建的, 会配合一个init脚本(或者iso)启动并创建普通用户, 默认root不能登录也没有密码, 而单机运行还是需要root的, 所以在安装前, 要设置一下root口令:

    virt-customize -a some.qcow2c --root-password password:[your password]
    

    import安装虚机

    命令

    virt-install --name vm_ub01 --vcpus 4 --memory 8192 --disk /data/vms/vm_ubtu.qcow2 --graphics none --import --os-type linux --os-variant ubuntu20.04 --network bridge=br0,model=virtio
    

    网络配置

    这些是在虚机上执行的, 在install --import之后, 虚机网卡是未启动的, 而且net-tools也没安装, 只能通过基础的ip命令进行操作

    # 查看ip, 可以看到有一个未启动的ens3网口
    ip addr
    # 启动nes3, 
    ip link set ens3 up
    # 再次查看, 并无ipv4地址
     ip addr
    # 查看网卡硬件信息, 这里可以看到mac地址和网口名称
    lshw -class network
    # 增加网络配置
    vi /etc/netplan/99_config.yaml
    # 内容开始
    network:
      version: 2
      renderer: networkd
      ethernets:
        ens3:
          dhcp4: true
    # 内容结束, 用下面的命令启用
    netplan apply
    # 再次查看, 这时候就有ip了
    ip addr
    # ping检查连通性
    ping 202.38.64.1
    

    启动sshd失败

    虚机上启动ssh, 出现ssh.service: Start request repeated too quick的错误
    sshd -t检查, 提示no hostkey

    sshd: no hostkeys available -- exiting.
    

    重新生成hostkey

    ssh-keygen -A
    ssh-keygen: generating new host keys: RSA DSA ECDSA ED25519 
    

    之后重启sshd就正常了

    关于hostkey

    hostkey就是ssh服务中, 服务端的私钥, 正常情况是随着OpenSSH安装时一起生成的.

    In OpenSSH, host keys are usually stored in the /etc/ssh directory, in files starting with ssh_host_<rsa/dsa/ecdsa/ed25519>_key (the location can be changed in server configuration files).
    
    Host keys are normally generated automatically when OpenSSH is first installed or when the computer is first booted. The ssh-keygen program can be used for generating additional host keys or for replacing existing keys.
    

    root不能通过密码登录ssh

    需要修改/etc/ssh/sshd_config, 将这两行改成下面的值, 然后重启ssh服务

    PermitRootLogin yes
    PasswordAuthentication yes
    

    参考

  • 相关阅读:
    MQTT协议探究(三)
    MQTT协议探究(二)
    MQTT协议探究(一)
    MQTT图形化客户端比较
    Ubuntu编写开机启动脚本
    线程阻塞释放的5种方法
    (转)Linux vmstat命令实战详解
    (转)MongoDB 3.0 WT引擎参考配置文件
    (转)mongodb常用命令脚本化-自动化运维
    (转)MongoDB 实现currentOp定时捕获
  • 原文地址:https://www.cnblogs.com/milton/p/15382673.html
Copyright © 2020-2023  润新知