Category - Linux

? shell ?    2021-01-20 17:03:25    1296    0    0

打印全部列

  1. awk '{print $0}'

打印指定列

  1. awk '{print $2,$4}'

打印指定列并拼接字符串

  1. awk '{print $1,"hhah",$2}'
  2. awk '{print $1"hahah"$2}'

按指定分隔符取列

  1. awk -F: '{ print $1 }'

打印某一列及后面所有列的内容

  1. awk '{out=""; for(i=2;i<=NF;i++){out=out" "$i}; print out}'

打印最后一列,最后一列的前一列,NF表示最后一列的列号

  1. awk '{print $NF, $(NF-1)}'

打印指定一行,NR表示行号

  1. awk 'NR==6 {print $0}'

打印第六行,以 :分隔开的第一列

  1. awk -F ":" 'NR==6 {print $1}'

打印第4行以后的行

  1. git log --pretty=format:"%H-%h:%h" -n 15 |cat -n | awk 'NR>4 {print $0}'
  2. 5 1f021028e22d92119fa5a0821273596e0c37be87-1f02102:1f02102
  3. 6 f36b0ad5a376edf485979b357af4ac1df87cc443-f36b0ad:f36b0ad
  4. 7 e3776955f8f3592051603d2299b93d3b55cbc52a-e377695:e377695
  5. 8 94c50a1fd810d5c1e62535fac8f6db3c01c4c476-94c50a1:94c50a1
  6. git log --pretty=format:"%H-%h:%h" -n 15 |cat -n | awk 'NR<4 {print $0}'
  7. git log --pretty=format:"%H-%h:%h" -n 15 |cat -n | awk 'NR<=4 {print $0}'

条件用法
打印包含 Merge branch 的行 用/strings/

  1. git log --pretty=format:"%
2020-10-29 15:52:37    596    0    0

p2top3命令批量转化语法之后,还有一些 版本2和3之间用法的差异需要手动调整:

  1. if
  2. browser 抓取值
  3. 下载链接
2020-09-02 10:04:38    717    1    0

列表本目录所占空间

  1. du -sh ./

列表子目录所占空间

  1. du -sh ./*

列表子目录所占空间达到不足1M的

  1. du -sh ./* | grep "^[0-9]\{1,\}.\{0,1\}[0-9]\{1,\}[K]"

列表子目录所占空间达到1M 以上

  1. du -sh ./* | grep "^[0-9]\{1,\}.\{0,1\}[0-9]\{1,\}[M,G]"

列表子目录所占空间达到1G 以上

  1. du -sh ./* | grep "^[0-9]\{1,\}.\{0,1\}[0-9]\{1,\}[G]"

每个级别的列表再按第一列数值排序

  1. du -sh ./* | grep "^[0-9]\{1,\}.\{0,1\}[0-9]\{1,\}[K]"| sort -n
  2. du -sh ./* | grep "^[0-9]\{1,\}.\{0,1\}[0-9]\{1,\}[M]"| sort -n
  3. du -sh ./* | grep "^[0-9]\{1,\}.\{0,1\}[0-9]\{1,\}[G]"| sort -n

在排了序基础上再取最大10 个

  1. du -sh ./* | grep "^[0-9]\{1,\}.\{0,1\}[0-9]\{1,\}[G]" | sort -n | tail -n 10

2020-08-19 15:45:50    897    0    0
  1. # <file system> <mount point> <type> <options> <dump> <pass>

以下是官方对fstab 里六个字段用法的解释

The first field (fs_spec).

  1. For ordinary mounts, it will hold (a link to) a block special device node (as created by mknod(2)) for the device to be mounted, like /dev/cdrom' or/dev/sdb7'.

  2. LABEL= < label > or UUID= < uuid >
    LABEL=< label> or UUID=< uuid> may be given instead of a device name. This is the recommended method, as device names are often a coincidence of hardware detection order, and can change when other disks are added or removed. For example, LABEL=Boot' orUUID=3e6be9de-8139-11d1-9106-a43f08d823a6'. (Use a filesystem-specific tool like e2label(8), xfs_admin(8), or fatlabel(8) to set LABELs on filesystems).
    第一个字段可以是分区名,或标签名,设备id, 用来标记这一行所表示 的文件系统所在的设备块

  1. root@vm1:/vob/GuaKing/DevOps# cat /etc/fstab
  2. # <file system> <mount point> <type> <options> <dum
? qcow2 ? ? 分区 ?    2020-08-13 15:04:24    1131    1    0

官方参考文档

diskimage-builder code
https://opendev.org/openstack/diskimage-builder

官方分区设置

https://docs.openstack.org/diskimage-builder/latest/user_guide/building_an_image.html

If you wish to customise the top-level block-device-default.yaml file
from one of the block-device-* elements, set the environment variable
DIB_BLOCK_DEVICE_CONFIG. This variable must hold YAML structured
configuration data or be a file:// URL reference to a on-disk
configuration file.


在自己自定义的element 的根目录下,创建一个文件block-device-default.yaml,将分区方案按yaml 文件格式配置其中,如果element 下方有一个block-device-default.yaml,则会优先使用用户定义的分区配置,如果用户无定制,则使用默认配置:
https://opendev.org/openstack/diskimage-builder/src/branch/master/diskimage_builder/elements/block-device-mbr/block-device-default.yaml

或使用变量DIB_BLOCK_DEVICE_CONFIG定义分区配置,将block-device-default.yaml的文本赋值给DIB_BLOCK_DEVICE_CONFIG

https://github.com/GuaKing/qcow2build.git

官方的一个默认配置:

磁盘全部容量分配r

? qcow2 ? ? 分区 ?    2020-08-13 13:39:52    1092    1    0

ubuntu 扩展磁盘后如何扩展分区

背景: 使用kvm 安装 qcow2 文件 ,启用一个ubuntu 虚拟机。

直接扩展qcow2 镜像文件 的磁盘大小,并指定扩展在分区N

  1. qemu-img info a.qcow2
  2. qemu-img create -f qcow2 -o preallocation=metadata b.qcow2 60G
  3. virt-resize --expand /dev/sda1 a.qcow2 b.qcow2
  4. #第一个命令查看a.qcow2 镜像磁盘有多大(a 包含我们要安装系统镜像)
  5. #第二个命令创建一块 60G 的镜像磁盘文件(即最终磁盘容量,b须比a大)
  6. #第三个命令,将a 镜像内容拷到b镜像中,并将空闲空间(b'size-a'size)扩展到/dev/sda1 分区上。
  7. #即,如果a.qcow2 原磁盘大小是50G ,执行完命令后b.qcow2 除了是a.qcow2 的完整拷贝外,b.qcow2中的/deb/sda磁盘大小为60G,其中/dev/sda1 分区 比a.qcow2 中/dev/sda1 多了10G.

扩展磁盘,不分配到指定分区,进入系统后再分配

  1. qemu-img resize a.qcow2 +5G
  2. virsh destroy vm
  3. virsh start vm
  4. ## enter vm
  5. fdisk -l | grep sda (/dev/sda 磁盘大小增加了5g, 但是分区 /dev/sda1 未拿到这些空间)
  6. parted /dev/sda (将磁盘的未分配空间,分配给分区sda1
  7. (parted) p
  8. (parted) resizepart 1
  9. (parted) quit
  10. ## 执行log 如下:
  11. root@CASA-MOBILE:~# parted /dev/sda
  12. (parted) p <== remember space of all disk /dev/sda ,and remember the Number of sda1
? 分区 ?    2020-07-20 14:14:45    753    0    0

在使用diskimage-builder生成qcow2 镜像时,磁盘空间足够,但是执行到中间时会报
No space left
用df -h 查看时,/目录下的空间足够。
于是再执行,在执行过程中发现,diskimage-builder在执行过程解包和拷贝过程是会将一个/tmp/dib_build.xxxxxx的临时目录挂载在tmpfs上,而当前系统的tmpfs 只有3.9G 。

  1. root@vm1:~# df -h| grep tmpfs
  2. tmpfs 797M 3.3M 793M 1% /run
  3. tmpfs 3.9G 0 3.9G 0% /dev/shm
  4. tmpfs 5.0M 0 5.0M 0% /run/lock
  5. tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
  6. tmpfs 797M 0 797M 0% /run/user/0
  7. tmpfs 3.9G 3.8G 154M 97% /tmp/dib_build.whmZyNdv
  8. tmpfs 3.9G 0 3.9G 0% /tmp/dib_image.6Jdr8dSL

解决:

了解tmpfs是在哪里分配及分配规则:

https://wiki.archlinux.org/index.php/Tmpfs

tmpfs is a temporary filesystem that resides in memory and/or swap
partition(s). Mounting directories as tmpfs can be

2020-07-20 13:43:32    702    1    0

普通场景:

在ubuntu系统上,安装一系列依赖,及打包工具diskimage-builder, 然后在其上执行包含
disk-image-create 命令的脚本,生成qcow2/vmdk镜像。

容器内无差别构建:

每个人想拥有自己的构建环境 ,都需要另外在自己的系统上安装所需要的软件列表,会存在系统版本和软件版本不一的情况。

将其上环境构建成固定的dokcer 镜像, 每个人不管自己的设备是redhat, centos, ubuntu,都可以使用可控版本的打包容器进行构建,并且使用的软件版本是一致的。使用的镜像基于基础系统另装了哪些工具,都是可追溯可控版的。

Dockerfile

将需要定义的变量,需要预装的软件,都写进dockerfile

  1. FROM ubuntu:bionic AS DIB-env
  2. COPY sources.list /etc/apt/
  3. RUN apt-get update
  4. RUN apt-get install -y python-pip
  5. RUN apt-get install -y qemu-utils
  6. RUN apt-get install -y sudo
  7. RUN apt-get install -y curl
  8. RUN apt-get install -y squashfs-tools
  9. # Install python packages
  10. RUN pip install --upgrade pip
  11. RUN pip install networkx -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
  12. RUN pip install diskimage-builder -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
  13. ENV TEMP_PATH=/vob_temp
  14. RUN mkdir -p ${TEMP_PATH}
  15. COPY temp.tar.gz ${TEMP_PATH}
  16. WORKDIR ${TEMP_PATH}
  17. RUN tar zxvf temp.tar.gz
  18. RUN r
? python ? ? docker ?    2020-07-14 15:38:22    1274    0    0


Dockerfile 里的 pip install 总是failed .但是直接执行可以成功。
估计是docker build 时默认使用的pip 源有太慢导致

Dockerfile 里涉及的报错语句

  1. RUN pip install networkx

报错如下

  1. Step 9/15 : RUN pip install networkx
  2. ---> Running in 3a3f19067b2f
  3. Collecting networkx
  4. Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed ox/
  5. Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed ox/
  6. Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed ox/
  7. Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutErr
2020-04-28 15:32:23    922    0    0

系统:ubuntu

软件:dnsmasq

安装:

  1. apt-get install -y dnsmasq

配置 /etc/dnsmasq.conf:

  1. root@node248:/etc/resolvconf/resolv.conf.d# cat /etc/dnsmasq.conf | grep -v "^#|^$"
  2. port=53
  3. resolv-file=/etc/resolv.conf
  4. strict-order
  5. listen-address=172.0.11.248
  6. addn-hosts=/etc/hosts

配置 /etc/hosts

  1. root@node248:/etc/resolvconf/resolv.conf.d# cat /etc/hosts
  2. #.....
  3. 172.0.11.2 vm2
  4. 172.0.11.3 vm3
  5. 172.0.11.4 vm4.com

配置 上游 nameserver

  1. 使用服务器本身的dns 列表,则resolv-file=/etc/resolv.conf

  2. 使用服务器上另外的dns 列表文件,即上游dns,则resolv-file=/etc/dnsmasq.d/ 此目录下放置 *conf

重启服务:

  1. #14
  2. service dnsmasq restart
  3. #18
  4. systemctl restart dnsmasq

解析过程:

客户端将此服务器ip 设置为dns 之后,查找域名时,会通过服务器的/etc/hosts 去解析域名,然后再通过resolv-file 中列的dns 去解析。

附不同ubuntu系统dns-nameserver的设置:

UBUNTU 14

  1. root@vm1:/etc/dnsmasq.d# cat /etc/resolvconf/resolv.conf.d/base
  2. nameserver 172.0.11.248
  3. root@vm1:/etc/dnsmasq.d# cat /etc/resolv.conf
  4. # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
  5. #
3/7