Shellskript lässt sich nicht über Python ausführen

Antworten
Benutzeravatar
Sven.Beierlein
Beiträge: 36
Registriert: 01 Okt 2008, 16:20

Shellskript lässt sich nicht über Python ausführen

Beitrag von Sven.Beierlein »

Hallo,
ich wollte die Installation von Linux auf einem Client automatisieren dabei wollte ich mit mit einem Pythonskript mehrere Systembefehle auf dem Zielclient ausführen lassen, jedoch bekomme ich da mit dem chroot immer Probleme, deshalb habe ich mir 2 Bashskripte geschrieben die ich dann mit Pyhton ausführen wollte, jedoch lassen sich die Bashskripte nur per hand nachdem die Installation einen Fehler bringt und man sich am Zielclient eingeloggt hat, ausführen.
Vielleicht kann mir ja einer weiterhelfen :roll: hier die Skripte:

Zunächst das Pythonskript für die Installation:

Code: Alles auswählen

source = DEPOT_MOUNT + '/install/linux'
tarfile = '/sarge_projektimage_sda6._.tar.gz'

# Get first harddisk
disk = getHarddisks()[0]
ui.getMessageBox().addText( "Verwende Festplatte %s (%s MB).\n" %(disk.device, disk.size) )

# Create new partitiontable
disk.deletePartitionTable()

# writing partition table of /dev/sda
execute ('/sbin/sfdisk /dev/sda < ' + source + '/partitiontable')

# format partitions
ui.getMessageBox().addText("Formatiere angelegte Partitionen\n")
execute ('/sbin/mkswap /dev/sda5')
execute ('/sbin/mkfs -t ext3 /dev/sda6')
execute ('/sbin/mkfs -t ext3 /dev/sda7')
ui.getMessageBox().addText("Formatierung abgeschlossen\n")

#mount sda6
ui.getMessageBox().addText("Mounte sda6\n")
execute ('/bin/mkdir /mnt/sda6')
execute ('/bin/mount /dev/sda6 /mnt/sda6')

#change directory to /mnt/sda6
ui.getMessageBox().addText("Wechsle ins Verzeichnis /mnt/sda6\n")
execute ('cd /mnt/sda6')

# unpack tar-image from netmount to local hdd
ui.getMessageBox().addText("Entpacke Tar-File\n")
execute ('/bin/tar xzpf ' + source + tarfile + ' -C /mnt/sda6') 

# execute the final.sh to make devices and install GRUB-Bootloader
copy(source + '/chr.sh', '/mnt/sda6')
copy(source + '/final.sh', '/mnt/sda6')
#execute ('sh /mnt/sda6/final.sh')

#Do hardware inventory
hardwareInventory(filename=DEPOT_MOUNT + "/pcpatch/pclog/" + hostname + ".hw")

# Umount partition
disk.umountPartition(partition=1)

# Reboot
reboot()
Hier die Bashskripte:

Code: Alles auswählen

#!/mnt/sda6/bin/bash (ich habs auch schon mit /bin/bash oder /bin/sh ausprobiert aber ebenfalls erfolglos :-/  )
#final.sh

mount -o bind /proc /mnt/sda6/proc
mount -o bind /sys /mnt/sda6/sys
mount -o bind /dev /mnt/sda6/dev

/usr/sbin/chroot /mnt/sda6 ./chr.sh 

Code: Alles auswählen

#!/bin/bash

mkinitrd -o /boot/initrd.img-2.6.26.5 2.6.26.5
grub-install /dev/sda
Benutzeravatar
j.schneider
uib-Team
Beiträge: 1804
Registriert: 29 Mai 2008, 15:14

Re: Shellskript lässt sich nicht über Python ausführen

Beitrag von j.schneider »

Hallo,

vielleicht hilft die folgendes Skript weiter, mit dem man debian installieren kann:

Code: Alles auswählen

target = '/mnt/hd'
source = DEPOT_MOUNT + '/install/' + productId

def doChrooted(root, cmds):
        f = open( root + '/tmp/doit.sh', 'w')
        for cmd in cmds:                     
                print >> f, cmd              
        f.close()                            
        execute("chroot %s /bin/bash /tmp/doit.sh" % root)


execute("dpkg -i %s/binutils_*.deb" % source)
execute("dpkg -i %s/debootstrap_*.deb" % source)

# Get harddisks
disks = getHarddisks()

# Use first Harddisk
disk = disks[0]     
ui.getMessageBox().addText( "Verwende Festplatte %s (%s MB).\n" % (disk.device, round(disk.size/1024/1024)) )

# Check disk size
if (disk.size < 6000*1000*1000):
        # Disk smaller than 6000 MB => give up
        raise Exception("Festplatte zu klein!\n")

# Get current partitions
partitions = disk.getPartitions()

if not partitions:
        # No partition found on harddisk
        ui.getMessageBox().addText("Auf Festplatte %s sind keine Partitionen vorhanden.\n" % disk.device)

# Create new partitiontable
disk.deletePartitionTable()

disk.createPartition(start='0M', end='1024M', fs='linux-swap')
disk.createPartition(start='1024M', end='100%', fs='ext2', boot=True)

ui.getMessageBox().addText("Erstelle Auslagerungspartion auf %s1\n" % disk.device)
execute("mkswap %s1" % disk.device)                                               
execute("swapon %s1" % disk.device)                                               

ui.getMessageBox().addText("Erstelle Root-Dateisystem auf %s2\n" % disk.device)
execute("mkfs.ext3 %s2" % disk.device)                                         

disk.mountPartition(2, target)

ui.getMessageBox().addText("Installiere Basis-System...\n")
execute("debootstrap --verbose --arch i386 etch %s http://ftp.de.debian.org/debian" % target)

ui.getMessageBox().addText("Bereite System vor...\n")
f = open( os.path.join(target, 'etc', 'mtab'), 'w' ) 
print >> f, "%s2 / ext3 rw 0 0" % disk.device
f.close()                                            

f = open( os.path.join(target, 'etc', 'fstab'), 'w' )
print >> f, "# /etc/fstab: static file system information."
print >> f, "#"                                            
print >> f, "# <file system>   <mount point>   <type>       <options>                   <dump>  <pass>"
print >> f, "proc              /proc           proc         defaults                    0        0"    
print >> f, "%s1               none            swap         sw                          0        0" % disk.device
print >> f, "%s2               /               ext3         defaults,errors=remount-ro  0        1" % disk.device
print >> f, "/dev/cdrom        /media/cdrom    udf,iso9660  user,noauto                 0        0"              
f.close()                                                                                                        

f = open( os.path.join(target, 'etc', 'kernel-img.conf'), 'w' )
print >> f, "do_symlinks = yes"                                
print >> f, "relative_links = yes"                             
print >> f, "do_bootloader = no"                               
print >> f, "do_bootfloppy = no"                               
print >> f, "do_initrd = yes"                                  
print >> f, "link_in_boot = no"                                
print >> f, "postinst_hook = /sbin/update-grub"                
print >> f, "postrm_hook = /sbin/update-grub"                  
f.close()                                                      

f = open( os.path.join(target, 'etc', 'apt', 'sources.list'), 'w' )
print >> f, "deb http://ftp.de.debian.org/debian etch main non-free contrib"
print >> f, "deb-src http://ftp.de.debian.org/debian etch main non-free contrib"
print >> f, "deb http://security.debian.org/ etch/updates main"
f.close()

copy('/etc/network/interfaces', os.path.join(target, 'etc', 'network', 'interfaces'))

f = open( os.path.join(target, 'etc', 'hostname'), 'w' )
print >> f, hostname
f.close()

execute("mount -o bind /proc %s/proc" % target)
execute("mount -o bind /dev %s/dev" % target)
os.putenv("LD_PRELOAD", "")

cmds = [
        "export DEBIAN_PRIORITY=critical",
        "export DEBIAN_FRONTEND=noninteractive",
        "apt-get update",
        "apt-get --yes install console-common console-data grub linux-image-2.6-686",
        "grub-install --recheck %s" % disk.device,
        "update-grub -y",
        "rm /etc/localtime",
        "ln -s /usr/share/zoneinfo/%s /etc/localtime" % productProperties.get('timezone', 'Europe/Berlin'),
        "install-keymap /usr/share/keymaps/i386/%s.kmap.gz" % productProperties.get('console_keymap', 'qwertz/de-latin1-nodeadkeys'),
        "echo root:password | chpasswd",
]

doChrooted( target, cmds )

# Umount harddisk
execute("umount %s/proc" % target)
execute("umount %s/dev" % target)
disk.umountPartition(partition=2)

# Reboot
reboot()
Benutzeravatar
Sven.Beierlein
Beiträge: 36
Registriert: 01 Okt 2008, 16:20

Re: Shellskript lässt sich nicht über Python ausführen

Beitrag von Sven.Beierlein »

Ich habe jetzt mein Pythonskript soweit angepasst, bekomme jedoch folgenden Fehler:
Command 'chroot /mnt/sda6 /bin/bash /tmp/doit.sh' failed (32512): /bin/bash: error while loading shared libraries: /usr/local/lib/geo_override.so: cannot open shared object file: No such file or directory

Code: Alles auswählen

import os
target = '/mnt/sda6'
source = DEPOT_MOUNT + '/install/linux'
tarfile = '/sarge_projektimage_sda6._.tar.gz'

def doChrooted(root, cmds):
        f = open( root + '/tmp/doit.sh', 'w')
	for cmd in cmds:
	        print >> f, cmd
	f.close()
	execute("chroot %s /bin/bash /tmp/doit.sh" % root)

# Get first harddisk
disk = getHarddisks()[0]
ui.getMessageBox().addText( "Verwende Festplatte %s (%s MB).\n" %(disk.device, disk.size) )

# Create new partitiontable
disk.deletePartitionTable()

# writing partition table of /dev/sda
execute ('/sbin/sfdisk /dev/sda < ' + source + '/partitiontable')

# format partitions
ui.getMessageBox().addText("Formatiere angelegte Partitionen\n")
execute ('/sbin/mkswap /dev/sda5')
execute ('/sbin/mkfs -t ext3 /dev/sda6')
execute ('/sbin/mkfs -t ext3 /dev/sda7')
ui.getMessageBox().addText("Formatierung abgeschlossen\n")

#mount sda6
ui.getMessageBox().addText("Mounte sda6\n")
execute ('/bin/mkdir /mnt/sda6')
execute ('/bin/mount /dev/sda6 /mnt/sda6')

# unpack tar-image from netmount to local hdd
ui.getMessageBox().addText("Entpacke Tar-File\n")
execute ('/bin/tar xzpf ' + source + tarfile + ' -C /mnt/sda6') 

# executes to make devices and install GRUB-Bootloader
execute ("mount -o bind /proc %s/proc" % target)
execute ("mount -o bind /sys %s/sys" % target)
execute ("mount -o bind /dev %s/dev" % target)

cmds =[
       "mkinitrd -o /boot/initrd.img-2.6.26.5 2.6.26.5",
       "grub-install /dev/sda"
      ]

doChrooted( target, cmds )

#Do hardware inventory
hardwareInventory(filename=DEPOT_MOUNT + "/pcpatch/pclog/" + hostname + ".hw")

# Umount partition
disk.umountPartition(partition=1)

# Reboot
reboot()

Benutzeravatar
j.schneider
uib-Team
Beiträge: 1804
Registriert: 29 Mai 2008, 15:14

Re: Shellskript lässt sich nicht über Python ausführen

Beitrag von j.schneider »

Hallo,

bitte einmal so abändern:

Code: Alles auswählen

cmds =[
       "mkinitrd -o /boot/initrd.img-2.6.26.5 2.6.26.5",
       "grub-install /dev/sda"
      ]

os.putenv("LD_PRELOAD", "")
doChrooted( target, cmds )
Gruß
Jan
Benutzeravatar
Sven.Beierlein
Beiträge: 36
Registriert: 01 Okt 2008, 16:20

Re: Shellskript lässt sich nicht über Python ausführen

Beitrag von Sven.Beierlein »

Jo, danke habs auch gerade gesehen :P jetzt gehts thx
Antworten