23 Mayıs 2020 Cumartesi

Live (Bottable) USB Yaratma

Live USB Ne İşe Yarar
İşletim sisteminin kurmadan USB'den boot edebilmeyi sağlar. Açıklaması şöyle
With a bootable Ubuntu USB stick, you can:

- Install or upgrade Ubuntu
- Test out the Ubuntu desktop experience without touching your PC configuration
- Boot into Ubuntu on a borrowed machine or from an internet cafe
- Use tools installed by default on the USB stick to repair or fix a broken configuration
1. Ubuntunun Yeni Bir Sürümünü Kurmadan Denemek İsteyelim
Adımlar burada.
1. Ubuntu ISO dosyası indirilir
2. "Startup Disk Creator" başlatılır. System->Administration->StartUp Disk Creator altındadır. Bu araç farklı dillerde olabilir. Bu yüzden İngilizce yazınca bulunmayabilir.
3. Source .iso dosyası seçilir
4. "Make Startup Disk" düğmesine tıklanır

2. Ventoy Kullanlılır
Adımlar burada

21 Mayıs 2020 Perşembe

lscpu komutu

Giriş
lshw, lsusb, lsblk, lspcii, lsmod gibi komutlarla kardeştir. İşlemcileri listeler. Linux işletim sistemi CPU değişse de etkilenmez. Açıklaması şöyle.
Ubuntu (and generally speaking Linux) don't particularly care what hardware they're running on, and will perform a check on all devices at boot rather than assuming that the same devices are there each time. So long as your CPU is compatible with your chipset, Ubuntu will see it and start using it. Ubuntu 18.04 is also only available in the 64-bit version, so that's not a concern either.

Windows, on the other hand, has licensing to worry about. When you buy a copy of Windows, the idea is that you buy it for a single machine, so Windows tends to more or less "lock" itself to a system's hardware. This doesn't mean that you can't change hardware, but if you do it too frequently (like I've managed to do) it will start to say your key is invalid. If you're in a dual-boot system, this is something to keep in mind.

seçeneksiz çalıştırma
Örnek
Şöyle yaparız. 2 işlemci gösterir. 2 socket x 1 core x 1 threads per core = 2 işlemci
$ lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                2
On-line CPU(s) list:   0,1
Thread(s) per core:    1
Core(s) per socket:    1
Socket(s):             2
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 63
Model name:            Intel(R) Xeon(R) CPU E5-2680 v3 @ 2.50GHz
Stepping:              0
CPU MHz:               2494.224
BogoMIPS:              4988.44
Hypervisor vendor:     VMware
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              30720K
NUMA node0 CPU(s):     0,1
Örnek
Şöyle yaparız. 16 işlemci gösterir. 1 socket x 8 core x 2 threads per core = 16 işlemci
$ lscpu
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   39 bits physical, 48 bits virtual
CPU(s):                          16
On-line CPU(s) list:             0-15
Thread(s) per core:              2
Core(s) per socket:              8
Socket(s):                       1
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           165
Model name:                      Intel(R) Core(TM) i7-10700T CPU @ 2.00GHz
Stepping:                        5
CPU MHz:                         1991.998
BogoMIPS:                        3983.99
Hypervisor vendor:               Microsoft
Virtualization type:             full
L1d cache:                       256 KiB
L1i cache:                       256 KiB
L2 cache:                        2 MiB
L3 cache:                        16 MiB
Vulnerability Itlb multihit:     KVM: Mitigation: VMX unsupported
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Enhanced IBRS, IBPB conditional, RSB filling
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxs
                                 r sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology cpui
                                 d pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdran
                                 d hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp ibrs_enhance
                                 d fsgsbase bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt xsaveopt xsavec
                                 xgetbv1 xsaves flush_l1d arch_capabilities

CPU op-mode Alanı

Eğer çıktı şöyle ise hem 32 bit hem de 64 bit işletim sistemi çalıştırabilir.
CPU op-mode(s):      32-bit, 64-bit
CPU(s) Alanı
Şöyle yaparız. 4 işlemci gösterir.
lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    1
Core(s) per socket:    4
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 58
Model name:            Intel(R) Core(TM) i5-3450 CPU @ 3.10GHz
Stepping:              9
CPU MHz:               1600.132
BogoMIPS:              6200.36
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              6144K
NUMA node0 CPU(s):     0-3
Örnek - CPU Sayısı Hesaplama
Elimizde şöyle bir çıktı olsun. Burada CPU sayısı 80 gösteriyor. 
CPU(s):                          80
On-line CPU(s) list:             0-79
Thread(s) per core:              2
Core(s) per socket:              20
Socket(s):                       2
NUMA node(s):                    2
Model name:                      Intel(R) Xeon(R) Gold 6242R CPU @ 3.10GHz
NUMA node0 CPU(s):               0-19,40-59
NUMA node1 CPU(s):               20-39,60-79
Cpu(s) sayısı şöyle hesaplanır.
... you have 2 physical CPUs, which each have 20 cores, which each can run 2 threads (hyperthreading). So 2x20x2 = 80, which is the number of CPUs displayed.
Örnek  - MHz Ortalaması
Açıklaması şöyle
This command shows the current "average" clock between cores (CPU MHz), and the max normal clock (CPU max MHZ).
Şöyle yaparız
$ lscpu | grep "MHz"
CPU MHz:                         1181.650
CPU max MHz:                     2200.0000
CPU min MHz:                     800.0000
Eğer şu anki durumu görmek istersek şöyle yaparız
$ cat /proc/cpuinfo | grep "MHz"
cpu MHz         : 1260.714
cpu MHz         : 1070.646
cpu MHz         : 1124.995
cpu MHz         : 1234.689
Açıklaması şöyle
This command shows the current clock for each processor core: 
Peki CPU Clock Nedir?
Açıklaması şöyle
Modern clocks are originally generated by quartz crystal oscillators of about 20MHz or so, and then the frequency is multiplied by one or more phase-locked loops to generate the clock signals for different parts of the system. (such as 4GHz for a CPU core).

Flags Alanı
Çıktı şöyle olabilir
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush
 dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc arch_perfmon pebs bts cpuid
 aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm xsave lahf_lm pti
 tpr_shadow vnmi flexpriority dtherm

f16c  half precision suppport anlamına gelir.


20 Mayıs 2020 Çarşamba

du komutu

Giriş
du komutu "disc usage" anlamına gelir.

-b seçeneği
Açıklaması şöyle. Disk space yerine, dosya büyüklüğünü gösterir.
du by default shows not the size of the file(s), but the disk space that they are using. You need to use the -b option to get sum of file sizes, instead of total of disk space used.
Aradaki farkı görmek için şöyle yaparız.
% printf test123 > a
% ls -l a
-rw-r--r-- 1 mnalis mnalis 7 Feb  1 19:57 a
% du -h a
4,0K    a
% du -hb a
7       a
-c seçeneği - grand total
Açıklaması şöyle
c: also display a grand total
En çok yer kaplayan dosyaları görmek için şöyle yaparız.
du -cha --max-depth=1 / | grep -E "M|G"
Şöyle yaparız.
du -sch /backup-root/
-h - human readable
Açıklaması şöyle.
h: show sizes in human readable format (1K, 1M, 1G, ...)
Sayı yerine megabyte, gigabyte gibi okunabilir çıktı verir. Şöyle yaparız.
du -hsc * | sort -h
Örnek
Şöyle yaparız
du -h d1

-s seçeneği
Açıklaması şöyle. Her bir kalem için çıktı verir.
s: summarize: display only a total for each argument
Açıklaması şöyle.
Instead of the default output, report only the total sum for each of the specified files.
Örnek
Şöyle yaparız.
du -sh * | sort -h
Çıktı olarak şunu alırız.
15K file1backup
16K Desktop


16 Mayıs 2020 Cumartesi

bash kodlama - for döngüsü

Giriş
İmzası şöyle.
for name [ [in [words …] ] ; ] do commands; done
- Döngüde genellikle do komutu for komutu ile aynı satıra konulur.
- Veya do komutu kendi başına bir satıra konulur.
- Eğer do ile komutu aynı satıra koyarsak bence okuması daha zor kodlar ortaya çıkıyor.

do/Komut/done Hangi Satıra Konulmalı
Komutları kendi başına bir satıra koymak bence daha iyi. Açıklaması şöyle.
A command list is a sequence of one or more simple commands separated or terminated by a newline or ; (semicolon). Furthermore, reserved words like do and done are normally preceded by a newline or ; ... In turn each time the command list following do is executed.
Örnek
Tek satır döngülerde şöyle yaparız.
$ for i in $(jot 2); do echo $i; done
Örnek
Şöyle yaparız. Ancak bence do ve komut aynı satırda olduğu için okuması zor.
for f in *
  do cmd1
  cmd2
done
Örnek
Şöyle yaparız. do komutu ayrı bir satırda
$ for i in $(jot 2)
> do
>     echo $i
> done
Break Nasıl Yapılır
Örnek - break
For döngüsünü öğrenirken sanırım en başta break; yapmayı öğrenmek gerekiyor. Şöyle yaparız.
for i in {1..10}; do
  sleep 10
  OUTPUT=$(systemctl is-active etcd)
  if [[ $OUTPUT == active ]]; then
    echo "The result is successful"
    break
  else
    echo "The result is unsuccessful"
  fi
done
Döngüde Kullanılabilen Aralık
1. {...} şeklinde sayı aralığı verilebilir. Aralık için değişken kullanılamaz
2. Değişken'in Değeri Kullanılabilir
3. seq komutuyla sayı aralığı üretilebilir.
4. uygulama çıktısı kullanılabilir
Örnek - sayı aralığı
Şöyle yaparız
for i in {0800..9999}; do
  for j in {001..032}; do
    wget http://site.com/"$i-$j".jpg
  done
done
Örnek - harf aralığı
Şöyle yaparız.
for d in {A..Z}; do 
  mkdir "$d"; 
  cd "$d"; 
done
Örnek - değişken
Şöyle yaparız.
for((i=1;i<=$rows;i++)); do
    echo "$RANDOM"
done
Örnek - array
Şöyle yaparız.
#! /bin/bash
servers=( 192.xxx.xxx.2 192.xxx.xxx.3
          192.xxx.xxx.4 192.xxx.xxx.5
          192.xxx.xxx.6 192.xxx.xxx.7
)

for server in "${servers[@]}" ; do
    echo "$server"
done
Örnek - uygulama çıktısı
Şöyle yaparız.
for node in `ls ~/sagLogs/`; do  
  foo &  
done
Örnek - seq
Şöyle yaparız.
for i in `seq 1 2000`; do
  mv file.$i.pdb file.pdb.$i
done
Örnek - seq
Şöyle yaparız.
for n in `seq 1 $count`
  do var=${numbers[0]}
done
Örnek - glob
glob'layarak dönmek için şöyle yaparız.
for globFile in lib/*.sh; do
  [ -f "$globFile" ] || continue
  source "$globFile"
done
Örnek - glob
data dizinin altında train ve test isimli iki dizin olsun. glob'layarak bu dizinlerdeki dosyaları saymak için şöyle yaparız. ${#files[@]} ile array'in uzunluğu alınır.
for dir in /data/*; do
  files=( "$dir"/*/* ); printf "%s\t%s\n" "$dir:" "${#files[@]}";
done
Çıktı olarak şunu alırız.
/data/test:     5432
/data/train:    1234
Örnek - yanlış glob
Bash pitfalls sayfasında find komutunun çıktısını döngü ile dolaşmak yanlış deniyor. Şu kod hatalı
for entry in `find . -type f`; do
  echo $entry 
done
Doğrusu find ve -print0'ı beraber kullanmak. Şöyle yaparız.
while IFS= read -r -d '' entry; do
  printf 'Processing: %s\n' "$entry"
done < <(find . -type f -print0 | sort -z)
Örnek - $@
Eğer for döngüsüne üzerinde yürüyeceği bir dizi vermezsek "positional parameters" üzerinde dolaşır. Şöyle yaparız.
#!/bin/bash
for var;
do
  echo "$var"
done
Çıktı olarak şunu alırız.
$ ./test a b c
a
b
c
Aslında şu kod ile aynı şeydir.
for i in "$@"; do
  something with "$i"
done