#!/bin/bash
#
# makedevs.sh: Script for creating nodes and links in the /dev directory,
# which are missing. Usually it will be started via the boot script and with &
# or from a script which is started via the boot script and with &.
# Existing files remain unchanged, forgotten ones will be created.
# Usefull also for a kernel without devfs support.
# See also devices.txt with the Linux allocated devices
# (/usr/src/linux/Documentation/devices.txt).
# Such scripts are often used with a busybox linux and few devices.
# Similar scripts often have the name mkdev.sh, but they are usually for some
# few and special devices, e. g. ISDN devices.
#
# Todo: Check dead links ([ -L $FILE ]), check parameters of existing nodes
#
# I made this script for OpenSuSE and Debian because Debian Lenny some day 
# lost the device files for my USB keys, USB card readers and USB scanners, so they
# could not be mounted and not be seen by "fdisk -l"/"scanimage -L", although the 
# kernel messages did show the devices. It seems a preveously booted OS initilazed
# the hardware so that Debian could not initialise correct (long ago i saw similar
# problems with SuSE), so this script is a workaround; the clean solution is a cold
# start with at minimum one minute no power.
# Dr. Rolf Freitag, nobodyo at web.de. License: GPL (GNU Public license) V3.
# Version 1.1, 2009-06-24, Status: mature for OpenSuSE and Debian Lenny.
# Version 1.2, 2010-03-29, Addes i2c devices and corrected ttyUSB part.

# Uncomment the next line for debugging/testing or simply see the script working.
#set -x

# Wait one minute to give the device nodes and links enough time to appear at /dev.
#sleep 60

mkdir -p /dev
cd /dev

if [ ! -e mem ]; then
  mknod mem c 1 1
fi
if [ ! -e kmem ]; then
  mknod kmem c 1 2
fi
if [ ! -e null ]; then
  mknod null c 1 3
fi
if [ ! -e port ]; then
  mknod port c 1 4
fi
if [ ! -e zero ]; then
  mknod zero c 1 5
fi
if [ ! -e core ]; then
#  mknod core c 1 6
  ln -s /proc/kcore core
fi
if [ ! -e full ]; then
  mknod full c 1 7
fi
if [ ! -e random ]; then
  mknod random c 1 8
fi
if [ ! -e urandom ]; then
  mknod urandom c 1 9
fi


if [ ! -e ram0 ]; then
  mknod ram0 b 1 0
fi
if [ ! -e ram1 ]; then
  mknod ram1 b 1 1
fi
if [ ! -e ram2 ]; then
  mknod ram2 b 1 2
fi
if [ ! -e ram3 ]; then
  mknod ram3 b 1 3
fi
if [ ! -e ram4 ]; then
  mknod ram4 b 1 4
fi
if [ ! -e ram5 ]; then
  mknod ram5 b 1 5
fi
if [ ! -e ram6 ]; then
  mknod ram6 b 1 6
fi
if [ ! -e ram7 ]; then
  mknod ram7 b 1 7
fi
#if [ ! -e initrd ]; then
#  mknod initrd b 1 250
#fi


if [ ! -e ptyp0 ]; then
  mknod ptyp0 c 2 0
fi
if [ ! -e ptyp1 ]; then
  mknod ptyp1 c 2 1
fi
if [ ! -e ptyp2 ]; then
  mknod ptyp2 c 2 2
fi
if [ ! -e ptyp3 ]; then
  mknod ptyp3 c 2 3
fi
if [ ! -e ptyp4 ]; then
  mknod ptyp4 c 2 4
fi
if [ ! -e ptyp5 ]; then
  mknod ptyp5 c 2 5
fi
if [ ! -e ptyp6 ]; then
  mknod ptyp6 c 2 6
fi
if [ ! -e ptyp7 ]; then
  mknod ptyp7 c 2 7
fi


if [ ! -e fd0 ]; then
  mknod fd0 b 2 0
fi
if [ ! -e fd1 ]; then
  mknod fd1 b 2 1
fi


if [ ! -e ttyp0 ]; then
  mknod ttyp0 c 3 0
fi
if [ ! -e ttyp1 ]; then
  mknod ttyp1 c 3 1
fi
if [ ! -e ttyp2 ]; then
  mknod ttyp2 c 3 2
fi
if [ ! -e ttyp3 ]; then
  mknod ttyp3 c 3 3
fi
if [ ! -e ttyp4 ]; then
  mknod ttyp4 c 3 4
fi
if [ ! -e ttyp5 ]; then
  mknod ttyp5 c 3 5
fi
if [ ! -e ttyp6 ]; then
  mknod ttyp6 c 3 6
fi
if [ ! -e ttyp7 ]; then
  mknod ttyp7 c 3 7
fi


if [ ! -e hda ]; then
  mknod hda b 3 0
fi
if [ ! -e hda1 ]; then
  mknod hda1 b 3 1
fi
if [ ! -e hda2 ]; then
  mknod hda2 b 3 2
fi
if [ ! -e hda3 ]; then
  mknod hda3 b 3 3
fi
if [ ! -e hda4 ]; then
  mknod hda4 b 3 4
fi
if [ ! -e hda5 ]; then
  mknod hda5 b 3 5
fi
if [ ! -e hda6 ]; then
  mknod hda6 b 3 6
fi
if [ ! -e hda7 ]; then
  mknod hda7 b 3 7
fi
if [ ! -e hda8 ]; then
  mknod hda8 b 3 8
fi


if [ ! -e hdb ]; then
  mknod hdb b 3 64
fi
if [ ! -e hdb1 ]; then
  mknod hdb1 b 3 65
fi
if [ ! -e hdb2 ]; then
  mknod hdb2 b 3 66
fi
if [ ! -e hdb3 ]; then
  mknod hdb3 b 3 67
fi
if [ ! -e hdb4 ]; then
  mknod hdb4 b 3 68
fi
if [ ! -e hdb5 ]; then
  mknod hdb5 b 3 69
fi
if [ ! -e hdb6 ]; then
  mknod hdb6 b 3 70
fi
if [ ! -e hdb7 ]; then
  mknod hdb7 b 3 71
fi
if [ ! -e hdb8 ]; then
  mknod hdb8 b 3 72
fi


if [ ! -e tty0 ]; then
  mknod tty0 c 4 0
fi
if [ ! -e tty1 ]; then
  mknod tty1 c 4 1
fi
if [ ! -e tty2 ]; then
  mknod tty2 c 4 2
fi
if [ ! -e tty3 ]; then
  mknod tty3 c 4 3
fi
if [ ! -e tty4 ]; then
  mknod tty4 c 4 4
fi
if [ ! -e tty5 ]; then
  mknod tty5 c 4 5
fi
if [ ! -e tty6 ]; then
  mknod tty6 c 4 6
fi
if [ ! -e tty7 ]; then
  mknod tty7 c 4 7
fi


if [ ! -e ttyS0 ]; then
  mknod ttyS0 c 4 64
fi
if [ ! -e ttyS1 ]; then
  mknod ttyS1 c 4 65
fi
if [ ! -e ttyS2 ]; then
  mknod ttyS2 c 4 66
fi
if [ ! -e ttyS3 ]; then
  mknod ttyS3 c 4 67
fi


if [ ! -e tty ]; then
  mknod tty c 5 0
fi
if [ ! -e console ]; then
  mknod console c 5 1
fi
if [ ! -e ptmx ]; then
  mknod ptmx c 5 2
fi

# cua seems to be outdated
#if [ ! -e cua0 ]; then
#  mknod cua0 c 5 64
#fi
#if [ ! -e cua1 ]; then
#  mknod cua1 c 5 65
#fi
#if [ ! -e cua2 ]; then
#  mknod cua2 c 5 66
#fi
#if [ ! -e cua3 ]; then
#  mknod cua3 c 5 67
#fi


if [ ! -e lp0 ]; then
  mknod lp0 c 6 0
fi


if [ ! -e vcs ]; then
  mknod vcs c 7 0
fi
if [ ! -e vcs1 ]; then
  mknod vcs1 c 7 1
fi
if [ ! -e vcs2 ]; then
  mknod vcs2 c 7 2
fi
if [ ! -e vcs3 ]; then
  mknod vcs3 c 7 3
fi
if [ ! -e vcs4 ]; then
  mknod vcs4 c 7 4
fi
if [ ! -e vcs5 ]; then
  mknod vcs5 c 7 5
fi
if [ ! -e vcs6 ]; then
  mknod vcs6 c 7 6
fi
if [ ! -e vcs7 ]; then
  mknod vcs7 c 7 7
fi


if [ ! -e loop0 ]; then
  mknod loop0 b 7 0
fi
if [ ! -e loop1 ]; then
  mknod loop1 b 7 1
fi
if [ ! -e loop2 ]; then
  mknod loop2 b 7 2
fi
if [ ! -e loop3 ]; then
  mknod loop3 b 7 3
fi
if [ ! -e loop4 ]; then
  mknod loop4 b 7 4
fi
if [ ! -e loop5 ]; then
  mknod loop5 b 7 5
fi
if [ ! -e loop6 ]; then
  mknod loop6 b 7 6
fi
if [ ! -e loop7 ]; then
  mknod loop7 b 7 7
fi


if [ ! -e sda ]; then
  mknod sda b 8 0
fi
if [ ! -e sda1 ]; then
  mknod sda1 b 8 1
fi
if [ ! -e sda2 ]; then
  mknod sda2 b 8 2
fi
if [ ! -e sda3 ]; then
  mknod sda3 b 8 3
fi
if [ ! -e sda4 ]; then
  mknod sda4 b 8 4
fi
if [ ! -e sda5 ]; then
  mknod sda5 b 8 5
fi
if [ ! -e sda6 ]; then
  mknod sda6 b 8 6
fi
if [ ! -e sda7 ]; then
  mknod sda7 b 8 7
fi
if [ ! -e sda8 ]; then
  mknod sda8 b 8 8
fi


if [ ! -e sdb ]; then
  mknod sdb b 8 16
fi
if [ ! -e sdb1 ]; then
  mknod sdb1 b 8 17
fi
if [ ! -e sdb2 ]; then
  mknod sdb2 b 8 18
fi
if [ ! -e sdb3 ]; then
  mknod sdb3 b 8 19
fi
if [ ! -e sdb4 ]; then
  mknod sdb4 b 8 20
fi
if [ ! -e sdb5 ]; then
  mknod sdb5 b 8 21
fi
if [ ! -e sdb6 ]; then
  mknod sdb6 b 8 22
fi
if [ ! -e sdb7 ]; then
  mknod sdb7 b 8 23
fi
if [ ! -e sdb8 ]; then
  mknod sdb8 b 8 24
fi


if [ ! -e sdc ]; then
  mknod sdc b 8 32
fi
if [ ! -e sdc1 ]; then
  mknod sdc1 b 8 33
fi


if [ ! -e sdd ]; then
  mknod sdd b 8 48
fi
if [ ! -e sdd1 ]; then
  mknod sdd1 b 8 49
fi


if [ ! -e sde ]; then
  mknod sde b 8 64
fi
if [ ! -e sde1 ]; then
  mknod sde1 b 8 65
fi


if [ ! -e sdf ]; then
  mknod sdf b 8 80
fi
if [ ! -e sdf1 ]; then
  mknod sdf1 b 8 81
fi


if [ ! -e sdg ]; then
  mknod sdg b 8 96
fi
if [ ! -e sdg1 ]; then
  mknod sdg1 b 8 97
fi


if [ ! -e sdh ]; then
  mknod sdh b 8 112
fi
if [ ! -e sdh1 ]; then
  mknod sdh1 b 8 113
fi


if [ ! -e sdi ]; then
  mknod sdi b 8 128
fi
if [ ! -e sdi1 ]; then
  mknod sdi1 b 8 129
fi


if [ ! -e sdj ]; then
  mknod sdj b 8 144
fi
if [ ! -e sdj1 ]; then
  mknod sdj1 b 8 145
fi


if [ ! -e sdk ]; then
  mknod sdk b 8 160
fi
if [ ! -e sdk1 ]; then
  mknod sdk1 b 8 161
fi


if [ ! -e sdl ]; then
  mknod sdl b 8 176
fi
if [ ! -e sdl1 ]; then
  mknod sdl1 b 8 177
fi


if [ ! -e sdm ]; then
  mknod sdm b 8 192
fi
if [ ! -e sdm1 ]; then
  mknod sdm1 b 8 192
fi


if [ ! -e sdn ]; then
  mknod sdn b 8 208
fi
if [ ! -e sdn1 ]; then
  mknod sdn1 b 8 209
fi


if [ ! -e sdo ]; then
  mknod sdo b 8 224
fi
if [ ! -e sdo1 ]; then
  mknod sdo1 b 8 225
fi


if [ ! -e sdp ]; then
  mknod sdp b 8 240
fi
if [ ! -e sdp1 ]; then
  mknod sdp1 b 8 241
fi


if [ ! -e psaux ]; then
  mknod psaux c 10 1
fi
if [ ! -e rtc ]; then
  mknod rtc c 10 135
fi
if [ ! -e nvram ]; then
  mknod nvram c 10 144
fi


if [ ! -e sr0 ]; then
  mknod sr0 b 11 0
fi
if [ ! -e sr1 ]; then
  mknod sr1 b 11 1
fi


if [ ! -e sg0 ]; then
  mknod sg0 c 21 0
fi
if [ ! -e sg1 ]; then
  mknod sg1 c 21 1
fi
if [ ! -e sg2 ]; then
  mknod sg2 c 21 2
fi
if [ ! -e sg3 ]; then
  mknod sg3 c 21 3
fi


if [ ! -e fb0 ]; then
  mknod fb0 c 29 0
fi
if [ ! -e fb1 ]; then
  mknod fb1 c 29 32
fi
chmod a+rw fb*


if [ ! -e adsp ]; then
  mknod adsp c 14 12
fi
if [ ! -e audio ]; then
  mknod audio c 14 4
fi
if [ ! -e audio1 ]; then
  mknod audio1 c 14 20
fi
if [ ! -e dmmidi ]; then
  mknod dmmidi c 14 9
fi
if [ ! -e dsp ]; then
  mknod dsp c 14 3
fi
if [ ! -e dsp1 ]; then
  mknod dsp1 c 14 19
fi
if [ ! -e mixer ]; then
  mknod mixer c 14 0
fi
if [ ! -e mixer1 ]; then
  mknod mixer1 c 14 16
fi
if [ ! -e midi ]; then
  mknod midi c 14 2
fi
if [ ! -e sequencer ]; then
  mknod sequencer c 14 1
fi
if [ ! -e sequencer2 ]; then
  mknod sequencer2 c 14 8
fi

if [ ! -e video0 ]; then
  mknod video0 c 81 0
fi

if [ ! -e usbscanner0 ]; then
  mknod usbscanner0 c 180 48
fi

if [ ! -e ttyUSB0 ]; then
  mknod ttyUSB0 c 188 0
fi
if [ ! -e ttyUSB1 ]; then
  mknod ttyUSB1 c 188 1
fi
if [ ! -e ttyUSB2 ]; then
  mknod ttyUSB2 c 188 2
fi
if [ ! -e ttyUSB3 ]; then
  mknod ttyUSB3 c 188 3
fi
if [ ! -e ttyUSB4 ]; then
  mknod ttyUSB4 c 188 4
fi

if [ ! -e parport0 ]; then
  mknod parport0 c 99 0
fi


mkdir -p bus/usb/001
mkdir -p bus/usb/002


# for device at libusb:001:001
if [ ! -e bus/usb/001/001 ]; then
  mknod bus/usb/001/001 c 189 0
fi
if [ ! -e bus/usb/001/002 ]; then
  mknod bus/usb/001/002 c 189 1
fi
if [ ! -e bus/usb/001/003 ]; then
  mknod bus/usb/001/003 c 189 2
fi
if [ ! -e bus/usb/001/004 ]; then
  mknod bus/usb/001/004 c 189 3
fi
if [ ! -e bus/usb/002/001 ]; then
  mknod bus/usb/002/001 c 189 128
fi
if [ ! -e bus/usb/002/002 ]; then
  mknod bus/usb/002/002 c 189 129
fi
if [ ! -e bus/usb/002/003 ]; then
  mknod bus/usb/002/003 c 189 130
fi
if [ ! -e bus/usb/002/004 ]; then
  mknod bus/usb/002/004 c 189 131
fi


#[ -c /dev/ipmi0 ] || mknod /dev/ipmi0 c `grep ipmidev /proc/devices | awk "{print \\$1}"` 0
[ -c /dev/ipmi0 ] || mknod /dev/ipmi0 c 253 0
[ -c /dev/watchdog ] || mknod /dev/watchdog c 10 130


# i2c part from superodoctor 2.69.298 from SuperMicro.
# The number of devices to create (max: 256, default to 32)
typeset -i NUMBER
NUMBER=32
# The owner and group of the devices
OUSER=root
OGROUP=root
# The mode of the devices
MODE=600
typeset -i i
i=0
while [ $i -lt $NUMBER ] ; do
  if [ ! -c /dev/i2c-$i ] ; then
    echo /dev/i2c-$i
    mknod -m $MODE /dev/i2c-$i c 89 $i || exit
    chown "$OUSER:$OGROUP" /dev/i2c-$i || exit
  fi
  i=$[$i + 1]
done


if [ ! -e core ]; then
  ln -sf /proc/kcore      core
fi
if [ ! -e fd ]; then
  ln -sf /proc/self/fd    fd
fi
if [ ! -e stdin ]; then
  ln -sf fd/0             stdin
fi
if [ ! -e stdout ]; then
  ln -sf fd/1             stdout
fi
if [ ! -e stderr ]; then
  ln -sf fd/2             stderr
fi

mkdir -p pts

# first TV/video card
ln -s /dev/video0 /dev/v4l/video0 2>/dev/null
ln -s /dev/vbi0 /dev/v4l/vbi0 2>/dev/null

# DCF77 receiver
#ln -s /dev/ttyS1 /dev/refclock-0

# USB scanner access for users (not only root)
chmod -R a+rwX /dev/bus  2>/dev/null

# sound for users
chmod a+rw /dev/dsp* 2>/dev/null
chmod a+rw /dev/audio* 2>/dev/null

# cloop
mknod /dev/cloop b 240 0 2>/dev/null

# frandom, http://www.billauer.co.il/frandom.html
mknod /dev/frandom c 235 11
chmod 444 /dev/frandom
mknod /dev/erandom c 235 12
chmod 444 /dev/erandom

#chmod +x *


