Mounting Debian ISO files as Offline Software Repository


If you are not connected to internet, then still you can install packages
from Debian Installer ISOs.

For example, if Debian 7.0 (Wheezy) ISOs are in directory /media/FreeAgent GoFlex Drive/Softwares/Debian_7.0_Wheezy
directory and we want to mount these iso in /mnt/Debian/ directory. The following command can be used:

$ sudo $(which mount_debian.pl) -d /mnt/Debian -g -r "/media/FreeAgent GoFlex Drive/Softwares/Debian_7.0_Wheezy"
Using iso(s) dir '/media/FreeAgent GoFlex Drive/Softwares/Debian_7.0_Wheezy'
Using mount point dir '/mnt/Debian'
...
Total 11 iso(s) mounted successfully.

Unmounting of the iso(s) can be achieved by the following command:

$ sudo $(which mount_debian.pl) -d /mnt/Debian -u -g -r
Running: umount "/mnt/Debian"/*.iso
Running: rmdir "/mnt/Debian"/*.iso
Running: cp "/etc/apt/sources.list.net" "/etc/apt/sources.list"
Running: apt-get update
...

Usage of the command:

$ sudo $(which mount_debian.pl)
/home/mitesh/Perl/mount_debian.pl: Please provide Dir where Debian installer iso(s) are present.
USAGE: /home/mitesh/Perl/mount_debian.pl [options] iso_dir
          -a apt_sources_dir        default  /etc/apt
          -d mount_base_dir         default  /mnt/Debian
          -g                        debug on 
          -r                        apt-get update 
          -u                        unmount 
          iso_dir                   Dir where Debian installer iso(s) are present.

PS: iso_dir is not required with -u command

The mount_debian.sh perl script is given as follows:

#!/usr/bin/perl -w
#===============================================================================
#
#         FILE:  mount_debian.pl
#
#        USAGE:  ./mount_debian.pl 
#
#  DESCRIPTION:  
#
#      OPTIONS:  ---
# REQUIREMENTS:  ---
#         BUGS:  ---
#        NOTES:  ---
#       AUTHOR:  Mitesh Singh Jat (mitesh)
#      COMPANY:  
#      VERSION:  1.0
#      CREATED:  11/07/2013 10:30:04 PM IST
#     REVISION:  ---
#===============================================================================

use strict;
use warnings;

use Getopt::Std;
my $apt_sources_dir = "/etc/apt";
my $mount_point_base = "/mnt/Debian";
my $umount = 0;
my $reload = 0;
my $debug = 0;

sub usage()
{
    print STDERR "USAGE: $0 [options] <iso_dir>\n";
    print STDERR "          -a <apt_sources_dir>    apt sources dir: default  $apt_sources_dir\n";
    print STDERR "          -d <mount_base_dir>     mount point dir: default  $mount_point_base\n";
    print STDERR "          -g                      debug on \n";
    print STDERR "          -r                      apt-get update \n";
    print STDERR "          -u                      unmount \n";
    print STDERR "          <iso_dir>               Dir where Debian installer iso(s) are present.\n";
}

sub run_cmd()
{
    my $cmd = $_[0];
    print "Running: $cmd\n" if $debug;
    system("$cmd");
    my $retval = $?; 
    if ($retval != 0)
    {   
        print STDERR "Error in running: $cmd\nExit code = $retval\n";
    }   
    return ($retval);
}

sub run_cmd_get()
{
    my $cmd = $_[0];
    print "Running: $cmd\n" if $debug;
    my @outs = `$cmd`;
    my $retval = $?;
    if ($retval < 0)
    {
        print STDERR "Error in running: $cmd\nExit code = $retval\n";
        exit($retval);
    }
    chomp(@outs);
    return (@outs);
}
sub take_backup() 
{
    my $file1 = $_[0];
    my $file2 = $_[1];
    my $cmd = "cp \"$file1\" \"$file2\"";
    my $retval = &run_cmd("$cmd");
    if ($retval != 0)
    {
        print STDERR "$0: Cannot copy \"$file1\" to \"$file2\"\n";
        exit(-1);
    }
    return 1;
}
sub reload_repo()
{
    my $file1 = $_[0];
    my $file2 = $_[1];

    my $cmd = "cp \"$file1\" \"$file2\"";
    my $retval = &run_cmd($cmd);
    exit(-1) if ($retval != 0);

    $cmd = "apt-get update";
    $retval = &run_cmd($cmd);
    exit(-1) if ($retval != 0);
}

my %opts;
getopt('a:d:', \%opts);

foreach my $opt (sort keys %opts)
{
    if (!defined($opts{$opt}))
    {
        print STDERR "$0: Requires value for option '$opt'\n";
        &usage();
        exit(-1);
    }
    else 
    {
        if ($opts{$opt} =~ m/^-/) 
        {
            print STDERR "$0: Requires value for option '$opt'\n";
            &usage();
            exit(-1);
        }
    }
}
$apt_sources_dir = $opts{"a"} if (defined($opts{"a"}));
$mount_point_base = $opts{"d"} if (defined($opts{"d"}));
$debug = 1 if (defined($opts{"g"}));
$reload = 1 if (defined($opts{"r"}));
$umount = 1 if (defined($opts{"u"}));

my $apt_dvd = "$apt_sources_dir/sources.list.dvd";
my $apt_net = "$apt_sources_dir/sources.list.net";
my $apt_orig = "$apt_sources_dir/sources.list";

my $retval = 0;
my $cmd;
$retval = `whoami`;
chomp($retval);
unless ($retval eq "root")
{
    print STDERR "$0: Please run this script as 'root' user.\n";
    &usage();
    exit(-1);
}

unless ($umount)
{

if (@ARGV != 1) 
{
    print STDERR "$0: Please provide Dir where Debian installer iso(s) are present.\n";
    &usage();
    exit(-1);
}
my $iso_dir = "$ARGV[0]";
## Check for iso dir presence
unless (-d "$iso_dir") 
{
    print STDERR "$0: Please make sure the iso(s) dir '$iso_dir' is present\n";
    exit(-1);
}
print "Using iso(s) dir '$iso_dir'\n" if ($debug);

## Check for destination dir
unless (-d "$mount_point_base") 
{
    $cmd = "mkdir -p \"$mount_point_base\"";
    $retval = &run_cmd("$cmd");
    if ($retval != 0) 
    {
        print STDERR "$0: Cannot create dir '$mount_point_base'\n";
        exit(-1);
    }
}
print "Using mount point dir '$mount_point_base'\n" if ($debug);

## Taking backup of ftp/http line sources.list
my $is_backed_up = 0;
if (-f "$apt_net") 
{
    ## Take backup only when ftp / http (internet)
    $cmd = "grep -v -e \"^#\" -e \"^\$\" \"$apt_orig\" | egrep -c \"(ftp|http)://\"";
    my @nets = &run_cmd_get($cmd);
    if ($nets[0] > 0) 
    {
        $is_backed_up = &take_backup("$apt_orig", "$apt_net");
    }
}
else
{
    $is_backed_up = &take_backup("$apt_orig", "$apt_net");
}

## Get loop devices
$cmd = "ls /dev/loop[0-9]*";
my @temps = &run_cmd_get($cmd);
my %loop_devices;
my $nloop_devices = @temps;
foreach my $temp (@temps) 
{
    $loop_devices{$temp} = 1;
}
$cmd = "ls \"$iso_dir\" | grep -i \"\.iso\$\"";
@temps = &run_cmd_get($cmd);
my %iso_images;
my $niso_images = @temps;
foreach my $temp (@temps) 
{
    $iso_images{$temp} = 1;
}
$cmd = "df | awk '{print \$1}' | grep \"/dev/loop\"";
@temps = &run_cmd_get($cmd);
my %mounted_loop_devices;
my $nmounted_loop_devices = @temps;
foreach my $temp (@temps) 
{
    $mounted_loop_devices{$temp} = 1;
}

## Check for free loop devices
my $nfree_loops = $nloop_devices - $nmounted_loop_devices;
if ($niso_images > $nfree_loops)
{
    print "Less available loop devices $nfree_loops than isos $niso_images\n" if ($debug);
    my $req_loops = $niso_images - $nfree_loops; 
    if ($nmounted_loop_devices == 0) 
    {
        print "Reloading loop kernel module with extra $req_loops loop devices\n" if $debug;
        $cmd = "rmmod loop";
        $retval = &run_cmd($cmd);
        print "$retval\n";
        exit(-1) if ($retval != 0);
        $cmd = "modprobe loop max_loop=$niso_images";
        $retval = &run_cmd($cmd);
        exit(-1) if ($retval != 0);
    }
    else 
    {
        print STDERR "Please reload 'loop' kernel module. After unloading following loop devices...\n";
        foreach my $temp (sort keys %mounted_loop_devices)
        {
            print STDERR "$temp\n";
        }
        print STDERR "\nrmmod loop\n";
        my $temp = $nmounted_loop_devices + $req_loops;
        print STDERR "\nmodprobe loop max_loop=$temp\n\n";
        exit(-2);
    }
}

## Create entries in sources.list file
open(SFH, ">$apt_orig") or die("$0: Cannot write in file '$apt_orig'\n");
foreach my $iso (sort keys %iso_images)
{
    print "Mounting iso $iso ...\n" if $debug;
    my $dest_dir = "$mount_point_base/$iso";
    unless (-d "$dest_dir") 
    {
        $cmd = "mkdir -p \"$dest_dir\"";
        $retval = &run_cmd($cmd);
        exit(-1) if ($retval != 0);
    }
    $cmd = "df \"$dest_dir\" | grep -c \"^/dev/loop.*\.iso\$\"";
    my @mounts = &run_cmd_get($cmd); 
    if ($mounts[0] == 0) ## Mount only if not mounted
    {
        $cmd = "mount -o loop,ro \"$iso_dir/$iso\" \"$dest_dir\"";
        $retval = &run_cmd($cmd);
        exit(-1) if ($retval != 0);

        ## read distro version dir in dists/
        $cmd = "ls -l \"$dest_dir/dists\" | grep \"^d\" | awk '{print \$(NF)}' | head -1";
        my $version = `$cmd`;
        if (defined($version) and ($version ne ""))
        {
            $cmd = "ls -l \"$dest_dir/dists\"/*/ | grep \"^d\" | sort -u | awk '{print \$(NF)}' | awk '{sec=sec\" \"\$1} END{print sec}'";
            my $sections = `$cmd`;
            if (defined($sections))
            {
                chomp($sections);
                chomp($version);
                print SFH "deb file://$dest_dir $version $sections\n";
            }
        }
    }
}
print SFH "\n## Automatically generated by $0 ##\n";
close(SFH);
print "Total $niso_images iso(s) mounted successfully.\n" if $debug;

## Reload repository if sources.list is updated
if ($is_backed_up and $reload)
{
    &reload_repo("$apt_orig", "$apt_dvd");
}
exit(0);
}
#######################################
## umount code                       ##
#######################################

$cmd = "umount \"$mount_point_base\"/*.iso";
$retval = &run_cmd($cmd);
exit(-1) if ($retval != 0);
$cmd = "rmdir \"$mount_point_base\"/*.iso";
$retval = &run_cmd($cmd);
exit(-1) if ($retval != 0);
if ($reload)
{
    &reload_repo("$apt_net", "$apt_orig");
}
exit(0);

PS: The above script can be used with any Debian Package Management related distros like Ubuntu, Linux Mint, etc.

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 

Offline Package Repository in Debian


If you are not connected to internet, then still you can install packages
from Debian Installer DVDs. (Many thanks to my friend P. A. Venkatesh,
for inspiring me to use Debian).

1. Configuration for /etc/apt/sources.list. Just put these 3 lines in it.

deb file:///mnt/debian/disk1/ etch main contrib

deb file:///mnt/debian/disk2/ etch main contrib

deb file:///mnt/debian/disk3/ etch main contrib

2. Create directories.

# mkdir -p /mnt/debian/disk1
# mkdir /mnt/debian/disk2
# mkdir /mnt/debian/disk3

3. Mount *.iso (ISO images) file to to above directories.

# mount -o loop /debian-40r1-amd64-DVD-1.iso /mnt/debian/disk1
# mount -o loop /debian-40r1-amd64-DVD-2.iso /mnt/debian/disk2
# mount -o loop /debian-40r1-amd64-DVD-3.iso /mnt/debian/disk3

4. Now start synaptic package manager and click reload button. And Viola!
you will be able to see packages. Now you can install any package you want. 🙂