Upload
julian-mcdaniel
View
217
Download
0
Embed Size (px)
Citation preview
Lecture 3: Unix installation&testing
Guntis BarzdinsGirts FolkmanisJuris 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"); }}
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.c
“toyshell” palaišana# /usr/bin/gcc toyshell.c# cc toyshell.c# ./a.out$toyshell$> envUSER=rootHOME=/rootTERM=vt100PATH=/root/bin:/usr/local/bin:/bin:/usr/binSHELL=/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 root # more /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/bin/falsedaemon:x:2:2:daemon:/sbin:/bin/falseadm:x:3:4:adm:/var/adm:/bin/falselp:x:4:7:lp:/var/spool/lpd:/bin/falsesync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/halt...guest:x:405:100:guest:/dev/null:/dev/nullnobody:x:65534:65534:nobody:/:/bin/falsegirtsf:x:1000:100::/home/girtsf:/bin/bashdima:x:1001:100::/home/dima:/bin/bashguntis:x:1002:100::/home/guntis:/bin/bashstudents:x:1003:100::/home/students:/bin/bashunix root #
unix root # more /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/bin/falsedaemon:x:2:2:daemon:/sbin:/bin/falseadm:x:3:4:adm:/var/adm:/bin/falselp:x:4:7:lp:/var/spool/lpd:/bin/falsesync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/halt...guest:x:405:100:guest:/dev/null:/dev/nullnobody:x:65534:65534:nobody:/:/bin/falsegirtsf:x:1000:100::/home/girtsf:/bin/bashdima:x:1001:100::/home/dima:/bin/bashguntis:x:1002:100::/home/guntis:/bin/bashstudents:x:1003:100::/home/students:/bin/bashunix root #
unix root # more /etc/shadowroot:$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/shadowroot:$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 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 shadowunix etc #
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 shadowunix etc #
unix root # more /etc/group root::0:rootbin::1:root,bin,daemondaemon::2:root,bin,daemonsys::3:root,bin,admadm::4:root,adm,daemontty::5:girtsfdisk::6:root,admlp::7:lpmem::8:kmem::9:wheel::10:root,girtsffloppy::11:rootmail::12:mail...users::100:games,girtsfnofiles:x:200:qmail:x:201:postfix:x:207:postdrop:x:208:smmsp:x:209:smmspslocate::245:portage::250:portageutmp:x:406:nogroup::65533:nobody::65534:unix root #
unix root # more /etc/group root::0:rootbin::1:root,bin,daemondaemon::2:root,bin,daemonsys::3:root,bin,admadm::4:root,adm,daemontty::5:girtsfdisk::6:root,admlp::7:lpmem::8:kmem::9:wheel::10:root,girtsffloppy::11:rootmail::12:mail...users::100:games,girtsfnofiles:x:200:qmail:x:201:postfix:x:207:postdrop:x:208:smmsp:x:209:smmspslocate::245:portage::250:portageutmp:x:406:nogroup::65533:nobody::65534:unix root #
Security in UNIX
cp a.out /bin/toyshellchmod 777 /bin/toyshellmkdir /home/lapsapasswd lapsagunzip –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 dev/hda dev/hdb ...
SCSI dev/sda dev/sdb ...
Partition identification:
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 Virtual File System UFS / FFS UNIX File System – used interchangeably with FFS (Fast File System) NFS Network File System – developed by Sun RFS Remote File System – developed by AT&T S5FS Original System V file system VxFS Veritas Journaling File System JFS Journaled File System – AIX CFS Cluster file system – Tru64 EXT2/EXT3 2nd/3rd Extended File System – Linux systems GFS Global File System – Linux ReiserFS Journaling file system – Linux XFS Extended File System – SGI and others CXFS Clustered Extended File System – SGI QFS 64-bit very large file system – Sun Solaris HFS Hierarchical File System – S/390 UNIX, OS/2, MacOS X NTFS Windows NT/2000/2003 FAT16/ FAT32 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. For ext2, ext3 and reiserfs: the start of the partition must stay fixed. 2. For ext2, ext3: the partition you copy to must be bigger or exactly the same size as the partition you copy from. 3. For ext2 and ext3: the checking is limited to ensuring the resize and copy commands will be ok. 4. 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.
5. Parted supports both FAT16 and FAT32. Parted can convert file systems between FAT16 and FAT32, if necessary.
6. 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 -v64-bit amd64 applications32-bit i386 applications
SPARC Darwin Minix
Unix Instalacijano 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 GeometriesThe 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.
Installing NetBSD Partitions
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 bydevice files. These are phoney files locatedin /dev. Here are a few entries:
brw-rw---- 1 root disk 3, 0 May 5 1998 hdabrw-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
conventiondrive name drive controller drive number
/dev/hda 1 1/dev/hdb 1 2/dev/hdc 2 1/dev/hdd 2 2
Linuxpartition 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 youroperating system from the drive you are about topartition, 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.
Resources1. 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);
}
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 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);
}