Linux Lab Exercises: 2010 April 14

Norman McEntire

norman.mcentire@servin.com

Copyright (c) 2010 Servin Corporation.


Linux Lab Exercises: 2010 April 14

Create Custom Repository: createrepo

Create Custom RPM Package

Setup FTP Installation Server

Setup NFS Installation Server

Setup HTTP Installation Server


Create Custom Repository: createrepo

In this lab exercise, you will use the createrepo command to create a custom yum repository. You will also use the yum command to verify the operation of the repository.


Step 1. Issue the yum repolist command and observe the results.

# yum repolist

Step 2. From the output of the previous yum repolist command, make a note of your repo ids.


Step 3. From the output of the previous yum repolist command, make a note of your repo names.


Step 4. Use the ls command to display the files in the /etc/yum.repos.d directory. How many files are in this directory? And what are the names of the files?

# ls /etc/yum.repos.d

Step 5. Look at the contents of one or more files in the /etc/yum.repos.d directory, observing that some of the entries have enabled=1.

[CentOS]
name=CentOS $releasever - $basearch
baseurl=...
enabled=1
gpgcheck=1
gpgkey=...

Step 6. Now you are going to create your own repository. Begin by creating a directory named /pub/myrepo.

# mkdir -p /pub/myrepo

Step 7. Copy a few .rpm files into the newly created directory. For this lab exercise, the names of the .rpm files does not matter; any .rpm files will do.


Step 8. Use the which command to determine if you have the createrepo command on your system.

# which createrepo

Step 9. If you do not have the createrepo command, then use yum to install the required package.

# yum install createrepo

Step 10. Change to the /pub/myrepo directory and then execute the createrepo command.

# cd /pub/myrepo
# createrepo .

NOTE: Observe the "." after the createrepo command, which means to create a repository in the current directory.


Step 11. Use your editor to create a file named /etc/yum.repos.d/MyRepo.repo with the following contents:

[MyRepoID]
name=MyRepo Name
baseurl=file:///pub/myrepo
enabled=1

Step 12. Issue the yum repolist and observe the results. You should see your new repository in the list.

# yum repolist

End of Lab Exercise


Create Custom RPM Package

In this lab exercise, you will use the rpmbuild command to create a custom rpm package.


Step 1. Create a directory named hello-1.0.0.

# mkdir hello-1.0.0

Step 2. Create a directory named hello-1.0.0, then change into the directory

# mkdir hello-1.0.0
# cd hello-1.0.0

Step 3. Use your editor to create a file named Makefile. (NOTE: Make sure you press the TAB key for the indented lines. Using spaces will not work.)

# Makefile for hellod

all:
	gcc -o hellod hellod.c

clean:
	rm hellod

Step 4. Use your editor to create a file named hellod.c.

#include <unistd.h>
#include <signal.h>

int running = 1;

void do_sig(int signo) {
	running = 0;
}

int main() {
	daemon(0, 0);
	signal(SIGINT, do_sig);
	while (running)
		sleep(1);
	return 0;
}

Step 5. Use your editor to create a file named hello.

#!/bin/sh
#
#   Linux chkconfig stuff:
#
#   chkconfig: 35 48 52
#   description: Demo hello service for \
#                Linux course
#

# Source function library.
. /etc/init.d/functions

DAEMON=hellod

prog=hello

start () {
	echo -n $"Starting $prog: "

	# start daemon
	daemon $DAEMON
        RETVAL=$?
	echo
	[ $RETVAL = 0 ] && touch /var/lock/subsys/$prog
	return $RETVAL
}

stop () {
	# stop daemon
	echo -n $"Stopping $prog: "
	killproc $DAEMON
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && rm -f /var/lock/subsys/$prog
}

restart() {
	stop
	start
}

case $1 in
	start)
		start
	;;
	stop)
		stop
	;;
	restart)
		restart
	;;
	condrestart)
		[ -f /var/lock/subsys/$prog ] && restart || :
	;;	
	status)
		status $DAEMON
		RETVAL=$?
	;;
	*)

	echo $"Usage: $prog {start|stop|restart|condrestart|status}"
	exit 3
esac

exit $RETVAL

Step 6. Use the following commands to test your hello code:

# make
# cp hellod /usr/sbin/.
# cp hello /etc/rc.d/init.d/.
# chkconfig --add hello
# service hello start
# service hello status
# service hello stop
# chkconfig --del hello
# make clean

Step 7. Change up a directory from the hello-1.0.0 directory.

# cd ..

Step 8. Create a tarball of the hello-1.0.0 directory.

# tar zcf hello-1.0.0.tar.gz hello-1.0.0

Step 9. Create a new directory named rpmdemo, then change into the directory.

# mkdir rpmdemo
# cd rpmdemo

Step 10. Create the RPM build directory structure.

# mkdir BUILD RPMS SOURCES SPECS SRPMS

Step 11. Copy the tarball you created in a previous step into the SOURCES directory:

# cp ../hello-1.0.0.tar.gz SOURCES/.

Step 12. Use your editor to create a file named README; you can put any text inside the file that you like:

# vim SOURCES/README

Step 13. Use your editor to create a file name hello.spec with the following contents:

Name: hello
Version: 1.0.0
Release: 1
Summary: Hello demo package for Linux course
Group: Development/Tools
Vendor: Servin
URL: http://servin.com
Packager: Norman McEntire <norman.mcentire@servin.com>
License: GPL

BuildRoot: %{_tmppath}/%{name}-%{version}-root

Source0: %{name}-%{version}.tar.gz
Source1: README

%description
This hello demo package provides both a hellod daemon
and also a hello script.

# rpmbuild -bp to test up to prep stage
%prep
%setup
%build
make

%install
rm -rf $RPM_BUILD_ROOT
mkdir -p $RPM_BUILD_ROOT/usr/sbin
mkdir -p $RPM_BUILD_ROOT/etc/rc.d/init.d
cp hellod $RPM_BUILD_ROOT/usr/sbin
cp hello $RPM_BUILD_ROOT/etc/rc.d/init.d
mkdir -p $RPM_BUILD_ROOT/etc/sysconfig
touch $RPM_BUILD_ROOT/etc/sysconfig/hello
mkdir -p $RPM_BUILD_ROOT/usr/share/doc/hello
cp %{_sourcedir}/README $RPM_BUILD_ROOT/usr/share/doc/hello

%clean
rm -rf $RPM_BUILD_ROOT

%files
# Default attribute: mode, user, group
%defattr(-,root,root)
/usr/sbin/hellod
/etc/rc.d/init.d/hello
%config(noreplace) /etc/sysconfig/hello
%doc /usr/share/doc/hello/README

%post
/sbin/chkconfig --add hello
exit 0

%preun
/sbin/service hello stop
/sbin/chkconfig --del hello
exit 0

Step 14. Verify that the RPM Package does not have syntax errors.

# rpmbuild --define "_topdir $(pwd)" --nobuild SPECS/hello.spec

Step 15. Verify the preparation stage.

# rpmbuild --define "_topdir $(pwd)" -bp SPECS/hello.spec

Step 16. Verify the compile stage.

# rpmbuild --define "_topdir $(pwd)" -bc SPECS/hello.spec

Step 17. Verify the install stage;

# rpmbuild --define "_topdir $(pwd)" -bi SPECS/hello.spec

Step 18. Build all (both binary, .rpm, and source, .src.rpm, RPM packages.

# rpmbuild --define "_topdir $(pwd)" -ba SPECS/hello.spec

Step 19. Observe the resulting binary RPM package:

# find RPMS

Step 20. Observe the resulting source RPM package:

# find SRPMS

End of Lab Exercise


Setup FTP Installation Server

In this lab exercise, you will setup an FTP (File Transfer Protocol) server. You will configure the server to provide both a kickstart file and the complete installation files from the CentOS DVD.

You will test out your results by doing a networking install, reading both the kickstart file and the installation files from the FTP server.


Step 1. Use the rpm command to determine if the vsftpd (very secure ftp daemon) server package is on your system.

# rpm --query vsftpd

Step 2. If the vsftpd package is not installed, install it using yum.

# yum install vsftpd

Step 3. Use the rpm command to read info about the package.

# rpm --query --info vsftpd

Step 4. Use the rpm command to display the list of files included in the package.

# rpm --query --list vsftpd

Step 5. Change to the location of the FTP shared directory:

# cd /var/ftp/pub

Step 6. Place a kickstart file into the directory, making sure that you change the permissions to read of the kickstart file.

# cp /root/anaconda-ks.cfg /var/ftp/pub/ftp-ks.cfg
# chmod a+r ftp-ks.cfg

Step 7. Edit the kickstart file as follows, making sure that you use the IP address of the machine that is running the ftp server.

# Kickstart file automatically generated by anaconda.

install
#cdrom
url --url ftp://IP_ADDR/pub/CentOS_5.4_Final
lang en_US.UTF-8
keyboard us
...

Step 8. Copy the complete contents of your CentOS DVD to the /var/ftp/pub directory.

# cp -r /media/CentOS_5.4_Final /var/ftp/pub/.

Step 9. For this exercise, make sure that your firewall is flushed:

# iptables --flush

Step 10. Startup the ftp server:

# service vsftpd start

Step 11. On another virtual machine, open a web browser and enter the URL shown below, confirming that you can access the FTP server. Replace IP_ADDR with the address of your server.

ftp://IP_ADDR/pub

Step 12. Create another virtual machine, and boot from the CentOS DVD, and enter the following at the boot prompt, replacing the IP_ADDR with the address of your server.

boot: linux ks=ftp://IP_ADDR/pub/ftp-ks.cfg

Step 13. When the DVD install displays the message similar to "Looking for an IP address", press the eject button to eject the DVD. (NOTE: Do not eject the DVD too early; wait until you see the installation program looking for an IP address.)


Step 14. Observe the that installation program finds the kickstart file on the FTP server, then starts the graphical installation.

NOTE: You do not need to continue the installation once you confirm that it has found the kickstart and install files from the FTP server.


End of Lab Exercise


Setup NFS Installation Server

In this lab exercise, you will setup an NFS server that will share the same files that were shared by the FTP server.


Step 1. The /etc/exports file controls the directories that are shared by NFS. Edit the file to match the following:

NOTE: Make sure you have completed the previous FTP lab before doing this lab.

/var/ftp/pub *(ro)

Step 2. Copy a new kickstart file as follows:

# cp /var/ftp/pub/ftp-ks.cfg /var/ftp/pub/nfs-ks.cfg

Step 3. Edit the /var/ftp/pub/nfs-ks.cfg file to match the following, replacing IP_ADDR with the IP address of your NFS server.

install
#cdrom
#url --url ftp://IP_ADDR/pub/CentOS_5.4_Final
nfs --server IP_ADDR --dir /var/ftp/pub/CentOS_5.4_Final
lang en_US.UTF-8
keyboard us
...

Step 4. Startup the NFS server

# service nfs start

Step 5. Reset the virtual machine you used in the previous FTP lab exercise, and again boot from the CentOS DVD, entering the boot command as follows:

boot: linux ks=nfs:IP_ADDR:/var/ftp/pub/nfs-ks.cfg

Step 6. When the DVD install displays the message similar to "Looking for an IP address", press the eject button to eject the DVD. (NOTE: Do not eject the DVD too early; wait until you see the installation program looking for an IP address.)


Step 7. Observe the that installation program finds the kickstart file on the NFS server, then starts the graphical installation.

NOTE: You do not need to continue the installation once you confirm that it has found the kickstart and install files from the NFS server.


End of Lab Exercise


Setup HTTP Installation Server

In this lab exercise, you will setup a HTTP server that will share the same files that were shared by the FTP and NFS servers.


Step 1. The /etc/httpd/conf/httpd.conf file controls the configuration of the Apache Web Server. Edit the file to change the document root as follows:

NOTE: Make sure you have completed the previous FTP and NFS labs before doing this lab.

#DocumentRoot "/var/www/html"
DocumentRoot "/var/ftp/pub"

Step 2. Copy a new kickstart file as follows:

# cp /var/ftp/pub/ftp-ks.cfg /var/ftp/pub/http-ks.cfg

Step 3. Edit the /var/ftp/pub/http-ks.cfg file to match the following, replacing IP_ADDR with the IP address of your HTTP server.

install
#cdrom
#url --url ftp://IP_ADDR/pub/CentOS_5.4_Final
#nfs --server IP_ADDR --dir /var/ftp/pub/CentOS_5.4_Final
url --url http://IP_ADDR/CentOS_5.4_Final
lang en_US.UTF-8
keyboard us
...

Step 4. Startup the HTTP server

# service httpd start

Step 5. Reset the virtual machine you used in the previous FTP and NFS lab exercises, and again boot from the CentOS DVD, entering the boot command as follows:

boot: linux ks=http://IP_ADDR/http-ks.cfg

Step 6. When the DVD install displays the message similar to "Looking for an IP address", press the eject button to eject the DVD. (NOTE: Do not eject the DVD too early; wait until you see the installation program looking for an IP address.)


Step 7. Observe the that installation program finds the kickstart file on the HTTP server, then starts the graphical installation.

NOTE: You do not need to continue the installation once you confirm that it has found the kickstart and install files from the HTTP server.


End of Lab Exercise