Yuav Pib Siv Hom Neeg Siv Li Cas ntawm Linux

Taw qhia los ntawm tus neeg txhais lus: Tawm tsam qhov keeb kwm yav dhau los ntawm kev nkag mus rau ntau hom ntim rau hauv peb lub neej, nws tuaj yeem ua tau zoo heev thiab muaj txiaj ntsig los tshawb xyuas nrog cov thev naus laus zis dab tsi no tag nrho tau pib ib zaug ib zaug. Qee tus ntawm lawv tuaj yeem siv tau zoo rau niaj hnub no, tab sis tsis yog txhua tus neeg nco qab txog cov txheej txheem no (lossis paub, yog tias lawv tsis raug ntes thaum lawv loj hlob sai). Ib qho thev naus laus zis no yog Tus Neeg Siv Hom Linux. Tus neeg sau ntawm tus thawj tau ua qhov ncaj ncees ntawm kev khawb, txheeb xyuas qhov twg ntawm qhov kev txhim kho qub tseem ua haujlwm thiab qhov tsis zoo, thiab muab tso ua ke ib yam dab tsi zoo li cov lus qhia ib ntus ntawm yuav ua li cas los tsim UML hauv tsev rau koj tus kheej hauv 2k19 ua. Thiab yog, peb tau caw tus sau thawj tsab ntawv mus rau Habr Cadey, yog li yog tias koj muaj lus nug, nug ua lus Askiv hauv cov lus.

Yuav Pib Siv Hom Neeg Siv Li Cas ntawm Linux

Tus neeg siv hom hauv Linux yog, qhov tseeb, qhov chaw nres nkoj ntawm Linux ntsiav rau nws tus kheej. Hom no tso cai rau koj khiav tag nrho Linux kernel raws li tus neeg siv cov txheej txheem thiab feem ntau siv los ntawm cov neeg tsim khoom los kuaj cov tsav tsheb. Tab sis hom no kuj tseem muaj txiaj ntsig zoo li lub cuab yeej rau kev sib cais dav dav, lub hauv paus ntsiab lus uas zoo ib yam li kev ua haujlwm ntawm cov tshuab virtual. Hom no muab kev cais ntau dua li Docker, tab sis tsawg dua li lub tshuab virtual uas muaj puv npo zoo li KVM lossis Virtual Box.

Zuag qhia tag nrho, Tus Neeg Siv Hom yuav zoo li lub cuab yeej coj txawv txawv thiab nyuaj siv, tab sis nws muaj nws cov kev siv. Tom qab tag nrho, qhov no yog tag nrho-fledged Linux kernel khiav raws li ib tug neeg siv unprivileged. Cov yam ntxwv no tso cai rau cov cai uas tsis ntseeg siab ua haujlwm yam tsis muaj kev hem thawj rau lub tshuab ua haujlwm. Thiab txij li qhov no yog tag nrho-fledged kernel, nws cov txheej txheem yog cais los ntawm tus tswv tshuab, uas yog cov txheej txheem khiav hauv Tus Neeg Siv Hom yuav tsis pom rau tus tswv tsev. Qhov no tsis zoo li lub thawv Docker tsoos, uas lub tshuab ua haujlwm ib txwm pom cov txheej txheem hauv qhov chaw cia. Saib ntawm daim pstree no los ntawm ib qho ntawm kuv cov servers:

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

Thiab piv qhov no rau pstree ntawm Linux ntsiav hauv Hom Neeg Siv:

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

Thaum ua haujlwm nrog Docker ntim, kuv tuaj yeem pom los ntawm tus tswv tsev cov npe ntawm cov txheej txheem uas khiav hauv cov qhua. Qhov no tsis tuaj yeem ua tau nrog Linux Tus Neeg Siv Hom. Nws txhais li cas? Qhov no txhais tau hais tias kev soj ntsuam cov cuab yeej khiav los ntawm Linux cov kev tshawb xyuas subsystem tsis pom cov txheej txheem khiav hauv cov qhua system. Tab sis nyob rau hauv tej lub sij hawm no feature yuav ua tau ib tug ob-edged ntaj.

Feem ntau, tag nrho cov ncej hauv qab no yog ib phau ntawm kev tshawb fawb thiab kev sim siab kom ua tiav qhov xav tau. Txhawm rau ua qhov no, kuv yuav tsum tau siv ntau yam cuab yeej qub, nyeem cov ntaub ntawv kernel, koom nrog kev daws teeb meem hnyav ntawm cov lej sau thaum kuv tseem nyob hauv tsev kawm theem qis, thiab tseem tinker nrog Heroku tsim siv tshwj xeeb binary hauv kev tshawb nrhiav cov cuab yeej kuv xav tau. Tag nrho cov hauj lwm no coj mus rau cov txiv neej ntawm kuv IRC hu kuv ua ib tug khawv koob. Kuv vam tias cov ntawv tshaj tawm no yuav ua cov ntaub ntawv txhim khu kev qha rau ib tus neeg ua ib yam, tab sis nrog cov kernels tshiab thiab OS versions.

hloov

Kev teeb tsa Linux Tus Neeg Siv Hom muaj ob peb kauj ruam:

  • txhim kho kev vam khom ntawm tus tswv tsev;
  • rub tawm Linux kernel;
  • kernel tsim teeb;
  • kernel sib dhos;
  • txhim kho binary;
  • teeb tsa tus qhua cov ntaub ntawv system;
  • xaiv cov kernel launch tsis;
  • teeb tsa lub network qhua;
  • launching tus qhua kernel.

Kuv xav tias yog tias koj txiav txim siab ua txhua yam no koj tus kheej, feem ntau koj yuav ua txhua yam uas tau piav qhia hauv qee qhov Ubuntu lossis Debian-zoo li system. Kuv tau sim siv tag nrho cov saum toj no hauv kuv qhov kev faib khoom nyiam - Alpine, tab sis tsis muaj dab tsi ua haujlwm, pom meej vim tias Linux ntsiav tau nruj nruj rau glibc-isms rau cov tsav tsheb hauv Hom Neeg Siv. Kuv npaj yuav tshaj tawm qhov no mus rau sab saud tom qab kuv thaum kawg pom qhov teeb meem.

Txhim kho kev vam meej ntawm tus tswv tsev

Ubuntu xav kom tsawg kawg ntawm cov pob khoom hauv qab no los tsim lub Linux ntsiav (xws li kev txhim kho huv):

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

Koj tuaj yeem nruab lawv siv cov lus txib hauv qab no (raws li hauv paus lossis siv sudo):

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

Thov nco ntsoov tias khiav cov ntawv qhia zaub mov customization program rau Linux ntsiav yuav xav tau kev teeb tsa libncurses-dev. Thov xyuas kom meej tias nws tau teeb tsa siv cov lus txib hauv qab no (raws li hauv paus lossis siv sudo):

apt-get -y install libncurses-dev

Downloading lub kernel

Txiav txim siab qhov twg los rub tawm thiab tom qab ntawd tsim lub kernel. Koj yuav tsum tau faib kwv yees li 1,3 GB ntawm qhov chaw hard drive rau kev ua haujlwm no, yog li xyuas kom koj muaj nws.

Ces mus rau kernel.org thiab tau txais qhov URL mus download tau qhov tseeb ruaj khov ntsiav version. Thaum lub sij hawm sau ntawv no: https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.1.16.tar.xz

Download tau cov ntaub ntawv no siv 'wget':

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

Thiab extract nws nrog 'tar':

tar xJf linux-5.1.16.tar.xz

Tam sim no peb nkag mus rau hauv phau ntawv teev npe tsim thaum unpacking tarball:

cd linux-5.1.16

Teeb tsa lub kernel tsim

Lub kernel build system yog ib txheej Ua cov ntaub ntawv с ntau cov cuab yeej kev cai thiab cov ntawv sau los ua kom cov txheej txheem. Txhawm rau pib, qhib qhov program teeb tsa online:

make ARCH=um menuconfig

Nws yuav ua tiav ib feem ntawm lub rooj sib txoos thiab tso saib lub dialog box rau koj. Thaum lub teeb tshwm rau hauv qab ntawm lub qhov rais[Select]', koj tuaj yeem teeb tsa cov chaw siv Spacebar lossis Enter cov yuam sij. Kev taw qhia ib ncig ntawm lub qhov rais yog ib txwm siv cov keyboard xub "nce" thiab "down", thiab xaiv cov ntsiab lus yog "sab laug" lossis "txoj cai".

Qhov taw qhia saib -> txhais tau hais tias koj nyob rau hauv ib qho submenu, uas tuaj yeem nkag tau siv tus yuam sij Enter. Txoj kev tawm yog pom tseeb los ntawm '[Exit]'.

suav nrog cov kev xaiv hauv qab no hauv '[Select]' thiab xyuas kom meej tias muaj '[*]' cim ib sab ntawm lawv:

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

Qhov ntawd yog nws, koj tuaj yeem tawm lub qhov rais no los ntawm kev xaiv '[Exit]'. Tsuas yog xyuas kom meej tias thaum kawg koj raug ceeb toom kom txuag lub teeb tsa thiab xaiv '[Yes]'.

Kuv xav kom koj ua si ib ncig nrog cov ntsiav tsim kev xaiv tom qab nyeem cov ntawv no. Los ntawm cov kev sim no koj tuaj yeem kawm ntau yam txog seb cov tshuab ua haujlwm qis qis ua haujlwm li cas thiab cov chij sib txawv cuam tshuam li cas cov kernel tau tsim.

Tsim lub kernel

Lub Linux ntsiav yog ib qho kev pab cuam loj uas ua ntau yam. Txawm hais tias qhov kev teeb tsa me me no ntawm cov khoom qub qub, kev sib dhos tuaj yeem siv sijhawm ntev. Yog li tsim lub kernel siv cov lus txib hauv qab no:

make ARCH=um -j$(nproc)

Rau dab tsi? Cov lus txib no yuav qhia rau peb tus tsim kom siv tag nrho cov muaj CPU cores thiab threads thaum lub sij hawm tsim. Pab neeg $(nproc) Thaum kawg ntawm Build inserts cov zis ntawm cov lus txib nproc, uas yog ib feem coreutils hauv tus qauv Ubuntu tsim.

Tom qab qee lub sijhawm, peb cov kernel yuav muab tso ua ke rau hauv cov ntaub ntawv executable ./linux.

Txhim kho binary

Txij li Tus Neeg Siv Hom hauv Linux tsim binary li niaj zaus, koj tuaj yeem nruab nws zoo li lwm yam khoom siv. Nov yog qhov kuv tau ua:

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

Nws tseem tsim nyog ua kom paub tseeb tias ~/bin yog nyob rau hauv koj $PATH:

export PATH=$PATH:$HOME/bin

Configuring Guest File System

Tsim ib tug directory rau cov qhua cov ntaub ntawv system:

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

Qhib alpinelinux.org thiab hauv download seem nrhiav qhov txuas download tam sim no MINI ROOT FILESYSTEM. Thaum lub sijhawm sau ntawv no yog:

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

Download tau no tarball siv 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

Tam sim no nkag mus rau tus qhua cov ntaub ntawv kaw lus cov npe thiab qhib lub archive:

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

Cov kauj ruam tau piav qhia yuav tsim ib daim ntawv me me ntawm qhov system template. Vim txoj kev ua haujlwm, kev txhim kho pob khoom los ntawm Alpine apk tus thawj tswj yuav nyuaj heev. Tab sis qhov FS no yuav txaus los ntsuas lub tswv yim dav dav.

Peb kuj xav tau ib lub cuab yeej tsi kom tsis txhob nco noj cov txheej txheem zombie peb qhua kernel.

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

Tsim Kernel Command Line

Lub Linux kernel, zoo li feem ntau lwm cov kev pab cuam, muaj cov lus txib kab lus uas tuaj yeem nkag tau los ntawm kev qhia tus yuam sij. --help.

Nws tus kheej - pab

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.

Lub vaj huam sib luag no qhia txog qhov tseem ceeb ntawm kev tso tawm. Cia peb khiav lub kernel nrog qhov tsawg kawg nkaus uas yuav tsum tau muaj cov kev xaiv:

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

Cov kab saum toj no qhia peb cov kernel hauv qab no:

  • Cia peb xav tias lub hauv paus filesystem yog pseudo ntaus ntawv /dev/root.
  • Xaiv hostfs raws li lub hauv paus ntaub ntawv system tsav tsheb.
  • Mount cov qhua filesystem peb tsim ntawm lub hauv paus ntaus ntawv.
  • Thiab yog, nyob rau hauv hom nyeem-sau.
  • Siv tsuas yog 64 megabytes ntawm RAM (koj tuaj yeem siv tsawg dua nyob ntawm seb koj npaj yuav ua li cas, tab sis 64 MB zoo li yog qhov zoo tshaj plaws).
  • Lub kernel cia li pib /bin/sh yuav ua li cas init- txheej txheem.

Khiav cov lus txib no thiab koj yuav tsum tau txais qee yam zoo li hauv qab no:

Lwm daim ntawv

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

Cov manipulations saum toj no yuav muab rau peb tsawg kawg qhua system, tsis muaj tej yam zoo li /proc los yog muab hostname. Piv txwv li, sim cov lus txib hauv qab no:

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

Txhawm rau tshem tawm ntawm tus qhua system, nkag mus exit los yog nias tswj-d. Qhov no yuav tua lub plhaub ua raws li kev ceeb toom kernel:

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

Peb tau txais cov ntsiav no ntshai vim tias Linux ntsiav xav tias cov txheej txheem pib ua haujlwm ib txwm ua. Yog tsis muaj nws, lub kaw lus tsis tuaj yeem ua haujlwm thiab kaw. Tab sis txij li qhov no yog tus txheej txheem siv hom, cov txiaj ntsig tau xa nws tus kheej mus rau SIGABRT, uas ua rau kev tawm mus.

Teeb tsa lub network qhua

Tab sis qhov no yog qhov uas txhua yam pib mus tsis ncaj ncees rau peb. Kev sib tham hauv Tus Neeg Siv Hom Linux yog qhov twg tag nrho lub tswv yim ntawm kev txwv "neeg siv hom" pib poob sib nrug. Tom qab tag nrho, lub network feem ntau txwv nyob rau theem system muaj cai kev ua qauv rau cov laj thawj pom tseeb rau peb txhua tus.

Nco tseg per.: Koj tuaj yeem nyeem ntxiv txog ntau txoj kev xaiv rau kev ua haujlwm nrog lub network hauv UML no.

Taug kev mus slirp

Txawm li cas los xij, muaj ib qho cuab yeej qub thiab tsis muaj kev txhawb nqa hu ua Slirp, nrog tus neeg siv hom Linux tuaj yeem cuam tshuam nrog lub network. Nws ua haujlwm zoo li tus neeg siv-theem TCP / IP pawg thiab tsis tas yuav muaj kev tso cai rau kev khiav haujlwm. Cov cuab yeej no yog tso tawm xyoo 1995, thiab qhov hloov tshiab kawg yog hnub tim 2006. Slirp yog qub heev. Thaum lub sij hawm tsis muaj kev txhawb nqa thiab hloov tshiab, compilers tau tuaj txog tam sim no uas tam sim no cov cuab yeej no tsuas yog piav qhia raws li "code rot".

Yog li, cia peb rub tawm Slirp los ntawm Ubuntu repositories thiab sim khiav nws:

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)

Au kuv tus Vajtswv. Cia peb nruab Slirp debugger thiab saib seb peb tuaj yeem txiav txim siab dab tsi tshwm sim ntawm no:

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.

Qhov yuam kev yog ntaus rau peb kab no. Cia peb saib ntawm stacktrace, tej zaum ib yam dab tsi yuav pab peb muaj:

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

Ntawm no peb pom tias kev sib tsoo tshwm sim thaum lub sijhawm pib ntawm lub ntsiab voj, thaum slirp sim xyuas lub sijhawm. Nws yog lub sijhawm no uas kuv yuav tsum tso tseg sim debug. Tab sis cia saib seb Slirp, ua los ntawm kev xaiv, ua haujlwm. Kuv rov rub tawm cov ntaub ntawv ncaj qha los ntawm lub xaib Sourceforge, vim hais tias luag ib yam dab tsi los ntawm qhov hais kom ua kab yog qhov mob:

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

Ntawm no peb pom cov lus ceeb toom txog undefined built-in functions, uas yog, hais txog lub impossibility ntawm txuas cov resulting binary ntaub ntawv. Nws zoo nkaus li ntawm 2006 thiab tam sim no gcc nres tsim cov cim siv hauv kev ua haujlwm nruab nrab. Wb sim hloov lo lus tseem ceeb inline mus rau ib qho kev pom khoob thiab saib qhov tshwm sim:

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

Tsis yog. Qhov no tsis ua haujlwm ib yam. Tseem nrhiav tsis tau cov cim rau cov haujlwm no.

Lub sijhawm no kuv tso tseg thiab pib tshawb nrhiav ntawm Github Heroku tsim pob. Kuv txoj kev xav yog tias qee qhov Heroku tsim pob yuav muaj cov binaries kuv xav tau. Thaum kawg, kuv txoj kev tshawb nrhiav tau coj kuv mus nyob ntawm no. Kuv downloaded thiab unpacked uml.tar.gz thiab pom cov hauv qab no:

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*

Qhov no yog ib tug slirp binary! Nws puas ua haujlwm?

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

Nws tsis poob - yog li nws yuav tsum ua haujlwm! Cia peb cog no binary hauv ~/bin/slirp:

cp slirp ~/bin/slirp

Nyob rau hauv rooj plaub tus creator ntawm lub pob tshem tawm nws, I ua daim iav.

Kev teeb tsa network

Tam sim no cia peb teeb tsa lub network ntawm peb cov qhua tseem ceeb. Cia peb hloov kho qhov tso tawm tsis pub dhau:

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

Tam sim no cia peb qhib lub 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

Thawj ob configuration commands /proc и /sys tsim nyog rau kev ua haujlwm ifconfig, uas teeb tsa lub network interface los sib txuas lus nrog Slirp. Pab neeg route teeb tsa lub kernel routing rooj los yuam kom tag nrho cov tsheb khiav mus los ntawm Slirp qhov. Cia peb kuaj qhov no nrog cov lus nug 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

Аботает!

Nco tseg rau .: Pom tseeb, thawj cov ntawv tshaj tawm tau sau rau ntawm lub desktop nrog ib daim npav network, lossis qee qhov kev teeb tsa uas tsis tas yuav tsum tau tsav tsheb ntxiv. Ntawm lub laptop nrog WiFi 8265 los ntawm Intel, qhov yuam kev tshwm sim thaum tsa lub 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
/ #

Thaj, lub kernel tsis tuaj yeem sib txuas lus nrog lub network tsav tsav. Kev sim sau cov firmware rau hauv cov ntsiav, hmoov tsis, tsis kho qhov teeb meem. Thaum lub sijhawm tshaj tawm, tseem tsis tau muaj kev daws teeb meem hauv qhov kev teeb tsa tshwj xeeb no. Ntawm kev teeb tsa yooj yim (piv txwv li, hauv Virtualbox), lub interface raug tsa kom raug.

Cia peb automate lub redirection siv lub plhaub ntawv hauv qab no:

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

Thiab kos nws raws li executable:

chmod +x init.sh

Thiab tom qab ntawd cia peb hloov pauv rau cov kab hais kom ua kernel:

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

Thiab cia peb rov hais dua:

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

Lub network ruaj khov!

docker cov ntaub ntawv

Txhawm rau ua kom yooj yim rau koj los xyuas tag nrho cov no, kuv tau sau Dockerfile, uas automates feem ntau ntawm cov kauj ruam tau piav qhia thiab yuav tsum muab koj nrog kev teeb tsa ua haujlwm. kuv kuj muaj npaj kernel configuration, uas muaj txhua yam uas tau piav qhia hauv tus ncej. Tab sis nws yog ib qho tseem ceeb kom nkag siab tias ntawm no kuv tau piav qhia txog qhov tsawg kawg nkaus.

Kuv vam tias tsab xov xwm no tau pab koj nkag siab yuav ua li cas coj tuaj rau cov qhua tuaj noj mov. Nws tau dhau los ua qee yam dab, tab sis cov ntawv tshaj tawm tau npaj los ua cov lus qhia dav dav ntawm cov ncauj lus ntawm kev sib sau, txhim kho thiab teeb tsa Tus Neeg Siv Hom hauv Linux raws li niaj hnub versions ntawm kev ua haujlwm ntawm tsev neeg no. Cov kauj ruam tom ntej yuav tsum suav nrog kev txhim kho cov kev pabcuam thiab lwm yam software uas twb muaj lawm hauv cov qhua. Txij li thaum Docker ntim cov duab tsuas yog txhawb nqa tarballs, koj yuav tsum muaj peev xwm rho tawm cov duab ntawm docker export, thiab tom qab ntawd txiav txim siab txoj hauv kev los nruab nws hauv lub hauv paus ntawm cov qhua kernel cov ntaub ntawv kaw lus. Zoo, ces khiav lub plhaub ntawv.

Ua tsaug tshwj xeeb rau Rkeene los ntawm #lobsters ntawm Freenode. Yog tsis muaj nws pab hauv kev debugging Slirp, kuv yuav tsis tau txais qhov deb. Kuv tsis paub yuav ua li cas nws Slackware system ua haujlwm kom raug nrog slirp, tab sis kuv Ubuntu thiab Alpine systems tsis lees txais slirp thiab binary Rkeene qhia rau kuv. Tab sis nws txaus rau kuv tias yam tsawg kawg ib yam ua haujlwm rau kuv.

Tau qhov twg los: www.hab.com

Ntxiv ib saib