2021年12月15日 星期三

Little and Big Endianess, Network Byte Order, Host Byte Order

https://en.wikipedia.org/wiki/Endianness


Boiled Egg

In Gulliver's Travels, there is a discussion about how to break the shell of a boiled egg from the big of little end.


C programming

int x = 0x01234567;  // x has 4 bytes, low byte is 67
&x;  // address of &x is 0x100

Then x actually occupies 4 bytes at addresses of 0x100, 0x101, 0x102, 0x103.

if the high byte of 01 is stored at low address of 0x100, it is called big endianess.
If the low byte of 67 is stored at low address of 0x100, it is called little endianess.

x86 (i386) CPU uses little endianess. CPU order means host byte order.

Network byte order uses big endianess.


Berkeley Socket API

hotn means host byte order to network byte order
ntoh means network byte order to host byte order

#include <arpa/inet.h>
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);


C++ CPU endianess check

const int num = 1;
if (*(char *)&num == 1) {
    printf("Little-Endian\n");  # AMD Ryzen
} else {
    printf("Big-Endian\n");
}

Python CPU endianess check

import sys
print(sys.byteorder)  #  'little' on AMD Ryzen


Ubuntu endianess check

Terminal lscpu says AMD Ryzen uses Littel Endian

$ lscpu
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   43 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
NUMA node(s):                    1
Vendor ID:                       AuthenticAMD
CPU family:                      23
Model:                           113
Model name:                      AMD Ryzen 7 3700X 8-Core Processor

Python to bytes(byteorder='big' or 'small')

16 0x10
32 0x20
64 0x40
128 0x80
256 0x100
512 0x200
1024 0x400 or 0x0400

(1024).to_bytes(2, byteorder='big')  # 0x0400
b'\x04\x00'
(1025).to_bytes(2, byteorder='big')  # 0x0401
b'\x04\x01'  # MSB at beginning of byte array

Python 0x01234567 to bytes

(0x01234567).to_bytes(4, byteorder='big')  # network byte order
b'\x01#Eg'  # 01 23 45 67  #MSB 01 at beginning of byte array (big endian is natural for human)

(0x01234567).to_bytes(4, byteorder='little')  # host byte order
b'gE#\x01  # 67 45 23 01 # MSB 01 at end of byte array,

C++ memcpy

int main() {
char buffer[4] = {};
unsigned int ival = 0x01234567;
memcpy(buffer, (char*)&ival, sizeof(unsigned int));
hexdump(buffer, 4);
return 0;
}

000000: 67 45 23 01                                      gE#.

// LSB 67 is at begin of byte array (low address), little endianess
// MSB 01 at end of byte array, i.e. little endian by memcpy()



2021年12月14日 星期二

Docker on Ubuntu 2021

Docker on Ubuntu 2021

https://cwhu.medium.com/docker-tutorial-101-c3808b899ac6


Manual

cd docker-demo-app
npm install  # will read package.json file, install dependencies to node_modules
npm start # will read package.json file, display name@version, run script of "node index.js"
firefox 127.0.0.1:3000


Dockerfile

FROM node:10.15.3-alpine
WORKDIR /app
ADD . /app
RUN npm install
EXPOSE 3000
CMD node index.js

FROM means to load nodejs
WORKDIR creates /app under docker
ADD adds current directory (of Dockerfile)'s files to /app
RUN npm install
EXPOSE 3000 port from docker container to external
CME node index.js starts the http server

remember to add a .dockerignore besides Dockerfile that contains "node_modules" so that when ADD the binaries will not be in the image file, or else the image will be big.


Build docker

sudo docker build . -t docker-demo-app  # This builds an image with a tag "docker-demo-app"
sudo docker images  # list all images in all repos. the tag "docker-demo-app" appears in REPOSITORY

REPOSITORY        TAG              IMAGE ID       CREATED          SIZE
docker-demo-app   latest           af571dd0e6cb   10 minutes ago   73.4MB


Run, check, stop container from image

sudo docker run -p 3000:3000 -it af571dd0e6cb
listening on port 3000  # this is from index.js console.log("listen on port 3000")

# possible error starting userland proxy: listen tcp4 0.0.0.0:3000: bind: address already in use.
# now CTRL+C will not work

sudo docker ps

CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                                       NAMES

49cf1fee8d4d   af571dd0e6cb   "/bin/sh -c 'node in…"   4 minutes ago   Up 4 minutes   0.0.0.0:3000->3000/tcp, :::3000->3000/tcp   eager_northcutt

sudo docker stop 49cf1fee8d4d


docker run

sudo docker run -p 3000:3000 -it <IMAGE ID>  # is equivalent to below
sudo docker run -p 3000:3000 -it d18ba1dfd218 echo 123  # 123
sudo docker run -p 3000:3000 -it d18ba1dfd218 ls  # Dockerfile node_modules index.js 3 more files
sudo docker run -p 3000:3000 -it d18ba1dfd218 pwd  # /app
sudo docker run -p 3000:3000 -it d18ba1dfd218 ps  # PID 1 USER root TiIME 0:00 COMMAND ps



More notes here (read only)


# 01 Docker Helle World



install curl, add docker repo

=============================

https://www.runoob.com/docker/ubuntu-docker-install.html

curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

sudo apt-key fingerprint 0EBFCD88

sudo add-apt-repository \

   "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ \

  $(lsb_release -cs) \

  stable"


test docker

===========

sudo docker run hello-world

sudo docker run ubuntu:15.10 /bin/echo "Hello world"

sudo docker run ubunty:15.10 /bin/bash  # run then exit

sudo docker run -it ubuntu:15.10 /bin/bash

# -i for interactive STDIN

# -t for terminal

/# cat /proc/version  # Linux version 5.11.0

# exit or CTRL+D to quit


backend mode

============

sudo docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep1; done"

d65680c3740dff7cd8ba30fa4e476432bc5a3918a96a98b589ef99e6fc90d47d

# this is a container id 64 bytes


sudo docker ps

CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS              PORTS     NAMES

d65680c3740d   ubuntu:15.10   "/bin/sh -c 'while t…"   About a minute ago   Up About a minute             frosty_kepler

# STATUS can be 7 of Union[Up,Created,Restarting,Removing,Paused,Exited,Dead]


sudo docker logs d65680c3740dff7

sudo docker logs d65680c3740dff

sudo docker logs d65680c3740df

sudo docker logs d65680c3740d

sudo docker logs d656

sudo docker logs d65

sudo docker logs d6

sudo docker logs frosty_kepler

sudo docker stop frosty_kepler



# 02 Container


commands

========

sudo docker images

sudo docker run -it ubuntu:14.04 /bin/bash

sudo docker pull ubuntu:13.10

sudo docker search httpd

sudo docker pull httpd

sudo docker run httpd

sudo docker rmi httpd

sudo docker images

sudo docker rmi httpd



docker commit

=============

commit a changed docker image as another image


sudo docker run -it ubuntu:15.10 /bin/bash

apt-get --version  #  apt 1.0.10.2ubuntu1

root@44ee34f316fa

apt-get update

exit


sudo docker container ls -a | grep 44ee34f316fa

44ee34f316fa   ubuntu:15.10   "/bin/bash"              5 minutes ago       Exited (0) 4 minutes ago                                                     vibrant_spence


sudo docker commit -m="has update" -a="runoob" 44ee34f316fa runoob/ubuntu:v2

sudo docker commit -m="has update" -a="runoob" 44ee34f316fa bunbun:v3

sudo docker commit -m="has update" -a="runoob" 44ee34f316fa bun/gun:v3

REPOSITORY        TAG              IMAGE ID       CREATED          SIZE

bun/gun           v3               fb4c50ff7da4   10 seconds ago   137MB

bunbun            v3               184d7f91f982   14 seconds ago   137MB

runoob/ubuntu     v2               ab7bc95f9950   18 seconds ago   137MB


sudo docker rmi runoob/ubuntu:v2

sudo docker rmi bunbun:v3

sudo docker rmi bun/gun:v3



# 03 Dockerfile, docker build


Dockerfile

==========


vim Dockerfile


FROM    centos:6.7

MAINTAINER      Fisher "fisher@sudops.com"

RUN     /bin/echo 'root:123456' |chpasswd

RUN     useradd runoob

RUN     /bin/echo 'runoob:123456' |chpasswd

RUN     /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local

EXPOSE  22

EXPOSE  80

CMD     /usr/sbin/sshd -D


docker build

============


sudo docker build -t runoob/centos:6.7 .

// Tag an image (-t)

// Dockerfile from .


Step 1/9 : FROM    centos:6.7

6.7: Pulling from library/centos

cbddbc0189a0: Pull complete 

Digest: sha256:4c952fc7d30ed134109c769387313ab864711d1bd8b4660017f9d27243622df1

Status: Downloaded newer image for centos:6.7

 ---> 9f1de3c6ad53

Step 2/9 : MAINTAINER      Fisher "fisher@sudops.com"

 ---> Running in c3cb2314855a

Removing intermediate container c3cb2314855a

 ---> 0b636d52aae7

Step 3/9 : RUN     /bin/echo 'root:123456' |chpasswd

 ---> Running in ed30059f2f19

Removing intermediate container ed30059f2f19

 ---> 974b0c91f900

Step 4/9 : RUN     useradd runoob

 ---> Running in 53590233c07a

Removing intermediate container 53590233c07a

 ---> d6f24b904317

Step 5/9 : RUN     /bin/echo 'runoob:123456' |chpasswd

 ---> Running in b0fe55fdeea9

Removing intermediate container b0fe55fdeea9

 ---> 05dd496ca42d

Step 6/9 : RUN     /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local

 ---> Running in 7b67eb2a5f8e

Removing intermediate container 7b67eb2a5f8e

 ---> 803ed0f06699

Step 7/9 : EXPOSE  22

 ---> Running in 8139bbe1ad71

Removing intermediate container 8139bbe1ad71

 ---> 8d2db7a61261

Step 8/9 : EXPOSE  80

 ---> Running in 75b4e8f5d60f

Removing intermediate container 75b4e8f5d60f

 ---> ed01cf55131e

Step 9/9 : CMD     /usr/sbin/sshd -D

 ---> Running in 933cd65d3946

Removing intermediate container 933cd65d3946

 ---> 5bdb04f9d136

Successfully built 5bdb04f9d136

Successfully tagged runoob/centos:6.7




docker tag

==========


sudo docker images

REPOSITORY        TAG              IMAGE ID       CREATED         SIZE

runoob/centos     6.7              5bdb04f9d136   3 minutes ago   191MB


docker tag 5bdb04f9d136 runoob/centos:dev

docker tag 5bdb04f9d136 runoob/centos:bunbun


REPOSITORY        TAG              IMAGE ID       CREATED         SIZE

runoob/centos     6.7              5bdb04f9d136   4 minutes ago   191MB

runoob/centos     bunbun           5bdb04f9d136   4 minutes ago   191MB

runoob/centos     dev              5bdb04f9d136   4 minutes ago   191MB



# 04 docker port


docker run -d -P training/webapp python app.py

# latest: Pulling from training/webapp

# -d Detached mode

# -P docker run [OPTIONS] --publish-all , -P    Publish all exposed ports to random ports

# Docker identifies every port from "Dockerfile EXPOSE" and "--expose 8080"

# port mapped to epehmeral port range usually 32768 to 61000


sudo docker inspect training/webapp | vim - /Exposed

"ExposedPorts": {"5000/tcp": {}}


sudo docker inspect training/webapp | grep tcp

"5000/tcp": {}

"5000/tcp": {}


sudo docker ps

b06e5902f88b 0.0.0.0:49153->5000/tcp, :::49153->5000/tcp

# docker mapped host port 49153 to container port 5000


telnet 127.0.0.1 49153

Connected to 127.0.0.1


telnet 127.0.0.1 5000

Connection refused


firefox 127.0.0.1:49153

Hello world!


sudo docker run -d -p 5000:5000 training/webapp python app.py

sudo docker ps

1a93c1aa7e8d 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp

b06e5902f88b 0.0.0.0:49153->5000/tcp, :::49153->5000/tcp


sudo docker run -d -p 127.0.0.1:5001:5000 training/webapp python app.py

sudo docker ps

decf92a9d49c 127.0.0.1:5001->5000/tcp

1a93c1aa7e8d 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp

b06e5902f88b 0.0.0.0:49153->5000/tcp, :::49153->5000/tcp


sudo docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py

acd7d2f46eb7 5000/tcp, 127.0.0.1:5000->5000/udp

decf92a9d49c 127.0.0.1:5001->5000/tcp

1a93c1aa7e8d 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp

b06e5902f88b 0.0.0.0:49153->5000/tcp, :::49153->5000/tcp


sudo docker port strange_mclaren

sudo docker port b06e5902f88b

sudo docker port b06

5000/tcp -> 0.0.0.0:49153

5000/tcp -> :::49153


docker network

==============

sudo docker run -d -P --name runoob training/webapp python app.py

sudo docker ps -l  # latest

sudo docker network create -d bridge test-net

# bridge for bridge, overlay for swarm mode


sudo docker run -itd --name test1 --network test-net ubuntu /bin/bash

sudo docker run -itd --name test2 --network test-net ubuntu /bin/bash


sudo docker exec -it test1 /bin/bash

ping test2

command not found

apt-get update

apt install iputils-ping

ping test2

64 bytes from 87b91bb68862 (172.18.0.2): icmp_seq=1 ttl=64 time=0.031 ms


DNS

===


To be continued












End


2021年12月9日 星期四

Ubuntu increase UDP buffer size to 25MB 2021 and 2022 (multicast network card sysctl)

https://medium.com/@CameronSparr/increase-os-udp-buffers-to-improve-performance-51d167bb1360


Check:

$ sysctl net.core.rmem_max

net.core.rmem_max = 212992

$ sysctl net.core.rmem_default

net.core.rmem_default = 212992


Set:

$ sudo sysctl -w net.core.rmem_max=26214400

net.core.rmem_max = 26214400

$ sudo sysctl -w net.core.rmem_default=26214400

net.core.rmem_default = 26214400

2021年11月18日 星期四

Install MongoDB Compass on Ubuntu 20 2021

 https://www.how2shout.com/linux/install-mongodb-compass-gui-in-ubuntu-20-04-lts-linux/


https://www.mongodb.com/try/download/compass

mongodb-compass_1.29.4_amd64.deb


sudo apt install ./mongodb-compass_*_amd64.deb


MongoDB Compass

mongodb+srv://<leung>:<password>@cluster0.j807r.mongodb.net/test


a.py


import pymongo

from pymongo import MongoClient

with open('url.txt', 'r') as infile:

    url = infile.read()

connection = MongoClient(url)

print('list_database_names', connection.list_database_names())

db = connection.testdb

collection = db['instruments']

mydict = { "name": "RUNOOB", "age": 5, "url": "https://www.runoob.com" }

inserted = collection.insert_one(mydict) 

print(type(inserted))

print(inserted)


updated = collection.update_one({'name':'RUNOOB'}, {'$inc': {'age':1}}) 

print(type(updated))

print(updated)

2021年11月2日 星期二

Ubuntu 20 Google Remote Destktop 2021

https://kifarunix.com/install-and-setup-chrome-remote-desktop-on-ubuntu-20-04/


1. Install Chrome Remote Desktop Extension

2. Install Chrome Remote Desktop Package

3. mkdir ~/.config/chrome-remote-desktop

4. Enable on web

2021年10月28日 星期四

2021年10月27日 星期三

2021 VSCode ReactJS Code Formatter

 https://levelup.gitconnected.com/automatically-format-code-in-visual-studio-code-when-working-with-react-c48674a12dc5


VS Code > Extensions

Search prettier, install "Prettier - Code formatter"

CTRL + Shift + P, type "setting" to open VS Code command palette

Select "Preferences: Open User Settings", Toggle on "format on save"

Select "Default Formatter", Select "Prettier - Code formatter"

2007 to 2023 HP and Dell Servers Comparison

  HP Gen5 to Gen11  using ChatGPT HP ProLiant Gen Active Years CPU Socket Popular HP CPUs Cores Base Clock Max RAM Capacity Comparable Dell ...