Giunsa Pagsugod ang Paggamit sa Mode sa Gumagamit sa Linux

Pasiuna gikan sa tighubad: Batok sa backdrop sa kaylap nga pagsulod sa lain-laing mga matang sa mga sudlanan sa atong mga kinabuhi, kini mahimong medyo makapaikag ug mapuslanon sa pagpangita sa unsa nga mga teknolohiya kining tanan nagsugod sa usa ka panahon. Ang uban kanila mahimong magamit nga mapuslanon hangtod karon, apan dili tanan nakahinumdom sa ingon nga mga pamaagi (o nahibal-an, kung wala sila nadakpan sa ilang paspas nga pag-uswag). Usa sa maong teknolohiya mao ang User Mode Linux. Ang tagsulat sa orihinal nga naghimo sa usa ka patas nga kantidad sa pagkalot, sa paghunahuna kon hain sa mga daan nga mga kalamboan sa gihapon nagtrabaho ug nga wala, ug gibutang sa tingub sa usa ka butang sama sa sunod-sunod nga mga panudlo kon sa unsang paagi sa paghimo sa usa ka home-grown UML alang sa imong kaugalingon sa 2k19. Ug oo, among giimbitar ang tagsulat sa orihinal nga post sa Habr Cadey, busa kung naa kay pangutana, pangutana sa English sa comments.

Giunsa Pagsugod ang Paggamit sa Mode sa Gumagamit sa Linux

Ang User Mode sa Linux, sa tinuud, usa ka pantalan sa Linux kernel sa iyang kaugalingon. Kini nga mode nagtugot kanimo sa pagpadagan sa usa ka bug-os nga Linux kernel isip usa ka proseso sa user ug kasagarang gigamit sa mga developers sa pagsulay sa mga drayber. Apan kini nga paagi mapuslanon usab isip usa ka himan alang sa kinatibuk-ang pag-inusara, ang prinsipyo nga susama sa operasyon sa mga virtual machine. Kini nga mode naghatag og dugang nga pagkahimulag kay sa Docker, apan ubos pa kay sa usa ka hingpit nga virtual machine sama sa KVM o Virtual Box.

Sa kinatibuk-an, ang User Mode ingon og usa ka talagsaon ug lisud nga himan nga gamiton, apan kini adunay mga gamit niini. Human sa tanan, kini usa ka bug-os nga Linux kernel nga nagdagan ingon usa ka dili pribilihiyo nga tiggamit. Kini nga bahin nagtugot sa posibleng dili kasaligan nga code nga modagan nga walay bisan unsa nga hulga sa host machine. Ug tungod kay kini usa ka bug-os nga kernel, ang mga proseso niini nahimulag gikan sa host machine, kana Ang mga proseso nga nagdagan sulod sa User Mode dili makita sa host. Dili kini sama sa usa ka tradisyonal nga sudlanan sa Docker, diin kanunay nga makita sa host machine ang mga proseso sa sulod sa pagtipig. Tan-awa kini nga piraso sa pstree gikan sa usa sa akong mga server:

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}]

Ug itandi kini sa pstree sa Linux kernel sa User Mode:

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

Kung nagtrabaho kauban ang mga sudlanan sa Docker, makita nako gikan sa host ang mga ngalan sa mga proseso nga nagdagan sa bisita. Dili kini mahimo sa Linux User Mode. Unsay buot ipasabot niini? Kini nagpasabut nga ang mga himan sa pag-monitor nga nagdagan sa subsystem sa pag-audit sa Linux dili makakita mga proseso nga nagdagan sa sistema sa bisita. Apan sa pipila ka mga sitwasyon kini nga bahin mahimo nga usa ka duha ka sulab nga espada.

Sa kinatibuk-an, ang tibuuk nga post sa ubos usa ka koleksyon sa panukiduki ug dili maayo nga mga pagsulay aron makab-ot ang gitinguha nga sangputanan. Aron mahimo kini, kinahanglan nako nga mogamit sa lainlaing mga karaan nga mga himan, magbasa sa mga gigikanan sa kernel, moapil sa intensive debugging sa code nga gisulat sa elementarya pa ako, ug usab mag-tinker sa Heroku build gamit ang usa ka espesyal nga binary sa pagpangita sa mga himan nga akong gikinahanglan. Kining tanan nga trabaho mitultol sa mga lalaki sa akong IRC nga nagtawag kanako nga usa ka salamangkero. Nanghinaut ko nga kini nga post magsilbi nga kasaligan nga dokumentasyon alang sa usa ka tawo nga buhaton ang parehas nga butang, apan adunay mas bag-ong mga kernel ug mga bersyon sa OS.

kausaban

Ang pag-set up sa Linux User Mode naglakip sa daghang mga lakang:

  • pag-instalar sa mga dependency sa host;
  • pag-download sa Linux kernel;
  • setup sa pagtukod sa kernel;
  • asembliya sa kernel;
  • pag-instalar sa binary;
  • pag-set up sa guest file system;
  • pagpili sa mga parameter sa paglansad sa kernel;
  • paghimo sa usa ka bisita nga network;
  • paglansad sa kernel sa bisita.

Nagtuo ko nga kung magdesisyon ka nga buhaton kining tanan sa imong kaugalingon, lagmit buhaton nimo ang tanan nga gihulagway sa pipila nga sistema nga sama sa Ubuntu o Debian. Gisulayan nako nga ipatuman ang tanan sa ibabaw sa akong paborito nga pag-apod-apod - Alpine, apan walay nagtrabaho, dayag tungod kay ang Linux kernel hugot nga gigapos sa glibc-isms alang sa mga drayber sa User Mode. Nagplano ko nga i-report kini sa upstream pagkahuman nako mahibal-an ang problema.

Pag-instalar sa mga dependency sa host

Ang Ubuntu nanginahanglan labing menos sa mosunod nga mga pakete aron matukod ang Linux kernel (nagtuo nga usa ka limpyo nga pag-install):

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

Mahimo nimo kining i-install gamit ang mosunod nga sugo (ingon nga gamut o sudo):

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

Palihug timan-i nga ang pagpadagan sa programa sa pag-customize sa menu alang sa Linux kernel magkinahanglan og pag-instalar libncurses-dev. Palihug siguroha nga kini na-install gamit ang mosunod nga sugo (ingon nga gamut o gamit ang sudo):

apt-get -y install libncurses-dev

Pag-download sa kernel

Tinoa kung asa i-download ug dayon itukod ang kernel. Kinahanglan nimo nga igahin ang gibana-bana nga 1,3 GB nga espasyo sa hard drive alang niini nga operasyon, busa siguroha nga naa nimo kini.

Unya adto sa kernel.org ug kuhaa ang URL aron ma-download ang pinakabag-o nga stable nga bersyon sa kernel. Sa panahon sa pagsulat niini nga post: https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.1.16.tar.xz

I-download kini nga file gamit ang 'wget':

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

Ug i-extract kini gamit ang 'tar':

tar xJf linux-5.1.16.tar.xz

Karon mosulod kami sa direktoryo nga gihimo sa pag-unpack sa tarball:

cd linux-5.1.16

Pag-set up sa usa ka kernel build

Ang sistema sa pagtukod sa kernel usa ka set Paghimo og mga file с daghan naandan nga mga himan ug mga script aron ma-automate ang proseso. Aron makasugod, ablihi ang online setup program:

make ARCH=um menuconfig

Kini partially makompleto ang asembliya ug magpakita sa usa ka dialog box alang kanimo. Sa diha nga ang kahayag makita sa ubos sa bintana[Select]', mahimo nimong i-configure ang mga setting gamit ang Spacebar o Enter key. Ang pag-navigate sa palibot sa bintana sama sa naandan gamit ang mga arrow sa keyboard nga "up" ug "down", ug ang pagpili sa mga elemento mao ang "wala" o "tuo".

Ang indikasyon sa pagtan-aw -> nagpasabut nga naa ka sa usa ka submenu, nga mahimong ma-access gamit ang Enter key. Ang paagi sa paggawas klaro nga pinaagi sa '[Exit]'.

Ilakip ang mosunod nga mga opsyon sa '[Select]' ug siguroha nga adunay '[*]' nga simbolo sunod kanila:

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

Mao ra kana, mahimo nimong mogawas kini nga bintana pinaagi sa sunod-sunod nga pagpili sa '[Exit]'. Siguruha lang nga sa katapusan giaghat ka nga i-save ang pagsumpo ug pilia ang '[Yes]'.

Girekomenda ko nga magdula ka sa mga kapilian sa pagtukod sa kernel pagkahuman mabasa kini nga post. Gikan sa kini nga mga eksperimento daghan ka nga makakat-on bahin sa kung giunsa ang mga mekaniko sa ubos nga lebel sa kernel ug kung giunsa ang lainlaing mga bandila makaapekto kung giunsa ang paghimo sa kernel.

Pagtukod sa kernel

Ang Linux kernel usa ka dako nga programa nga naghimo sa daghang mga butang. Bisan sa kini nga gamay nga pag-configure sa mas daan nga hardware, ang pag-assemble mahimong magdugay. Busa pagtukod sa kernel gamit ang mosunod nga sugo:

make ARCH=um -j$(nproc)

Para unsa? Ang kini nga mando magsulti sa among magtutukod nga gamiton ang tanan nga magamit nga mga cores sa CPU ug mga hilo sa panahon sa proseso sa pagtukod. Team $(nproc) sa katapusan sa Build insert ang output sa command nproc, nga maoy bahin coreutils sa standard nga pagtukod sa Ubuntu.

Pagkahuman sa pila ka oras, ang among kernel ma-compile sa usa ka executable file ./linux.

Pag-instalar sa binary

Tungod kay ang Mode sa Gumagamit sa Linux nagmugna usa ka regular nga binary, mahimo nimo kini i-install sama sa bisan unsang uban nga gamit. Ania kung giunsa nako kini gibuhat:

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

Angayan usab nga sigurohon kana ~/bin naa sa imong $PATH:

export PATH=$PATH:$HOME/bin

Pag-configure sa Guest File System

Paghimo og direktoryo alang sa guest file system:

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

Ablihi ang alpinelinux.org ug sa seksyon sa pag-download pangitaa ang kasamtangan nga link sa pag-download MINI ROOT FILESYSTEM. Sa panahon sa pagsulat kini mao ang:

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

I-download kini nga tarball gamit ang 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

Karon pagsulod sa direktoryo sa sistema sa file sa bisita ug i-unpack ang archive:

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

Ang mga lakang nga gihulagway maghimo usa ka gamay nga template sa file system. Tungod sa paagi sa pagtrabaho sa sistema, ang pag-install sa mga pakete pinaagi sa Alpine apk manager mahimong labi ka lisud. Apan kini nga FS igo na sa pagtimbang-timbang sa kinatibuk-ang ideya.

Nagkinahanglan usab kami og himan tini aron mahunong ang pagkonsumo sa memorya mga proseso sa zombie among bisita kernel.

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

Paghimo usa ka Kernel Command Line

Ang Linux kernel, sama sa kadaghanan sa ubang mga programa, adunay mga argumento sa command line nga mahimong ma-access pinaagi sa pagtino sa yawe --help.

Mismo—tabang

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.

Kini nga panel nagpasiugda sa mga nag-unang mga parameter sa paglansad. Atong pagdagan ang kernel nga adunay labing gamay nga gikinahanglan nga set sa mga kapilian:

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

Ang mga linya sa ibabaw nagsulti sa among kernel sa mosunod:

  • Ibutang nato nga ang root filesystem usa ka pseudo device /dev/root.
  • Pilia hostfs ingon usa ka driver sa sistema sa root file.
  • I-mount ang guest filesystem nga among gibuhat sa root device.
  • Ug oo, sa read-write mode.
  • Gamita lang ang 64 megabytes sa RAM (mahimo nimong gamiton ang labi ka gamay depende sa imong plano nga buhaton, apan ang 64 MB daw ang labing kaayo nga kantidad).
  • Ang kernel awtomatikong magsugod /bin/sh sa unsa nga paagi init- proseso.

Pagdalagan kini nga sugo ug kinahanglan ka nga makakuha og usa ka butang sama sa mosunod:

Laing sheet

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
/ # 

Ang mga manipulasyon sa ibabaw maghatag kanato gamay nga sistema sa bisita, nga walay mga butang sama sa /proc o gi-assign nga hostname. Pananglitan, sulayi ang mosunod nga mga sugo:

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

Aron maka-log out sa guest system, pagsulod exit o press control-d. Kini magpabuto sa kabhang nga gisundan sa usa ka kernel panic:

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

Nakuha namo kini nga kernel panic tungod kay ang Linux kernel naghunahuna nga ang proseso sa pagsugod kanunay nga nagdagan. Kung wala kini, ang sistema dili na molihok ug ma-shut down. Apan tungod kay kini usa ka proseso sa user mode, ang resulta nga resulta nagpadala sa iyang kaugalingon sa SIGABRT, nga nagpadulong sa exit.

Pag-set up ug guest network

Apan dinhi nagsugod ang mga butang nga dili maayo alang kanamo. Ang networking sa User Mode Linux kung diin ang tibuuk nga konsepto sa usa ka limitado nga "mode sa gumagamit" nagsugod sa pagkaguba. Human sa tanan, ang network kasagaran limitado sa lebel sa sistema pribilihiyo execution modes alang sa klaro nga mga rason alang kanatong tanan.

Nota per .: Makabasa ka og dugang mahitungod sa lain-laing mga opsyon alang sa pagtrabaho uban sa usa ka network sa UML dinhi.

Panaw sa pag-slirp

Bisan pa, adunay usa ka karaan ug halos wala gisuportahan nga himan nga gitawag Slirp, diin ang User Mode Linux maka-interact sa network. Kini naglihok halos sama sa usa ka user-level TCP/IP stack ug wala magkinahanglan sa bisan unsa nga sistema permiso sa pagdagan. Kini nga himan mao ang gipagawas kaniadtong 1995, ug ang kataposang update kay napetsahan 2006. Ang Slirp tigulang na kaayo. Sa panahon nga walay suporta ug mga update, ang mga compiler miabot na kaayo nga karon kini nga himan mahulagway lamang nga "pagkadunot sa code".

Mao nga, i-download naton ang Slirp gikan sa mga repositoryo sa Ubuntu ug sulayan kini nga ipadagan:

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)

Oh akong pagkamaayo. Atong i-install ang Slirp debugger ug tan-awon kung mahibal-an ba naton kung unsa ang nahitabo dinhi:

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.

Ang kasaypanan naghampak kanato niini nga linya. Atong tan-awon ang stacktrace, tingali adunay makatabang kanato didto:

(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.

Dinhi atong makita nga ang pagkahagsa nahitabo sa panahon sa pagsugod sa main loop, sa diha nga ang slirp mosulay sa pagsusi sa mga timeout. Niining higayona kinahanglan kong mohunong sa pagsulay sa pag-debug. Apan tan-awon nato kung ang Slirp, nga gitukod gikan sa mga lahi, nagtrabaho. Gi-download nako pag-usab ang archive direkta gikan sa site Sourceforge, tungod kay ang pag-drag sa usa ka butang gikan didto pinaagi sa command line usa ka sakit:

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

Dinhi atong makita ang mga alerto mahitungod sa wala matino nga built-in nga mga gimbuhaton, nga mao, mahitungod sa imposibilidad sa pag-link sa resulta nga binary file. Mopatim-aw nga tali sa 2006 ug karon ang gcc mihunong sa paghimo og mga simbolo nga gigamit sa gitukod nga intermediate function. Atong sulayan ang pag-ilis sa keyword inline sa usa ka walay sulod nga komento ug tan-awa ang resulta:

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

Dili. Dili usab kini molihok. Dili gihapon makit-an ang mga simbolo alang niini nga mga gimbuhaton.

Niini nga punto mihunong ako ug nagsugod sa pagpangita sa Github Mga pakete sa paghimo sa Heroku. Ang akong teorya mao nga ang pipila ka Heroku build package maglangkob sa mga binary nga akong gikinahanglan. Sa katapusan, ang akong pagpangita mitultol kanako dinhi ra. Gi-download nako ug gi-unpack uml.tar.gz ug nakit-an ang mosunod:

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*

Kini usa ka slirp binary! Mogana ba kini?

./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 ...

Dili kini mahulog - busa kini kinahanglan nga molihok! Atong itanom kini nga binary sa ~/bin/slirp:

cp slirp ~/bin/slirp

Kung ang tiglalang sa package magtangtang niini, I naghimo ug salamin.

Pag-ayo sa network

Karon atong i-configure ang network sa atong guest core. Atong i-update ang mga parametro sa paglansad:

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

Karon atong palihokon ang network:

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

Ang unang duha ka mga sugo sa pag-configure /proc и /sys gikinahanglan alang sa trabaho ifconfig, nga nagbutang ug network interface aron makigkomunikar sa Slirp. Team route nagtakda sa kernel routing table aron pugson ang tanang trapiko nga ipadala pinaagi sa Slirp tunnel. Atong susihon kini sa usa ka pangutana sa 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

Nagtrabaho!

Note per .: Dayag, ang orihinal nga post gisulat sa desktop nga adunay wired network card, o uban pang configuration nga wala magkinahanglan ug dugang nga mga drayber. Sa usa ka laptop nga adunay WiFi 8265 gikan sa Intel, usa ka sayup ang nahitabo kung gipataas ang network

/ # 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
/ #

Dayag, ang kernel dili makakomunikar sa driver sa network drive. Ang pagsulay sa pagtipon sa firmware sa kernel, sa kasubo, wala matul-id ang sitwasyon. Sa panahon sa pagmantala, wala pay solusyon nga makit-an niining partikular nga configuration. Sa mas simple nga mga config (pananglitan, sa Virtualbox), ang interface gipataas sa husto.

Atong i-automate ang redirection gamit ang mosunod nga shell script:

#!/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

Ug markahi kini nga ma-executable:

chmod +x init.sh

Ug unya maghimo kita mga pagbag-o sa linya sa mando sa kernel:

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

Ug atong balikon:

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

Stable ang network!

docker file

Aron mas sayon ​​​​alang kanimo ang pagsusi niining tanan, akong gikolekta Dockerfile, nga nag-automate sa kadaghanan sa mga lakang nga gihulagway ug kinahanglan maghatag kanimo sa usa ka nagtrabaho nga configuration. naa sad ko andam nga pag-configure sa kernel, nga naglangkob sa tanan nga gihulagway sa post. Apan importante nga masabtan nga dinhi akong gilatid lamang ang minimum nga mga setting.

Nanghinaut ko nga kini nga post nakatabang kanimo nga masabtan kung giunsa ang pagdala sa usa ka kernel sa bisita. Kini nahimo nga usa ka matang sa mangtas, apan ang publikasyon gituyo aron mahimong usa ka komprehensibo nga giya sa hilisgutan sa pag-assemble, pag-instalar ug pag-configure sa User Mode sa Linux ubos sa modernong mga bersyon sa mga operating system niini nga pamilya. Ang sunod nga mga lakang kinahanglan nga maglakip sa pag-install sa mga serbisyo ug uban pang software nga naa na sa sulod sa guest system. Tungod kay ang mga imahe sa sudlanan sa Docker gi-promote ra nga mga tarball, kinahanglan nimo nga makuha ang imahe pinaagi sa docker export, ug dayon hibal-i ang agianan aron ma-install kini sa gamut sa guest kernel file system. Aw, dayon pagdagan ang script sa shell.

Espesyal nga salamat sa Rkeene gikan sa #lobsters sa Freenode. Kung wala ang iyang tabang sa pag-debug sa Slirp, dili ako makaabut niini. Wala koy ideya kung giunsa ang iyang sistema sa Slackware nga nagtrabaho sa husto sa slirp, apan ang akong Ubuntu ug Alpine nga sistema wala modawat sa slirp ug ang binary nga Rkeene nagsugyot kanako. Apan igo na alang kanako nga labing menos usa ka butang nga molihok alang kanako.

Source: www.habr.com

Idugang sa usa ka comment