Category: Linux

  • dd硬盘克隆

    有两块 500G 的 2.5 寸盘,想把一块盘 (有系统和各种数据) 中的数据完全克隆到另外一块盘。

    先用 live cd 启动。识别到分别为 sdc, sda

    然后使用

    dd if=/dev/sdc of=/dev/sda bs=32M

    500G 的内容用了6295.12s,平均 79.4MB/s

    比重装系统和拷文件快多了

     

  • xfs 分区

    要使用 xfs 文件系统,先分区,后格式化

    安装 xfs 套件

    yum install xfsprogs

    通过 fdisk 或 parted 进行分区

    parted /dev/vdb
    (parted) print                                                            
    Error: /dev/vdb: unrecognised disk label
    Model: Virtio Block Device (virtblk)                                      
    Disk /dev/vdb: 1563GB
    Sector size (logical/physical): 512B/512B
    Partition Table: unknown
    Disk Flags: 
    
    parted /dev/vdb
    mklabel gpt
    mkpart primary xfs 0% 100%
    print
    quit
    
    

    自动脚本 auto_parted.sh

    parted /dev/vdb << EOF
    mklabel gpt
    mkpart primary xfs 0% 100%
    print
    quit
    EOF
    

    格式化为 xfs

    [root@localhost ~]# mkfs.xfs /dev/xvdb1
    meta-data=/dev/xvdb1 isize=256 agcount=4, agsize=7864318 blks
    = sectsz=512 attr=2, projid32bit=0
    data = bsize=4096 blocks=31457270, imaxpct=25
    = sunit=0 swidth=0 blks
    naming =version 2 bsize=4096 ascii-ci=0
    log =internal log bsize=4096 blocks=15359, version=2
    = sectsz=512 sunit=0 blks, lazy-count=1
    realtime =none extsz=4096 blocks=0, rtextents=0

    mkfs.xfs

    自动生成 fstab

    uuid=$(blkid -s UUID -o value /dev/sdb1)
    echo "UUID=$uuid /data                   xfs     defaults        0 0" >> /etc/fstab
    
  • 进程和线程

    学习《计算机操作系统》

    引入进程,就是为了多个程序能够并发执行。提高系统资源利用率,增加系统吞吐量。进程是指在系统中能独立运行并作为资源分配的基本单位,它是由一组机器指令,数据和堆栈组成的,是一个能独立运行的活动实体。由于进程拥有自己的资源,故使调度付出的开销较大。

    引入线程,线程是比进程更小的单位,在一个进程中可以包含若干个线程,它们可以利用进程所拥有的资源。在引入线程的 OS 中,通常都是把进程作为分配资源的基本单位,而把线程作为独立运行和独立调度的基本单位。由于线程比进程更小,基本上不拥有资源,故对它的调度所付出的开销就会小得多,能更高效地提高系统内多个程序间并发执行的程度。

  • curl测试http请求各时间段

    curl 简单又强大,要好好学习。

    test_output=/tmp/curl-test
    test_times=3000
    test_url="http://jpuyy.com/wp-content/uploads/2012/03/bg.gif"
    for i in $(seq 1 $test_times);
        do curl -o /dev/null -s -w 'time_namelookup:%{time_namelookup} time_connect:%{time_connect} time_appconnect:%{time_appconnect} time_pretransfer:%{time_pretransfer} time_redirect:%{time_redirect} time_starttransfer:%{time_starttransfer} time_total:%{time_total}' $test_url >> $test_output 2>&1
        echo >> $test_output
    done
    

    按总时间(最后一段)排序,取出 10 次用时最长的访问

    cat /tmp/curl-test | sort -k 7 | tail -n 10

    服务器上到底花了多少时间呢

    time_starttransfer - time_pretransfer

    保存一个 format 文件

    
    
    \n
                  url_effective:  %{url_effective}\n
                      http_code:  %{http_code}\n
                time_namelookup:  %{time_namelookup}\n
                   time_connect:  %{time_connect}\n
                time_appconnect:  %{time_appconnect}\n
               time_pretransfer:  %{time_pretransfer}\n
                  time_redirect:  %{time_redirect}\n
             time_starttransfer:  %{time_starttransfer}\n
                   num_connects:  %{num_connects}\n
                  num_redirects:  %{num_redirects}\n
                                ----------\n
                     time_total:  %{time_total}\n
    \n
                  size_download:  %{size_download}\n
                    size_header:  %{size_header}\n
                   size_request:  %{size_request}\n
    
    

    curl 请求的时候加 -w @curl-format 调用此格式

    --resolve file.jpuyy.com:443:10.10.5.11
    

    测试 http 和 https latency
    https://sking7.github.io/articles/44961356.html

  • shell 生成 mac 地址

    MACADDR="52:54:$(dd if=/dev/urandom count=1 2>/dev/null | md5sum | sed 's/^\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4/')"; echo $MACADDR
  • 磁盘分区表 MBR 和 GPT

    MBR(Master Boot Record)

    最大只支持 2 TB 的盘,最多只支持 4 个主分区
    信息只存储在一个区域

    GPT(GUID partition table)
    是 MBR 的替代,属于 UEFI,磁盘的每一个分区都有唯一的识别号,支持更大的盘和更多的分区
    信息存储在多个区域,当一部分受损后可修复

    当 GPT 分区的盘在老的只支持 mbr 的工具下查看时,也会读到分区信息,只不过看到的是只有一个分区,这是为了防止用户误认为盘是空盘。

    当大于 2TB 盘安装安装 CentOS 7 时报错

    Your BIOS-based system needs a special partition to boot from a GPT disk label. To continue, please create a 1MiB 'biosboot' type partition

    对于这个问题,解决办法是在 kickstart 文件中添加

    part biosboot --fstype=biosboot --size=1