分类目录归档:技术

Emacs 集成 Github Copilot

copilot.el 是一个非官方实现的 Github Copilot 插件,实测在 x86/arm64 、macos/debian 下均可使用,下面介绍最关键的几个配置步骤。

准备

  • Emacs >= 27
  • 安装 node.js
  • emacs 安装好 e, editorconfig 插件

以上是对您环境最基础的要求,确认无误后继续下一步。

配置 copilot.el

官方可以直接引用 github 链接作为插件,但考虑到网络环境问题,在这里使用手动克隆代码库来引用。

运行以下命令将 copilot.el 克隆到 emacs 配置文件夹:

$ cd ~/.emacs.d
$ git clone https://github.com/zerolfx/copilot.el.git

克隆完毕后,在 emacs 配置中将 copilot.el 载入,并配置一些必要的内容:

比如将以下内容写入 ~/.emacs.d/init.el

;;copilot
(add-to-list 'load-path
         (expand-file-name (concat user-emacs-directory "copilot.el")))
(require 'copilot)
;; copilot automatically provide completions
(add-hook 'prog-mode-hook 'copilot-mode)

; complete by copilot first, then auto-complete
(defun my-tab ()
  (interactive)
  (or (copilot-accept-completion)
      (ac-expand nil)))

(with-eval-after-load 'auto-complete
  ; disable inline preview
  (setq ac-disable-inline t)
  ; show menu if have only one candidate
  (setq ac-candidate-menu-min 0))

(define-key copilot-completion-map (kbd "
<tab>") 'copilot-accept-completion)
(define-key copilot-completion-map (kbd "TAB") 'copilot-accept-completion)

以上配置均来自代码库最基础的配置,可以实现最直观的功能。

使用

配置完毕后即可开始使用,首先需要登陆:

# 登陆
M-x copilot-login

如果一切顺利,完成授权,就可以开始使用了。

重启 Emacs,尝试一下吧:

参考文献

PVE 批量创建、配置虚拟机 IP、网卡、启停及销毁

# 从模版批量派生,100 虚拟机仅需2 min
for i in {001..128}; do qm clone 336 11$i --name stl-ceph-bare-node$i --pool stl-ceph-cluster2; done

# 使用 cloud-init 批量配置 ip,需在模版提前装好 cloud-init 软件包并添加设备
for i in {1..128}; do num=`echo $i | awk '{printf("%03d",$0)}'`; qm set 10${num}  --ipconfig1 ip=10.24.88.$i/24,gw=10.24.88.254; done

# 批量断开网络接口
for i in {002..128}; do qm set 10$i --net0 virtio,link_down=1; done

# 批量开机
for i in {001..128}; do qm start 10$i; done

# 批量销毁
for i in {001..111}; do qm destroy 10$i; done

PVE 虚拟机导入 vmdk 磁盘

  1. 先把磁盘镜像上传到 pve 服务器中,若空间不足可挂载本地存储;
  2. 把 vmdk 转为 qcow2
qemu-img convert -O qcow2 test.vmdk test.qcow2

注:-O 是大写的字母 O

  1. 使用 qm importdisk 命令导入
$ qm importdisk  
<vmid> <images-name> <storage pool>  --format=<disk-fs> 

# vmid:vm的id 例如102
# images-name:磁盘镜像的名字
# storage poll: 存储磁盘镜像的位置,一般写存储的名称,如pve01data
# disk-fs: 磁盘镜像格式  raw/vmdk/qcow2

参考文献

使用 dos2unix 解决跨操作系统换行符问题

dos2unix 是将 Windows 格式文件转换为 Unix/Linux 格式的实用命令。

unix2dos 则是和 dos2unix 互为孪生的一个命令,将 Linux&Unix 格式文件转换为 Windows 格式文件的命令。

各个操作系统安装方法如下:

OS X
    brew install dos2unix
Debian
    apt-get install dos2unix 
Ubuntu
    apt-get install dos2unix 
Alpine
    apk add dos2unix 
Arch Linux
    pacman -S dos2unix 
Kali Linux
    apt-get install dos2unix 
CentOS
    yum install dos2unix 
Fedora
    dnf install dos2unix 
Windows (WSL2)
    sudo apt-get update sudo apt-get install dos2unix 
Raspbian
    apt-get install dos2unix 
Dockerfile
    dockerfile.run/dos2unix 
Docker
    docker run cmd.cat/dos2unix dos2unix

下面以 15分钟学会Emacs Lisp 教学源文件为例展示效果。

$ dos2unix learn-emacs-lisp-zh.el
dos2unix: converting file learn-emacs-lisp-zh.el to Unix format...

转换前后的对比如下:

上方是转换后的文件,下方是转换前的文件,使用 Emacs 打开。

可以看到,转换前该文件使用的是 Windows 风格的换行符,转换后该文件使用 Unix 风格换行符。

Unix 系统中:每行结尾只有 “”,即 \n; Windows 系统中:每行结尾是 “”,即 \r\n; Mac 系统中:每行结尾是 “”,即 \r“。

参考文献

Prometheus 部署 Black Exporter 黑盒监控 DNS-TCP-ICMP

建议软件包安装,二进制安装请自行解决配置问题,docker安装需考虑网络问题。

linux 软件包直接部署

apt install prometheus-blackbox-exporter

二进制部署

# 下载安装
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.23.0/blackbox_exporter-0.23.0.linux-amd64.tar.gz
tar -xvf  blackbox_exporter-0.23.0.linux-amd64.tar.gz
mv blackbox_exporter-0.23.0.linux-amd64/ /usr/local/blackbox_exporter

# 创建配置文件
cat > /data/black_exporter/black_exporter.yml << EOF
modules:
  http_2xx:
    prober: http
    timeout: 20s
    http:
      preferred_ip_protocol: "ip4"
  http_post_2xx_query:
    prober: http
    timeout: 20s
    http:
      preferred_ip_protocol: "ip4" ##使用ipv4
      method: POST
      headers:
        Content-Type: application/json ##header头
      body: '{"hmac":"","params":{"publicFundsKeyWords":"xxx"}}' ##传参
  tls_connect_tls:
    prober: tcp
    timeout: 5s
    tcp:
      tls: true
  tcp_connect:
    prober: tcp
    timeout: 5s
 #
  pop3s_banner:
    prober: tcp
    tcp:
      query_response:
      - expect: "^+OK"
      tls: true
      tls_config:
        insecure_skip_verify: false
  ssh_banner:
    prober: tcp
    tcp:
      query_response:
      - expect: "^SSH-2.0-"
  irc_banner:
    prober: tcp
    tcp:
      query_response:
      - send: "NICK prober"
      - send: "USER prober prober prober :prober"
      - expect: "PING :([^ ]+)"
        send: "PONG ${1}"
      - expect: "^:[^ ]+ 001"
  icmp:
    prober: icmp
    timeout: 20s
EOF

# 配置systemd
cat > /etc/systemd/system/blackbox_exporter.service <<EOF
[Unit]
Description=blackbox_exporter
Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/bin/blackbox_exporter  --config.file=/data/blackbox-exporter/black-exporter.yml
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

# 启动服务
systemctl enable blackbox_exporter.service 
systemctl start blackbox_exporter.service 
systemctl status blackbox_exporter.service 

Docker 部署

docker run --rm \
  -p 9115/tcp \
  --name blackbox_exporter \
  -v $(pwd):/config \
  quay.io/prometheus/blackbox-exporter:latest --config.file=/config/blackbox.yml

# 创建配置文件
cat > /data/docker/blackbox-exporter/black-exporter.yml << EOF
modules:
  http_2xx:
    prober: http
    timeout: 20s
    http:
      preferred_ip_protocol: "ip4"
  http_post_2xx_query:
    prober: http
    timeout: 20s
    http:
      preferred_ip_protocol: "ip4" ##使用ipv4
      method: POST
      headers:
        Content-Type: application/json ##header头
      body: '{"hmac":"","params":{"publicFundsKeyWords":"xxx"}}' ##传参
  tls_connect_tls:
    prober: tcp
    timeout: 5s
    tcp:
      tls: true
  tcp_connect:
    prober: tcp
    timeout: 5s
 #
  pop3s_banner:
    prober: tcp
    tcp:
      query_response:
      - expect: "^+OK"
      tls: true
      tls_config:
        insecure_skip_verify: false
  ssh_banner:
    prober: tcp
    tcp:
      query_response:
      - expect: "^SSH-2.0-"
  irc_banner:
    prober: tcp
    tcp:
      query_response:
      - send: "NICK prober"
      - send: "USER prober prober prober :prober"
      - expect: "PING :([^ ]+)"
        send: "PONG ${1}"
      - expect: "^:[^ ]+ 001"
  icmp:
    prober: icmp
    timeout: 20s
EOF

docker run -d \
  --net myDefault \
  --restart always \
  -p 9115:9115/tcp \
  --name blackbox-exporter \
  -v /data/docker/blackbox-exporter:/config \
  songtianlun/blackbox-exporter:v0.23.0 --config.file=/config/black-exporter.yml

测试使用

curl http://192.168.5.152:9115/probe?target=www.frytea.com&module=http_2xx

prometeus 使用

# https monitor
  - job_name: 'hci-https-monitor'
    metrics_path: /probe
    params:
      module: [tls_connect_tls]
    static_configs:
      - targets:
        - 'https://192.168.5.221:8006'
        - 'https://192.168.5.222:8006'
        - 'https://192.168.5.187:8006'
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 10.19.0.1:9115  # The blackbox exporter's real hostname:port.

# http monitor
  - job_name: 'hci-http-monitor'
    metrics_path: /probe
    params:
      module: [http_2xx]
    static_configs:
      - targets:
        - 'http://192.168.5.221:3000'
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 10.19.0.1:9115  # The blackbox exporter's real hostname:port.

  # icmp 监控
  - job_name: 'hci-icmp-monitor'
    scrape_interval: 1m
    metrics_path: /probe
    params:
      module: [ "icmp" ]
    static_configs:
      - targets:
        - 192.168.5.254
        - 192.168.5.221
        - 192.168.5.222
        - 192.168.5.187
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 10.19.0.1:9115  # The blackbox exporter's real hostname:port.

Grafana导入Dashboard-ID:7587

参考文献

Paralles Desktop 修改虚拟机用户密码

最近在使用 Paralles Desktop 过程中,发现一旦忘记用户密码,是一件挺麻烦的事情。尝试各种方法打不开 grub 启动菜单。

后来发现可以使用其提供的命令工具修改用户密码,很好用,在此记录:

# 首先获取虚拟机 UUID
$ prlctl list
UUID                                    STATUS       IP_ADDR         NAME
{fdf05394-dc61-4d07-b6e5-e81ef5277a64}  running      -               Ubuntu 22.04 ARM64

# 设定用户密码
$ prlctl set fdf05394-dc61-4d07-b6e5-e81ef5277a64 --userpasswd 
<USERNAME>:<PASSWORD>
Authentication tokens updated successfully.
Success. The operation was successfully completed.

The VM has been successfully configured.

参考文献

m1 MacBook 安装 asahi linux 磁盘调整失败解决

尝试在 MacBook Air m1 安装 asahi linux ,在磁盘分区过程遭遇报错,具体过程没有留下来,是类似这样的报错:

error: doc-id tree: record exists for doc-id 64, file-id 9665861 but no inode references this doc-id

大致解决是要进恢复模式,之后使用急救程序尝试修复。

尝试后发现还是报错,后来在 asahi linux 的 github 找到一个类似问题,使用下列方法解决:

I had a similar issue, where /dev/disk3s5 had warnings that wouldn’t go away, and I solved it as follows:

Boot into recovery and open a terminal
`diskutil unmountdisk /dev/disk3`
`fsck_apfs /dev/disk3` (and hit y in response to the various prompts)

I believe unmounting was the important part.

之后磁盘分区就不报错了。

参考文献: https://github.com/AsahiLinux/asahi-installer/issues/88#issuecomment-1095019173

Linux 使用 chrony 进行 NTP 时间同步及自建方法

chrony是网络时间协议的实现。它可以替代ntpd,后者是NTP的参考实现。它在类Unix操作系统上运行,并在GNU GPL v2下发布。

服务端和客户端配置文件都是同一个,分别配置为服务器和客户端即可使用, 配置文件在 /etc/chrony.conf or /etc/chrony/chrony.conf ,具体看版本, 可以使用 man chrony 确认一下。

服务端配置

服务端配置:

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server s1a.time.edu.cn iburst
server ntp.aliyun.com iburst

# Allow NTP client access from local network.
allow 192.168.8.0/24

开启同步

systemctl enable chronyd
systemctl restart chronyd

# 查看时间同步状态
timedatectl status
# 开启网络时间同步
timedatectl set-ntp true

客户端配置

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 192.168.8.5 iburst

# Allow NTP client access from local network.

开启同步

systemctl enable chronyd
systemctl restart chronyd

# 查看时间同步状态
timedatectl status
# 开启网络时间同步
timedatectl set-ntp true

常用命令

# 查看 ntp_servers
chronyc sources -v

# 查看 ntp_servers 状态
chronyc sourcestats -v

# 查看 ntp_servers 是否在线
chronyc activity -v

# 查看 ntp 详细信息
chronyc tracking -v

参考文献