#!/usr/bin/env python # -*- coding: UTF8 -*- # # Install Nonux on harddisk # Author: Marcel J. Zwiebel # # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either # version 3.0 of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details: # import os import gtk import time import sys import getopt g_exedir = '/hd_install' g_mntdir = '/hd_install/mount' g_bootfilesdir = '/hd_install/bootfiles' g_installdev = '' g_gui = 0 g_writelilo = False g_installok = False g_bootdisk = '' def runbg( program, *args ): return os.spawnvp( os.P_WAIT, program, (program,) + args ) def write_status( statmsg ): global g_exedir global g_gui if ( g_gui == 1 ): statfile = g_exedir statfile += '/stat' os.system( 'echo "%(statmsg)s" > %(statfile)s' % vars() ) else: print( statmsg ) def execute_install( installpart ): global g_mntdir global g_gui global g_bootfilesdir global g_installdev global g_writelilo global g_installok global g_bootdisk mntdir = g_mntdir bootfilesdir = g_bootfilesdir _partname = {} _osname = {} osidx = 0 space_error = False wincounter = 1 winname = '' bootsafemode = False bootdisk = '' if ( g_gui == 0 ): pcheck = os.popen("fdisk -l | grep /dev/ | grep -iv Schijf | grep -iv Wissel | grep -iv Uitgebreid | grep -iv Disk | grep -iv Swap | grep -iv Ext | grep %s" % installpart ) pvalid = pcheck.readline() pvalid = pvalid.strip() if ( pvalid == '' ): print( 'Unknown or not valid partition %s.\nInstallation terminated.\n' % installpart ) return ask = '' while ( (ask != 'y') & (ask != 'n') ): ask = raw_input( 'Installation of Nonux onto partition %s\nAll data on this partition will be deleted!\nContinue (y/n)? ' % installpart) if ( ask == 'n' ): return write_status('\nPlease wait.\n\nAnalyzing the hard disk drive...') partition = os.popen("fdisk -l | grep /dev/ | grep -iv Schijf | grep -iv Wissel | grep -iv Uitgebreid | grep -iv Disk | grep -iv Swap | grep -iv Ext | cut -d ' ' -f1") filesystem = os.popen("fdisk -l | grep /dev/ | grep -iv Schijf | grep -iv Wissel | grep -iv Uitgebreid | grep -iv Disk | grep -iv Swap | grep -iv Ext") partname = partition.readline() osname = filesystem.readline() warn_multiboot = False while partname: partname = partname.strip() os.system( 'mount %(partname)s %(mntdir)s' % vars() ) osname = osname.strip() if osname[13] == '*': bootdisk = partname[:8] # check for Windows primary partitions is_win_part = False parttype = osname[52:54] parttype = parttype.strip() # windows partition types (no hidden ones) if ( parttype == '6' or parttype == '7' or parttype == 'b' or parttype == 'c' or parttype == 'e' ): is_win_part = True # check if primary partition primaries = os.popen( 'parted %s print | grep prima' % partname[:8] ) primname = primaries.readline() primname = primname.strip() if ( primname != '' ): is_win_part = False while primname: ppnr = primname[:2] if ( ppnr.strip() == partname[8] ): is_win_part = True primname = primaries.readline() osname = osname[-(len(osname)-56): ] if ( is_win_part == True ): osname += " (Windows)" warn_multiboot = True if ( g_gui == 0 ): if ( partname == installpart ): sizecmd = ( 'df %s | grep /dev/ | cut -c21-30' % partname ) psize = os.popen( sizecmd ) partsize = psize.readline() partsize = partsize.strip() if ( partsize == '' ): partsize = int(0) if ( int(partsize) < 3670016 ): space_error = True if ( partname != installpart): if ( is_win_part == True ): _partname[osidx] = partname _osname[osidx]= osname osidx = osidx + 1 time.sleep(3) os.system( 'umount %s' % partname ) time.sleep(2) partname = partition.readline() osname = filesystem.readline() if ( g_gui == 0 ): if ( space_error == True ): print( '\nPartition %s is to small!\nThe minimal partition size has to be 3.5 GB.\nInstallation terminated.\n' % installpart ) return g_installdev = installpart write_status('\nChanging administrator password...\n\n') msgtext = "It is recommended to change now the administrator" msgtext += "password which will be used after installation\n" msgtext += "of Nonux onto your hard disk drive.\n\n\n" msgtext += "See for more information:\n" msgtext += "http://www.nonux.nl/en/" if ( g_gui == 1 ): os.spawnl( os.P_WAIT, '/hd_install/changerootpw.py' ) os.spawnlp( os.P_WAIT, 'userpasswd' ) else: print( msgtext ) os.system( 'passwd' ) cupscheck = os.popen('ps -A | grep cupsd') cupsstat = cupscheck.readline() cupsstat = cupsstat.strip() if ( cupsstat != '' ): write_status('\nPlease wait.\n\nStopping print system ...' ) retrycount = 0 while ( (cupsstat != '') & (retrycount < 5) ): os.system('/etc/rc.d/rc.cups stop') time.sleep(2) cupscheck = os.popen('ps -A | grep cupsd') cupsstat = cupscheck.readline() cupsstat = cupsstat.strip() retrycount = retrycount + 1 if ( cupsstat != '' ): write_status('\nAn error has occured.\nRestart the LiveCD and try again.\n(error stopping cups)') return write_status('\nInstalling Nonux.\n\nFormatting partition %s ...' % installpart ) mntcmd = ( 'umount %s' % installpart ) os.system( mntcmd ) time.sleep(3) oscmd = ( 'mke2fs -b 4096 -j -L Nonux -q %s' % installpart ) osres = os.system( oscmd ) if osres != 0: write_status('\nA critical error has occured.\nInstallation terminated.\n(error formatting partition)') return mntcmd = 'mount %(installpart)s %(mntdir)s' % vars() osres = os.system( mntcmd ) if osres != 0: write_status('\nA critical error has occured.\nInstallation terminated.\n(error mounting partition)') return else: if ( g_gui == 1 ): write_status('\nInstalling Nonux.\n\nMaking swap file ...') else: print( 'Making swap file...' ) os.system( 'dd if=/dev/zero of=%s/swapfile bs=1024 count=524288' % mntdir ) if ( g_gui == 1 ): write_status('\nInstalling Nonux.\n\nActivating swap file ...') else: print( 'Activating swap file...' ) os.system( 'mkswap %s/swapfile' % mntdir ) os.system( 'swapon %s/swapfile' % mntdir ) os.system( 'mkdir %s/{boot,tmp,proc,sys,media,mnt}' % mntdir ) msgbase = ('\nInstalling Nonux.\n\nCopying files') msgbaseshort = ( 'Copying files' ) msgtext = msgbase msgtext += ' /boot ...' msgtextshort = msgbaseshort msgtextshort += ' /boot...' if ( g_gui == 1 ): write_status( msgtext ) else: write_status( msgtextshort ) os.system( 'cp --preserve=all %(bootfilesdir)s/{System.map,initrd,liloboot.bmp,map,vmlinuz} %(mntdir)s/boot' % vars() ) for cpdir in ( 'bin', 'dev', 'etc', 'home', 'lib', 'opt', 'root', 'sbin', 'usr', 'var', 'media' ): msgtext = msgbase msgtext += ' /' msgtext += cpdir msgtext += ' ...' msgtextshort = msgbaseshort msgtextshort += ' /' msgtextshort += cpdir msgtextshort += "..." if ( g_gui == 1 ): write_status( msgtext ) else: write_status( msgtextshort ) os.system( 'cp --preserve=all -R /%(cpdir)s %(mntdir)s/' % vars() ) if ( g_gui == 1 ): write_status('\nInstalling Nonux.\n\nUpdating configuration ...') else: print( 'Updating configuration...' ) os.system( 'echo "1" > %s/tmp/isinstall' % mntdir ) os.system( 'chown dusr %s/tmp/isinstall' % mntdir ) os.system( 'cp --preserve=all %(mntdir)s/etc/bootsplash/themes/nonux/config/bootsplash-1024x768.cfg.hd %(mntdir)s/etc/bootsplash/themes/nonux/config/bootsplash-1024x768.cfg' % vars() ) os.system( 'rm -f %s/etc/bootsplash/themes/nonux/config/bootsplash-1024x768.cfg.hd' % mntdir ) # autostart on first GNOME login os.system( 'mv %(mntdir)s/home/dusr/.config/autostart/changepw.desktop.disable %(mntdir)s/home/dusr/.config/autostart/changepw.desktop' % vars() ) os.system( 'cp --preserve=all /etc/inittab_dusr %s/etc/inittab' % mntdir ) os.system( 'rm -f %s/etc/inittab_dusr' % mntdir ) os.system( 'rm -f %s/usr/share/applications/hd_install.desktop' % mntdir ) os.system( 'rm -f %s/home/dusr/Desktop/hd_install.desktop' % mntdir ) os.system( 'echo "1" > %s/etc/ishd' % mntdir ) os.system( 'mv %(mntdir)s/usr/bin/halt.disable %(mntdir)s/usr/bin/halt' % vars() ) try: o_file = open( ('%s/etc/fstab' % mntdir ), 'w' ) except: os.system( 'umount %s' % installpart ) write_status('\nA critical error has occured.\nInstallation terminated.\n(error making fstab)') return o_file.write( '/swapfile swap swap defaults,noatime 0 0\n' ) o_file.write( '%s / ext3 defaults,noatime 1 1\n' % installpart ) o_file.write( 'devpts /dev/pts devpts auto 0 0\n' ) o_file.close if ( g_gui == 1 ): write_status('\nInstalling Nonux.\n\nConfiguring boot manager ...') else: print( 'Configuring boot manager...' ) try: o_file = open( ('%s/etc/lilo.conf' % mntdir ), 'w' ) except: os.system( 'umount %s' % installpart ) write_status('\nA critical error has occured.\nInstallation terminated.\n(error making lilo.conf)') return if ( bootdisk == '' ): bootsector = os.popen('fdisk -l | grep /dev/hda:') if bootsector.readline() != '': o_file.write( 'boot = /dev/hda\n' ) bootdisk = '/dev/hda' else: bootsector = os.popen('fdisk -l | grep /dev/sda:') if bootsector.readline() != '': o_file.write( 'boot = /dev/sda\n' ) bootdisk = '/dev/sda' else: o_file.write( 'boot = %s\n' % bootdisk ) g_bootdisk = bootdisk if ( osidx > 0 ): o_file.write( 'prompt\n' ) o_file.write( 'timeout = 300\n' ) else: o_file.write( 'timeout = 0\n' ) if ( bootsafemode == False ): o_file.write( 'append=" splash=silent"\n' ) o_file.write( 'initrd=/boot/initrd\n' ) if ( bootsafemode == False ): o_file.write( 'vga=791\n\n' ) o_file.write( 'bitmap=/boot/liloboot.bmp\n' ) o_file.write( 'bmp-colors=0,251,233,251,229,233\n' ) o_file.write( 'bmp-table=60p,90p,1,18\n' ) o_file.write( 'bmp-timer=95p,400p,0,251,251\n\n' ) o_file.write( 'image = /boot/vmlinuz\n' ) o_file.write( ' root = %s\n' % installpart ) o_file.write( ' label = Nonux\n read-only\n' ) oscounter = osidx while ( osidx > 0 ): osidx = ( osidx - 1 ) partname = _partname[osidx] osname = _osname[osidx] if ( wincounter > 1 ): winname = ( 'Windows%s' % wincounter ) else: winname = 'Windows' # note: windows only can boot from first disk and (primary) partionnumbers less than 5 and # partition 4 not a logical disk if ( partname.find( bootdisk ) > -1 and len( partname ) == 9 ): # fallback check if parted detection didn't work if ( partname[8] == '1' or partname[8] == '2' or partname[8] == '3' or partname[8] == '4' ): wincounter = ( wincounter + 1 ) o_file.write( '\nother = %s\n' % partname ) o_file.write( ' label = %s\n' % winname ) #o_file.write( ' table = %s\n' % bootdisk ) o_file.write( ' boot-as=0x80\n' ) o_file.close time.sleep(1) os.system( 'sync' ) time.sleep(2) # check MBR for GRUB bootmanager if bootdisk != '': os.system( 'dd ibs=512 count=1 if=%s of=/tmp/mbr.dump' % bootdisk ) else: bootsector = os.popen('fdisk -l | grep /dev/hda:') if bootsector.readline() != '': os.system( 'dd ibs=512 count=1 if=/dev/hda of=/tmp/mbr.dump' ) else: bootsector = os.popen('fdisk -l | grep /dev/sda:') if bootsector.readline() != '': os.system( 'dd ibs=512 count=1 if=/dev/sda of=/tmp/mbr.dump' ) grub_check = os.popen( 'cat /tmp/mbr.dump | grep -i grub' ) grub_installed = grub_check.readline() grub_installed = grub_installed.strip() if ( grub_installed != '' ): # GRUB is used as bootmanager, ask if to be replaced by LILO if ( g_gui == 1 ): msgdialog = gtk.MessageDialog( None, gtk.DIALOG_MODAL, gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, 'Boot manager GRUB has been detected.\nNonux uses LILO. You can configure GRUB\nyourself or choose for automatic\ninstallation of LILO (only Nonux and\nWindows partitions)\n\nInstall LILO into the MBR (y/n)?' ) response = msgdialog.run() msgdialog.destroy() if response == gtk.RESPONSE_YES: g_writelilo = True else: ask = '' while ( (ask != 'y') & (ask != 'n') ): ask = raw_input( 'Boot manager GRUB has been detected.\nNonux uses LILO. You can configure GRUB\nyourself or choose for automatic\ninstallation of LILO (only Nonux and\nWindows partitions)\n\nInstall LILO into the MBR (y/n)? ' ) if ( ask == 'y' ): g_writelilo = True else: # GRUB not detected, install LILO if ( oscounter > 0 ): # Nonux and Windows (multiboot) if ( g_gui == 1 ): msgdialog = gtk.MessageDialog( None, gtk.DIALOG_MODAL, gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, "Install boot manager?\nIf you don't know what that means,\nsay 'Yes'." ) response = msgdialog.run() msgdialog.destroy() if response == gtk.RESPONSE_YES: g_writelilo = True bootsafemode = False else: ask = '' while ( (ask != 'y') & (ask != 'n') ): ask = raw_input( 'Install boot manager (y/n)?\nIf you dont know what that means, say "y". ' ) if ( ask == 'y' ): g_writelilo = True ask = '' while ( (ask != 'y') & (ask != 'n') ): ask = 'y' if ( ask == 'n' ): bootsafemode = True else: # only boot Nonux g_writelilo = True if ( g_gui == 1 ): bootsafemode = False else: ask = '' while ( (ask != 'y') & (ask != 'n') ): ask = 'y' if ( ask == 'n' ): bootsafemode = True g_installok = True def main(): global g_gui global g_exedir global g_installdev global g_writelilo global g_installok global g_mntdir global g_bootdisk exedir = g_exedir mntdir = g_mntdir partname = '' try: opts, args = getopt.getopt( sys.argv[1:], "g", ["device="]) except getopt.GetoptError: print( '\nWrong option(s):\n--device, for example: --device=/dev/hda1\n -g for gui output\n' ) sys.exit(2) pid = os.getpid() os.system( 'echo "%(pid)s" > %(exedir)s/pid' % vars() ) for o, a in opts: if o == '-g': g_gui = 1 if o == '--device': partname = a if ( partname == '' ): print( '\nOption "--device" is required.\nFor example: --device=/dev/hda1\n' ) sys.exit(2) write_status('\n\nPlease wait...\n') # disable autobrowsing volumes os.system( 'killall gnome-volume-manager' ) time.sleep(4) execute_install( partname ) if ( g_installok == True ): if ( g_writelilo == True ): if ( g_gui == 1 ): write_status('\nInstalling Nonux.\n\nInstalling boot manager ...') else: print( 'Installing boot manager...' ) os.system( '/sbin/lilo -r %s/ -w' % mntdir ) time.sleep(1) os.system( 'sync' ) time.sleep(2) bootdisk = g_bootdisk # check of lilo was written to MBR os.system( 'dd ibs=512 count=1 if=%s of=/tmp/mbr.dump' % bootdisk ) lilo_check = os.popen( 'cat /tmp/mbr.dump | grep -i lilo' ) lilo_installed = lilo_check.readline() lilo_installed = lilo_installed.strip() if ( lilo_installed == '' ): msgtext = "\nA critical error has occurred writing the\n" msgtext += "boot manager.\n" msgtext += "Although Nonux has been installed onto your\n" msgtext += ( "hard drive, partition %s, it can not\n" % bootdisk ) msgtext += "automatically startup on the next reboot.\n" if ( g_gui == 1 ): msgdialog = gtk.MessageDialog( None, gtk.DIALOG_MODAL, gtk.MESSAGE_WARNING, gtk.BUTTONS_OK, msgtext ) msgdialog.run() msgdialog.destroy() else: print( msgtext ) os.system( 'umount %s' % g_installdev ) os.system( 'echo "0" > %s/pid' % exedir ) if ( g_installok == True ): time.sleep(1) write_status('\nInstallation was successful!\n\nReboot the computer without\nthe LiveCD in the CD/DVD drive.') # renable GNOME volume manager os.system( '/usr/bin/gnome-volume-manager &' ) sys.exit(0) if __name__ == "__main__": main()