Raspiでx86バイナリを動かす

Step 1: Run this command
#
apt-get update && apt-get install qemu qemu-user qemu-user-static binfmt-support debootstrap binutils

Step 2: Create a folder for your chroot.
Tip: Use mount –bind for proper df support within the environment (optional)
#
mkdir /chroot
mkdir /tmp/mnt/
mount –bind /chroot /tmp/mnt/

Step 3: Run debootstrap in the chroot dir. This can be done with Debian or Ubuntu, but for this tutorial I’ll be using Debian.
#
cd /tmp/mnt/
debootstrap –foreign –arch i386 sid ./ http://httpredir.debian.org/debian

Step 4: The previous step only ran the first half of debootstrap. Now we need to mount a few basic directories and copy the static version of qemu-i386 into the environment. Run the following:
#
mount -t sysfs sys sys/
mount -t proc proc proc/
mount –bind /dev dev/
mount –bind /dev/pts dev/pts/
mount –bind /dev/shm dev/shm/
cp /usr/bin/qemu-i386-static usr/bin/

Step 5: Now we execute the chroot for the first time and run the second half of debootstrap.
#
chroot . /debootstrap/debootstrap –second-stage

Step 6: The environment is complete! Now we enter the environment.
#
chroot ./ /bin/bash –login -i

Tips:
A few environment variables may be set wrong when first entering the environment. You can fix it with the following:
#
export LANGUAGE=”C”
export LC_ALL=”C”
unset HOME
export HOME=/root/
source ~/.bashrc
source ~/.profile

The easiest way to do this so you don’t have to set those variables every time you chroot in is to install an SSH server and configure it to an alternate port than the main one Raspbian is running. That way OpenSSH will set up your environment variables automatically. If you go that route then your initial command should be:
chroot ./ /usr/sbin/sshd

Then just SSH localhost with the alternate port. Keep in mind you’ll need to change a couple variables in sshd_config besides the port, such as permit root login and x11 forwarding. If set up properly you should be able to connect to your environment + run graphical applications with
#
ssh -xY root@localhost -p myportnumber

You will have to remount the chroot + special directories on every reboot. You can do this manually every time or make things easier by creating a script.

To make the script:
#
nano /usr/bin/chrootmount

This will open nano and tell it to create a file in /usr/bin. Now enter the following into the script:

#!/bin/bash
mkdir /tmp/mnt/
mount –bind /chroot /tmp/mnt/
mount -t sysfs sys /tmp/mnt/sys/
mount -t proc proc /tmp/mnt/proc/
mount –bind /dev /tmp/mnt/dev/
mount –bind /dev/pts /tmp/mnt/dev/pts/
mount –bind /dev/shm /tmp/mnt/dev/shm/
chroot /tmp/mnt/ /usr/sbin/sshd

Now save the script (Ctrl+X, Y) and chmod it to 755 so it’s executable after that you should be able to mount your chroot + run your SSH server on boot by just running chrootmount.

If you created the script in the previous tip and want to make it even easier by having it run automatically on boot, we can do this with a systemd service.
#
nano /etc/systemd/system/chrootmount.service

This will open nano once more. Now add the following:

[Unit]
Description = mounts chroot on boot

[Service]
Type=one-shot
ExecStart=/usr/bin/chrootmount

[Install]
WantedBy=multi-user.target

Save the script (Ctrl+X, Y) and enable it with
#
systemctl enable chrootmount.service

Now the system will automatically execute the chroot mount process + run the ssh server on every boot. So on boot you’ll be able to access your environment by just opening terminal and typing:

ssh -xY root@localhost -p myportnumber

If you want to access your main filesystem from within the environment, you’ll need to bind mount it. Add this to your chrootmount script:
mkdir /tmp/mnt/mainfs
mount –bind / /tmp/mnt/mainfs/

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS