Video Screencast Help
Symantec to Separate Into Two Focused, Industry-Leading Technology Companies. Learn more.

Add the Power of Python to the Linux Preboot Environment

Created: 21 Dec 2006 • Updated: 28 May 2009 | 3 comments
Language Translations
eorme's picture
+3 3 Votes
Login to vote

Deployment Solution in the past few years has given us several preboot options to choose from. We no longer have to settle for DOS batch scripting, but we now have Linux and WinPE to choose from. Ironically, many people have stuck with DOS because Linux bash scripting is very daunting, and WinPE is a separately purchased package. Well, now there is an easy way to install a Python interpreter in Linux so you can now write more robust scripts on Linux with better logging, more intuitive logic, etc. Doing this will increase your speed of scripting, as well as make scripts more manageable.

Pre-requisites: Deployment Solution 6.5SP1 or newer.

Since the Linux preboot in Deployment Solution is built from existing files that reside on the server, we do not have the luxury of using the regular installer for Python. Instead, we will be doing a manual installation, but since most of the files are in a few select locations this is relatively easy.

There is also the problem of the preboot disk size being limited to 32 megabytes (64 megabytes in 64-bit environments). This makes it difficult to include all the libraries (modules) that makes Python so useful. I got around this issue by only copying the Python binary and a shared library to the preboot image itself, and redirecting the PYTONHOME environment variable to a directory on the DS share. This means that only the libraries (modules) that are used are downloaded. It also means that adding Python support only adds about 3.5 Megabytes to your preboot image size.

It should be noted that if you are pressed for space in your Linux preboot image, you could have the interpreter and shared library on the eXpress share and use symbolic links to point to them. But since BootDisk creator doesn't provide a mechanism for symbolic links, the symbolic links would have to be made on the fly by your scripts.

If you have only a few select libraries you are using, there is also the possibility of having everything self contained in the preboot itself. The default location for the libraries are "/usr/local/lib/python2.5". This choice is left up to you for your individual situation. I have chosen in this example to have the binary and the shared library on the image, and the libraries on the eXpress share, because it leaves all libraries available for use, and has minimal impact on the Linux preboot image size.

Note: I should mention here that this is in no way endorsed or supported by Altiris, and you do this at your own risk. When doing something like this always make sure to do it in a test environment first.

So, lets get started. I have already prepared all of the files for you. They contain the Python interpreter compiler on Linux using gcc version 3.2, as well as a shared library. If you are interested in how I built the files that I will have you download, I will explain this at the end of the article under the heading "How to construct the files available for download".

Note: These files contain GPL software, and are in no way associated with Altiris. I provide them for convenience only.

Step 1:

Download these two files:

License: GPL
By clicking the download link below, you agree to the terms and conditions in the GPL License
File Size: 1.36 MB Download: python_binaries.zip
File Size: 13.9 MB Download: pythonLibraries.zip
Support: User-contributed tools on the Juice are not supported by Altiris Technical Support. If you have questions about a tool, please communicate directly with the author by visiting their profile page and clicking the 'contact' tab.

Note: These are x86 versions. If you are running in x64 or Itanium Linux, you'll need to build your own files. (read the section at the end of the article).

Step 2:

Unzip pythonLibraries.zip to your desktop, then copy the folder labeled "python" to your eXpress share. Once you have done that, it should have this structure:

….eXpress/python/lib/python2.5/…..

Step 3:

Unzip the pythonbinaries.zip file to your desktop. There should be two files, "python" and "libutil.so.1". We will pull files from it in a later step.

Step 4:

Open the Deployment Console, and launch the PXE Configuration Utility. Click on your Linux configuration and press "Edit", then click "Edit Boot Image".

Step 5:

Right click the root of your configuration (It will have "(MenuOption…)" in the title), and create a folder called lib. Now right click the lib folder and click "Add File". Browse to your desktop where you unzipped the file in step 3. Find libutil.so.1, and press "open". You should now see that file as being inside the lib folder.

Step 6:

Create a folder called "usr" off the configuration (it will be a sibling to lib). Then create a folder called "bin" within that folder. Add the file called "python" (once again go to your desktop where you unzipped pythonbinaries.zip to) to the bin folder. This is the actual Python binary.

Step 7:

Click next, and then next again. You should now see that your Linux preboot is rebuilding. When it is done click "Finish"

Step 8:

Click OK, and then press "Save" (VERY IMPORTANT). You should see a little progress bar running next to the OK button. Go ahead and press OK (it is not necessary to wait for the progress bar to stop).

Python is now installed in the preboot. Because of the way Deployment Solution is designed, it is easiest to have your scripts reside as files on the eXpress share rather than having them as embedded scripts within Deployment Server. I would suggest making a folder called "pythonScripts" off of your eXpress share.

Since Python is a very high level language, most people will want to use a program such as IDLE to compose their code. Using this tool will help catch syntactical bugs in your programs, and also give you a Python interpreter shell to work with. Visit http://www.python.org and install a windows version of Python (which includes IDLE).

All scripts that are embedded scripts are run through the bash interpreter, and therefore we cannot enter our Python scripts into them directly. In order to run these referenced scripts we must use an embedded script to call them and then pipe them into the python interpreter.

Steps To Run These Scripts

Step 1:

Create a new job in the Deployment Console.

Step 2:

Press "Add", and then "Run Script". The "Run Script" window will appear with a large text window. Enter the following script.

#Set the location of the python libraries
export PYTHONHOME=/mnt/ds/python
#Run the Script
dos2unix /mnt/ds/pythonScripts/myscript.py | /usr/bin/python

Note: The character between the two paths is the pipe symbol, it is on the same key as the backslash. It may be possible to use "cat" instead of dos2unix, but using dos2unix ensures that there will be no interpreter errors due to Windows newlines.

Step 3:

Replace "myscript.py" with the script you intend to run.

You're done! You can start writing Python, putting them in your pythonScript folder and referencing them in embedded jobs. Here is a good test script to see if your Python environment works properly:

#!/usr/bin/python
File = open("/mnt/ds/pythonScripts/pythonHello.txt","w")
File.writelines("Hello from the Python World")
File.close()

This will write "Hello from the Python World" to a file on your eXpress share.

You can also make functions that will run a command and record all output to a log file. The following is an example using a class.

import os,string
class Runner:

	def __init__(self,logfile):
        	self.logfile = logfile
	
	def run(self,cmd):
		'''Runs a command putting the standard out in the logfile'''
		handle = os.popen(cmd, 'r') 
		log = open(self.logfile, 'a')
		log.write("\n===========================================\n")
		log.write("Running Command: " + cmd + "\n")
		log.write("===========================================\n")
		log.write(string.join(handle.readlines()))
		handle.close()
		log.close()

For more information on Python programming and the modules available please visit: http://www.python.org.

How to construct the files available for download

The files I made available for download are easy to create if you have a working Linux system available to you. You will have to construct your own if you want a different version of Python (we are using 2.5), or need to compile it for another architecture). You could also construct them yourself if you want to customize any Python compiler options etc.

Step 1:

Set up a Linux box with a major distribution (I used Fedora Core 5) and download the Python source. http://www.python.org/ftp/python/2.5/Python-2.5.ta...

Step 2:

Untar it, and then in the untarred directory execute the following commands:

./configure CC=gcc32
make
make install

This will configure, compile and install Python on your system (it may be already, but this will ensure the latest version). You'll notice that I changed the compiler to gcc32. I did this because according to the Python documentation, Python compiled on gcc 4.1 or newer has potential problems. Fedora offers the gcc32 as an add-on package and can be installed by running "yum install compat*gcc32*" on your command line.

Step 3:

Create a new directory called "python" and then create a folder called "lib" in it. Then copy the "/usr/local/lib/python2.5" directory into that folder. Use a zip utility to compress the python directory. This is your pythonLibraries.zip file.

Step 4:

The pythonbinaries zip has two files in it, libutil.so.1 and python. The first is the file "/lib/libutil-2.4.so" renamed to be "libutil.so.1", and the second is copied from "/usr/bin/python". Zip these two files together into one file and you're ready for follow the instructions above!

Comments 3 CommentsJump to latest comment

Aurelien_VF's picture

Thanks for this Tuto...

But something goes wrong if you try tu use subprocess...
Cannot import subprocess.

Have you tried ?

Aurel

0
Login to vote
eorme's picture

No, I haven't tried using subprocesses. I've kept my python scripts fairly simple.

If you're able to get around the problem, let me know.

+1
Login to vote
TheDude05's picture

The supplied library does not contain the modules written in C. You can download the full library from here.

http://ajwsoftware.com/python_libs.tar.gz

0
Login to vote