Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
MaximilianVINCENT

Black Hole Tutorial 03-Roots and Shells and Files Systems

Recommended Posts

OpenComputers 1.51 for Minecraft 1.70

Black Hole Tutorial 03- Roots and Shells and File Systems v1.1

I was going to write a basic programming tutorial when I realized there is one more maze we really should go through: OpenOS itself.
 

Make a superflat map in creative mode or use the one from last time. Turn on a computer you have installed OpenOS onto. After the booting process is done, what you are looking at is an input prompt for a program called the shell. It is the main interface to your files. (At the prompt, type in man sh for more information.) Type in ls and see what we start with.

 

bin boot etc lib mnt tmp usr init.lua

 

Unless you changed something, you are looking at a list of directory and file names. Do you know why all these directories are here, what they store and how they are meant to be used?

 

 

It All Starts at /

What you are seeing is based on the Filesystem Hierarchy Standard. It hasn't changed much since the early days of UNIX, over 40 years ago. Basically it is a definition of what kinds of files belong where.

/ - Root

Every file and directory starts from the root directory, sometimes called the trunk (like a tree).
When you turn on the computer, this is where you start at. This is similar to C:\ under Windows.

Notice that the rest of these start with '/', meaning they are branching off from root. This is different to what Windows uses ('\').

/bin - System programs that come with OpenOS.

/boot - contains files required to boot OpenOS. Look but do not touch.

/etc - used to store config files. Like the Registry in Windows.

/etc/rc.d - run control directory.

It controls what programs you want to start after booting (I think).

/lib - Libraries

contains the shared libraries used by OpenOS.

/mnt - Mount

where external file systems (floppies, Hard Drives, RAIDs, etc.) are attached (mounted) to root

/tmp - Temporary

Used to store files until the computer is shut down or rebooted. At that time everything in this directory is erased.

/usr - User

Programs, libraries, documentation, etc. for all user-related programs.

/usr/man
This is where the text files used by the man command are stored. Have you tried the 'man' program? Type in man cat.
Now type cat /usr/man/cat to see the same text.
   Side Note: You can and should write a man page for your programs, if not for others than yourself.

/usr/misc - For miscellaneous files, I suppose.

   While not necessary, but I would suggest adding these to help keep things organized:
 

/usr/bin - non-system programs.

(think stuff like Firefox, VLC, XTree, etc. Programs used by everyone on a computer beyond the system basics.) Windows equivalent is C:\Program Files.

/usr/lib - non-system libraries/APIs

/var - stands for variable files.

All files that change frequently are traditionally kept in a subdirectory of /var.

/var/log - log files (written records of what your programs have been doing, useful for debugging)
/var/lib - packages and database files

/home - user personal files (Optional, but traditional)

/home/bin - personal programs (you are probably seeing a pattern by now)

If you have multiple people using the same computer, give each person their own directory under /home (i.e. /home/max, /home/vincent). This way everyone has a place put their ocNotes.txt or TreeMuncher.lua. Windows equivalent is C:\users\max\.

Please remember, what I am saying above are just suggestions. Try different things and find out what works best for you. You could dump everything in root and probably be okay.

All these directories do not have to be on your boot drive. You can mount other filesystems and connect them to your root file structure where you please. More on that later in this tutorial.

All these directories may not make much sense for OpenComputers since it has no permission system. Under Linux/UNIX, everyone has an account to log into. Each account is set up to allow access to only the parts of the system that user needs, hopefully keeping any damage they can do to a minimum. The 'root' account has access to everything, just like you do now. Tread lightly, my friends. If you are working on some large projects, DO make backups onto alternative media and/or other computers, just like you would in the real world.

 

How to Get Around

File and directory names are case-sensitive. That means you can have 4 files named AB.txt, Ab.txt, aB.txt and ab.txt (unless you are running Windows, since OpenComputers saves files onto your PC's Hard Drive and Windows is case-insensitive).

try LS or Ls or lS and see what happens

 

These are the commands you be be using the most often. There are lots more, but this tutorial is long enough already.

 

ls
man ls
ls -M shows there should be 2 files in our root directory, but we see only one. where is the other file?
ls -a shows all files in the specified directory.

 

ah, a .osprop was hiding!
Starting a file with a . tells the OS it is meant to be a hidden file. Under Linux a lot of programs use this to hide their config files in the each user's /home directory.
 

ls /bin

If you do not recognize any of these, it is a good time to start learning. We go over the most basic commands below. Use man (try man man to start) on the ones you don't recognize. Not all will work, but most will.

cd
man cd if you haven't already.
current working directory means whatever directory you are currently in. pwd will show you where you are at. The far left of the command line shows the same.

mkdir
creates a directory
mkdir /usr/bin
ls /usr

rm
this will delete files, directories, and unmount filesystems.
rm /usr/bin
ls /usr

Warning: Use with caution, there is no undelete!

cat and more
Both are used to display files onto your screen. Cat all at once, more is controlled by your input.
cat /bin/shutdown.lua
more /bin/redstone.lua

try arrow down, enter and space bar

edit
This is how you create, edit and view your files.
cd /usr/misc
edit happy.txt

if /usr/misc/happy.txt does not yet exist, it will be created for you.
edit -r /boot/02_os.lua
that extra -r tells edit to not let you change anything in the file you loaded (i.e. Read Only).

 

Self Test: After running edit the first time, a config file is written. Can you guess where? (answers at bottom)

alias
Since most people still use Windows for some reason, you might be more used to dir than ls.
alias dir ls
now try dir -a
alias by itself shows the current list of aliases

 

Command History

At the prompt, use the up and down arrow keys. The shell keeps a list of the last ten commands you have entered.

Side Note: Shell Variables

type in echo $PATH, you will see a list of directories the shell looks in for programs you ask it to run.
When you type in ls it looks in the current directory and if not found there, it looks in the first directory in that list. If not there, then the next one and so on.

These are the shell variables I could find. Colons are used to separate entries and ends with a period by itself.
$PATH = /bin:/usr/bin:/home/bin:. (where to look for programs)
$HOME = /home (more useful with multiple users on same machine)
$MANPATH = /usr/man:.
$PWD = your current working directory
$PS1 = $PWD# (what is displayed at the prompt)

The reason I bring this up is that it can be useful to change these (adding /home/max/bin to the $PATH, for instance).
Thanks to dgelessus, I found out about the 'set' command. (No man page and it was not mentioned in the Linux Shell tutorials I looked at, that's why I missed it. Sorry folks.)

 

Type in set to see a list of the current shell variables, looks like I missed quite a few. SHELL, EDITOR and PAGER point to the programs you want to use for that purpose. HISTSIZE is for the size of the command history. The rest I am not sure about yet.

Example: set HISTSIZE='20' to double your Command History.

 

 

File Systems and your Media

Have you tried putting more than one hard drive in a computer? It is a good way to initially expand your long-term storage. Plus if you somehow destroy the OS on your boot hard drive, the data on your second drive is safe. I would suggest to put /home on different media than your boot drive. This makes it easy to upgrade your computer and keep all your important stuff in one place. /var is also recommended if you plan on lots of data collection (think of log files out of control, filling your boot drive to capacity and possible crash).

Unlike Windows, there is no D:\, E:\, etc for each new drive added to a computer. Everything is connected to root somewhere, primarily in /mnt, secondarily where you want it.

ls /mnt shows the first three characters of the address of file systems mounted and ready for use on this computer. Not that handy.
components shows all items connected to this computer and their full addresses, look inside computer to compare.
df (Disk Free) to see mounted filesystems, how much room is available and where they are connected to root. So much better.

tmpfs (Temporary FileSystem) is a small amount of memory setup as a RAM Drive that will be erased when the computer is turned off. Even if you are running OpenOS off the floppy, it is available. You might not even need a Hard Drive if your programs are small enough and you do not let the power run out.

 

OpenOS is the default label on the hard drive you booted from, openos is the floppy disk. Small difference but important.

 

Time for an upgrade!
Type shutdown at the prompt to turn off computer.
add another hard drive, look at its address (mine starts with a7f, replace with the first three characters of your hard drive), turn computer on
component and df again to see what has changed
cd /mnt/a7f and ls to see it is a blank drive.
Let's give it a label in addition to an address. label -a a7f data
Type df again and find "data" in the list. Go look at new label on the Hard Drive in the Computer.

With all this potential,  we have to put something on this new drive.
make sure you are still in /mnt/a7f and create a directory mkdir doc
cd doc
start a new file edit ocNotes.txt
take a few notes on what you have learned so far, save and exit.

Self Test: Mount a floppy, Copy all of /usr onto it. Label it 'UsrBackup'. Look in /bin and figure out how to unmount the floppy. Use man pages for help.

Now constantly going to /mnt/??? is not the best way to access other filesystems. We are going to attach this Hard Drive to root at /home.
mount a7f /home or mount data /home

ls /home to see if it worked
 
This is cool, but we have a problem. If we reboot the computer, it will not remember that we mounted 'data' at /home.

Note: Next part paraphrased from http://ocdoc.cil.li/tutorial:oc3_hard_drives

We need to add a program that mounts the disk at /home for us when it is inserted. Every hard drive can have such an autorun script. It has to be named autorun or autorun.lua and must be in the root directory of the hard drive we want mounted, in this case 'data'. This script is automatically executed when the computer mounts our hard drive.
So before we reboot:

cd /home or cd /mnt/a7f
edit autorun.lua

Paste or type the next four lines:

-- Mount this Hard Drive to /home
local fs = require("filesystem")
local proxy = ...
fs.mount(proxy, "/home")


Save and Quit
Lastly, reboot

ls should show us /home is available.


Bonus: Not Enough Room!

 

You want a geological database, projected to be just over 10MB in size. You could try to portion it out to 24 floppy disks, swap out hard drives, or set up a RAID (Redundant Array of Independant Drives) of Tier 3 Hard Drives wired up or next to your computer. It turns three Hard Drives into a single drive, but at a cost. Any Hard Drives added to a RAID will be wiped. If you take out any of the Hard Drives in a RAID all data on all drives are lost.  Works like a Disk Drive with a LOT more room. (If you want to learn more, look up 'RAID Level 0'.)

 

 

Least Signifigant Bits

  • Ctrl-Alt-C to exit a running program
  • Ctrl-C to restart computer while at the command line (think warm boot)
  • One more directory: /net - where entire remote file systems are mounted. WIll be very useful later on.
  • Look in minecraft/save/(world name)/opencomputers. You will find all the file systems in there sorted by component address.
  • The edit.cfg is in /etc
  • If you are having problems, post it on the Support Sub-Forum. Additional information, constructive critism or praise of this tutorial should be posted below.

End of Line.

 

Edit v1.1: Added dgelessus's comments into the tutorial, Command History. Spelling Errors.

Link to post
Share on other sites

Nice tutorial, I like the detail - I have a good amount of experience with the OpenOS shell, but I could probably still learn something from here.

 

A few comments:

  • The whole case sensitivity thing is not entirely true. Although ls ≠ Ls, if you're on Windows you still can't have ls.py and Ls.py in the same folder, because Windows is case-insensitive like that.
  • In a normal Unix shell you'd assign envvars using "export name=value", "set name=value" or simply "name=value". I think there is a "set" command in OpenOS.
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.