Lecture 3: Unix installation&testing Guntis Barzdins Girts Folkmanis Juris Krumins Mājas darbs #1: Katram instalēt atšķirīgu* Unix paveidu Pētījumā (aptuveni 5-10 lpp) aprakstīt gūto pieredzi: Ar ko šī Unix versija atšķiras no citām, kāpēc to izvēlējāties Unix instalācijas process Galveno soļu screenshoti Svarīgākās konfigurācijas opcijas, jūsu izvēle Izveidot lietotāju “lapsa”, pārbaudīt ka var pieslēgties Aplikācijas “toyshell” kompilācija, uzlabošana Nokompilēt un pārbaudīt “toyshell” darbību Papildināt “toyshell” funkcionalitāti (help, cd, ctrl/D, setenv,...)** Panākt lai lietotājs “lapsa” pieslēdzoties nonāk jūsu “toyshell” un var tajā veikt sakarīgas darbības * - vairāku vienādu Unix paveidu gadījumā, vērtējums būs stingrāks ** - vairāk signāli, systemcall vērtējumu uzlabos #include <stdlib.h> #include <stdio.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <signal.h> #define MAXLINE 200 #define MAXARG 20 extern char **environ; void env(void){ int i; for(i=0;environ[i]!=NULL;i++){ printf("%s\n",environ[i]); } } void exitsh(int status){ _exit(status); } void execute(char *arg[]){ pid_t pid; int status; pid=fork(); if(pid>0){ wait(&status); } else if (pid==0) { execvp(arg[0],arg); printf("Komanda nav atrasta\n"); exitsh(0); } else { printf("Kluda fork() sistemas izsaukuma\n"); } } toyshell.c int main (void){ char cmd[MAXLINE]; char *cmdp; char *av[MAXARG]; int i; while(1){ printf("$toyshell$> "); fgets(cmd,sizeof(cmd),stdin); if(strcmp(cmd,"env\n")==0){ env(); } else if(strcmp(cmd,"exit\n")==0){ exitsh(0); } else { cmdp=cmd; for(i=0;i<MAXARG;i++){ av[i]=strtok(cmdp," \t\n"); cmdp=NULL; } execute(av); } } return(0); } “toyshell” palaišana # /usr/bin/gcc toyshell.c # cc toyshell.c # ./a.out $toyshell$> env USER=root HOME=/root TERM=vt100 PATH=/root/bin:/usr/local/bin:/bin:/usr/bin SHELL=/bin/sh $toyshell$> ps PID TTY TIME CMD 126 co 0:00 -sh 95 c1 0:00 getty 435 p1 0:00 ./a.out 436 p1 0:00 ps $toyshell$> exit # passwd, shadow, group files unix etc # ls -l passwd shadow group -rw-r--r-- 1 root root 705 Sep 23 15:36 group -rw-r--r-- 1 root root 1895 Sep 24 18:20 passwd -rw------- 1 root root 634 Sep 24 18:22 shadow unix etc # unix root # more /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/bin/false daemon:x:2:2:daemon:/sbin:/bin/false adm:x:3:4:adm:/var/adm:/bin/false lp:x:4:7:lp:/var/spool/lpd:/bin/false sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt ... guest:x:405:100:guest:/dev/null:/dev/null nobody:x:65534:65534:nobody:/:/bin/false girtsf:x:1000:100::/home/girtsf:/bin/bash dima:x:1001:100::/home/dima:/bin/bash guntis:x:1002:100::/home/guntis:/bin/bash students:x:1003:100::/home/students:/bin/bash unix root # unix root # more /etc/shadow root:$1$VlYbWsrd$GUs2cptio.rKlGHgAMBzr.:12684:0::::: halt:*:9797:0::::: ... guest:*:9797:0::::: nobody:*:9797:0::::: girtsf:$1$u6UEWKT2$w5K28n2iAB2wNWtyPLycP1:12684:0:99999:7::: dima:$1$BQCdIBdV$xzzlj4s8XT6L9cLAmcoV50:12684:0:99999:7::: guntis:$1$fiJF/0BT$Py9JiQQL6icajjQVyMZ7//:12684:0:99999:7::: students:$1$wueon8yh$nLpUpNOKr8yTYaEnEK6OJ1:12685:0:99999:7::: unix root # unix root # more /etc/group root::0:root bin::1:root,bin,daemon daemon::2:root,bin,daemon sys::3:root,bin,adm adm::4:root,adm,daemon tty::5:girtsf disk::6:root,adm lp::7:lp mem::8: kmem::9: wheel::10:root,girtsf floppy::11:root mail::12:mail ... users::100:games,girtsf nofiles:x:200: qmail:x:201: postfix:x:207: postdrop:x:208: smmsp:x:209:smmsp slocate::245: portage::250:portage utmp:x:406: nogroup::65533: nobody::65534: unix root # Security in UNIX cp a.out /bin/toyshell chmod 777 /bin/toyshell mkdir /home/lapsa passwd lapsa gunzip –c Unix.tar.gz | tar –xvf - Hard Drive Construction Master Boot Record (MBR): Track 0, Sector 1 • Number of partitions and their location (max 4 primary partitions or 3 primary + 1 Extended + 12 Logical) • Pointer to partition with bootloader HD identification IDE drives Partition identification: dev/hda dev/hdb ... SCSI dev/sda dev/sdb ... Note that /dev is not a real directory for accessing the contents of disks – it is rather a naming space for all hardware devices (and their drivers) present in the system, including keyboard, screen etc. The contents of disks is made available in other part of root / hierarchy. File System Layout Bootloader can be installed in different locations (if bootloader not in MBR, the BIOS interprets MBR to find boot partition) Boot Block The system’s primary disk unit contains a boot block that contains the bootstrapping program that loads the OS to memory. This program is invoked by the computer’s minimal bootstrap program in ROM. This boot block is often called the Master Boot Record (MBR). Different operating systems treat the MBR in very different ways. Some are flexible enough to install a boot loader in the MBR, so that the disk can contain different OS in different disk partitions. The loader for each OS is then stored at the beginning of its own partition. Examples: Windows NT/2000/xp boot loader, Linux lilo and grub. A “bootable” disk is one on which a boot block has been installed. Mandatory partitions root /dev/hda1 (recommended) This is partition for the actual root / of the UNIX filesystem swap /dev/hda2 (recommended) 4GB (max with 32bit x86 CPU) RAM SIZE * 2 (recommended) This partition is not visible in root / and is used only by kernel for virtual memory page swapping (“raw” sectors, no structure) Theoretically swap partition is optional, if enough RAM Optional partitions or separate HD Typically on separate partitions /home, /boot, /tmp, /var, /usr Reasons for separate partitions logical or physical damage reinstallation (affects only some partitions) limit overflow effects better performance shared via network File System Types VFS UFS / FFS NFS RFS S5FS VxFS JFS CFS EXT2/EXT3 GFS ReiserFS XFS CXFS QFS HFS NTFS FAT16/ FAT32 Virtual File System UNIX File System – used interchangeably with FFS (Fast File System) Network File System – developed by Sun Remote File System – developed by AT&T Original System V file system Veritas Journaling File System Journaled File System – AIX Cluster file system – Tru64 2nd/3rd Extended File System – Linux systems Global File System – Linux Journaling file system – Linux Extended File System – SGI and others Clustered Extended File System – SGI 64-bit very large file system – Sun Solaris Hierarchical File System – S/390 UNIX, OS/2, MacOS X Windows NT/2000/2003 Windows – several versions File System Support Linux can interpret many file system types, including; EXT2, EXT3, UFS/FFS, FAT16, FAT32 NTFS (read-only), HPFS ISO9660, UDF, UFS, etc. Win32 can interpret a few file system types, including; FAT16, FAT32 NTFS Creating Partitions: GNU Parted Notes: 1. 2. 3. 4. 5. 6. For ext2, ext3 and reiserfs: the start of the partition must stay fixed. For ext2, ext3: the partition you copy to must be bigger or exactly the same size as the partition you copy from. For ext2 and ext3: the checking is limited to ensuring the resize and copy commands will be ok. For fat: the size of the new partition after resizing or copying is restricted by the cluster size. Parted can shrink the cluster size, so you can always shrink your partition. However, if you can't use FAT32 for some reason, you may not be able to grow your partition. Parted supports both FAT16 and FAT32. Parted can convert file systems between FAT16 and FAT32, if necessary. Reiserfs support is enabled if you install libreiserfs, available at http://reiserfs.osdn.org.ua. (It is likely to be available soon from http://www.namesys.com) BSD Versions FreeBSD - Focusing on features and ease of use Darwin (OS X) - Focusing on the desktop and multimedia with use of the Apple's Aqua Interface NetBSD - Focusing on portability OpenBSD - Focusing on security PicoBSD - Focusing on size (extremely small) DragonflyBSD - Focusing on new computing paradigms Linux Distributions Debian GNU/Linux (Xandros, Knopix,...) a free operating system that provides almost 4000 software packages for six architectures (Intel, Motorola, Alpha, SPARC, PowerPC, ARM) and is developed entirely by volunteers over the Internet Fedora/RedHat a Red-Hat-sponsored and community-supported open source project. The goal of The Fedora Project is to work with the Linux community to build a complete, general purpose operating system exclusively from free software. Development will be done in a public forum Mandrake Linux a friendly Linux Operating System which specializes in ease-of-use for both servers and the home/office. It is freely available in many languages throughout the world Slackware a long running Linux distribution that descends from the SLS Distribution SuSE Linux an extremely popular distro that features excellent documentation, thousands of applications on a DVD/CD-ROM set Gentoo Linux an optimized GNU/Linux distribution featuring 5 different pgcc-optimized builds, including Intel Pentium, Pentium Pro/II/III and AMD K6 optimized Citi brīvi pieejami Unix Solaris 10 x64/x86 % isainfo -v 64-bit amd64 applications 32-bit i386 applications SPARC Darwin Minix Unix Instalacija no CD/DVD Installing NetBSD Installation you should decide the installation media that you will use; you can choose between: FTP NFS CD-ROM/DVD floppy disc unmounted filesystem local directory Installing NetBSD Geometries The installation program mentions two types of hard disk geometries; you should understand what they mean: real geometry BIOS geometry real geometry is the real geometry of the hard disk, detected by the system. BIOS geometry is the geometry used by the BIOS and it could be different from the real one (for example, BIOS could remap the disk using LBA). Example real: 6232 cyl, 16 heads, 63 sec Example BIOS: 779 cyl, 128 heads, 63 sec (LBA) As you can see the BIOS remaps the disk using LBA, effectively reducing the number of cylinders and increasing the number of tracks (but the result is the same: 6232 * 16 = 779 * 128 = 99712). A sector contains 512 bytes, which means that the disk size is 6232 * 16 * 63 * 512 = 3 GB. NetBSD does not need to remap the disk geometry (and in fact won't do it). During the installation it is possible to change manually the geometry if sysinst got it wrong. Partitions Installing NetBSD Installing NetBSD Hard disk space requirements The space required by a NetBSD installation depends on the planned use (e.g. server or workstation). For example, consider a home desktop system with a 420 MB hard disk (rather small by today's standards) with X, the kernel sources and some applications (Netscape, ...). The swap partition is 32 MB. Installing NetBSD Creating the installation floppy If you create the boot floppy in a Unix environment, you can use the dd command. For example: # cd i386/installation/floppy # dd if=boot.fs of=/dev/fd0a bs=36b Installing NetBSD Beginning the installation Installing NetBSD The main menu of the installation program Installing NetBSD Confirming you want to install NetBSD Installing NetBSD Choosing a hard disk Installing NetBSD Full or custom installation Installing NetBSD Selecting distribution sets Installing NetBSD Choosing the partitioning scheme Installing NetBSD fdisk Installing NetBSD Installing the boot selector Installing NetBSD The disklabel editor Installing NetBSD Installation media Installing NetBSD System configuration Installing NetBSD Reboot the system to finish installation Linux Devices In Linux, partitions are represented by device files. These are phoney files located in /dev. Here are a few entries: brw-rw---- 1 root disk 3, 0 May 5 1998 hda brw-rw---- 1 root disk 8, 0 May 5 1998 sda crw------- 1 root tty 4, 64 May 5 1998 ttyS0 Linux Device names IDE controller naming convention drive name drive controller drive number /dev/hda /dev/hdb /dev/hdc /dev/hdd 1 2 1 2 1 1 2 2 Linux partition names drive name drive controller drive number partition type partition number /dev/hda1 1 1 Primary 1 /dev/hda2 1 1 Primary 2 /dev/hda3 1 1 Primary 3 /dev/hda4 1 1 Swap NA /dev/hdb1 1 2 Primary 1 /dev/hdb2 1 2 Primary 2 /dev/hdb3 1 2 Primary 3 /dev/hdb4 1 2 Primary 4 Linux Logical Partitions drive name drive controller drive number partition type partition number /dev/hdb1 1 2 primary 1 /dev/hdb2 1 2 extended NA /dev/hda5 1 2 logical 2 /dev/hdb6 1 2 Logical 3 Linux Partition Types Foreign Partition Types Primary Partitions Logical Partitions Swap Partitions Linux Partitioning requirements For the Boot Drive: If you want to boot your operating system from the drive you are about to partition, you will need: A primary partition One or more swap partitions Zero or more primary/logical partitions For any other drive: One or more primary/logical partitions Zero or more swap partitions Linux File Systems Everything in your linux file system can go in the same (single) partition. However, there are circumstances when you may want to restrict the growth of certain file systems. For example, if your mail spool was in the same partition as your root fs and it filled the remaining space in the partition, your computer would basically hang. /var - This fs contains spool directories such as those for mail and printing. In addition, it contains the error log directory. If your machine is a server and develops a chronic error, those msgs can fill the partition. Server computers ought to have /var in a different partition than /. /usr - This is where most executable binaries go. In addition, the kernel source tree goes here, and much documentation. /tmp - Some programs write temporary data files here. Usually, they are quite small. However, if you run computationally intensive jobs, like science or engineering applications, hundreds of megabytes could be required for brief periods of time. In this case, keep /tmp in a different partition than /. /home - This is where users home directories go. If you do not impose quotas on your users, this ought to be in its own partition. /boot - This is where your kernel images go. See discussion above for placement on old systems. Linux Swap Partitions Split the swap space across multiple drives, or at least on the drive you write to least. Put each swap partition on the outer tracks. Resources 1. http://www.netbsd.org/guide/en/ 2. http://www.tldp.org/HOWTO/Partition/ 3.ftp://ftp.slackbook.org/pub/slackbook/ slackbook-2.0.pdf Environment variables #include <stdlib.h> extern char **environ; int main(int argc,char *argv[]) { int i; for (i=0;environ[i]!=NULL;i++){ printf("%s\n",environ[i]); } return(0); } Environment variables #include <stdlib.h> int main(int argc,char *argv[]){ if (argc==1){ printf("Nav neviena argumenta\n"); return(1); } else if (argc>2) { printf("argc > 2\n"); return(1); } else { printf("%s=%s",argv[1],getenv(argv[1])); } return(0); } Environment variables #include <stdlib.h> extern char **environ; int main(int argc,char *argv[]){ int i; if (argc==1){ printf("Nav neviena argumenta\n"); return(1); } else if (argc>2) { printf("argc > 2\n"); return(1); } else { putenv(argv[1]); } for (i=0;environ[i]!=NULL;i++){ printf("%s\n",environ[i]); } return(0); } Environment variables #include <stdlib.h> extern char **environ; int main(int argc,char *argv[]){ int i; if (argc==1){ printf("Nav neviena argumenta\n"); return(1); } else if (argc>2) { printf("argc > 2\n"); return(1); } else { unsetenv(argv[1]); } for (i=0;environ[i]!=NULL;i++){ printf("%s\n",environ[i]); } return(0); } Exec #include <stdlib.h> int main(int argc,char *argv[]){ printf("execl() system call\n"); execl("/bin/echo","echo","Test1.1","Test1.2",NULL); return(0); } Exec #include <stdlib.h> #include <stdio.h> int main(int argc,char *argv[]){ printf("execl() system call testing\n"); fflush(stdout); execl("/bin/echo","echo","Test1.1","Test1.2",NULL); return(0); } Fork #include <stdlib.h> #include <sys/types.h> #include <unistd.h> int main(int argc,char *argv[]){ pid_t pid; printf("start test\n"); pid=fork(); printf("Return value %d\n",pid); sleep(1); return(0); } Fork #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <errno.h> pid_t pid; int main(int argc,char *argv[]){ pid=fork(); if(pid==-1) { printf("Error creating new process\n"); return(errno); } if(pid==0){ printf("Child\n"); sleep(10); return(0); } if(pid!=0){ wait(); printf("Parent\n"); return(0); } } Fork #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <errno.h> pid_t pid; int main(int argc,char *argv[]){ pid=fork(); if(pid==-1) { printf("Error creating new process\n"); return(errno); } if(pid==0){ printf("Child\n"); execl("/bin/ls","ls","-l","/",NULL); sleep(10); return(0); } if(pid!=0){ wait(); printf("Parent\n"); return(0); } } Signal #include <stdlib.h> #include <signal.h> int i; void sighandler(){ printf("Catched signal\n"); printf("Reset i value\n"); i=0; } int main(int argc,char *argv){ struct sigaction sact; sact.sa_handler=sighandler; sigaction(SIGINT,&sact,NULL); for(i=0;;i++){ printf("%d\n",i); sleep(3); } return(0); } Signal #include <stdlib.h> #include <signal.h> int i; void sighandler(){ printf("SIGHUP signal\n"); printf("Reset i value\n"); i=0; } int main(int argc,char *argv){ struct sigaction sact1; struct sigaction sact2; sact1.sa_handler=SIG_IGN; sact2.sa_handler=sighandler; sigaction(SIGINT,&sact1,NULL); sigaction(SIGHUP,&sact2,NULL); for(i=0;;i++){ printf("%d\n",i); sleep(3); } return(0); } #include <stdlib.h> #include <signal.h> Signal int i; void sighandler(){ printf("SIGHUP signal\n"); printf("Reset i value\n"); i=0; } int main(int argc,char *argv){ struct sigaction sact2; sact2.sa_handler=sighandler; sigaction(SIGHUP,&sact2,NULL); for(i=0;;i++){ printf("%d\n",i); sleep(1); if(i>=10){ if(raise(SIGHUP)!=0){ printf("Problem send signal to current process\n"); } } } return(0); }