New IT forum Follow us on Twitter
21 May 2012, 06:53:26 pm *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: TonidoPlug2 - Now in stock!
 
   Home   SHOP Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Linux Twiddly Bits  (Read 1090 times)
Confusticated
New IT customer
Hero Member
*
Posts: 511


« on: 07 October 2011, 01:39:44 pm »

Convert a file of u-boot environment variables to setenv variables.

You have screendumped\cut'n'pasted your u-boot environment variables and
saved them in a file. Now you want the commands to paste back at the u-boot prompt.

Code:
[bldrbob@localhost ~]$ ./env-setenv.sh dreamplug-envars.txt
setenv bootcmd 'setenv ethact egiga0; ${x_bootcmd_ethernet}; setenv ethact egiga1; ${x_bootcmd_ethernet}; ${x_bootcmd_usb}; ${x_bootcmd_kernel}; setenv bootargs ${x_bootargs} ${x_bootargs_root}; bootm 0x6400000;';
setenv bootdelay '3';
setenv baudrate '115200';
setenv x_bootcmd_ethernet 'ping 192.168.2.1';
setenv x_bootcmd_usb 'usb start';
setenv x_bootcmd_kernel 'fatload usb 0 0x6400000 uImage';
setenv x_bootargs 'console=ttyS0,115200';
setenv x_bootargs_root 'root=/dev/sda2 rootdelay=10';
setenv ethact 'egiga0';
setenv ethaddr 'F0:AD:4E:00:7E:xx';
setenv eth1addr 'F0:AD:4E:00:7E:xx';
setenv stdin 'serial';
setenv stdout 'serial';
setenv stderr 'serial';
[bldrbob@localhost ~]$

Contents of 'env-setenv.sh'
Code:
#!/bin/bash
sed -e "s/\(^.[^=]*\)=\(.*$\)/setenv \1 '\2';/1" $@

TWFM YMMV
« Last Edit: 07 October 2011, 02:11:27 pm by Confusticated » Logged

Advocatus Diaboli - My agenda is not to give you the answer, but to guide your thoughts so you derive it for yourself!
Confusticated
New IT customer
Hero Member
*
Posts: 511


« Reply #1 on: 08 October 2011, 07:23:20 am »

Mount a dd'd disk image file

I threw up together this embarrassment whilst burning the midnight oil.
Somebody please write a better version so I can delete my shame and this post.

Code:
#!/bin/bash
# vim:ai:sw=2
N=0
if [ -n "$1" ] ; then
  if [ -f "$1" ] ; then
    fdisk -l "$1" | grep "^${1##*/}" | awk '{print $2}' | \
    while read S ; do
      N=$[$N+1]
      sudo mkdir -p /mnt/tmp$N
      sudo mount -o loop,owner,offset=$[512*$S] "$1" /mnt/tmp$N
      echo "/mnt/tmp$N"
      ls /mnt/tmp$N
    done
  else
    echo "Can't find \"$1\""
  fi
else
  for T in /mnt/tmp[0-9]* ; do
    if grep -q $T /proc/mounts >> /dev/null 2>&1 ; then
      echo "unmounting \"$T\""
      sudo umount $T
    fi
  done
fi

Quote
[bldrbob@localhost ~]$ ./img-mount.sh Dreamplug-Ubuntu.img
[sudo] password for bldrbob:
/mnt/tmp1
uImage
/mnt/tmp2
bin   dev  home  lost+found  mnt  proc  sbin     srv  tmp  var
boot  etc  lib   media       opt  root  selinux  sys  usr
[bldrbob@localhost ~]$
[bldrbob@localhost ~]$ ./img-mount.sh
unmounting "/mnt/tmp1"
unmounting "/mnt/tmp2"
[bldrbob@localhost ~]$


EDIT: As can be seen the script requires loopback support, this is commonly available by default in most distributions, but may not be included in the kernel running on your plug.
« Last Edit: 12 December 2011, 08:32:37 pm by Confusticated » Logged

Advocatus Diaboli - My agenda is not to give you the answer, but to guide your thoughts so you derive it for yourself!
Confusticated
New IT customer
Hero Member
*
Posts: 511


« Reply #2 on: 14 October 2011, 03:13:47 pm »

'chroot' into an arm root filesystem (i.e. a mounted disk image file).

Quote
[bldrbob@localhost ~]$ ./img-mount.sh Dreamplug-Ubuntu.img
[sudo] password for bldrbob:
/mnt/tmp1
uImage
/mnt/tmp2
bin   dev  home  lost+found  mnt  proc  sbin     srv  tmp  var
boot  etc  lib   media       opt  root  selinux  sys  usr
[bldrbob@localhost ~]$

[bldrbob@localhost ~]$sudo ./chroot-arm-rootfs.sh /mnt/tmp2
[root@localhost:/# ls /root
init_setup.sh
[root@localhost:/# exit
[bldrbob@localhost ~]$

This script assumes:
that 'qemu-arm-static' has been downloaded\installed to /usr/bin.
the kernel has support for 'binfmt_misc' compiled in (most do).
that you have the gcc compiler installed.

chroot-arm-rootfs.sh
Code:
#!/bin/bash
# vim:ai:sw=2

[ $(id -u) -eq 0 ] || {
  echo "You must be root to chroot"
  exit -1
}

[ $# -ne 2 ] || {
  echo "Usage: ${0##*/} </directory/path/of/rootfs>"
  exit -1
}

ROOTFS="$1"

[ -d ${ROOTFS}/tmp ] || {
  echo "This does not appear to be a valid rootfs"
  exit -1
}

[ -x ${ROOTFS}/tmp/qemu-arm-static ] || \
  cp -a /usr/bin/qemu-arm-static ${ROOTFS}/tmp

[ -x ${ROOTFS}/tmp/qemu-wrapper ] || {
  cat >/tmp/qemu-wrapper.c << EOF
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char **argv, char **envp) {
        char *newargv[argc + 3];

        newargv[0] = argv[0];    
        newargv[1] = "-cpu";
        newargv[2] = "arm926";
  
        memcpy(&newargv[3], &argv[1], sizeof(*argv) * (argc - 1));    
        newargv[argc + 2] = NULL;
        return execve("/tmp/qemu-arm-static", newargv, envp);
}
EOF
  gcc -static /tmp/qemu-wrapper.c -o ${ROOTFS}/tmp/qemu-wrapper
}

[ -d /proc/sys/fs/binfmt_misc ] || \
  modprobe binfmt_misc

[ -f /proc/sys/fs/binfmt_misc/register ] || \
  mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc

[ -f /proc/sys/fs/binfmt_misc/arm ] &&
  echo -1 >> /proc/sys/fs/binfmt_misc/arm

NAME="arm"
TYPE="M"
OFFSET=""
MAGIC="\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00"
MASK="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"
INTERPRETER="/tmp/qemu-wrapper"
FLAGS=""
echo ":${NAME}:${TYPE}:${OFFSET}:${MAGIC}:${MASK}:${INTERPRETER}:${FLAGS}" >> /proc/sys/fs/binfmt_misc/register
chroot ${ROOTFS} /bin/bash -l
« Last Edit: 16 October 2011, 09:04:29 pm by Confusticated » Logged

Advocatus Diaboli - My agenda is not to give you the answer, but to guide your thoughts so you derive it for yourself!
Confusticated
New IT customer
Hero Member
*
Posts: 511


« Reply #3 on: 31 October 2011, 07:51:42 pm »

'dd' or 'tar', that old chestnut!

If you don't want to use 'dd' to write your SDHC Cards, then make your own gzip'ped tar file from a New IT image...

Code:
[bldrbob@localhost ~] ls
NewIT-GuiPlug-v2.7-4Gb-29oct11-Dreamplug.img.gz
[bldrbob@localhost ~] gzip -dc NewIT-GuiPlug-v2.7-4Gb-29oct11-Dreamplug.img.gz > NewIT-GuiPlug-v2.7-4Gb-29oct11-Dreamplug.img
[bldrbob@localhost ~] mount-img.sh NewIT-GuiPlug-v2.7-4Gb-29oct11-Dreamplug.img
[sudo] password for bldrbob:
/mnt/tmp1
uImage
/mnt/tmp2
bin   dev  home  lost+found  mnt  proc  sbin     srv  tmp  var
boot  etc  lib   media       opt  root  selinux  sys  usr
[bldrbob@localhost ~] cp /mnt/tmp1/uImage NewIT-GuiPlug-v2.7-4Gb-29oct11-Dreamplug.uImage
[bldrbob@localhost ~] sudo tar -C /mnt/tmp2 -czf NewIT-GuiPlug-v2.7-4Gb-29oct11-Dreamplug.tgz .
[bldrbob@localhost ~] mount-img.sh
unmounting "/mnt/tmp1"
unmounting "/mnt/tmp2"
[bldrbob@localhost ~] rm -f NewIT-GuiPlug-v2.7-4Gb-29oct11-Dreamplug.img
[bldrbob@localhost ~] ls NewIT-GuiPlug-v2.7-4Gb-29oct11-Dreamplug.*
NewIT-GuiPlug-v2.7-4Gb-29oct11-Dreamplug.img.gz
NewIT-GuiPlug-v2.7-4Gb-29oct11-Dreamplug.tgz
NewIT-GuiPlug-v2.7-4Gb-29oct11-Dreamplug.uImage

NB see earlier in this thread for 'mount-img.sh'
« Last Edit: 02 November 2011, 10:54:29 pm by Confusticated » Logged

Advocatus Diaboli - My agenda is not to give you the answer, but to guide your thoughts so you derive it for yourself!
Confusticated
New IT customer
Hero Member
*
Posts: 511


« Reply #4 on: 09 November 2011, 06:28:55 pm »

Make a 4GB image file for use with 'mount-img.sh', which you can then un-tar (or whatever) onto.

Quote
[bldrbob@localhost ~]$ ./make-img.sh
Creating blank image...done
Creating partitions...done
[sudo] password for bldrbob:
Creating DOS filesystem
Creating ext4 filesystem
Tidying up..done
[bldrbob@localhost ~]$ls -l DreamPlug.img
-rw-rw-r--. 1 bldrbob bldrbob 4102029312 Nov  9 18:17 DreamPlug.img

Code:
#!/bin/bash
if [ -e DreamPlug.img ] ; then
  echo "DreamPlug.img already exists, quitting!"
  exit -1
fi
echo -en "Creating blank image..."
# Flash should be filled with 0xFF in preference to 0x00
# Dont have a source of 0xFF's, be imaginative
# 1,048,576 Bytes
dd if=/dev/zero count=2048 | tr -t '\000' '\377' | dd of=DreamPlug.img >/dev/null 2>&1
# 4,102,029,312 Bytes
dd if=DreamPlug.img of=DreamPlug.img bs=1048576 seek=1048576 conv=notrunc oflag=append count=3911 >/dev/null 2>&1
echo "done"
echo -en "Creating partitions..."
fdisk DreamPlug.img >/dev/null 2>&1 << EOF
n
p
1
2048
+16M
t
1
6
n
p
2
34816
8011775
w
EOF
echo "done"
PART1=$(sudo losetup -f)
sudo losetup -o $[2048*512] ${PART1} DreamPlug.img
PART2=$(sudo losetup -f)
sudo losetup -o $[34816*512] ${PART2} DreamPlug.img
echo "Creating DOS filesystem"
sudo mkdosfs -n DreamPlug ${PART1} 16384 >/dev/null 2>&1
echo "Creating ext4 filesystem"
sudo mke2fs -L DreamPlug -t ext4 ${PART2} >/dev/null 2>&1
echo -en "Tidying up.."
sudo losetup -d ${PART2} ${PART1}
echo -e "done"
exit 0

NB THERE ARE NO SAFETY CHECKS - if you trash your system with this, it's your fault!
« Last Edit: 10 November 2011, 12:52:43 am by Confusticated » Logged

Advocatus Diaboli - My agenda is not to give you the answer, but to guide your thoughts so you derive it for yourself!
Confusticated
New IT customer
Hero Member
*
Posts: 511


« Reply #5 on: 24 February 2012, 03:36:06 pm »

A number of the NewIT SD image files have been specifically created allowing room for flash wear'n'tear.
This little script will 'short copy' prune the surplus space off the end of the uncompressed SD image file, which will then enable the copy to be written to an undersized (already worn) SD card.

prune-img.sh
Code:
#!/bin/bash
# vim:ai:sw=2
set -e
if [ ! -r "$1" ] ; then
  echo "Cannot read \"$1\""
  echo -e "Usage:\n${0##*/} <diskfile.img>"
  exit -1
fi
T=0
while read P S E A G ; do
  if [ "$S" == "*" ] ; then
    S=$E
    E=$A
  fi
  if [ $E -gt $T ] ; then
    T=$E
  fi
done < <(fdisk -l "$1" | grep "^$1")
T=$(($T+1))
dd if=$1 of=$1-pruned bs=512 count=$T

If you are low on workspace, changing the last line to that below will truncate the original image rather than 'short copy' it.
Code:
dd if=/dev/null of=$1 bs=512 seek=$T

No Guarantees, No Safety Checks, No Responsibility Taken, Your Risk!
« Last Edit: 24 February 2012, 05:53:02 pm by Confusticated » Logged

Advocatus Diaboli - My agenda is not to give you the answer, but to guide your thoughts so you derive it for yourself!
Confusticated
New IT customer
Hero Member
*
Posts: 511


« Reply #6 on: 24 February 2012, 03:47:30 pm »

Got fed up typing the same stuff over'n'over again when looking at an SD image file in gparted.
This script 'sets up'\'tears down' an uncompressed SD image file in a way that gparted accepts.
THIS SCRIPT NECESSARILY RUNS AS ROOT AND CREATES & DELETES, YOU HAVE BEEN WARNED!!!

gparted-img.sh
Code:
#!/bin/bash
# vim:ai:sw=2
set -e
if [ "$(id -u)" -ne 0 ] ; then
  sudo su -c "$0 $@"
fi
if [ ! -r "$1" ] ; then
  echo "Cannot read \"$1\""
  echo -e "Usage:\n${0##*/} <diskfile.img>"
  exit -1
fi
L[0]="$(losetup -f)"
losetup ${L[0]} "$1"
I=1
while read P[${I}] S E A G ; do
  if [ "$S" == "*" ] ; then
    S=$E
    E=$A
  fi
  L[${I}]=$(losetup -f)
  ln -s "${L[${I}]}" "${P[${I}]}"
  losetup "${L[${I}]}" -o $(($S*512)) "$1"
  I=$(($I+1))
done < <(fdisk -l ${L[0]} | grep "^${L[0]}")
gparted ${L[0]}
while [ ${I} -gt 0 ] ; do
  I=$(($I-1))
  losetup -d "${L[${I}]}"
  [ $I -ne 0 ] && rm -f "${P[${I}]}"
done

DISCLAIMER: everything, as always.
Logged

Advocatus Diaboli - My agenda is not to give you the answer, but to guide your thoughts so you derive it for yourself!
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!