Tuesday, August 21, 2012

Auto Mounting of Debian Offline Repository ISOs

If you are not connected to internet, then still you can install packages from Debian Installer ISOs.
For example, if Debian 6.0 (Squeeze) ISOs are in directory
/media/FreeAgent GoFlex Drive/Softwares/Debian_6_0/
directory and we want to mount these iso in
/mnt/Debian/
directory. The following command can be used:
$ sudo $(which mount_debian.sh) /media/FreeAgent\ GoFlex\ Drive/Softwares/Debian_6_0 /mnt/Debian
The shell script mount_debian.sh is given below.
You need to reload the debian repository (if internet repository is used before this command), to take packages from these mounted ISOs. Because the /etc/apt/sources.list is backed up and updated with these mounted ISOs.
$ sudo apt-get update 

In order to, unmount the ISOs, the following command can be given:
$ sudo $(which umount_debian.sh) /mnt/Debian/
This command will recover original /etc/apt/sources.list
mount_debian.sh
#!/bin/bash

DEBIAN_DIR="/mnt/Debian"
ISO_DIR="/mnt/temp/Softwares/Debian_5_0_5"
APT_DIR="/etc/apt"

## Run as root always
user_id=`whoami`

if [[ "$user_id" != "root" ]]
then
    echo "ERROR: please run this script as 'root' user."
    exit
fi    

if [ -d "$APT_DIR" ]
then
    echo -n ""
else
    echo "ERROR: This is not Debian System" 
    echo "       apt sources dir $APT_DIR is not present"
    exit
fi    

if [ $# -ge 1 ]
then
    ISO_DIR="$1"
else    
    echo "USAGE: $0 <ISO_DIR> <DEBIAN_DIR>"
    exit
fi    
    
if [ $# -ge 2 ]
then
    DEBIAN_DIR="$2"
fi    

## Check for ISO_DIR
if [ -d "$ISO_DIR" ]
then
    echo -n ""
else
    echo "$0: Please have dir with iso images: $ISO_DIR"
    exit
fi

if [ -d "$DEBIAN_DIR" ]
then
    echo -n ""
else
    mkdir -p "$DEBIAN_DIR"
fi
niso=0
nmount=0
apt_dvd="$APT_DIR/sources.list.dvd"
apt_net="$APT_DIR/sources.list.net"
apt_orig="$APT_DIR/sources.list"
if [ -f "$apt_net" ]
then
    echo -n ""
else
    # Take backup 
    cp $apt_orig $apt_net
fi    
echo "# Automatically generated by $0" > $apt_dvd
for iso in `ls "$ISO_DIR" | grep -i "\.iso$"`
do
    niso=`expr $niso + 1`
    n=`echo "$iso" | sed 's/.*\([0-9]\).iso/\1/'`
    echo "Mounting Disk $n..."
    dest_dir="$DEBIAN_DIR/Disk$n"
    if [ -d "$dest_dir" ]
    then
        echo -n ""
    else
        mkdir -p "$dest_dir"
    fi    
    echo "mount -o loop \"$ISO_DIR/$iso\" $dest_dir"
    mount -o loop "$ISO_DIR/$iso" $dest_dir
    if [ $? -ne 0 ]
    then
        echo "ERROR: Cannot mount $iso on $dest_dir";
    else
        nmount=`expr $nmount + 1`
        version=`ls -l $dest_dir/dists | grep "^d" | awk '{print $(NF)}'`
        secs=""
        for section in `ls -l $dest_dir/dists/stable/ | grep "^d" | awk '{print $(NF)}'`
        do
            secs=`echo "$secs $section"`
        done    
        echo "deb file://$dest_dir $version $secs" >> $apt_dvd
    fi    
done
echo ""
echo "Total Disks Mounted = $nmount/$niso"

echo ""
echo "Creating new $apt_orig..."
if [ $nmount -eq $niso ] 
then
    if [ $nmount -ne 0 ]
    then
        cp $apt_dvd $apt_orig
        echo "Created new $apt_orig"
    fi
fi    

exit 0
umount_debian.sh
#!/bin/bash

DEBIAN_DIR="/mnt/Debian"
ISO_DIR="/mnt/temp/Softwares/Debian_5_0_5"
APT_DIR="/etc/apt"

## Run as root always
user_id=`whoami`

if [[ "$user_id" != "root" ]]
then
    echo "$0: please run this script as root user."
    exit
fi    

if [ -d "$APT_DIR" ]
then
    echo -n ""
else
    echo "ERROR: This is not Debian System"
    echo "       apt sources dir $APT_DIR is not present"
    exit
fi    

if [ $# -ge 1 ]
then
    DEBIAN_DIR="$1"
else    
    echo "USAGE: $0 <DEBIAN_DIR>"
    exit
fi    
    
if [ -d "$DEBIAN_DIR" ]
then
    echo -n ""
else
    echo "$0: Debian dir $DEBIAN_DIR is not present"
    exit
fi

apt_dvd="$APT_DIR/sources.list.dvd"
apt_net="$APT_DIR/sources.list.net"
apt_orig="$APT_DIR/sources.list"
if [ -f "$apt_net" ]
then
    echo -n ""
else
    # Take backup 
    cp $apt_orig $apt_net
fi    
niso=0
nmount=0
for iso in `ls $DEBIAN_DIR | grep "^Disk[0-9]"`
do
    niso=`expr $niso + 1`
    n=`echo "$iso" | sed 's/.*\([0-9]\)$/\1/'`
    echo "Unmounting Disk $n..."
    dest_dir="$DEBIAN_DIR/Disk$n"
    if [ -d "$dest_dir" ]
    then
        echo "umount $dest_dir"
        umount $dest_dir
        if [ $? -ne 0 ]
        then
            echo "ERROR: Cannot umount $dest_dir";
        else
            nmount=`expr $nmount + 1`
        fi    
    fi    
done
echo ""
echo "Total Disks Unmounted = $nmount/$niso"

echo ""
#echo "Restoring $apt_orig..."
if [ $nmount -eq $niso ] 
if [ $nmount -eq $niso ] 
then
    if [ $nmount -ne 0 ]
    then
        cp $apt_net $apt_orig
         #echo "Restored $apt_orig"
    fi
fi    

exit 0

Related blogs:
Blog: Offline Package Repository in Debian
Blog: Offline Package Repository in Debian 

1 comment:

Anonymous said...

These scripts can be used for auto mounting Ubuntu CD/DVD iso as repository.