วิธีเริ่มใช้งาน User Mode ใน Linux

บทนำจากนักแปล: ท่ามกลางฉากหลังของการที่คอนเทนเนอร์หลากหลายประเภทเข้ามาในชีวิตของเรา การค้นหาว่าเทคโนโลยีทั้งหมดเริ่มต้นขึ้นในครั้งเดียวนั้นค่อนข้างน่าสนใจและมีประโยชน์ทีเดียว บางส่วนสามารถนำมาใช้ประโยชน์ได้จนถึงทุกวันนี้ แต่ไม่ใช่ทุกคนที่จำวิธีการดังกล่าวได้ (หรือรู้ว่าพวกเขาไม่ได้ถูกจับได้ในระหว่างการพัฒนาอย่างรวดเร็ว) เทคโนโลยีหนึ่งดังกล่าวคือ User Mode Linux ผู้เขียนต้นฉบับได้ขุดลึกลงไปมากมาย โดยหาคำตอบว่าการพัฒนาแบบเก่าอันไหนที่ยังใช้งานได้และอันไหนที่ยังไม่ได้ผล และรวบรวมคำแนะนำทีละขั้นตอนเกี่ยวกับวิธีการสร้าง UML แบบโฮมบรูว์ให้กับตัวเองใน 2k19 ใช่แล้ว เราได้เชิญผู้เขียนโพสต์ต้นฉบับมาที่ Habr เคดี้ดังนั้นหากคุณมีคำถามใด ๆ ให้ถามเป็นภาษาอังกฤษในความคิดเห็น

วิธีเริ่มใช้งาน User Mode ใน Linux

โหมดผู้ใช้ใน Linux ที่จริงแล้วคือพอร์ตของเคอร์เนล Linux ของตัวมันเอง โหมดนี้ช่วยให้คุณสามารถเรียกใช้เคอร์เนล Linux เต็มรูปแบบเป็นกระบวนการของผู้ใช้ และนักพัฒนามักใช้เพื่อทดสอบไดรเวอร์ แต่โหมดนี้ยังมีประโยชน์ในฐานะเครื่องมือแยกทั่วไปซึ่งมีหลักการคล้ายกับการทำงานของเครื่องเสมือน โหมดนี้ให้การแยกมากกว่า Docker แต่น้อยกว่าเครื่องเสมือนที่มีคุณสมบัติครบถ้วนเช่น KVM หรือ Virtual Box

โดยทั่วไปแล้ว User Mode อาจจะดูเหมือนเป็นเครื่องมือที่แปลกและใช้งานยากแต่ก็ยังมีการใช้งานอยู่ ท้ายที่สุดแล้ว นี่คือเคอร์เนล Linux เต็มรูปแบบที่ทำงานจากผู้ใช้ที่ไม่มีสิทธิ์ คุณลักษณะนี้อนุญาตให้โค้ดที่อาจไม่น่าเชื่อถือทำงานโดยไม่มีภัยคุกคามต่อเครื่องโฮสต์ และเนื่องจากนี่คือเคอร์เนลที่มีคุณสมบัติครบถ้วน กระบวนการของมันจึงถูกแยกออกจากเครื่องโฮสต์ นั่นก็คือ กระบวนการที่ทำงานภายในโหมดผู้ใช้จะไม่ปรากฏแก่โฮสต์. สิ่งนี้ไม่เหมือนกับคอนเทนเนอร์ Docker ทั่วไป ซึ่งในกรณีนี้เครื่องโฮสต์จะมองเห็นกระบวนการภายในพื้นที่เก็บข้อมูลเสมอ ดู pstree ชิ้นนี้จากเซิร์ฟเวอร์ตัวใดตัวหนึ่งของฉัน:

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

และเปรียบเทียบสิ่งนี้กับ pstree ของเคอร์เนล Linux ในโหมดผู้ใช้:

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

เมื่อทำงานกับคอนเทนเนอร์ Docker ฉันสามารถดูชื่อของกระบวนการที่ทำงานอยู่ในแขกได้จากโฮสต์ ด้วยโหมดผู้ใช้ Linux สิ่งนี้ไม่สามารถทำได้ มันหมายความว่าอะไร? ซึ่งหมายความว่าเครื่องมือตรวจสอบที่ทำงานผ่านระบบย่อยการตรวจสอบของ Linux ไม่เห็น กระบวนการที่ทำงานอยู่ในระบบเกสต์ แต่ในบางสถานการณ์ คุณลักษณะนี้อาจกลายเป็นดาบสองคมได้

โดยทั่วไป โพสต์ทั้งหมดด้านล่างนี้เป็นการรวบรวมการวิจัยและความพยายามคร่าวๆ เพื่อให้ได้ผลลัพธ์ตามที่ต้องการ ในการทำเช่นนี้ ฉันต้องใช้เครื่องมือโบราณต่างๆ อ่านแหล่งที่มาของเคอร์เนล มีส่วนร่วมในการแก้ไขโค้ดที่เขียนเมื่อตอนที่ฉันยังอยู่ในโรงเรียนประถมอย่างเข้มข้น และยังปรับแต่ง Heroku บิวด์โดยใช้ไบนารีพิเศษเพื่อค้นหาเครื่องมือที่ฉันต้องการ งานทั้งหมดนี้ทำให้คนใน IRC เรียกฉันว่านักมายากล ฉันหวังว่าโพสต์นี้จะทำหน้าที่เป็นเอกสารที่เชื่อถือได้สำหรับคนที่ทำสิ่งเดียวกัน แต่มีเคอร์เนลและระบบปฏิบัติการเวอร์ชันใหม่กว่า

การตั้งค่า

การตั้งค่าโหมดผู้ใช้ Linux ทำได้ในหลายขั้นตอน:

  • การติดตั้งการพึ่งพาบนโฮสต์
  • ดาวน์โหลดเคอร์เนล Linux;
  • การกำหนดค่าการสร้างเคอร์เนล
  • การประกอบเคอร์เนล
  • การติดตั้งไบนารี
  • การตั้งค่าระบบไฟล์เกสต์
  • การเลือกพารามิเตอร์การเรียกใช้เคอร์เนล
  • การตั้งค่าเครือข่ายแขก
  • เริ่มต้นเคอร์เนลของแขก

ฉันคิดว่าถ้าคุณตัดสินใจทำเอง คุณน่าจะทำทุกอย่างที่อธิบายไว้ในระบบ Ubuntu หรือ Debian บางตัว ฉันพยายามใช้สิ่งที่กล่าวมาทั้งหมดในการแจกจ่ายที่ฉันชื่นชอบ - อัลไพน์ แต่ไม่มีอะไรเกิดขึ้น เห็นได้ชัดว่าเป็นเพราะเคอร์เนล Linux มี glibc-isms ที่มีผลผูกพันอย่างหนักสำหรับไดรเวอร์ในโหมดผู้ใช้ ฉันวางแผนที่จะรายงานเรื่องนี้ไปยังต้นน้ำหลังจากที่ฉันเข้าใจปัญหาแล้ว

การติดตั้งการพึ่งพาบนโฮสต์

Ubuntu ต้องการแพ็คเกจอย่างน้อยต่อไปนี้เพื่อสร้างเคอร์เนล Linux (สมมติว่าเป็นการติดตั้งใหม่ทั้งหมด):

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

คุณสามารถติดตั้งได้โดยใช้คำสั่งต่อไปนี้ (ในฐานะ root หรือใช้ sudo):

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

โปรดทราบว่าการรันโปรแกรมปรับแต่งเมนูสำหรับเคอร์เนล Linux จะต้องมีการติดตั้ง libncurses-dev. โปรดตรวจสอบให้แน่ใจว่ามีการติดตั้งโดยใช้คำสั่งต่อไปนี้ (ในฐานะ root หรือใช้ sudo):

apt-get -y install libncurses-dev

ดาวน์โหลดเคอร์เนล

กำหนดตำแหน่งที่จะดาวน์โหลด จากนั้นสร้างเคอร์เนล คุณจะต้องจัดสรรพื้นที่ฮาร์ดไดรฟ์ประมาณ 1,3 GB สำหรับการดำเนินการนี้ ดังนั้นโปรดตรวจสอบให้แน่ใจว่าคุณมีพื้นที่ว่าง

จากนั้นไป kernel.org และรับ URL เพื่อดาวน์โหลดเคอร์เนลเสถียรล่าสุด ในขณะที่เขียนนี่คือ: https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.1.16.tar.xz

ดาวน์โหลดไฟล์นี้โดยใช้ 'wget':

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

และสกัดด้วย 'tar':

tar xJf linux-5.1.16.tar.xz

ตอนนี้เราเข้าสู่ไดเร็กทอรีที่สร้างขึ้นเมื่อทำการแกะ tarball:

cd linux-5.1.16

การตั้งค่าการสร้างเคอร์เนล

ระบบการสร้างเคอร์เนลเป็นชุด Makefiles с มากมาย เครื่องมือและสคริปต์ที่กำหนดเองเพื่อทำให้กระบวนการเป็นแบบอัตโนมัติ ขั้นแรก ให้เปิดโปรแกรมการตั้งค่าแบบโต้ตอบ:

make ARCH=um menuconfig

มันจะสร้างและแสดงกล่องโต้ตอบให้คุณบางส่วน เมื่อไร '[Select]' คุณสามารถกำหนดการตั้งค่าได้โดยใช้ปุ่ม Spacebar หรือ Enter การนำทางไปรอบๆ หน้าต่างเป็นปกติโดยใช้ลูกศรบนแป้นพิมพ์ "ขึ้น" และ "ลง" และการเลือกองค์ประกอบคือ "ซ้าย" หรือ "ขวา"

ตัวชี้มุมมอง -> หมายความว่าคุณอยู่ในเมนูย่อยซึ่งเข้าถึงได้โดยใช้ปุ่ม Enter ทางออกชัดเจนคือทาง'[Exit]'

รวมตัวเลือกต่อไปนี้ใน '[Select]' และตรวจสอบให้แน่ใจว่ามีสัญลักษณ์ '[*]' อยู่ข้างๆ:

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

เพียงเท่านี้ คุณสามารถออกจากหน้าต่างนี้ได้โดยเลือก '[Exit]'. เพียงตรวจสอบให้แน่ใจว่าในตอนท้ายคุณจะได้รับแจ้งให้บันทึกการกำหนดค่าและเลือก '[Yes]'

ฉันขอแนะนำให้คุณลองใช้ตัวเลือกการสร้างเคอร์เนลหลังจากอ่านโพสต์นี้ จากการทดลองเหล่านี้ คุณสามารถเรียนรู้ได้มากมายในแง่ของการทำความเข้าใจการทำงานของกลไกเคอร์เนลระดับต่ำและผลกระทบของแฟล็กต่างๆ ในการประกอบ

การสร้างเคอร์เนล

เคอร์เนล Linux เป็นโปรแกรมขนาดใหญ่ที่ทำสิ่งต่างๆ ได้มากมาย แม้ว่าจะมีการกำหนดค่าขั้นต่ำบนฮาร์ดแวร์เก่า แต่ก็อาจต้องใช้เวลาพอสมควรในการสร้าง ดังนั้นสร้างเคอร์เนลโดยใช้คำสั่งต่อไปนี้:

make ARCH=um -j$(nproc)

เพื่ออะไร? คำสั่งนี้จะบอกให้ผู้สร้างของเราใช้คอร์ CPU และเธรดที่มีอยู่ทั้งหมดในระหว่างกระบวนการสร้าง ทีม $(nproc) ที่ส่วนท้ายของ Build จะแทนที่เอาต์พุตของคำสั่ง nprocซึ่งเป็นส่วนหนึ่งของ coreutils ในรุ่นมาตรฐานของ Ubuntu

หลังจากนั้นสักครู่ เคอร์เนลของเราจะถูกคอมไพล์เป็นไฟล์ปฏิบัติการ ./linux.

การติดตั้งไบนารี

เนื่องจากโหมดผู้ใช้ใน Linux สร้างไบนารีปกติ คุณจึงสามารถติดตั้งได้เหมือนกับยูทิลิตี้อื่นๆ นี่คือวิธีที่ฉันทำ:

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

นอกจากนี้ยังควรตรวจสอบให้แน่ใจด้วย ~/bin อยู่ในของคุณ $PATH:

export PATH=$PATH:$HOME/bin

การตั้งค่าระบบไฟล์เกสต์

สร้างไดเร็กทอรีสำหรับระบบไฟล์เกสต์:

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

เปิดอัลไพน์ลินุกซ์.org และใน ส่วนการดาวน์โหลด ค้นหาลิงค์ดาวน์โหลดจริง MINI ROOT FILESYSTEM. ในขณะที่เขียนสิ่งนี้คือ:

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

ดาวน์โหลด tarball นี้โดยใช้ 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

ตอนนี้เข้าสู่ไดเร็กทอรีของระบบไฟล์เกสต์และแตกไฟล์เก็บถาวร:

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

ขั้นตอนที่อธิบายจะสร้างเทมเพลตระบบไฟล์ขนาดเล็ก เนื่องจากวิธีการทำงานของระบบ การติดตั้งแพ็คเกจผ่านตัวจัดการ Alpine apk จึงเป็นเรื่องยากมาก แต่ FS นี้จะเพียงพอที่จะประเมินแนวคิดทั่วไป

เรายังต้องมีเครื่องมือ ทีนี่ เพื่อลดการใช้หน่วยความจำ กระบวนการซอมบี้ เคอร์เนลแขกของเรา

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

การสร้างบรรทัดคำสั่งเคอร์เนล

เคอร์เนล Linux เช่นเดียวกับโปรแกรมอื่นๆ ส่วนใหญ่มีอาร์กิวเมนต์บรรทัดคำสั่งที่สามารถเข้าถึงได้โดยการระบุคีย์ --help.

ตัวเขาเอง - ช่วย

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.

แผงนี้เน้นพารามิเตอร์การเปิดตัวหลัก มารันเคอร์เนลด้วยชุดตัวเลือกขั้นต่ำที่จำเป็น:

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

บรรทัดด้านบนบอกเคอร์เนลของเราดังต่อไปนี้:

  • สมมติว่าระบบไฟล์รูทเป็นอุปกรณ์หลอก /dev/root.
  • เลือก โฮสต์ เป็นไดรเวอร์ระบบไฟล์รูท
  • ติดตั้งระบบไฟล์เกสต์ที่เราสร้างขึ้นบนอุปกรณ์รูท
  • และใช่ ในโหมดอ่าน-เขียน
  • ใช้ RAM เพียง 64 MB (คุณสามารถใช้ RAM ได้น้อยกว่ามากขึ้นอยู่กับสิ่งที่คุณวางแผนจะทำ แต่ดูเหมือนว่า 64 MB จะเป็นจำนวนที่เหมาะสมที่สุด)
  • เคอร์เนลเริ่มทำงานโดยอัตโนมัติ /bin/sh ในขณะที่ init-กระบวนการ.

รันคำสั่งนี้และคุณควรได้รับสิ่งต่อไปนี้:

อีกแผ่นหนึ่ง

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

กิจวัตรข้างต้นจะทำให้เรา ระบบแขกขั้นต่ำโดยไม่มีสิ่งที่ต้องการ /proc หรือชื่อโฮสต์ที่ได้รับมอบหมาย ตัวอย่างเช่น ลองใช้คำสั่งต่อไปนี้:

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

หากต้องการออกจากระบบของแขก ให้พิมพ์ exit หรือกด control-d สิ่งนี้จะยิงเชลล์ตามด้วยความตื่นตระหนกของเคอร์เนล:

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

เราได้รับความตื่นตระหนกของเคอร์เนลเนื่องจากเคอร์เนล Linux คิดว่ากระบวนการเริ่มต้นทำงานอยู่เสมอ หากไม่มีสิ่งนี้ ระบบจะไม่สามารถทำงานได้อีกต่อไปและล่ม แต่เนื่องจากนี่เป็นกระบวนการในโหมดผู้ใช้ ผลลัพธ์ที่ได้จึงส่งตัวมันเองไป SIGABRTซึ่งส่งผลให้เกิดผลลัพธ์

การตั้งค่าเครือข่ายแขก

และนี่คือจุดที่สิ่งต่างๆ เริ่มผิดพลาด ระบบเครือข่ายในโหมดผู้ใช้ Linux เป็นจุดที่แนวคิดทั้งหมดของ "โหมดผู้ใช้" ที่จำกัดเริ่มแตกสลาย โดยปกติแล้วในระดับระบบ เครือข่ายจะถูกจำกัด มีสิทธิพิเศษ โหมดการดำเนินการด้วยเหตุผลที่ชัดเจนสำหรับเราทุกคน

บันทึก. ต่อ.: คุณสามารถอ่านเพิ่มเติมเกี่ยวกับตัวเลือกต่าง ๆ สำหรับการทำงานกับเครือข่ายใน UML ที่นี่.

เดินทางไปสลิร์ป

อย่างไรก็ตาม มีเครื่องมือโบราณและแทบไม่ได้รับการรองรับที่เรียกว่า สลิปเปอร์ซึ่ง User Mode Linux สามารถโต้ตอบกับเครือข่ายได้ มันทำงานเหมือนกับสแต็ก TCP/IP ระดับผู้ใช้ และไม่ต้องใช้สิทธิ์ของระบบในการรัน เครื่องมือนี้ก็คือ เปิดตัวในปี 1995และการอัปเดตล่าสุดคือวันที่ ปีที่ 2006. สเลิร์ปมันเก่ามาก ในช่วงเวลาที่ไม่มีการสนับสนุนและการอัปเดต คอมไพเลอร์ได้พัฒนาไปไกลจนขณะนี้เครื่องมือนี้สามารถอธิบายได้ว่าเป็นเท่านั้น รหัสเน่า.

ดังนั้นมาดาวน์โหลด Slirp จากที่เก็บ Ubuntu แล้วลองเรียกใช้:

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)

โอ้พระเจ้า มาติดตั้งดีบักเกอร์ของ Slirp และดูว่าเราสามารถทราบได้ว่าเกิดอะไรขึ้นที่นี่:

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.

ความผิดพลาดเกิดขึ้นในตัวเรา บรรทัดนี้. ลองดูที่ stacktrace บางทีอาจมีบางอย่างสามารถช่วยเราได้:

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

ที่นี่เราจะเห็นว่าความผิดพลาดเกิดขึ้นระหว่างการเริ่มต้นของลูปหลัก เมื่อ slirp พยายามตรวจสอบการหมดเวลา ในตอนนี้เองที่ฉันต้องเลิกพยายามแก้ไขจุดบกพร่อง แต่มาดูกันว่า Slirp ที่สร้างมาแปลก ๆ ใช้งานได้จริงหรือไม่ ฉันดาวน์โหลดไฟล์เก็บถาวรอีกครั้งจากเว็บไซต์โดยตรง Sourceforgeเนื่องจากการลากบางอย่างจากที่นั่นผ่านบรรทัดคำสั่งเป็นเรื่องที่ยุ่งยาก:

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

ที่นี่เราเห็นการแจ้งเตือนเกี่ยวกับฟังก์ชันในตัวที่ไม่ได้กำหนด ซึ่งก็คือ เกี่ยวกับการไม่สามารถลิงก์ไฟล์ไบนารี่ที่เป็นผลลัพธ์ได้ ปรากฏว่าระหว่างปี 2006 ถึงจุดนี้ gcc หยุดสร้างสัญลักษณ์ที่ใช้ในฟังก์ชันในตัวของไฟล์ที่คอมไพล์ระดับกลาง เรามาลองแทนที่คีย์เวิร์ดกันดีกว่า inline ในความคิดเห็นที่ว่างเปล่าและดูผลลัพธ์:

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

ไม่. สิ่งนี้ใช้ไม่ได้เช่นกัน ยังไม่พบสัญลักษณ์สำหรับฟังก์ชันเหล่านี้

เมื่อมาถึงจุดนี้ ฉันยอมแพ้และเริ่มมองหา Github แพ็คเกจสร้าง Heroku. ทฤษฎีของฉันคือแพ็คเกจ Heroku build บางอันจะมีไบนารีที่ฉันต้องการ ในที่สุด การค้นหาของฉันก็พาฉันไป ที่นี่. ฉันดาวน์โหลดและแตกไฟล์แล้ว uml.tar.gz และพบสิ่งต่อไปนี้:

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*

นี่คือไบนารีที่ลื่นไหล! เขาทำงานหรือเปล่า?

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

ไม่ขัดข้อง - ดังนั้นมันควรจะใช้งานได้! มาปลูกไบนารีนี้กัน ~/bin/slirp:

cp slirp ~/bin/slirp

ในกรณีที่ผู้สร้างแพ็คเกจลบออก I ทำกระจก.

การกำหนดค่าเครือข่าย

ตอนนี้เรามาตั้งค่าเครือข่ายบนเคอร์เนลแขกของเรากันดีกว่า อัปเดตตัวเลือกการเปิดตัว:

linux 
  root=/dev/root 
  rootfstype=hostfs 
  rootflags=$HOME/prefix/uml-demo 
  rw 
  mem=64M 
  eth0=slirp,,$HOME/bin/slirp 
  init=/bin/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

คำสั่งการกำหนดค่าสองคำสั่งแรก /proc и /sys จำเป็นสำหรับการทำงาน ifconfigซึ่งตั้งค่าอินเทอร์เฟซเครือข่ายเพื่อสื่อสารกับ Slirp ทีม route ตั้งค่าตารางเส้นทางเคอร์เนลเพื่อบังคับให้การรับส่งข้อมูลทั้งหมดส่งผ่านอุโมงค์ Slirp มาตรวจสอบสิ่งนี้ด้วยการสืบค้น 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

มันได้ผล!

หมายเหตุต่อ: เห็นได้ชัดว่าโพสต์ต้นฉบับเขียนบนเดสก์ท็อปด้วยการ์ดเครือข่ายแบบมีสาย หรือการกำหนดค่าอื่น ๆ ที่ไม่ต้องใช้ไดรเวอร์เพิ่มเติม บนแล็ปท็อปที่มี WiFi 8265 จาก Intel เกิดข้อผิดพลาดขณะเพิ่มเครือข่าย

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

เห็นได้ชัดว่าเคอร์เนลไม่สามารถสื่อสารกับไดรเวอร์การ์ดเครือข่ายได้ น่าเสียดายที่ความพยายามที่จะคอมไพล์เฟิร์มแวร์ลงในเคอร์เนลไม่สามารถแก้ไขสถานการณ์ได้ ในขณะที่เผยแพร่ ไม่พบวิธีแก้ไขปัญหาในการกำหนดค่านี้ ในการกำหนดค่าที่ง่ายกว่า (เช่นใน Virtualbox) อินเทอร์เฟซจะเพิ่มขึ้นอย่างถูกต้อง

มาเปลี่ยนเส้นทางโดยอัตโนมัติด้วยเชลล์สคริปต์ต่อไปนี้:

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

และทำเครื่องหมายว่าปฏิบัติการได้:

chmod +x init.sh

จากนั้นเราจะทำการเปลี่ยนแปลงบรรทัดคำสั่งเคอร์เนล:

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

และขอทำซ้ำ:

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

เครือข่ายมีเสถียรภาพ!

ไฟล์นักเทียบท่า

เพื่อให้ง่ายต่อการตรวจสอบทั้งหมดนี้ฉันได้รวบรวมไว้ ไฟล์นักเทียบท่าซึ่งจะดำเนินการขั้นตอนส่วนใหญ่ที่อธิบายไว้โดยอัตโนมัติและควรจัดเตรียมการกำหนดค่าการทำงานให้กับคุณ ฉันยังมี เคอร์เนลที่กำหนดค่าไว้ล่วงหน้าซึ่งมีทุกสิ่งที่อธิบายไว้ในโพสต์ แต่สิ่งสำคัญคือต้องเข้าใจว่าที่นี่ฉันได้ระบุเฉพาะการตั้งค่าขั้นต่ำเท่านั้น

ฉันหวังว่าโพสต์นี้จะช่วยให้คุณเข้าใจวิธีเรียกใช้เคอร์เนลของแขก มันกลายเป็นสัตว์ประหลาดบางชนิด แต่สิ่งพิมพ์นี้ถือเป็นคำแนะนำที่ครอบคลุมเกี่ยวกับการสร้างการติดตั้งและการกำหนดค่าโหมดผู้ใช้ใน Linux ภายใต้ระบบปฏิบัติการเวอร์ชันทันสมัยของตระกูลนี้ ขั้นตอนต่อไปควรรวมถึงการติดตั้งบริการและซอฟต์แวร์อื่นๆ ที่อยู่ในระบบเกสต์อยู่แล้ว เนื่องจากอิมเมจคอนเทนเนอร์ Docker เป็นเพียง tarball ที่เผยแพร่ คุณจึงควรแยกรูปภาพผ่านช่องทางนี้ได้ docker exportจากนั้นกำหนดเส้นทางการติดตั้งในรูทของระบบไฟล์ของเคอร์เนลเกสต์ จากนั้นรันเชลล์สคริปต์

ขอขอบคุณเป็นพิเศษสำหรับ Rkeene จาก #lobsters บน Freenode หากไม่มีความช่วยเหลือจากเขาในการดีบัก Slirp ฉันคงมาได้ไกลขนาดนี้ ฉันไม่รู้ว่าระบบ Slackware ของเขาทำงานอย่างถูกต้องกับ slirp ได้อย่างไร แต่ระบบ Ubuntu และ Alpine ของฉันไม่ยอมรับ slirp และไบนารี Rkeene แนะนำให้ฉัน แต่มันก็เพียงพอแล้วสำหรับฉันที่อย่างน้อยก็มีบางอย่างที่เหมาะกับฉัน

ที่มา: will.com

เพิ่มความคิดเห็น