Skip to content

Instantly share code, notes, and snippets.

@kid1412-net
Last active March 28, 2023 03:07
Show Gist options
  • Save kid1412-net/362ed2c3b089a25f3667a27183da63e6 to your computer and use it in GitHub Desktop.
Save kid1412-net/362ed2c3b089a25f3667a27183da63e6 to your computer and use it in GitHub Desktop.

Using Rosetta 2 in Fedora ARM Linux VM on Apple M1

Preparation

Fedora 37 Workstation Live ISO (ARM aarch64)

https://getfedora.org/en/workstation/download/

UTM 4 (with macOS Ventura)

https://docs.getutm.app/advanced/rosetta/

https://developer.apple.com/documentation/macos-release-notes/macos-13-release-notes

How to

Create a virtual machine in UTM 4

  • Check Apple Virtualization
  • Check Enable Rosetta (x86_64 emulation)

Mount Rosetta disk

sudo mkdir /media/rosetta
sudo mount -t virtiofs rosetta /media/rosetta

Enable Rosetta 2 in Fedora ARM Linux VM

echo ":rosetta:M::\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x3e\x00:\xff\xff\xff\xff\xff\xfe\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/media/rosetta/rosetta:F" | sudo tee /proc/sys/fs/binfmt_misc/register

Check Rosetta 2 support

ll /proc/sys/fs/binfmt_misc/

Expected output

total 0
-rw-r--r--. 1 root root 0 Feb 17 06:30 python
--w-------. 1 root root 0 Feb 17 07:07 register
-rw-r--r--. 1 root root 0 Feb 17 07:07 rosetta
-rw-r--r--. 1 root root 0 Feb 17 06:43 status

Try with x86-64 binary

wget https://github.com/PowerShell/PowerShell/releases/download/v7.3.2/powershell-7.3.2-linux-x64.tar.gz

mkdir ~/powershell_x64
tar -xvzf powershell-7.3.2-linux-x64.tar.gz -C ~/powershell_x64/

cd ~/powershell_x64/

./pwsh

rosetta error: failed to open elf at /lib64/ld-linux-x86-64.so.2
Trace/breakpoint trap (core dumped)

References

[UTM Docs] - Rosetta - macOS 13+

[benjamintoll.com] On BINFMT_MISC

[peternguyen93's GitHub Gist]Using Rosetta2 in Docker on ArchLinux arm64 - Apple M1

[Apple Developer Videos] Create macOS or Linux virtual machines

[Apple Developer Documentation] Running Intel Binaries in Linux VMs with Rosetta

[kali.org] Running x86 code on ARM devices

[Microsoft Learn] Installing PowerShell on Raspberry Pi OS

Running Command Line Application in Fedora Linux ARM using Rosetta 2

Running steps

Setup Rosseta 2 in Linux as shown in 01_rosetta2_fedora_linux_vm.md

Install neccesary Linux x86-64 libraries

sudo mkdir /opt/packages

sudo dnf install glibc glibc.i686 libstdc++ --releasever=/ --forcearch=x86_64 --installroot=/opt/packages -y

Create a symbolic link to Linux x86-64 library

sudo ln -s /opt/packages/lib64/ld-linux-x86-64.so.2 /lib64/

Download unrar Linux x86-64 binary

cd Downloads
wget https://www.rarlab.com/rar/rarlinux-x64-621.tar.gz

Extract unrar Linux x86-64 binary

tar -xvzf rarlinux-x64-621.tar.gz -C .

Check rar binary architecture

file rar/rar
rar: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, stripped

Try to get help of rar Linux x86-64 binary

cd rar
LD_LIBRARY_PATH=/opt/packages/lib64 ./unrar /h

UNRAR 6.21 freeware      Copyright (c) 1993-2023 Alexander Roshal

Usage:     unrar <command> -<switch 1> -<switch N> <archive> <files...>
               <@listfiles...> <path_to_extract/>

<Commands>
  e             Extract files without archived paths
  l[t[a],b]     List archive contents [technical[all], bare]
  p             Print file to stdout
  t             Test archive files
  v[t[a],b]     Verbosely list archive contents [technical[all],bare]
  x             Extract files with full path

...

Try to use rar to compress, uncompress and compare the result

md5sum rarlinux-x64-621.tar.gz 
d31ef68b281179d29729aaa81d232b9c  rarlinux-x64-621.tar.gz

LD_LIBRARY_PATH=/opt/packages/lib64 rar a -p123456 rar612.rar rarlinux-x64-621.tar.gz 

mkdir back

mv rarlinux-x64-621.tar.gz back/

LD_LIBRARY_PATH=/opt/packages/lib64 unrar x rar612.rar

md5sum rarlinux-x64-621.tar.gz 
d31ef68b281179d29729aaa81d232b9c  rarlinux-x64-621.tar.gz

Script to automatically setup Rosetta 2 in Fedora Linux ARM

80_rosetta2_fedora_setup.sh

or

https://bit.ly/kidrosetta2

References

History

  • 2023/Mar/23: Newly create

Running GUI Applications in Fedora Linux ARM using Rosetta 2

Running steps

Setup Rosetta 2 using script

wget https://bit.ly/kidrosetta2
chmod +x kidrosetta2
sudo ./kidrosetta2

Try Sublime Text 64-bit

cd Downloads

wget https://download.sublimetext.com/sublime_text_build_4143_x64.tar.xz

tar -xvJf sublime_text_build_4143_x64.tar.xz -C .

cd sublime_text

file sublime_text

LD_LIBRARY_PATH=/opt/packages/lib64 ./sublime_text

Try StarDict

sudo dnf install stardict --releasever=/ --forcearch=x86_64 --installroot=/opt/packages

file /opt/packages/usr/bin/stardict 
/opt/packages/usr/bin/stardict: ELF 64-bit LSB pie executable, x86-64, version 1 

LD_LIBRARY_PATH=/opt/packages/lib64  /opt/packages/usr/bin/stardict

ps -ef | grep rose
liveuser    4861    4702  7 21:50 pts/0    00:00:00 /media/rosetta/rosetta /opt/packages/usr/bin/stardict

Try AnyDesk

cd Downloads

sudo dnf install gtk2 libxkbfile gtkglext-libs -y --releasever=/ --forcearch=x86_64 --installroot=/opt/packages

sudo dnf install pangox-compat.x86_64 -y --releasever=32 --forcearch=x86_64 --installroot=/opt/packages 

wget https://download.anydesk.com/linux/generic-linux/anydesk-6.0.1-amd64.tar.gz

tar -xvzf anydesk-6.0.1-amd64.tar.gz -C .

cd anydesk-6.0.1

file anydesk
anydesk: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=ca72df43a997711254511ed17c9ef43d40edef74, stripped

LD_LIBRARY_PATH=/opt/packages/lib64 ./anydesk

No window pop up, no error display!!!

History

  • 2023/Mar/23: Newly create

Running Command Line Applications in Fedora Linux ARM using Rosetta 2 And Mock

Running Steps

Setup Rosetta 2 using script

wget https://bit.ly/kidrosetta2
chmod +x kidrosetta2

vim kidrosetta2     # Only enable first 3 steps!!!

sudo ./kidrosetta2

Install Mock

sudo dnf install mock -y

Create Fedora x86-64 environment using Mock

mock -r fedora-37-x86_64 --forcearch=x86_64 shell

Try some commands in Mock x86-64

<mock-chroot> sh-5.2# uname -a
Linux 6e9e243ac5894078a1bb848a5bccd24d 6.0.7-301.fc37.aarch64 #1 SMP PREEMPT_DYNAMIC Fri Nov 4 18:13:35 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

<mock-chroot> sh-5.2# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain
::1       localhost localhost.localdomain localhost6 localhost6.localdomain6

<mock-chroot> sh-5.2# cat /etc/resolv.conf 

<mock-chroot> sh-5.2# which ping
/usr/bin/which: no ping in (/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin)

<mock-chroot> sh-5.2# ping google.com
sh: ping: command not found

<mock-chroot> sh-5.2# which zip
/usr/bin/zip

<mock-chroot> sh-5.2# file /usr/bin/zip
/usr/bin/zip: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=3b70ebe222b7e0994a9dfa1cae71493f685c2d31, for GNU/Linux 3.2.0, stripped
<mock-chroot> sh-5.2# zip --help
Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license.
Zip 3.0 (July 5th 2008). Usage:
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
  The default action is to add or replace zipfile entries from list, which
  can include the special name - to compress standard input.
  If zipfile and list are omitted, zip compresses stdin to stdout.
  -f   freshen: only changed files  -u   update: only changed or new files
  -d   delete entries in zipfile    -m   move into zipfile (delete OS files)
  -r   recurse into directories     -j   junk (don't record) directory names
  ...
  -T   test zipfile integrity       -X   eXclude eXtra file attributes
  -y   store symbolic links as the link instead of the referenced file
  -e   encrypt                      -n   don't compress these suffixes
  -h2  show more help
  

<mock-chroot> sh-5.2# which $SHELL
/bin/bash

<mock-chroot> sh-5.2# 

<mock-chroot> sh-5.2# which dnf
/usr/bin/which: no dnf in (/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin)

<mock-chroot> sh-5.2# which wget
/usr/bin/which: no wget in (/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin)

<mock-chroot> sh-5.2# which which
/usr/bin/which

<mock-chroot> sh-5.2# file /usr/bin/which 
/usr/bin/which: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=bdc77dcac615ca4cc39f03ce3d41348113f48242, for GNU/Linux 3.2.0, stripped

<mock-chroot> sh-5.2# which curl
/usr/bin/curl

<mock-chroot> sh-5.2# file /usr/bin/curl 
/usr/bin/curl: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=d0cedd17f019655ffd4a28e6c2a53cf75c0473fd, for GNU/Linux 3.2.0, stripped

<mock-chroot> sh-5.2# cat /etc/*release*
Fedora release 37 (Thirty Seven)
NAME="Fedora Linux"
VERSION="37 (Thirty Seven)"
ID=fedora
VERSION_ID=37
VERSION_CODENAME=""
PLATFORM_ID="platform:f37"
PRETTY_NAME="Fedora Linux 37 (Thirty Seven)"
ANSI_COLOR="0;38;2;60;110;180"
LOGO=fedora-logo-icon
CPE_NAME="cpe:/o:fedoraproject:fedora:37"
DEFAULT_HOSTNAME="fedora"
HOME_URL="https://fedoraproject.org/"
DOCUMENTATION_URL="https://docs.fedoraproject.org/en-US/fedora/f37/system-administrators-guide/"
SUPPORT_URL="https://ask.fedoraproject.org/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_BUGZILLA_PRODUCT="Fedora"
REDHAT_BUGZILLA_PRODUCT_VERSION=37
REDHAT_SUPPORT_PRODUCT="Fedora"
REDHAT_SUPPORT_PRODUCT_VERSION=37
SUPPORT_END=2023-11-14
Fedora release 37 (Thirty Seven)
Fedora release 37 (Thirty Seven)
cpe:/o:fedoraproject:fedora:37

<mock-chroot> sh-5.2# cat /proc/cpuinfo 
processor	: 0
BogoMIPS	: 48.00
Features	: fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 asimddp sha512 asimdfhm dit uscat ilrcpc flagm ssbs sb paca pacg dcpodp flagm2 frint
CPU implementer	: 0x00
CPU architecture: 8
CPU variant	: 0x0
CPU part	: 0x000
CPU revision	: 0

processor	: 1
BogoMIPS	: 48.00
Features	: fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 asimddp sha512 asimdfhm dit uscat ilrcpc flagm ssbs sb paca pacg dcpodp flagm2 frint
CPU implementer	: 0x00
CPU architecture: 8
CPU variant	: 0x0
CPU part	: 0x000
CPU revision	: 0

<mock-chroot> sh-5.2# dmidecode
sh: dmidecode: command not found

Mock help

[liveuser@localhost-live Downloads]$ mock --help
usage: 
       mock [options] {--init|--clean|--scrub=[all,chroot,cache,root-cache,c-cache,yum-cache,dnf-cache,lvm,overlayfs]}
       mock [options] [--rebuild] /path/to/srpm(s)
       mock [options] [--chain] /path/to/srpm(s)
       mock [options] --buildsrpm {--spec /path/to/spec --sources /path/to/src|
       --scm-enable [--scm-option key=value]}
       mock [options] {--shell|--chroot} <cmd>
       mock [options] --installdeps {SRPM|RPM}
       mock [options] --install PACKAGE
       mock [options] --copyin path [..path] destination
       mock [options] --copyout path [..path] destination
       mock [options] --scm-enable [--scm-option key=value]
       mock [options] --dnf-cmd arguments
       mock [options] --yum-cmd arguments

options:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  --rebuild             rebuild the specified SRPM(s)
  --chain               build multiple RPMs in chain loop
  --buildsrpm           Build a SRPM from spec (--spec ...) and
                        sources(--sources ...) or from SCM
  --debug-config        Prints all options in config_opts
  --debug-config-expanded
                        Prints all options in config_opts with jinja template
                        values already expanded
  --shell               run the specified command interactively within the
                        chroot. Default command: /bin/sh
  --chroot              run the specified command noninteractively within the
                        chroot.
  --clean               completely remove the specified chroot
  --scrub [all|chroot|cache|root-cache|c-cache|yum-cache|dnf-cache]
                        completely remove the specified chroot or cache dir or
                        all of the chroot and cache
  --init                initialize the chroot, do not build anything
  --installdeps         install build dependencies for a specified SRPM or
                        SPEC file
  -i, --install         install packages using package manager
  --list-chroots        List all chroot's configs
  --update              update installed packages using package manager
  --remove              remove packages using package manager
  --orphanskill         Kill all processes using specified buildroot.
  --copyin              Copy file(s) into the specified chroot
  --copyout             Copy file(s) from the specified chroot

Check x86-64 folders created by Mock

Installation folder

[liveuser@localhost-live ~]$ ll /var/lib/mock/
total 8
drwxrwxr-x. 4 root mock 4096 Mar 23 11:41 fedora-37-x86_64
drwxrwxr-x. 4 root mock 4096 Mar 23 11:41 fedora-37-x86_64-bootstrap

Cache folder

[root@localhost-live /]# cd var/
[root@localhost-live var]# cd cache/
[root@localhost-live cache]# cd mock/

[root@localhost-live mock]# du -sh *
414M	fedora-37-x86_64
375M	fedora-37-x86_64-bootstrap

[root@localhost-live mock]# find .
.
./fedora-37-x86_64
./fedora-37-x86_64/yum_cache
./fedora-37-x86_64/dnf_cache
./fedora-37-x86_64/dnf_cache/fedora.solv
./fedora-37-x86_64/dnf_cache/expired_repos.json
./fedora-37-x86_64/dnf_cache/updates-filenames.solvx
./fedora-37-x86_64/dnf_cache/fedora-a3256cee7c7d69ad
./fedora-37-x86_64/dnf_cache/fedora-a3256cee7c7d69ad/metalink.xml
./fedora-37-x86_64/dnf_cache/fedora-a3256cee7c7d69ad/packages
./fedora-37-x86_64/dnf_cache/fedora-a3256cee7c7d69ad/packages/make-4.3-11.fc37.x86_64.rpm
./fedora-37-x86_64/dnf_cache/fedora-a3256cee7c7d69ad/packages/crypto-policies-20220815-1.gite4ed860.fc37.noarch.rpm
./fedora-37-x86_64/dnf_cache/fedora-a3256cee7c7d69ad/packages/xxhash-libs-0.8.1-3.fc37.x86_64.rpm
./fedora-37-x86_64/dnf_cache/fedora-a3256cee7c7d69ad/packages/unzip-6.0-58.fc37.x86_64.rpm
./fedora-37-x86_64/dnf_cache/fedora-a3256cee7c7d69ad/packages/xeyes-1.2.0-5.fc37.x86_64.rpm
...
./fedora-37-x86_64-bootstrap/dnf_cache/updates-fd4d3d0d1c34d49a/packages/readline-8.2-2.fc37.x86_64.rpm
./fedora-37-x86_64-bootstrap/dnf_cache/updates-fd4d3d0d1c34d49a/packages/openssl-libs-3.0.8-1.fc37.x86_64.rpm
./fedora-37-x86_64-bootstrap/dnf_cache/updates-fd4d3d0d1c34d49a/packages/libidn2-2.3.4-1.fc37.x86_64.rpm
./fedora-37-x86_64-bootstrap/dnf_cache/updates-fd4d3d0d1c34d49a/packages/libffi-3.4.4-1.fc37.x86_64.rpm
./fedora-37-x86_64-bootstrap/dnf_cache/updates-fd4d3d0d1c34d49a/packages/elfutils-libs-0.189-1.fc37.x86_64.rpm

Rosetta 2 check

[liveuser@localhost-live ~]$ ps -ef | grep rose
root        5531    5530  0 11:57 pts/0    00:00:00 /media/rosetta/rosetta /bin/sh -i -l
liveuser    5572    3333  0 11:57 pts/1    00:00:00 grep --color=auto rose

Install gcc in Mock

[liveuser@localhost-live ~]$  mock -r fedora-37-x86_64 --forcearch=x86_64  -i gcc

Cross-compile x86-64 binary in Mock

mock -r fedora-37-x86_64 --forcearch=x86_64 shell

<mock-chroot> sh-5.2# cat hello.c # Created from /var/lib/mock/fedora-37-x86_64/buildroot

#include <stdio.h>

int main() {
  printf("Hello World!\n");
}

<mock-chroot> sh-5.2# gcc hello.c -o hello

<mock-chroot> sh-5.2# file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=f0057b33fca0a896d0ecb53bf4c5474f6473afbb, not stripped

<mock-chroot> sh-5.2# ./hello 
Hello World!

References

History

  • 2023/Mar/28: Newly create. Add Mock help.
#!/bin/sh
# History
# 2023/Mar/17: Newly create. Add xeyes
echo "-- 1. Mount Rosetta 2 disk"
sudo sudo mkdir /media/rosetta
sudo mount -t virtiofs rosetta /media/rosetta
echo "-- 2. Enable Rosetta 2 for x86-64 binary files"
echo ":rosetta:M::\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x3e\x00:\xff\xff\xff\xff\xff\xfe\xfe\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/media/rosetta/rosetta:F" | sudo tee /proc/sys/fs/binfmt_misc/register
echo "-- 3. Check Rosetta 2 setup"
ls -alh /proc/sys/fs/binfmt_misc/
echo "-- 4. Install neccesary Linux x86-64 libraries"
sudo mkdir /opt/packages
sudo dnf install glibc glibc.i686 libstdc++ xeyes --releasever=/ --forcearch=x86_64 --installroot=/opt/packages -y
echo "-- 5. Create a symbolic link to Linux x86-64 library"
sudo ln -s /opt/packages/lib64/ld-linux-x86-64.so.2 /lib64/
echo "-- 6. Download unrar Linux x86-64 binary"
cd Downloads
wget https://www.rarlab.com/rar/rarlinux-x64-621.tar.gz
echo "--7. Extract unrar Linux x86-64 binary"
tar -xvzf rarlinux-x64-621.tar.gz -C .
echo "--8. Check unrar binary architecture"
file rar/rar
echo ""
echo "Done!"
echo ""
echo "Try to run it by using this command!!!"
echo ""
echo "LD_LIBRARY_PATH=/opt/packages/lib64 ./rar/rar /h"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment