Skip to content

Instantly share code, notes, and snippets.

@kity-linuxero
Last active May 21, 2019 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kity-linuxero/f41ab940b373d5ceee63ca077a4b31b8 to your computer and use it in GitHub Desktop.
Save kity-linuxero/f41ab940b373d5ceee63ca077a4b31b8 to your computer and use it in GitHub Desktop.

Sistemas Operativos 2019

Ejercicio Entregable “Compilación de sistemas con Buildroot”

Se crearon las imágenes:

Siguiendo las instrucciones de la práctica bzImage rootfs.cpio.xz rootfs.ext2 y un enlace simbólico rootfs.ext4

Primer error:

Kconfig fragment '/home/cristian/buildroot/boards/qemu/x86/linux.config' for 'linux' does not exist
linux/linux.mk:528: recipe for target '/home/cristian/buildroot/boards/qemu/x86/linux.config' failed
make: *** [/home/cristian/buildroot/boards/qemu/x86/linux.config] Error 1

Solución: $(TOPDIR)/boards/qemu/x86/linux.config

se debió cambiar a $(TOPDIR)/board/qemu/x86/linux.config

Una vez que están las imágenes se procedió a:

Probar las imágenes:

#kvm -m 32 -kernel bzImage --initrd rootfs.cpio.xz -append "root=/dev/sda console=ttyS0" -nographic rootfs.ext4

Funcionaba y booteaba correctamente.

Agregar un script de arranque (/etc/init.d) en la imagen ext4fs que imprima “Hola” y el nombre de los integrantes del grupo.

Se agregó el archivo /etc/init.d/S99nombre Donde aparece Hola... y mi nombre con mi legajo.

Se agregó info extra en /etc/issue

Luego se procedió a la mofificación del initrd

Descompresión del archivo rootfs.cpio.xz xz -dv rootfs.cpio.xz

Se extrae el archivo cpio cpio -idv < ../rootfs.cpio

Se borra el link simbólico rm /sbin/init

Se agrega el siguiente script como /sbin/init

#!/bin/sh
# El mount de /dev que estaba acá no es necesario
/bin/mount -t proc proc /proc
/bin/mount -o remount,rw /
/bin/mkdir -p /dev/pts /dev/shm
/bin/mount -a
/sbin/swapon -a
/bin/ln -sf /proc/self/fd /dev/fd
/bin/ln -sf /proc/self/fd/0 /dev/stdin
/bin/ln -sf /proc/self/fd/1 /dev/stdout
/bin/ln -sf /proc/self/fd/2 /dev/stderr
/bin/hostname -F /etc/hostname

# Código de ustedes...
# Intentando hacer el switch root (a cruzar los dedos)...
sleep 1
# Montar el rootfs...
/sbin/switch_root /mnt/rootfs

mount --move /dev /mnt/rootfs/dev # Si no está switch_root no encuentra nada en `/dev`

# Código de ustedes...
# Ejecutando busybox...
sleep 1
/bin/busybox

Se dan permisos de ejecución chmod +x /sbin/init

El script para armado del initramfs find . | cpio -H newc -ov > ../rootfs.cpio ; xz --check=crc32 ../rootfs.cpio

Script de arranque

sudo kvm -m 64 -kernel bzImage --initrd rootfs.cpio.xz -append "root=/dev/sda console=ttyS0" -nographic rootfs.ext4


Corrección

El initrd es ignorado por el kernel porque no tiene el formato correcto y por esto pasa desapercibido que el script /sbin/init tiene errores por lo que no hace lo pedido. Revisar los tips y faq publicados para volver a generar el initrd. Revisar el script. Verificar que los archivos del initramfs no pierdan su propietario al desempaquetarlo (la mayoría debería pertenecer a root). Usar los comandos xz y cpio con los parámetros necesarios para que el formato sea adecuado: xz --check=crc32 y cpio -H newc -o.

REentrega

Descomprimir rootfs.cpio.xz

xz -d rootfs.cpio.xz

Se extraen los archivos del cpio

cpio -idv < ../rootfs.cpio

Se verificó que el archivo cpio perdió el owner root.

$ ls -l
total 64
drwxr-xr-x 2 cristian cristian 4096 may 20 08:29 bin
drwxr-xr-x 4 cristian cristian 4096 may 20 08:29 dev
drwxr-xr-x 5 cristian cristian 4096 may 20 08:29 etc
-rwxr-xr-x 1 cristian cristian  178 may 20 08:29 init
drwxr-xr-x 3 cristian cristian 4096 may 20 08:29 lib
lrwxrwxrwx 1 cristian cristian    3 may 20 08:29 lib32 -> lib
lrwxrwxrwx 1 cristian cristian   11 may 20 08:29 linuxrc -> bin/busybox
drwxr-xr-x 2 cristian cristian 4096 may 20 08:29 media
drwxr-xr-x 2 cristian cristian 4096 may 20 08:29 mnt
drwxr-xr-x 2 cristian cristian 4096 may 20 08:29 opt
drwxr-xr-x 2 cristian cristian 4096 may 20 08:29 proc
drwx------ 2 cristian cristian 4096 may 20 08:29 root
drwxr-xr-x 2 cristian cristian 4096 may 20 08:29 run
drwxr-xr-x 2 cristian cristian 4096 may 20 08:29 sbin
drwxr-xr-x 2 cristian cristian 4096 may 20 08:29 sys
drwxrwxrwt 2 cristian cristian 4096 may 20 08:29 tmp
drwxr-xr-x 6 cristian cristian 4096 may 20 08:29 usr
drwxr-xr-x 4 cristian cristian 4096 may 20 08:29 var

Se cambia el owner a root con el siguiente comando

chown -R root:root

$ ls -l
total 64
drwxr-xr-x 2 root root 4096 may 20 08:29 bin
drwxr-xr-x 4 root root 4096 may 20 08:29 dev
drwxr-xr-x 5 root root 4096 may 20 08:29 etc
-rwxr-xr-x 1 root root  178 may 20 08:29 init
drwxr-xr-x 3 root root 4096 may 20 08:29 lib
lrwxrwxrwx 1 root root    3 may 20 08:29 lib32 -> lib
lrwxrwxrwx 1 root root   11 may 20 08:29 linuxrc -> bin/busybox
drwxr-xr-x 2 root root 4096 may 20 08:29 media
drwxr-xr-x 2 root root 4096 may 20 08:29 mnt
drwxr-xr-x 2 root root 4096 may 20 08:29 opt
drwxr-xr-x 2 root root 4096 may 20 08:29 proc
drwx------ 2 root root 4096 may 20 08:29 root
drwxr-xr-x 2 root root 4096 may 20 08:29 run
drwxr-xr-x 2 root root 4096 may 20 08:29 sbin
drwxr-xr-x 2 root root 4096 may 20 08:29 sys
drwxrwxrwt 2 root root 4096 may 20 08:29 tmp
drwxr-xr-x 6 root root 4096 may 20 08:29 usr
drwxr-xr-x 4 root root 4096 may 20 08:29 var

Se vuelve a generar el archivo cpio:

Sobre el directorio donde está el fs descomprimido ejecutar

find . | cpio -H newc -ov > ../rootfsnew.cpio

Comprimir el archivo cpio

xz --check=crc32 rootfsnew.cpio

Errores

Intentando hacer el switch root (a cruzar los dedos)...
input: ImExPS/2 Generic Explorer Mouse as /devices/platform/i8042/serio1/input/input3
BusyBox v1.29.2 (2019-04-23 17:06:09 -03) multi-call binary.

Usage: switch_root [-c CONSOLE_DEV] NEW_ROOT NEW_INIT [ARGS]

Free initramfs and switch to another root fs:
chroot to NEW_ROOT, delete all in /, move NEW_ROOT to /,
execute NEW_INIT. PID must be 1. NEW_ROOT must be a mountpoint.

	-c DEV	Reopen stdio to DEV after switch
mount: mounting /dev on /mnt/rootfs/dev failed: No such file or directory
Kernel panic - not syncing: Attempted to kill init! exitcode=0x0000ff00

CPU: 0 PID: 1 Comm: init Not tainted 4.17.19 #1
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1ubuntu1 04/01/2014
Call Trace:
 dump_stack+0x58/0x76
 panic+0x8f/0x1b8
 do_exit+0x986/0x990
 ? vfs_read+0x80/0x120
 do_group_exit+0x2a/0x90
 sys_exit_group+0x11/0x20
 do_int80_syscall_32+0x6a/0x110
 entry_INT80_32+0x31/0x31
EIP: 0xb7eea4f3
EFLAGS: 00000286 CPU: 0
EAX: ffffffda EBX: 000000ff ECX: fffffff4 EDX: 080dc008
ESI: bfa3006c EDI: bfa30068 EBP: 00000000 ESP: bfa2ff34
 DS: 007b ES: 007b FS: 0000 GS: 0033 SS: 007b
Kernel Offset: 0x0 from 0xc1000000 (relocation range: 0xc0000000-0xc27dffff)
---[ end Kernel panic - not syncing: Attempted to kill init! exitcode=0x0000ff00
 ]---

Se debio corregir el script para que funcione correctamente, finalmente luego de los errores con /dev/consoley /dev/null

#!/bin/sh
# El mount de /dev que estaba acá no es necesario
/bin/mount -t proc proc /proc
/bin/mount -o remount,rw /
/bin/mkdir -p /dev/pts /dev/shm
/bin/mount -a
/sbin/swapon -a
/bin/ln -sf /proc/self/fd /dev/fd
/bin/ln -sf /proc/self/fd/0 /dev/stdin
/bin/ln -sf /proc/self/fd/1 /dev/stdout
/bin/ln -sf /proc/self/fd/2 /dev/stderr
/bin/hostname -F /etc/hostname

# Código de ustedes...

ROOT="/mnt/rootfs"
# Me quedo con el parámetro
ROOT_DEV=$(cat /proc/cmdline | cut -d '=' -f2 | cut -d ' ' -f1)

# montar el root
[ -d ${ROOT} ] || mkdir -p ${ROOT}
mount ${ROOT_DEV} ${ROOT}

# Creo los /dev/null y /dev/console
mknod -m 660 ${ROOT}/dev/console c 5 1
mknod -m 660 ${ROOT}/dev/null c 1 3

# switch to new rootfs and exec init
cd ${ROOT}
exec /sbin/switch_root . "/sbin/init"

mount --move /dev /mnt/rootfs/dev # Si no está switch_root no encuentra nada en /dev 

Cristian Giambruni Leg: 7499/6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment