Pehea e hoʻomaka ai e hoʻohana i ke ʻano mea hoʻohana ma Linux

Hoʻomaka mai ka unuhi ʻōlelo: Ma ke ʻano o ke komo nui ʻana o nā ʻano pahu like ʻole i loko o ko mākou ola, hiki ke hoihoi a pono ke ʻike me nā ʻenehana i hoʻomaka ai kēia mau mea i hoʻokahi manawa. Hiki ke hoʻohana pono ʻia kekahi o lākou a hiki i kēia lā, akā ʻaʻole hoʻomanaʻo ka poʻe a pau i ia ʻano hana (a ʻike paha, inā ʻaʻole i hopu ʻia i ko lākou ulu wikiwiki ʻana). ʻO kekahi o ia ʻenehana ʻo User Mode Linux. Ua hana ka mea kākau o ka mea kumu i ka nui o ka ʻeli ʻana, e noʻonoʻo ana i kahi o nā hanana kahiko e hana mau nei a ʻaʻole hoʻi, a hoʻohui i kahi mea e like me nā ʻōlelo aʻoaʻo i kēlā me kēia ʻaoʻao i ke ʻano o ka hana ʻana i kahi UML ulu no ʻoe iho. 2k19. A ʻae, kono mākou i ka mea kākau o ka pou mua iā Habr ʻO Cadey, no laila inā he mau nīnau kāu, e nīnau ma ka ʻōlelo Pelekania ma nā ʻōlelo.

Pehea e hoʻomaka ai e hoʻohana i ke ʻano mea hoʻohana ma Linux

ʻO ke ʻano o ka mea hoʻohana ma Linux, ʻoiaʻiʻo, he awa o ka kernel Linux iā ia iho. Hāʻawi kēia ʻano iā ʻoe e holo i kahi kernel Linux piha ma ke ʻano he mea hoʻohana a hoʻohana mau ʻia e nā mea hoʻomohala e hoʻāʻo i nā mea hoʻokele. Akā he mea pono kēia ʻano ma ke ʻano he mea hana no ka hoʻokaʻawale maʻamau, ʻo ke kumumanaʻo e like me ka hana o nā mīkini virtual. Hāʻawi kēia ʻano i kahi kaʻawale ma mua o Docker, akā ʻoi aku ka liʻiliʻi ma mua o kahi mīkini virtual piha e like me KVM a i ʻole Virtual Box.

Ma keʻano holoʻokoʻa, ʻike ʻia ke ʻano mea hoʻohana me he mea hana ʻē a paʻakikī hoʻi e hoʻohana ai, akā aia nō kāna mau hoʻohana. Ma hope o nā mea a pau, he kernel Linux piha kēia e holo nei ma ke ʻano he mea hoʻohana pono ʻole. Hiki i kēia hiʻohiʻona ke holo me ka ʻole o ka hoʻoweliweli i ka mīkini hoʻokipa. A no ka mea he kernel piha kēia, ua hoʻokaʻawale ʻia kāna mau kaʻina mai ka mīkini hoʻokipa, ʻo ia hoʻi ʻAʻole ʻike ʻia nā kaʻina hana e holo ana i loko o ka Mea Hoʻohana e ka mea hoʻokipa. ʻAʻole like kēia me kahi pahu Docker kuʻuna, kahi e ʻike mau ai ka mīkini hoʻokipa i nā kaʻina hana i loko o ka waihona. E nānā i kēia ʻāpana pstree mai kekahi o kaʻu mau kikowaena:

containerd─┬─containerd-shim─┬─tini─┬─dnsd───19*[{dnsd}]
           │                 │      └─s6-svscan───s6-supervise
           │                 └─10*[{containerd-shim}]
           ├─containerd-shim─┬─tini─┬─aerial───21*[{aerial}]
           │                 │      └─s6-svscan───s6-supervise
           │                 └─10*[{containerd-shim}]
           ├─containerd-shim─┬─tini─┬─s6-svscan───s6-supervise
           │                 │      └─surl
           │                 └─9*[{containerd-shim}]
           ├─containerd-shim─┬─tini─┬─h───13*[{h}]
           │                 │      └─s6-svscan───s6-supervise
           │                 └─10*[{containerd-shim}]
           ├─containerd-shim─┬─goproxy───14*[{goproxy}]
           │                 └─9*[{containerd-shim}]
           └─32*[{containerd}]

A e hoʻohālikelike i kēia me ka pstree o ka Linux kernel i ka Mea Hoʻohana:

linux─┬─5*[linux]
      └─slirp

Ke hana pū me nā pahu Docker, hiki iaʻu ke ʻike mai ka mea hoʻokipa i nā inoa o nā kaʻina hana e holo nei i ka malihini. ʻAʻole hiki kēia me ka Linux User Mode. He aha ka manaʻo? ʻO kēia ke ʻano o ka nānā ʻana i nā hāmeʻa e holo ana ma o ka subsystem hoʻoponopono o Linux ʻike ʻole nā kaʻina hana e holo nei i ka ʻōnaehana malihini. Akā i kekahi mau kūlana hiki ke lilo kēia hiʻohiʻona i ka pahi kaua ʻelua.

Ma ka laulā, ʻo ka pou holoʻokoʻa ma lalo nei he hōʻuluʻulu o ka noiʻi a me nā hoʻāʻo koʻikoʻi e hoʻokō i ka hopena i makemake ʻia. No ka hana ʻana i kēia, pono iaʻu e hoʻohana i nā mea hana kahiko, heluhelu i nā kumu kernel, komo i ka debugging koʻikoʻi o nā code i kākau ʻia i koʻu wā i ke kula haʻahaʻa, a me ka tinker pū me Heroku kūkulu me ka hoʻohana ʻana i kahi binary kūikawā no ka ʻimi ʻana i nā mea pono aʻu e pono ai. Ua alakaʻi kēia mau hana a pau i ka poʻe ma kaʻu IRC e kāhea ana iaʻu he kilokilo. Manaʻo wau e lilo kēia pou i palapala hilinaʻi no kekahi e hana like, akā me nā kernels hou a me nā mana OS.

hoʻoponopono

ʻO ka hoʻonohonoho ʻana i ke ʻano mea hoʻohana Linux e pili ana i kekahi mau ʻanuʻu:

  • hoʻokomo i nā hilinaʻi ma luna o ka mea hoʻokipa;
  • hoʻoiho ʻana i ka kernel Linux;
  • hoʻonohonoho hoʻonohonoho kernel;
  • hui kernel;
  • hoʻokomo i ka binary;
  • hoʻonohonoho i ka ʻōnaehana faila malihini;
  • ke koho ʻana i nā ʻāpana hoʻomaka kernel;
  • hoʻonohonoho i kahi pūnaewele malihini;
  • hoʻolana i ka kernel malihini.

Manaʻo wau inā hoʻoholo ʻoe e hana i kēia mau mea iā ʻoe iho, e hana paha ʻoe i nā mea āpau i wehewehe ʻia ma kekahi ʻōnaehana like ʻo Ubuntu a i ʻole Debian. Ua ho'āʻo wau e hoʻokō i nā mea āpau i luna ma kaʻu puʻupuʻu punahele - Alpine, akā ʻaʻohe mea i hana, ʻike ʻia no ka paʻa paʻa ʻana o ka kernel Linux i nā glibc-isms no nā mea hoʻokele i ka User Mode. Hoʻolālā wau e hōʻike i kēia i ka upstream ma hope o koʻu ʻike ʻana i ka pilikia.

Ke hoʻouka ʻana i nā hilinaʻi ma ka host

Pono ʻo Ubuntu i ka liʻiliʻi o kēia mau pūʻolo e kūkulu i ka kernel Linux (me ka manaʻo he hoʻonohonoho maʻemaʻe):

- 'build-essential'
- 'flex'
- 'bison'
- 'xz-utils'
- 'wget'
- 'ca-certificates'
- 'bc'
- 'linux-headers'

Hiki iā ʻoe ke hoʻouka iā lākou me kēia kauoha (e like me ke kumu a i ʻole sudo):

apt-get -y install build-essential flex bison xz-utils wget ca-certificates bc 
                   linux-headers-$(uname -r)

E ʻoluʻolu e hoʻomaopopo i ka holo ʻana i ka papahana hoʻonohonoho papa inoa no ka Linux kernel e pono ai ke kau ʻana libncurses-dev. E ʻoluʻolu e hoʻokomo ʻia me ka hoʻohana ʻana i kēia kauoha (e like me ke kumu a i ʻole ka hoʻohana ʻana i sudo):

apt-get -y install libncurses-dev

Ke hoʻoiho ʻana i ka kernel

E hoʻoholo i kahi e hoʻoiho ai a laila kūkulu i ka kernel. Pono ʻoe e hoʻokaʻawale ma kahi o 1,3 GB o ka wahi paʻa paʻa no kēia hana, no laila e mālama pono iā ʻoe.

A laila hele i narlt.org a e kiʻi i ka URL e hoʻoiho i ka mana kernel paʻa hou loa. I ka manawa e kākau ai i kēia pou: https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.1.16.tar.xz

Hoʻoiho i kēia faila me ka hoʻohana 'wget':

wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.1.16.tar.xz

A unuhi me 'tar':

tar xJf linux-5.1.16.tar.xz

I kēia manawa, komo mākou i ka papa kuhikuhi i hana ʻia i ka wā e wehe ai i ka tarball:

cd linux-5.1.16

Hoʻonohonoho i kahi kūkulu kernel

He hoʻonohonoho ka ʻōnaehana kūkulu kernel Hana i nā faila с lehulehu nā mea hana maʻamau a me nā palapala e hoʻokaʻawale i ke kaʻina hana. No ka hoʻomaka, wehe i ka polokalamu hoʻonohonoho pūnaewele:

make ARCH=um menuconfig

E hoʻopau hapa ia i ka hui a hōʻike i kahi pahu kamaʻilio no ʻoe. Ke ʻike ʻia ke kukui ma lalo o ka puka makani[Select]', hiki iā ʻoe ke hoʻonohonoho i nā hoʻonohonoho me ka hoʻohana ʻana i nā kī Spacebar a i ʻole Enter. ʻO ka hoʻokele a puni ka puka makani e like me ka mea maʻamau me ka hoʻohana ʻana i nā pua kīwī "up" a me "lalo", a ʻo ke koho ʻana i nā mea "hema" a i ʻole "'ākau".

ʻO ka hōʻailona ʻike -> ʻo ia hoʻi aia ʻoe i loko o kahi submenu, hiki ke kiʻi ʻia me ka hoʻohana ʻana i ke kī Enter. ʻIke ʻia ke ala i waho ma o '[Exit]'.

E hoʻokomo i nā koho i lalo i '[Select]' a e hōʻoia aia kahi hōʻailona '[*]' ma hope o lākou:

UML-specific Options:
  - Host filesystem
Networking support (enable this to get the submenu to show up):
  - Networking options:
    - TCP/IP Networking
UML Network devices:
  - Virtual network device
  - SLiRP transport

ʻO ia, hiki iā ʻoe ke haʻalele i kēia puka aniani ma ke koho ʻana i ka '[Exit]'. E hōʻoia wale i ka hopena e koi ʻia ʻoe e mālama i ka hoʻonohonoho a koho '[Yes]'.

Manaʻo wau iā ʻoe e pāʻani me nā koho kūkulu kernel ma hope o ka heluhelu ʻana i kēia pou. Mai kēia mau hoʻokolohua hiki iā ʻoe ke aʻo nui e pili ana i ka hana ʻana o ka mīkini kernel haʻahaʻa a me ka hopena o nā hae like ʻole i ke kūkulu ʻia ʻana o ka kernel.

Ke kūkulu ʻana i ka kernel

ʻO ka Linux kernel kahi papahana nui e hana i nā mea he nui. ʻOiai me kēia hoʻonohonoho liʻiliʻi ma nā lako kahiko, hiki i ka hui ke lawe i kahi manawa. No laila kūkulu i ka kernel me ka hoʻohana ʻana i kēia kauoha:

make ARCH=um -j$(nproc)

He aha ke kumu? E haʻi kēia kauoha i kā mākou mea kūkulu hale e hoʻohana i nā cores CPU āpau a me nā kaula i ka wā o ke kaʻina hana. Hui $(nproc) ma ka hope o Build e hoʻokomo i ka puka o ke kauoha nproc, he hapa ia coreutils i ka hana maʻamau ʻo Ubuntu.

Ma hope o kekahi manawa, e hōʻuluʻulu ʻia kā mākou kernel i kahi faila hiki ke hoʻokō ./linux.

Ke kau nei i ka binary

Ma muli o ka hana ʻana o ka mea hoʻohana ma Linux i kahi binary maʻamau, hiki iā ʻoe ke hoʻouka e like me nā pono ʻē aʻe. Penei kaʻu i hana ai:

mkdir -p ~/bin
cp linux ~/bin/linux

Pono nō hoʻi e hōʻoia i kēlā ~/bin aia i loko o kāu $PATH:

export PATH=$PATH:$HOME/bin

Ke hoʻonohonoho ʻana i ka Pūnaehana faila malihini

E hana i papa kuhikuhi no ka ʻōnaehana faila malihini:

mkdir -p $HOME/prefix/uml-demo
cd $HOME/prefix

E wehe i ka alpinelinux.org a i loko ʻāpana hoʻoiho e huli i ka loulou download i kēia manawa MINI ROOT FILESYSTEM. I ka wā i kākau ʻia ai kēia:

http://dl-cdn.alpinelinux.org/alpine/v3.10/releases/x86_64/alpine-minirootfs-3.10.0-x86_64.tar.gz

Hoʻoiho i kēia tarball me ka hoʻohana ʻana iā wget:

wget -O alpine-rootfs.tgz http://dl-cdn.alpinelinux.org/alpine/v3.10/releases/x86_64/alpine-minirootfs-3.10.0-x86_64.tar.gz

I kēia manawa e komo i ka papa kuhikuhi ʻōnaehana faila malihini a wehe i ka waihona:

cd uml-demo
tar xf ../alpine-rootfs.tgz

Na nā ʻanuʻu i wehewehe ʻia e hana i kahi ʻōnaehana waihona liʻiliʻi. Ma muli o ke ʻano o ka hana ʻana o ka ʻōnaehana, paʻakikī loa ka hoʻokomo ʻana i nā pūʻolo ma o ka luna Alpine apk. Akā, e lawa kēia FS e loiloi i ka manaʻo nui.

Pono mākou i kahi mea hana tini e hoʻopau i ka hoʻohana ʻana i ka hoʻomanaʻo nā hana zombie kā mākou kernel malihini.

wget -O tini https://github.com/krallin/tini/releases/download/v0.18.0/tini-static
chmod +x tini

Ke hana ʻana i kahi laina kauoha Kernel

ʻO ka Linux kernel, e like me ka hapa nui o nā papahana ʻē aʻe, aia nā manaʻo hoʻopaʻapaʻa laina kauoha e hiki ke kiʻi ʻia ma ke kuhikuhi ʻana i ke kī --help.

ʻO ia iho -kōkua

linux --help
User Mode Linux v5.1.16
        available at http://user-mode-linux.sourceforge.net/

--showconfig
    Prints the config file that this UML binary was generated from.

iomem=<name>,<file>
    Configure <file> as an IO memory region named <name>.

mem=<Amount of desired ram>
    This controls how much "physical" memory the kernel allocates
    for the system. The size is specified as a number followed by
    one of 'k', 'K', 'm', 'M', which have the obvious meanings.
    This is not related to the amount of memory in the host.  It can
    be more, and the excess, if it's ever used, will just be swapped out.
        Example: mem=64M

--help
    Prints this message.

debug
    this flag is not needed to run gdb on UML in skas mode

root=<file containing the root fs>
    This is actually used by the generic kernel in exactly the same
    way as in any other kernel. If you configure a number of block
    devices and want to boot off something other than ubd0, you
    would use something like:
        root=/dev/ubd5

--version
    Prints the version number of the kernel.

umid=<name>
    This is used to assign a unique identity to this UML machine and
    is used for naming the pid file and management console socket.

con[0-9]*=<channel description>
    Attach a console or serial line to a host channel.  See
    http://user-mode-linux.sourceforge.net/old/input.html for a complete
    description of this switch.

eth[0-9]+=<transport>,<options>
    Configure a network device.
    
aio=2.4
    This is used to force UML to use 2.4-style AIO even when 2.6 AIO is
    available.  2.4 AIO is a single thread that handles one request at a
    time, synchronously.  2.6 AIO is a thread which uses the 2.6 AIO
    interface to handle an arbitrary number of pending requests.  2.6 AIO
    is not available in tt mode, on 2.4 hosts, or when UML is built with
    /usr/include/linux/aio_abi.h not available.  Many distributions don't
    include aio_abi.h, so you will need to copy it from a kernel tree to
    your /usr/include/linux in order to build an AIO-capable UML

nosysemu
    Turns off syscall emulation patch for ptrace (SYSEMU).
    SYSEMU is a performance-patch introduced by Laurent Vivier. It changes
    behaviour of ptrace() and helps reduce host context switch rates.
    To make it work, you need a kernel patch for your host, too.
    See http://perso.wanadoo.fr/laurent.vivier/UML/ for further
    information.

uml_dir=<directory>
    The location to place the pid and umid files.

quiet
    Turns off information messages during boot.

hostfs=<root dir>,<flags>,...
    This is used to set hostfs parameters.  The root directory argument
    is used to confine all hostfs mounts to within the specified directory
    tree on the host.  If this isn't specified, then a user inside UML can
    mount anything on the host that's accessible to the user that's running
    it.
    The only flag currently supported is 'append', which specifies that all
    files opened by hostfs will be opened in append mode.

Hōʻike kēia panel i nā ʻāpana hoʻomaka nui. E holo kāua i ka kernel me ka liʻiliʻi o nā koho koho:

linux 
  root=/dev/root 
  rootfstype=hostfs 
  rootflags=$HOME/prefix/uml-demo 
  rw 
  mem=64M 
  init=/bin/sh

Hōʻike nā laina i luna i kā mākou kernel i kēia:

  • E noʻonoʻo kākou he polokalamu pseudo ka ʻōnaehana faila /dev/root.
  • E koho hostfs ma ke ʻano he mea hoʻokele ʻōnaehana waihona kumu.
  • E kau i ka waihona waihona malihini a mākou i hana ai ma ke kumu kumu.
  • A ʻae, ma ke ʻano heluhelu-kākau.
  • E hoʻohana wale i 64 megabytes o RAM (hiki iā ʻoe ke hoʻohana i ka liʻiliʻi ma muli o ka mea āu e manaʻo ai e hana, akā ʻo 64 MB ka nui o ka nui).
  • Hoʻomaka aunoa ka kernel /bin/sh pehea init-kaʻina hana.

E holo i kēia kauoha a loaʻa iā ʻoe kahi mea e like me kēia:

ʻO kekahi pepa ʻē aʻe

Core dump limits :
        soft - 0
        hard - NONE
Checking that ptrace can change system call numbers...OK
Checking syscall emulation patch for ptrace...OK
Checking advanced syscall emulation patch for ptrace...OK
Checking environment variables for a tempdir...none found
Checking if /dev/shm is on tmpfs...OK
Checking PROT_EXEC mmap in /dev/shm...OK
Adding 32137216 bytes to physical memory to account for exec-shield gap
Linux version 5.1.16 (cadey@kahless) (gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)) #30 Sun Jul 7 18:57:19 UTC 2019
Built 1 zonelists, mobility grouping on.  Total pages: 23898
Kernel command line: root=/dev/root rootflags=/home/cadey/dl/uml/alpine rootfstype=hostfs rw mem=64M init=/bin/sh
Dentry cache hash table entries: 16384 (order: 5, 131072 bytes)
Inode-cache hash table entries: 8192 (order: 4, 65536 bytes)
Memory: 59584K/96920K available (2692K kernel code, 708K rwdata, 588K rodata, 104K init, 244K bss, 37336K reserved, 0K cma-reserved)
SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=1, Nodes=1
NR_IRQS: 15
clocksource: timer: mask: 0xffffffffffffffff max_cycles: 0x1cd42e205, max_idle_ns: 881590404426 ns
Calibrating delay loop... 7479.29 BogoMIPS (lpj=37396480)
pid_max: default: 32768 minimum: 301
Mount-cache hash table entries: 512 (order: 0, 4096 bytes)
Mountpoint-cache hash table entries: 512 (order: 0, 4096 bytes)
Checking that host ptys support output SIGIO...Yes
Checking that host ptys support SIGIO on close...No, enabling workaround
devtmpfs: initialized
random: get_random_bytes called from setup_net+0x48/0x1e0 with crng_init=0
Using 2.6 host AIO
clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604462750000 ns
futex hash table entries: 256 (order: 0, 6144 bytes)
NET: Registered protocol family 16
clocksource: Switched to clocksource timer
NET: Registered protocol family 2
tcp_listen_portaddr_hash hash table entries: 256 (order: 0, 4096 bytes)
TCP established hash table entries: 1024 (order: 1, 8192 bytes)
TCP bind hash table entries: 1024 (order: 1, 8192 bytes)
TCP: Hash tables configured (established 1024 bind 1024)
UDP hash table entries: 256 (order: 1, 8192 bytes)
UDP-Lite hash table entries: 256 (order: 1, 8192 bytes)
NET: Registered protocol family 1
console [stderr0] disabled
mconsole (version 2) initialized on /home/cadey/.uml/tEwIjm/mconsole
Checking host MADV_REMOVE support...OK
workingset: timestamp_bits=62 max_order=14 bucket_order=0
Block layer SCSI generic (bsg) driver version 0.4 loaded (major 254)
io scheduler noop registered (default)
io scheduler bfq registered
loop: module loaded
NET: Registered protocol family 17
Initialized stdio console driver
Using a channel type which is configured out of UML
setup_one_line failed for device 1 : Configuration failed
Using a channel type which is configured out of UML
setup_one_line failed for device 2 : Configuration failed
Using a channel type which is configured out of UML
setup_one_line failed for device 3 : Configuration failed
Using a channel type which is configured out of UML
setup_one_line failed for device 4 : Configuration failed
Using a channel type which is configured out of UML
setup_one_line failed for device 5 : Configuration failed
Using a channel type which is configured out of UML
setup_one_line failed for device 6 : Configuration failed
Using a channel type which is configured out of UML
setup_one_line failed for device 7 : Configuration failed
Using a channel type which is configured out of UML
setup_one_line failed for device 8 : Configuration failed
Using a channel type which is configured out of UML
setup_one_line failed for device 9 : Configuration failed
Using a channel type which is configured out of UML
setup_one_line failed for device 10 : Configuration failed
Using a channel type which is configured out of UML
setup_one_line failed for device 11 : Configuration failed
Using a channel type which is configured out of UML
setup_one_line failed for device 12 : Configuration failed
Using a channel type which is configured out of UML
setup_one_line failed for device 13 : Configuration failed
Using a channel type which is configured out of UML
setup_one_line failed for device 14 : Configuration failed
Using a channel type which is configured out of UML
setup_one_line failed for device 15 : Configuration failed
Console initialized on /dev/tty0
console [tty0] enabled
console [mc-1] enabled
Failed to initialize ubd device 0 :Couldn't determine size of device's file
VFS: Mounted root (hostfs filesystem) on device 0:11.
devtmpfs: mounted
This architecture does not have kernel memory protection.
Run /bin/sh as init process
/bin/sh: can't access tty; job control turned off
random: fast init done
/ # 

E hāʻawi mai nā manipulations ma luna iā mākou ʻōnaehana malihini liʻiliʻi, me ka ʻole o nā mea like /proc a i ʻole ka inoa hoʻokipa i hāʻawi ʻia. No ka laʻana, e hoʻāʻo i kēia mau kauoha:

- uname -av
- cat /proc/self/pid
- hostname

No ka haʻalele ʻana i ka ʻōnaehana malihini, komo exit a i ʻole kaomi mana-d. E puhi kēia i ka pūpū a ukali ʻia e kahi kernel panic:

/ # exit
Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000000
fish: “./linux root=/dev/root rootflag…” terminated by signal SIGABRT (Abort)

Loaʻa iā mākou kēia kernel panic no ka mea ke manaʻo nei ka kernel Linux e holo mau ana ke kaʻina hana hoʻomaka. Me ka ʻole, ʻaʻole hiki i ka ʻōnaehana ke hana hou a pani. Akā, no ka mea he kaʻina hana hoʻohana kēia, hoʻouna ka hopena i ka hopena SIGABRT, e alakai ana i ka puka.

Hoʻonohonoho i kahi pūnaewele malihini

Akā ʻo kēia kahi e hoʻomaka ai nā mea e hewa ai no mākou. ʻO ka pūnaewele ma ka User Mode Linux kahi e hoʻomaka ai ka manaʻo holoʻokoʻa o kahi "mode hoʻohana" palena ʻole. Ma hope o nā mea a pau, ua kaupalena ʻia ka pūnaewele ma ka pae ʻōnaehana pono nā ʻano hoʻokō no nā kumu maopopo iā mākou a pau.

Nānā per .: Hiki iā ʻoe ke heluhelu hou aku e pili ana i nā koho like ʻole no ka hana ʻana me kahi pūnaewele ma UML maanei.

Huakai e slirp

Eia naʻe, aia kahi mea hana kahiko a kokoke ʻole i kākoʻo ʻia Slirp, kahi e hiki ai i ka User Mode Linux ke launa pū me ka pūnaewele. Hoʻohana ʻo ia e like me kahi waihona TCP/IP pae hoʻohana a ʻaʻole koi i nā ʻae ʻōnaehana e holo. ʻO kēia mea hana hoʻokuʻu ʻia i ka makahiki 1995, a ua kau ʻia ka mea hou hope 2006. He kahiko loa ʻo Slirp. I loko o ka manawa me ke kākoʻo ʻole a me nā mea hou, ua hele mai nā mea hoʻopili i kēia manawa hiki ke wehewehe ʻia kēia mea hana "code rot".

No laila, e hoʻoiho iā Slirp mai nā hale waihona ʻo Ubuntu a hoʻāʻo e holo:

sudo apt-get install slirp
/usr/bin/slirp
Slirp v1.0.17 (BETA)

Copyright (c) 1995,1996 Danny Gasparovski and others.
All rights reserved.
This program is copyrighted, free software.
Please read the file COPYRIGHT that came with the Slirp
package for the terms and conditions of the copyright.

IP address of Slirp host: 127.0.0.1
IP address of your DNS(s): 1.1.1.1, 10.77.0.7
Your address is 10.0.2.15
(or anything else you want)

Type five zeroes (0) to exit.

[autodetect SLIP/CSLIP, MTU 1500, MRU 1500, 115200 baud]

SLiRP Ready ...
fish: “/usr/bin/slirp” terminated by signal SIGSEGV (Address boundary error)

E ko'u mau akua. E hoʻokomo i ka Slirp debugger a ʻike inā hiki iā mākou ke ʻike i ka mea e hana nei ma aneʻi:

sudo apt-get install gdb slirp-dbgsym
gdb /usr/bin/slirp
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /usr/bin/slirp...Reading symbols from /usr/lib/debug/.build-id/c6/2e75b69581a1ad85f72ac32c0d7af913d4861f.debug...done.
done.
(gdb) run
Starting program: /usr/bin/slirp
Slirp v1.0.17 (BETA)

Copyright (c) 1995,1996 Danny Gasparovski and others.
All rights reserved.
This program is copyrighted, free software.
Please read the file COPYRIGHT that came with the Slirp
package for the terms and conditions of the copyright.

IP address of Slirp host: 127.0.0.1
IP address of your DNS(s): 1.1.1.1, 10.77.0.7
Your address is 10.0.2.15
(or anything else you want)

Type five zeroes (0) to exit.

[autodetect SLIP/CSLIP, MTU 1500, MRU 1500, 115200 baud]

SLiRP Ready ...

Program received signal SIGSEGV, Segmentation fault.
                                                    ip_slowtimo () at ip_input.c:457
457     ip_input.c: No such file or directory.

Ke kuʻi nei ka hewa iā mākou keia laina. E nānā kāua i ka stacktrace, malia paha e kōkua kekahi mea iā mākou ma laila:

(gdb) bt full
#0  ip_slowtimo () at ip_input.c:457
        fp = 0x55784a40
#1  0x000055555556a57c in main_loop () at ./main.c:980
        so = <optimized out>
        so_next = <optimized out>
        timeout = {tv_sec = 0, tv_usec = 0}
        ret = 0
        nfds = 0
        ttyp = <optimized out>
        ttyp2 = <optimized out>
        best_time = <optimized out>
        tmp_time = <optimized out>
#2  0x000055555555b116 in main (argc=1, argv=0x7fffffffdc58) at ./main.c:95
No locals.

Maanei mākou e ʻike ai i ka wā e hoʻomaka ai ka loop loop, i ka wā e hoʻāʻo ai ʻo slirp e nānā i nā manawa. I kēia manawa pono wau e haʻalele i ka hoʻāʻo ʻana i ka debug. Akā e ʻike kākou inā hana ʻo Slirp, i kūkulu ʻia mai nā ʻano like ʻole. Ua hoʻoiho hou au i ka waihona mai ka pūnaewele Ke kumuhana, no ka mea, he ʻeha ka huki ʻana i kekahi mea mai laila mai ma o ka laina kauoha:

cd ~/dl
wget https://xena.greedo.xeserv.us/files/slirp-1.0.16.tar.gz
tar xf slirp-1.0.16.tar.gz
cd slirp-1.0.16/src
./configure --prefix=$HOME/prefix/slirp
make

Maanei mākou e ʻike ai i nā mākaʻikaʻi e pili ana i nā hana i kūkulu ʻole ʻia, ʻo ia hoʻi, e pili ana i ka hiki ʻole ke hoʻopili i ka faila binary hopena. ʻIke ʻia ma waena o 2006 a i kēia manawa ua hoʻōki ʻo gcc i ka hana ʻana i nā hōʻailona i hoʻohana ʻia i nā hana waena i kūkulu ʻia. E ho'āʻo kākou e pani i ka huaʻōlelo inline i kahi ʻōlelo ʻole a nānā i ka hopena:

vi slirp.h
:6
a
<enter>
#define inline /**/
<escape>
:wq
make

ʻAʻole. ʻAʻole hana kēia. ʻAʻole hiki ke loaʻa nā hōʻailona no kēia mau hana.

I kēia manawa ua haʻalele wau a hoʻomaka wau e ʻimi ma Github Hana ʻo Heroku i nā pūʻolo. ʻO koʻu manaʻo ʻo ia kekahi o Heroku build package e loaʻa i nā binaries aʻu e pono ai. I ka hopena, ua alakaʻi kaʻu ʻimi iaʻu ma ʻaneʻi. Ua hoʻoiho a wehe wau uml.tar.gz a ua ʻike i kēia mau mea:

total 6136
-rwxr-xr-x 1 cadey cadey   79744 Dec 10  2017 ifconfig*
-rwxr-xr-x 1 cadey cadey     373 Dec 13  2017 init*
-rwxr-xr-x 1 cadey cadey  149688 Dec 10  2017 insmod*
-rwxr-xr-x 1 cadey cadey   66600 Dec 10  2017 route*
-rwxr-xr-x 1 cadey cadey  181056 Jun 26  2015 slirp*
-rwxr-xr-x 1 cadey cadey 5786592 Dec 15  2017 uml*
-rwxr-xr-x 1 cadey cadey     211 Dec 13  2017 uml_run*

He slirp binary kēia! He hana anei?

./slirp
Slirp v1.0.17 (BETA) FULL_BOLT

Copyright (c) 1995,1996 Danny Gasparovski and others.
All rights reserved.
This program is copyrighted, free software.
Please read the file COPYRIGHT that came with the Slirp
package for the terms and conditions of the copyright.

IP address of Slirp host: 127.0.0.1
IP address of your DNS(s): 1.1.1.1, 10.77.0.7
Your address is 10.0.2.15
(or anything else you want)

Type five zeroes (0) to exit.

[autodetect SLIP/CSLIP, MTU 1500, MRU 1500]

SLiRP Ready ...

ʻAʻole hāʻule - no laila pono e hana! E kanu kākou i kēia binary i loko ~/bin/slirp:

cp slirp ~/bin/slirp

Inā hoʻopau ka mea nāna i hana i ka pōʻai, I hana i aniani.

Hoʻonohonoho hoʻonohonoho pūnaewele

I kēia manawa e hoʻonohonoho mākou i ka pūnaewele ma luna o kā mākou malihini kipa. E hōʻano hou i nā ʻāpana hoʻomaka:

linux 
  root=/dev/root 
  rootfstype=hostfs 
  rootflags=$HOME/prefix/uml-demo 
  rw 
  mem=64M 
  eth0=slirp,,$HOME/bin/slirp 
  init=/bin/sh

I kēia manawa, hiki iā mākou ke ho'ā i ka pūnaewele:

mount -t proc proc proc/
mount -t sysfs sys sys/

ifconfig eth0 10.0.2.14 netmask 255.255.255.240 broadcast 10.0.2.15
route add default gw 10.0.2.2

ʻO nā kauoha hoʻonohonoho mua ʻelua /proc и /sys pono no ka hana ifconfig, ka mea e hoʻonohonoho i kahi kikowaena pūnaewele e kamaʻilio me Slirp. Hui route hoʻonohonoho i ka papa kuhikuhi ʻo kernel e koi aku i nā kaʻa a pau e hoʻouna ʻia ma o ka tunnel Slirp. E nānā i kēia me kahi nīnau DNS:

nslookup google.com 8.8.8.8
Server:    8.8.8.8
Address 1: 8.8.8.8 dns.google

Name:      google.com
Address 1: 172.217.12.206 lga25s63-in-f14.1e100.net
Address 2: 2607:f8b0:4006:81b::200e lga25s63-in-x0e.1e100.net

Hana ia!

Nānā i kēlā me kēia: Me he mea lā, ua kākau ʻia ka pou kumu ma ka pākaukau me kahi kāleka pūnaewele uea, a i ʻole kekahi hoʻonohonoho ʻē aʻe i koi ʻole i nā mea hoʻokele hou. Ma kahi pona me WiFi 8265 mai Intel, hiki mai kahi hewa i ka wā e hoʻonui ai i ka pūnaewele

/ # ifconfig eth0 10.0.2.14 netmask 255.255.255.240 broadcast 10.0.2.15
slirp_tramp failed - errno = 2
ifconfig: ioctl 0x8914 failed: No such file or directory
/ #

ʻIke ʻia, ʻaʻole hiki i ka kernel ke kamaʻilio me ka mea hoʻokele kaʻa pūnaewele. ʻO ka ho'āʻo e hōʻuluʻulu i ka firmware i loko o ka kernel, akā naʻe, ʻaʻole i hoʻoponopono i ke kūlana. I ka manawa o ka paʻi ʻana, ʻaʻole hiki ke loaʻa ka hopena i kēia hoʻonohonoho kikoʻī. Ma nā configs maʻalahi (no ka laʻana, ma Virtualbox), ua hoʻokiʻekiʻe pololei ʻia ka interface.

E hoʻokaʻawale i ka hoʻihoʻi ʻana me ka hoʻohana ʻana i ka script shell penei:

#!/bin/sh
# init.sh

mount -t proc proc proc/
mount -t sysfs sys sys/
ifconfig eth0 10.0.2.14 netmask 255.255.255.240 broadcast 10.0.2.15
route add default gw 10.0.2.2

echo "networking set up"

exec /tini /bin/sh

A e kaha iā ia he mea hiki ke hoʻokō:

chmod +x init.sh

A laila e hoʻololi kākou i ka laina kauoha kernel:

linux 
  root=/dev/root 
  rootfstype=hostfs 
  rootflags=$HOME/prefix/uml-demo 
  rw 
  mem=64M 
  eth0=slirp,,$HOME/bin/slirp 
  init=/init.sh

A e haʻi hou kākou:

SLiRP Ready ...
networking set up
/bin/sh: can't access tty; job control turned off

nslookup google.com 8.8.8.8
Server:    8.8.8.8
Address 1: 8.8.8.8 dns.google

Name:      google.com
Address 1: 172.217.12.206 lga25s63-in-f14.1e100.net
Address 2: 2607:f8b0:4004:800::200e iad30s09-in-x0e.1e100.net

Paʻa ka pūnaewele!

waihona docker

I mea e maʻalahi ai iā ʻoe ke nānā i kēia mau mea a pau, ua hōʻiliʻili wau dockerfile, ka mea e hoʻokaʻawale i ka hapa nui o nā ʻanuʻu i wehewehe ʻia a pono e hāʻawi iā ʻoe i kahi hoʻonohonoho hana. Ua loaʻa iaʻu hoʻonohonoho kernel mākaukau, aia nā mea a pau i wehewehe ʻia ma ka pou. Akā, he mea nui e hoʻomaopopo ma aneʻi ua wehewehe wau i nā hoʻonohonoho liʻiliʻi wale nō.

Manaʻo wau ua kōkua kēia pou iā ʻoe e hoʻomaopopo i ka lawe ʻana i kahi kernel malihini. Ua hoʻololi ʻia he ʻano monster, akā ua manaʻo ʻia ka paʻi ʻana e lilo i alakaʻi piha i ke kumuhana o ka hui ʻana, hoʻokomo a hoʻonohonoho ʻana i ke ʻano mea hoʻohana ma Linux ma lalo o nā mana hou o nā ʻōnaehana hana o kēia ʻohana. Pono e hoʻokomo i nā lawelawe a me nā polokalamu ʻē aʻe i loko o ka ʻōnaehana malihini. No ka mea ua hoʻolaha ʻia nā kiʻi pahu Docker i nā tarballs, pono ʻoe e hiki ke unuhi i ke kiʻi ma o docker export, a laila e hoʻoholo i ke ala e hoʻokomo ai i ke kumu o ka ʻōnaehana file kernel malihini. A laila, e holo i ka script shell.

Mahalo nui iā Rkeene mai #lobsters ma Freenode. Inā ʻaʻole kāna kōkua i ka hoʻopau ʻana iā Slirp, ʻaʻole wau e hiki i kēia mamao. ʻAʻohe oʻu manaʻo pehea e hana pololei ai kāna ʻōnaehana Slackware me ka slirp, akā ʻaʻole i ʻae kaʻu ʻōnaehana Ubuntu a me Alpine i ka slirp a ua ʻōlelo mai ka binary Rkeene iaʻu. Akā ua lawa iaʻu i ka liʻiliʻi loa e hana kekahi mea iaʻu.

Source: www.habr.com

Pākuʻi i ka manaʻo hoʻopuka